diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-06 17:20:12 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-06 17:20:12 +0200 |
commit | d9a149a230f9b320113020789beb78a883da3b40 (patch) | |
tree | 64925d938067e97b76d142cf915a5e1ce840246f | |
parent | 5fcbc757f62cdc9698a8f783864141f40fdff34e (diff) | |
download | lanes-d9a149a230f9b320113020789beb78a883da3b40.tar.gz lanes-d9a149a230f9b320113020789beb78a883da3b40.tar.bz2 lanes-d9a149a230f9b320113020789beb78a883da3b40.zip |
Converted a few more raw string pointers to std::string_view
-rw-r--r-- | deep_test/deep_test.cpp | 4 | ||||
-rw-r--r-- | src/compat.h | 30 | ||||
-rw-r--r-- | src/deep.cpp | 3 | ||||
-rw-r--r-- | src/intercopycontext.cpp | 8 | ||||
-rw-r--r-- | src/keeper.cpp | 2 | ||||
-rw-r--r-- | src/lane.cpp | 4 | ||||
-rw-r--r-- | src/lanes.cpp | 6 | ||||
-rw-r--r-- | 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 | |||
57 | { | 57 | { |
58 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, 1)) }; | 58 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, 1)) }; |
59 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); | 59 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); |
60 | lua_pushfstring(L, "%p:deep(%d)", lua_topointer(L, 1), _self->val); | 60 | std::ignore = luaG_pushstringview(L, "%p:deep(%d)", lua_topointer(L, 1), _self->val); |
61 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); | 61 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); |
62 | return 1; | 62 | return 1; |
63 | } | 63 | } |
@@ -177,7 +177,7 @@ struct MyClonableUserdata | |||
177 | [[nodiscard]] static int clonable_tostring(lua_State* L) | 177 | [[nodiscard]] static int clonable_tostring(lua_State* L) |
178 | { | 178 | { |
179 | MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); | 179 | MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); |
180 | lua_pushfstring(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val); | 180 | std::ignore = luaG_pushstringview(L, "%p:clonable(%d)", lua_topointer(L, 1), self->val); |
181 | return 1; | 181 | return 1; |
182 | } | 182 | } |
183 | 183 | ||
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 | |||
323 | #define STRINGVIEW_FMT "%.*s" | 323 | #define STRINGVIEW_FMT "%.*s" |
324 | 324 | ||
325 | // a replacement of lua_tolstring | 325 | // a replacement of lua_tolstring |
326 | [[nodiscard]] inline std::string_view luaG_tostringview(lua_State* L_, int idx_) | 326 | [[nodiscard]] inline std::string_view luaG_tostringview(lua_State* const L_, int const idx_) |
327 | { | 327 | { |
328 | size_t _len{ 0 }; | 328 | size_t _len{ 0 }; |
329 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; | 329 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; |
330 | return std::string_view{ _str, _len }; | 330 | return std::string_view{ _str, _len }; |
331 | } | 331 | } |
332 | 332 | ||
333 | [[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* L_, int idx_) | 333 | [[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* const L_, int const idx_) |
334 | { | 334 | { |
335 | size_t _len{ 0 }; | 335 | size_t _len{ 0 }; |
336 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; | 336 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; |
337 | return std::string_view{ _str, _len }; | 337 | return std::string_view{ _str, _len }; |
338 | } | 338 | } |
339 | 339 | ||
340 | [[nodiscard]] inline std::string_view luaG_optstringview(lua_State* L_, int idx_, std::string_view const& default_) | 340 | [[nodiscard]] inline std::string_view luaG_optstringview(lua_State* const L_, int const idx_, std::string_view const& default_) |
341 | { | 341 | { |
342 | if (lua_isnoneornil(L_, idx_)) { | 342 | if (lua_isnoneornil(L_, idx_)) { |
343 | return default_; | 343 | return default_; |
@@ -347,13 +347,19 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname | |||
347 | return std::string_view{ _str, _len }; | 347 | return std::string_view{ _str, _len }; |
348 | } | 348 | } |
349 | 349 | ||
350 | [[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* L_, std::string_view const& str_) | 350 | template<typename ...EXTRA> |
351 | [[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_) | ||
351 | { | 352 | { |
352 | #if LUA_VERSION_NUM == 501 | 353 | if constexpr (sizeof...(EXTRA) == 0) { |
353 | // lua_pushlstring doesn't return a value in Lua 5.1 | 354 | if constexpr (LUA_VERSION_NUM == 501) { |
354 | lua_pushlstring(L_, str_.data(), str_.size()); | 355 | // lua_pushlstring doesn't return a value in Lua 5.1 |
355 | return luaG_tostringview(L_, -1); | 356 | lua_pushlstring(L_, str_.data(), str_.size()); |
356 | #else | 357 | return luaG_tostringview(L_, -1); |
357 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; | 358 | } else { |
358 | #endif // LUA_VERSION_NUM > 501 | 359 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; |
359 | } \ No newline at end of file | 360 | } |
361 | } else { | ||
362 | static_assert((... && std::is_trivial_v<std::decay_t<EXTRA>>)); | ||
363 | return std::string_view{ lua_pushfstring(L_, str_.data(), std::forward<EXTRA>(extra_)...) }; | ||
364 | } | ||
365 | } | ||
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_, | |||
259 | LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? | 259 | LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? |
260 | if (_require_result != LuaError::OK) { | 260 | if (_require_result != LuaError::OK) { |
261 | // failed, raise the error in the proper state | 261 | // failed, raise the error in the proper state |
262 | std::ignore = luaG_pushstringview(errL_, luaG_tostringview(L_, -1)); | 262 | raise_luaL_error(errL_, luaG_tostringview(L_, -1)); |
263 | raise_lua_error(errL_); | ||
264 | } | 263 | } |
265 | } | 264 | } |
266 | } else { // already loaded, we are happy | 265 | } 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 { | |||
1188 | 1188 | ||
1189 | STACK_CHECK_START_REL(L1, 0); | 1189 | STACK_CHECK_START_REL(L1, 0); |
1190 | if (luaG_type(L1, L1_i) != LuaType::TABLE) { | 1190 | if (luaG_type(L1, L1_i) != LuaType::TABLE) { |
1191 | lua_pushfstring(L1, "expected package as table, got %s", luaL_typename(L1, L1_i)); | 1191 | std::string_view const _msg{ luaG_pushstringview(L1, "expected package as table, got a %s", luaL_typename(L1, L1_i)) }; |
1192 | STACK_CHECK(L1, 1); | 1192 | STACK_CHECK(L1, 1); |
1193 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 1193 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |
1194 | if (mode == LookupMode::LaneBody) { | 1194 | if (mode == LookupMode::LaneBody) { |
1195 | raise_lua_error(getErrL()); // that's ok, getErrL() is L1 in that case | 1195 | raise_luaL_error(getErrL(), _msg); |
1196 | } | 1196 | } |
1197 | return InterCopyResult::Error; | 1197 | return InterCopyResult::Error; |
1198 | } | 1198 | } |
@@ -1224,10 +1224,10 @@ namespace { | |||
1224 | if (_result == InterCopyResult::Success) { | 1224 | if (_result == InterCopyResult::Success) { |
1225 | lua_setfield(L2, -2, _entry.data()); // set package[entry] | 1225 | lua_setfield(L2, -2, _entry.data()); // set package[entry] |
1226 | } else { | 1226 | } else { |
1227 | lua_pushfstring(L1, "failed to copy package entry %s", _entry); | 1227 | std::string_view const _msg{ luaG_pushstringview(L1, "failed to copy package.%s", _entry.data()) }; |
1228 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 1228 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |
1229 | if (mode == LookupMode::LaneBody) { | 1229 | if (mode == LookupMode::LaneBody) { |
1230 | raise_lua_error(getErrL()); | 1230 | raise_luaL_error(getErrL(), _msg); |
1231 | } | 1231 | } |
1232 | lua_pop(L1, 1); | 1232 | lua_pop(L1, 1); |
1233 | break; | 1233 | 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 | |||
827 | keeper_.L = _K; | 827 | keeper_.L = _K; |
828 | 828 | ||
829 | // Give a name to the state | 829 | // Give a name to the state |
830 | lua_pushfstring(_K, "Keeper #%d", i_ + 1); // L_: settings _K: "Keeper #n" | 830 | std::ignore = luaG_pushstringview(_K, "Keeper #%d", i_ + 1); // L_: settings _K: "Keeper #n" |
831 | if constexpr (HAVE_DECODA_SUPPORT()) { | 831 | if constexpr (HAVE_DECODA_SUPPORT()) { |
832 | lua_pushvalue(_K, -1); // _K: "Keeper #n" Keeper #n" | 832 | lua_pushvalue(_K, -1); // _K: "Keeper #n" Keeper #n" |
833 | lua_setglobal(_K, "decoda_name"); // L_: settings _K: "Keeper #n" | 833 | 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 }; | |||
462 | lua_pushstring(L_, _ar.what); // L_: some_error {} {} what | 462 | lua_pushstring(L_, _ar.what); // L_: some_error {} {} what |
463 | lua_setfield(L_, -2, "what"); // L_: some_error {} {} | 463 | lua_setfield(L_, -2, "what"); // L_: some_error {} {} |
464 | } else if (_ar.currentline > 0) { | 464 | } else if (_ar.currentline > 0) { |
465 | lua_pushfstring(L_, "%s:%d", _ar.short_src, _ar.currentline); // L_: some_error {} "blah:blah" | 465 | std::ignore = luaG_pushstringview(L_, "%s:%d", _ar.short_src, _ar.currentline); // L_: some_error {} "blah:blah" |
466 | } else { | 466 | } else { |
467 | lua_pushfstring(L_, "%s:?", _ar.short_src); // L_: some_error {} "blah" | 467 | std::ignore = luaG_pushstringview(L_, "%s:?", _ar.short_src); // L_: some_error {} "blah" |
468 | } | 468 | } |
469 | lua_rawseti(L_, -2, static_cast<lua_Integer>(_n)); // L_: some_error {} | 469 | lua_rawseti(L_, -2, static_cast<lua_Integer>(_n)); // L_: some_error {} |
470 | } | 470 | } |
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) | |||
354 | lua_Debug _ar; | 354 | lua_Debug _ar; |
355 | lua_pushvalue(L, 1); // L: ... lane func | 355 | lua_pushvalue(L, 1); // L: ... lane func |
356 | lua_getinfo(L, ">S", &_ar); // L: ... lane | 356 | lua_getinfo(L, ">S", &_ar); // L: ... lane |
357 | lua_pushfstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "<name>" | 357 | std::ignore = luaG_pushstringview( |
358 | _L2, "%s:%d", _ar.short_src, _ar.linedefined | ||
359 | ); // L: ... lane L2: "<name>" | ||
358 | } | 360 | } |
359 | lane->changeDebugName(-1); | 361 | lane->changeDebugName(-1); |
360 | lua_pop(_L2, 1); // L: ... lane L2: | 362 | lua_pop(_L2, 1); // L: ... lane L2: |
@@ -706,7 +708,7 @@ LUAG_FUNC(configure) | |||
706 | lua_pushcclosure(L_, LG_require, 1); // L_: settings M lanes.require | 708 | lua_pushcclosure(L_, LG_require, 1); // L_: settings M lanes.require |
707 | lua_setfield(L_, -2, "require"); // L_: settings M | 709 | lua_setfield(L_, -2, "require"); // L_: settings M |
708 | 710 | ||
709 | lua_pushfstring( | 711 | std::ignore = luaG_pushstringview( |
710 | L_, | 712 | L_, |
711 | "%d.%d.%d", | 713 | "%d.%d.%d", |
712 | LANES_VERSION_MAJOR, | 714 | 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; | |||
21 | // ################################################################################################# | 21 | // ################################################################################################# |
22 | 22 | ||
23 | // use this instead of Lua's lua_error | 23 | // use this instead of Lua's lua_error |
24 | [[noreturn]] static inline void raise_lua_error(lua_State* L_) | 24 | [[noreturn]] static inline void raise_lua_error(lua_State* const L_) |
25 | { | 25 | { |
26 | std::ignore = lua_error(L_); // doesn't return | 26 | std::ignore = lua_error(L_); // doesn't return |
27 | assert(false); // we should never get here, but i'm paranoid | 27 | assert(false); // we should never get here, but i'm paranoid |
@@ -31,9 +31,9 @@ using namespace std::chrono_literals; | |||
31 | 31 | ||
32 | // use this instead of Lua's luaL_error | 32 | // use this instead of Lua's luaL_error |
33 | template <typename... ARGS> | 33 | template <typename... ARGS> |
34 | [[noreturn]] static inline void raise_luaL_error(lua_State* L_, char const* fmt_, ARGS... args_) | 34 | [[noreturn]] static inline void raise_luaL_error(lua_State* const L_, std::string_view const& fmt_, ARGS... args_) |
35 | { | 35 | { |
36 | std::ignore = luaL_error(L_, fmt_, std::forward<ARGS>(args_)...); // doesn't return | 36 | std::ignore = luaL_error(L_, fmt_.data(), std::forward<ARGS>(args_)...); // doesn't return |
37 | assert(false); // we should never get here, but i'm paranoid | 37 | assert(false); // we should never get here, but i'm paranoid |
38 | } | 38 | } |
39 | 39 | ||
@@ -41,9 +41,9 @@ template <typename... ARGS> | |||
41 | 41 | ||
42 | // use this instead of Lua's luaL_argerror | 42 | // use this instead of Lua's luaL_argerror |
43 | template <typename... ARGS> | 43 | template <typename... ARGS> |
44 | [[noreturn]] static inline void raise_luaL_argerror(lua_State* L_, int arg_, char const* extramsg_) | 44 | [[noreturn]] static inline void raise_luaL_argerror(lua_State* const L_, int const arg_, std::string_view const& extramsg_) |
45 | { | 45 | { |
46 | std::ignore = luaL_argerror(L_, arg_, extramsg_); // doesn't return | 46 | std::ignore = luaL_argerror(L_, arg_, extramsg_.data()); // doesn't return |
47 | assert(false); // we should never get here, but i'm paranoid | 47 | assert(false); // we should never get here, but i'm paranoid |
48 | } | 48 | } |
49 | 49 | ||
@@ -52,9 +52,9 @@ template <typename... ARGS> | |||
52 | #if LUA_VERSION_NUM >= 504 | 52 | #if LUA_VERSION_NUM >= 504 |
53 | // use this instead of Lua's luaL_typeerror | 53 | // use this instead of Lua's luaL_typeerror |
54 | template <typename... ARGS> | 54 | template <typename... ARGS> |
55 | [[noreturn]] static inline void raise_luaL_typeerror(lua_State* L_, int arg_, char const* tname_) | 55 | [[noreturn]] static inline void raise_luaL_typeerror(lua_State* const L_, int const arg_, std::string_view const& tname_) |
56 | { | 56 | { |
57 | std::ignore = luaL_typeerror(L_, arg_, tname_); // doesn't return | 57 | std::ignore = luaL_typeerror(L_, arg_, tname_.data()); // doesn't return |
58 | assert(false); // we should never get here, but i'm paranoid | 58 | assert(false); // we should never get here, but i'm paranoid |
59 | } | 59 | } |
60 | #endif // LUA_VERSION_NUM | 60 | #endif // LUA_VERSION_NUM |