From 3763be94cdd1a5cf26fec0f09784b18188fd3054 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 10 Jun 2024 14:51:17 +0200 Subject: luaG_typename returns a std::string_view --- src/compat.h | 13 ++++++++++--- src/intercopycontext.cpp | 2 +- src/lane.cpp | 4 ++-- src/lanes.cpp | 10 +++++----- src/state.cpp | 2 +- src/tools.cpp | 6 +++--- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/compat.h b/src/compat.h index bc8b165..e0a2e6f 100644 --- a/src/compat.h +++ b/src/compat.h @@ -357,7 +357,7 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname // a small helper to extract a full userdata pointer from the stack in a safe way template -[[nodiscard]] T* luaG_tofulluserdata(lua_State* L_, int index_) +[[nodiscard]] T* luaG_tofulluserdata(lua_State* const L_, int const index_) { LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); return static_cast(lua_touserdata(L_, index_)); @@ -366,7 +366,7 @@ template // ------------------------------------------------------------------------------------------------- template -[[nodiscard]] auto luaG_tolightuserdata(lua_State* L_, int index_) +[[nodiscard]] auto luaG_tolightuserdata(lua_State* const L_, int const index_) { LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); if constexpr (std::is_pointer_v) { @@ -378,11 +378,18 @@ template // ------------------------------------------------------------------------------------------------- -inline char const* luaG_typename(lua_State* L_, LuaType t_) +[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_) { return lua_typename(L_, static_cast(t_)); } +// ------------------------------------------------------------------------------------------------- + +[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, int const idx_) +{ + return luaG_typename(L_, luaG_type(L_, idx_)); +} + // ################################################################################################# // must keep as a macro as long as we do constant string concatenations diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index fcec38a..c7fcf14 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -462,7 +462,7 @@ void InterCopyContext::copy_cached_func() const "%s: source table '%s' found as %s in %s destination transfer database.", _from ? _from : "main", _fqn, - luaG_typename(L2, luaG_type(L2, -1)), + luaG_typename(L2, -1).data(), _to ? _to : "main"); } lua_remove(L2, -2); // L1: ... t ... L2: t diff --git a/src/lane.cpp b/src/lane.cpp index de513c2..3bb98b9 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -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.data(), luaG_typename(L_, luaG_type(L_, kKey))); + raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName.data(), luaG_typename(L_, kKey).data()); } } @@ -683,7 +683,7 @@ static void lane_main(Lane* lane_) // in case of error and if it exists, fetch stack trace from registry and push it push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] - DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, luaG_type(_L, 1))) << ")" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); // Call finalizers, if the script has set them up. // LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; diff --git a/src/lanes.cpp b/src/lanes.cpp index b7cd148..9b3b28c 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -479,7 +479,7 @@ LUAG_FUNC(lane_new) STACK_CHECK(_L2, 0); // Lane main function - [[maybe_unused]] int const errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? + [[maybe_unused]] int const _errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? LuaType const _func_type{ luaG_type(L_, kFuncIdx) }; if (_func_type == LuaType::FUNCTION) { DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl); @@ -497,11 +497,11 @@ LUAG_FUNC(lane_new) raise_luaL_error(L_, "error when parsing lane function code"); } } else { - raise_luaL_error(L_, "Expected function, got %s", luaG_typename(L_, _func_type)); + raise_luaL_error(L_, "Expected function, got %s", luaG_typename(L_, _func_type).data()); } STACK_CHECK(L_, 0); - STACK_CHECK(_L2, errorHandlerCount + 1); - LUA_ASSERT(L_, lua_isfunction(_L2, errorHandlerCount + 1)); + STACK_CHECK(_L2, _errorHandlerCount + 1); + LUA_ASSERT(L_, lua_isfunction(_L2, _errorHandlerCount + 1)); // revive arguments if (_nargs > 0) { @@ -520,7 +520,7 @@ LUAG_FUNC(lane_new) kLanePointerRegKey.setValue( _L2, [lane = _lane](lua_State* L_) { lua_pushlightuserdata(L_, lane); } // L_: [fixed] L2: eh? func args... ); - STACK_CHECK(_L2, errorHandlerCount + 1 + _nargs); + STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs); STACK_CHECK_RESET_REL(L_, 0); // all went well, the lane's thread can start working diff --git a/src/state.cpp b/src/state.cpp index 77e1fd9..50981a6 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -185,7 +185,7 @@ namespace state { std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; std::ignore = luaG_pushstring(L_, _stateType); // L_: on_state_create() "" if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { - raise_luaL_error(from_, "%s failed: \"%s\"", kOnStateCreate, lua_isstring(L_, -1) ? lua_tostring(L_, -1) : luaG_typename(L_, luaG_type(L_, -1))); + raise_luaL_error(from_, "%s failed: \"%s\"", kOnStateCreate, lua_isstring(L_, -1) ? lua_tostring(L_, -1) : luaG_typename(L_, -1).data()); } STACK_CHECK(L_, 0); } diff --git a/src/tools.cpp b/src/tools.cpp index efded98..49872f7 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -158,7 +158,7 @@ static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_) // Therefore, when we encounter an object for which a name was previously registered, we need to select the a single name // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded if (!_prevName.empty() && ((_prevName.size() < _newName.size()) || (_prevName <= _newName))) { - DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, luaG_type(L_, -3)) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, -3) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); // the previous name is 'smaller' than the one we just generated: keep it! lua_pop(L_, 3); // L_: ... {bfc} k } else { @@ -172,7 +172,7 @@ static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_) } else { lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" } - DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, luaG_type(L_, -2)) << " '" << _newName << "'" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, -2) << " '" << _newName << "'" << std::endl); // prepare the stack for database feed lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o @@ -349,7 +349,7 @@ namespace tools { lua_pop(L_, 3); // L_: } else { lua_pop(L_, 1); // L_: - raise_luaL_error(L_, "unsupported module type %s", luaG_typename(L_, luaG_type(L_, _in_base))); + raise_luaL_error(L_, "unsupported module type %s", luaG_typename(L_, _in_base).data()); } STACK_CHECK(L_, 0); } -- cgit v1.2.3-55-g6feb