From eb090da027012292e8856dc01856cde8880d3467 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 20 May 2024 17:11:20 +0200 Subject: Lane::debugName is a std::string_view --- src/compat.h | 13 ++++++++++++- src/deep.cpp | 18 +++++++++--------- src/deep.h | 2 +- src/intercopycontext.cpp | 8 ++++---- src/lane.cpp | 18 ++++++++---------- src/lane.h | 2 +- src/lanes.cpp | 20 ++++++++++---------- src/tracker.cpp | 2 +- src/universe.cpp | 2 +- 9 files changed, 47 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/compat.h b/src/compat.h index 3fb4e2d..b5afe17 100644 --- a/src/compat.h +++ b/src/compat.h @@ -254,9 +254,20 @@ LuaType luaG_getmodule(lua_State* L_, char const* name_); // ################################################################################################# // a replacement of lua_tolstring -inline std::string_view lua_tostringview(lua_State* L_, int idx_) +[[nodiscard]] inline std::string_view lua_tostringview(lua_State* L_, int idx_) { size_t _len{ 0 }; char const* _str{ lua_tolstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } + +[[nodiscard]] inline std::string_view lua_pushstringview(lua_State* L_, std::string_view const& str_) +{ +#if LUA_VERSION_NUM == 501 + // lua_pushlstring doesn't return a value in Lua 5.1 + lua_pushlstring(L_, str_.data(), str_.size()); + return lua_tostringview(L_, -1); +#else + return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; +#endif // LUA_VERSION_NUM > 501 +} \ No newline at end of file diff --git a/src/deep.cpp b/src/deep.cpp index 1685ebb..51c9250 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -184,7 +184,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) * used in this Lua state (metatable, registring it). Otherwise, increments the * reference count. */ -char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) +std::string_view DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) { // Check if a proxy already exists kDeepProxyCacheRegKey.getSubTableMode(L_, "v"); // L_: DPC @@ -192,7 +192,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int lua_rawget(L_, -2); // L_: DPC proxy if (!lua_isnil(L_, -1)) { lua_remove(L_, -2); // L_: proxy - return nullptr; + return std::string_view{}; } else { lua_pop(L_, 1); // L_: DPC } @@ -264,18 +264,18 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", _modname.data()); lua_insert(L_, -2); // L_: DPC proxy metatable prefix error lua_concat(L_, 2); // L_: DPC proxy metatable error - return lua_tostring(L_, -1); + return lua_tostringview(L_, -1); } } else { // already loaded, we are happy lua_pop(L_, 4); // L_: DPC proxy metatable } } else { // no L.registry._LOADED; can this ever happen? lua_pop(L_, 6); // L_: - return "unexpected error while requiring a module identified by DeepFactory::moduleName"; + return std::string_view{ "unexpected error while requiring a module identified by DeepFactory::moduleName" }; } } else { // a module name, but no require() function :-( lua_pop(L_, 4); // L_: - return "lanes receiving deep userdata should register the 'package' library"; + return std::string_view{ "lanes receiving deep userdata should register the 'package' library" }; } } } @@ -291,7 +291,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int lua_remove(L_, -2); // L_: proxy LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA); STACK_CHECK(L_, 0); - return nullptr; + return std::string_view{}; } // ################################################################################################# @@ -336,9 +336,9 @@ int DeepFactory::pushDeepUserdata(DestState L_, int nuv_) const raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); } - char const* const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy - if (_err != nullptr) { - raise_luaL_error(L_, _err); + std::string_view const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy + if (!_err.empty()) { + raise_luaL_error(L_, _err.data()); } STACK_CHECK(L_, 1); return 1; diff --git a/src/deep.h b/src/deep.h index 96461d6..dc32251 100644 --- a/src/deep.h +++ b/src/deep.h @@ -75,7 +75,7 @@ class DeepFactory [[nodiscard]] int pushDeepUserdata(DestState L_, int nuv_) const; [[nodiscard]] DeepPrelude* toDeep(lua_State* L_, int index_) const; static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); - [[nodiscard]] static char const* PushDeepProxy(DestState L_, DeepPrelude* o_, int nuv_, LookupMode mode_); + [[nodiscard]] static std::string_view PushDeepProxy(DestState L_, DeepPrelude* o_, int nuv_, LookupMode mode_); }; // ################################################################################################# diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 1487afd..08709e5 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -751,9 +751,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK(L1, _nuv); DeepPrelude* const u{ *lua_tofulluserdata(L1, L1_i) }; - char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u - if (errmsg != nullptr) { - raise_luaL_error(getErrL(), errmsg); + std::string_view const errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u + if (!errmsg.empty()) { + raise_luaL_error(getErrL(), errmsg.data()); } // transfer all uservalues of the source in the destination @@ -1142,7 +1142,7 @@ static char const* vt_names[] = { // transfers stuff from L1->_G["package"] to L2->_G["package"] // returns InterCopyResult::Success if everything is fine // returns InterCopyResult::Error if pushed an error message in L1 -// else raise an error in L1 +// else raise an error in whichever state is not a keeper [[nodiscard]] InterCopyResult InterCopyContext::inter_copy_package() const { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy_package()\n" INDENT_END(U))); diff --git a/src/lane.cpp b/src/lane.cpp index 3017806..e838b7d 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -46,7 +46,7 @@ static LUAG_FUNC(get_debug_threadname) { Lane* const _lane{ ToLane(L_, 1) }; luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); - lua_pushstring(L_, _lane->debugName); + std::ignore = lua_pushstringview(L_, _lane->debugName); return 1; } @@ -278,7 +278,7 @@ static int thread_index_number(lua_State* L_) lua_replace(L_, -3); // L_: lane n error() "error" lua_pushinteger(L_, 3); // L_: lane n error() "error" 3 lua_call(L_, 2, 0); // error(tostring(errstring), 3) -> doesn't return // L_: lane n - raise_luaL_error(L_, "%s: should not get here!", _lane->debugName); + raise_luaL_error(L_, "%s: should not get here!", _lane->debugName.data()); } else { lua_pop(L_, 1); // L_: lane n {uv} } @@ -345,7 +345,7 @@ static LUAG_FUNC(thread_index) lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k lua_concat(L_, 2); // L_: mt error "Unknown key: " lua_call(L_, 1, 0); // error( "Unknown key: " .. key) -> doesn't return // L_: mt - raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName, lua_typename(L_, lua_type(L_, kKey))); + raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName.data(), lua_typename(L_, lua_type(L_, kKey))); } } @@ -739,7 +739,7 @@ static void lane_main(Lane* lane_) lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil if (!lua_isnil(L_, -1)) { lua_remove(L_, -2); // L_: ud gc_cb|nil - lua_pushstring(L_, _lane->debugName); // L_: ud gc_cb name + std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: ud gc_cb name _have_gc_cb = true; } else { lua_pop(L_, 2); // L_: ud @@ -759,7 +759,7 @@ static void lane_main(Lane* lane_) // no longer accessing the Lua VM: we can close right now _lane->close(); // just in case, but _lane will be freed soon so... - _lane->debugName = ""; + _lane->debugName = std::string_view{ "" }; } // Clean up after a (finished) thread @@ -812,12 +812,12 @@ void Lane::changeDebugName(int nameIdx_) // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... kRegKey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); }); // L: ... "name" ... // keep a direct pointer on the string - debugName = lua_tostring(L, nameIdx_); + debugName = lua_tostringview(L, nameIdx_); // to see VM name in Decoda debugger Virtual Machine window lua_pushvalue(L, nameIdx_); // L: ... "name" ... "name" lua_setglobal(L, "decoda_name"); // L: ... "name" ... // and finally set the OS thread name - THREAD_SETNAME(debugName); + THREAD_SETNAME(debugName.data()); STACK_CHECK(L, 0); } @@ -920,9 +920,7 @@ void Lane::securizeDebugName(lua_State* L_) LUA_ASSERT(L_, lua_istable(L_, -1)); // we don't care about the actual key, so long as it's unique and can't collide with anything. lua_newtable(L_); // L_: lane ... {uv} {} - // Lua 5.1 can't do 'lane_->debugName = lua_pushstring(L_, lane_->debugName);' - lua_pushstring(L_, debugName); // L_: lane ... {uv} {} name - debugName = lua_tostring(L_, -1); + debugName = lua_pushstringview(L_, debugName); // L_: lane ... {uv} {} name lua_rawset(L_, -3); // L_: lane ... {uv} lua_pop(L_, 1); // L_: lane STACK_CHECK(L_, 0); diff --git a/src/lane.h b/src/lane.h index 03f795b..10045c8 100644 --- a/src/lane.h +++ b/src/lane.h @@ -81,7 +81,7 @@ class Lane // M: sub-thread OS thread // S: not used - char const* debugName{ "" }; + std::string_view debugName{ "" }; Universe* const U; lua_State* L; diff --git a/src/lanes.cpp b/src/lanes.cpp index 74e2507..22391d5 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -317,16 +317,16 @@ LUAG_FUNC(lane_new) lua_State* _L2{ lane->L }; STACK_CHECK_START_REL(_L2, 0); int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; - char const* const debugName{ (_name_idx > 0) ? lua_tostring(L, _name_idx) : nullptr }; - if (debugName) + std::string_view const _debugName{ (_name_idx > 0) ? lua_tostringview(L, _name_idx) : std::string_view{} }; + if (!_debugName.empty()) { - if (strcmp(debugName, "auto") != 0) { - lua_pushstring(_L2, debugName); // L: ... lane L2: "" + if (_debugName != "auto") { + std::ignore = lua_pushstringview(_L2, _debugName); // L: ... lane L2: "" } else { - lua_Debug ar; + lua_Debug _ar; lua_pushvalue(L, 1); // L: ... lane func - lua_getinfo(L, ">S", &ar); // L: ... lane - lua_pushfstring(_L2, "%s:%d", ar.short_src, ar.linedefined); // L: ... lane L2: "" + lua_getinfo(L, ">S", &_ar); // L: ... lane + lua_pushfstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "" } lane->changeDebugName(-1); lua_pop(_L2, 1); // L: ... lane L2: @@ -701,11 +701,11 @@ LUAG_FUNC(configure) STACK_CHECK(L_, 2); { - char const* _errmsg{ + std::string_view const _errmsg{ DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, 0, LookupMode::LaneBody) }; // L_: settings M timerLinda - if (_errmsg != nullptr) { - raise_luaL_error(L_, _errmsg); + if (!_errmsg.empty()) { + raise_luaL_error(L_, _errmsg.data()); } lua_setfield(L_, -2, "timer_gateway"); // L_: settings M } diff --git a/src/tracker.cpp b/src/tracker.cpp index 76b814d..3040154 100644 --- a/src/tracker.cpp +++ b/src/tracker.cpp @@ -94,7 +94,7 @@ void LaneTracker::tracking_add(Lane* lane_) while (_lane != TRACKING_END) { // insert a { name='', status='' } tuple, so that several lanes with the same name can't clobber each other lua_createtable(L_, 0, 2); // L_: {} {} - lua_pushstring(L_, _lane->debugName); // L_: {} {} "name" + std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name" lua_setfield(L_, -2, "name"); // L_: {} {} _lane->pushThreadStatus(L_); // L_: {} {} "status" lua_setfield(L_, -2, "status"); // L_: {} {} diff --git a/src/universe.cpp b/src/universe.cpp index 52aa368..4f21306 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -357,7 +357,7 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim Lane* _lane{ selfdestructFirst }; if (_lane != SELFDESTRUCT_END) { // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it) - raise_luaL_error(L_, "Zombie thread %s refuses to die!", _lane->debugName); + raise_luaL_error(L_, "Zombie thread %s refuses to die!", _lane->debugName.data()); } } } -- cgit v1.2.3-55-g6feb