From d9a149a230f9b320113020789beb78a883da3b40 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 6 Jun 2024 17:20:12 +0200 Subject: Converted a few more raw string pointers to std::string_view --- deep_test/deep_test.cpp | 4 ++-- src/compat.h | 30 ++++++++++++++++++------------ src/deep.cpp | 3 +-- src/intercopycontext.cpp | 8 ++++---- src/keeper.cpp | 2 +- src/lane.cpp | 4 ++-- src/lanes.cpp | 6 ++++-- src/macros_and_utils.h | 14 +++++++------- 8 files changed, 39 insertions(+), 32 deletions(-) diff --git a/deep_test/deep_test.cpp b/deep_test/deep_test.cpp index c330cbe..c75c360 100644 --- a/deep_test/deep_test.cpp +++ b/deep_test/deep_test.cpp @@ -57,7 +57,7 @@ void MyDeepFactory::deleteDeepObjectInternal(lua_State* const L_, DeepPrelude* c { MyDeepUserdata* const _self{ static_cast(MyDeepFactory::Instance.toDeep(L, 1)) }; _self->inUse.fetch_add(1, std::memory_order_seq_cst); - lua_pushfstring(L, "%p:deep(%d)", lua_topointer(L, 1), _self->val); + std::ignore = luaG_pushstringview(L, "%p:deep(%d)", lua_topointer(L, 1), _self->val); _self->inUse.fetch_sub(1, std::memory_order_seq_cst); return 1; } @@ -177,7 +177,7 @@ struct MyClonableUserdata [[nodiscard]] static int clonable_tostring(lua_State* L) { MyClonableUserdata* self = static_cast(lua_touserdata(L, 1)); - lua_pushfstring(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val); + std::ignore = luaG_pushstringview(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val); return 1; } diff --git a/src/compat.h b/src/compat.h index 58af985..0ae7759 100644 --- a/src/compat.h +++ b/src/compat.h @@ -323,21 +323,21 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname #define STRINGVIEW_FMT "%.*s" // a replacement of lua_tolstring -[[nodiscard]] inline std::string_view luaG_tostringview(lua_State* L_, int idx_) +[[nodiscard]] inline std::string_view luaG_tostringview(lua_State* const L_, int const idx_) { size_t _len{ 0 }; char const* _str{ lua_tolstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* L_, int idx_) +[[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* const L_, int const idx_) { size_t _len{ 0 }; char const* _str{ luaL_checklstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaG_optstringview(lua_State* L_, int idx_, std::string_view const& default_) +[[nodiscard]] inline std::string_view luaG_optstringview(lua_State* const L_, int const idx_, std::string_view const& default_) { if (lua_isnoneornil(L_, idx_)) { return default_; @@ -347,13 +347,19 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* L_, std::string_view const& str_) +template +[[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_) { -#if LUA_VERSION_NUM == 501 - // lua_pushlstring doesn't return a value in Lua 5.1 - lua_pushlstring(L_, str_.data(), str_.size()); - return luaG_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 + if constexpr (sizeof...(EXTRA) == 0) { + if constexpr (LUA_VERSION_NUM == 501) { + // lua_pushlstring doesn't return a value in Lua 5.1 + lua_pushlstring(L_, str_.data(), str_.size()); + return luaG_tostringview(L_, -1); + } else { + return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; + } + } else { + static_assert((... && std::is_trivial_v>)); + return std::string_view{ lua_pushfstring(L_, str_.data(), std::forward(extra_)...) }; + } +} diff --git a/src/deep.cpp b/src/deep.cpp index 97442cf..9538fdf 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -259,8 +259,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? if (_require_result != LuaError::OK) { // failed, raise the error in the proper state - std::ignore = luaG_pushstringview(errL_, luaG_tostringview(L_, -1)); - raise_lua_error(errL_); + raise_luaL_error(errL_, luaG_tostringview(L_, -1)); } } } else { // already loaded, we are happy diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index aa802d5..375a403 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -1188,11 +1188,11 @@ namespace { STACK_CHECK_START_REL(L1, 0); if (luaG_type(L1, L1_i) != LuaType::TABLE) { - lua_pushfstring(L1, "expected package as table, got %s", luaL_typename(L1, L1_i)); + std::string_view const _msg{ luaG_pushstringview(L1, "expected package as table, got a %s", luaL_typename(L1, L1_i)) }; STACK_CHECK(L1, 1); // raise the error when copying from lane to lane, else just leave it on the stack to be raised later if (mode == LookupMode::LaneBody) { - raise_lua_error(getErrL()); // that's ok, getErrL() is L1 in that case + raise_luaL_error(getErrL(), _msg); } return InterCopyResult::Error; } @@ -1224,10 +1224,10 @@ namespace { if (_result == InterCopyResult::Success) { lua_setfield(L2, -2, _entry.data()); // set package[entry] } else { - lua_pushfstring(L1, "failed to copy package entry %s", _entry); + std::string_view const _msg{ luaG_pushstringview(L1, "failed to copy package.%s", _entry.data()) }; // raise the error when copying from lane to lane, else just leave it on the stack to be raised later if (mode == LookupMode::LaneBody) { - raise_lua_error(getErrL()); + raise_luaL_error(getErrL(), _msg); } lua_pop(L1, 1); break; diff --git a/src/keeper.cpp b/src/keeper.cpp index d99cc50..3e806f9 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -827,7 +827,7 @@ void Keepers::initialize(Universe& U_, lua_State* L_, int const nbKeepers_, int keeper_.L = _K; // Give a name to the state - lua_pushfstring(_K, "Keeper #%d", i_ + 1); // L_: settings _K: "Keeper #n" + std::ignore = luaG_pushstringview(_K, "Keeper #%d", i_ + 1); // L_: settings _K: "Keeper #n" if constexpr (HAVE_DECODA_SUPPORT()) { lua_pushvalue(_K, -1); // _K: "Keeper #n" Keeper #n" lua_setglobal(_K, "decoda_name"); // L_: settings _K: "Keeper #n" diff --git a/src/lane.cpp b/src/lane.cpp index 2a2030d..b55b611 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -462,9 +462,9 @@ static constexpr RegistryUniqueKey kStackTraceRegKey{ 0x3F327747CACAA904ull }; lua_pushstring(L_, _ar.what); // L_: some_error {} {} what lua_setfield(L_, -2, "what"); // L_: some_error {} {} } else if (_ar.currentline > 0) { - lua_pushfstring(L_, "%s:%d", _ar.short_src, _ar.currentline); // L_: some_error {} "blah:blah" + std::ignore = luaG_pushstringview(L_, "%s:%d", _ar.short_src, _ar.currentline); // L_: some_error {} "blah:blah" } else { - lua_pushfstring(L_, "%s:?", _ar.short_src); // L_: some_error {} "blah" + std::ignore = luaG_pushstringview(L_, "%s:?", _ar.short_src); // L_: some_error {} "blah" } lua_rawseti(L_, -2, static_cast(_n)); // L_: some_error {} } diff --git a/src/lanes.cpp b/src/lanes.cpp index a36e051..c3d0928 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -354,7 +354,9 @@ LUAG_FUNC(lane_new) 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: "" + std::ignore = luaG_pushstringview( + _L2, "%s:%d", _ar.short_src, _ar.linedefined + ); // L: ... lane L2: "" } lane->changeDebugName(-1); lua_pop(_L2, 1); // L: ... lane L2: @@ -706,7 +708,7 @@ LUAG_FUNC(configure) lua_pushcclosure(L_, LG_require, 1); // L_: settings M lanes.require lua_setfield(L_, -2, "require"); // L_: settings M - lua_pushfstring( + std::ignore = luaG_pushstringview( L_, "%d.%d.%d", LANES_VERSION_MAJOR, diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 7bb0d2a..926e756 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -21,7 +21,7 @@ using namespace std::chrono_literals; // ################################################################################################# // use this instead of Lua's lua_error -[[noreturn]] static inline void raise_lua_error(lua_State* L_) +[[noreturn]] static inline void raise_lua_error(lua_State* const L_) { std::ignore = lua_error(L_); // doesn't return assert(false); // we should never get here, but i'm paranoid @@ -31,9 +31,9 @@ using namespace std::chrono_literals; // use this instead of Lua's luaL_error template -[[noreturn]] static inline void raise_luaL_error(lua_State* L_, char const* fmt_, ARGS... args_) +[[noreturn]] static inline void raise_luaL_error(lua_State* const L_, std::string_view const& fmt_, ARGS... args_) { - std::ignore = luaL_error(L_, fmt_, std::forward(args_)...); // doesn't return + std::ignore = luaL_error(L_, fmt_.data(), std::forward(args_)...); // doesn't return assert(false); // we should never get here, but i'm paranoid } @@ -41,9 +41,9 @@ template // use this instead of Lua's luaL_argerror template -[[noreturn]] static inline void raise_luaL_argerror(lua_State* L_, int arg_, char const* extramsg_) +[[noreturn]] static inline void raise_luaL_argerror(lua_State* const L_, int const arg_, std::string_view const& extramsg_) { - std::ignore = luaL_argerror(L_, arg_, extramsg_); // doesn't return + std::ignore = luaL_argerror(L_, arg_, extramsg_.data()); // doesn't return assert(false); // we should never get here, but i'm paranoid } @@ -52,9 +52,9 @@ template #if LUA_VERSION_NUM >= 504 // use this instead of Lua's luaL_typeerror template -[[noreturn]] static inline void raise_luaL_typeerror(lua_State* L_, int arg_, char const* tname_) +[[noreturn]] static inline void raise_luaL_typeerror(lua_State* const L_, int const arg_, std::string_view const& tname_) { - std::ignore = luaL_typeerror(L_, arg_, tname_); // doesn't return + std::ignore = luaL_typeerror(L_, arg_, tname_.data()); // doesn't return assert(false); // we should never get here, but i'm paranoid } #endif // LUA_VERSION_NUM -- cgit v1.2.3-55-g6feb