aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-10-25 15:06:41 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-10-25 15:06:41 +0200
commitb4bf1ad095452aabb12896040b522bf1746065a2 (patch)
treefdb4e8e7ddbf71e7f854f1c31dc149bfeb01c37e
parent8e33d8a2ca89d630f8890332df7e5737fc4608c8 (diff)
downloadlanes-b4bf1ad095452aabb12896040b522bf1746065a2.tar.gz
lanes-b4bf1ad095452aabb12896040b522bf1746065a2.tar.bz2
lanes-b4bf1ad095452aabb12896040b522bf1746065a2.zip
New compatibility helper luaG_rawgetfield
-rw-r--r--src/compat.h104
-rw-r--r--src/deep.cpp2
2 files changed, 58 insertions, 48 deletions
diff --git a/src/compat.h b/src/compat.h
index 0ea35bf..b1e8663 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -135,6 +135,53 @@ inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_)
135// ################################################################################################# 135// #################################################################################################
136// ################################################################################################# 136// #################################################################################################
137 137
138// must keep as a macro as long as we do constant string concatenations
139#define STRINGVIEW_FMT "%.*s"
140
141// a replacement of lua_tolstring
142[[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_)
143{
144 size_t _len{ 0 };
145 char const* _str{ lua_tolstring(L_, idx_, &_len) };
146 return _str ? std::string_view{ _str, _len } : "";
147}
148
149[[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_)
150{
151 size_t _len{ 0 };
152 char const* _str{ luaL_checklstring(L_, idx_, &_len) };
153 return std::string_view{ _str, _len };
154}
155
156[[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_)
157{
158 if (lua_isnoneornil(L_, idx_)) {
159 return default_;
160 }
161 size_t _len{ 0 };
162 char const* _str{ luaL_optlstring(L_, idx_, default_.data(), &_len) };
163 return std::string_view{ _str, _len };
164}
165
166template <typename... EXTRA>
167inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_)
168{
169 if constexpr (sizeof...(EXTRA) == 0) {
170 if constexpr (LUA_VERSION_NUM == 501) {
171 // lua_pushlstring doesn't return a value in Lua 5.1
172 lua_pushlstring(L_, str_.data(), str_.size());
173 return luaG_tostring(L_, kIdxTop);
174 } else {
175 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
176 }
177 } else {
178 static_assert((... && std::is_trivial_v<std::decay_t<EXTRA>>));
179 return std::string_view{ lua_pushfstring(L_, str_.data(), std::forward<EXTRA>(extra_)...) };
180 }
181}
182
183// #################################################################################################
184
138// use this in place of lua_absindex to save a function call 185// use this in place of lua_absindex to save a function call
139inline StackIndex luaG_absindex(lua_State* const L_, StackIndex const idx_) 186inline StackIndex luaG_absindex(lua_State* const L_, StackIndex const idx_)
140{ 187{
@@ -285,6 +332,16 @@ static inline LuaError luaG_resume(lua_State* const L_, lua_State* const from_,
285 332
286// ################################################################################################# 333// #################################################################################################
287 334
335static inline LuaType luaG_rawgetfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_)
336{
337 auto const _absIdx{ luaG_absindex(L_, idx_) };
338 luaG_pushstring(L_, name_); // L_: ... t ... name_
339 lua_rawget(L_, _absIdx); // L_: ... t ... <field>
340 return luaG_type(L_, kIdxTop);
341}
342
343// #################################################################################################
344
288template <size_t N> 345template <size_t N>
289static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) 346static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N])
290{ 347{
@@ -369,50 +426,3 @@ template <typename T>
369{ 426{
370 return luaG_typename(L_, luaG_type(L_, idx_)); 427 return luaG_typename(L_, luaG_type(L_, idx_));
371} 428}
372
373// #################################################################################################
374
375// must keep as a macro as long as we do constant string concatenations
376#define STRINGVIEW_FMT "%.*s"
377
378// a replacement of lua_tolstring
379[[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_)
380{
381 size_t _len{ 0 };
382 char const* _str{ lua_tolstring(L_, idx_, &_len) };
383 return _str ? std::string_view{ _str, _len } : "";
384}
385
386[[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_)
387{
388 size_t _len{ 0 };
389 char const* _str{ luaL_checklstring(L_, idx_, &_len) };
390 return std::string_view{ _str, _len };
391}
392
393[[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_)
394{
395 if (lua_isnoneornil(L_, idx_)) {
396 return default_;
397 }
398 size_t _len{ 0 };
399 char const* _str{ luaL_optlstring(L_, idx_, default_.data(), &_len) };
400 return std::string_view{ _str, _len };
401}
402
403template<typename ...EXTRA>
404inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_)
405{
406 if constexpr (sizeof...(EXTRA) == 0) {
407 if constexpr (LUA_VERSION_NUM == 501) {
408 // lua_pushlstring doesn't return a value in Lua 5.1
409 lua_pushlstring(L_, str_.data(), str_.size());
410 return luaG_tostring(L_, kIdxTop);
411 } else {
412 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
413 }
414 } else {
415 static_assert((... && std::is_trivial_v<std::decay_t<EXTRA>>));
416 return std::string_view{ lua_pushfstring(L_, str_.data(), std::forward<EXTRA>(extra_)...) };
417 }
418}
diff --git a/src/deep.cpp b/src/deep.cpp
index bfadeac..278d25f 100644
--- a/src/deep.cpp
+++ b/src/deep.cpp
@@ -216,7 +216,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_,
216 raise_luaL_error(errL_, "Bad DeepFactory::createMetatable overload: unexpected pushed value"); 216 raise_luaL_error(errL_, "Bad DeepFactory::createMetatable overload: unexpected pushed value");
217 } 217 }
218 // if the metatable contains a __gc, we will call it from our own 218 // if the metatable contains a __gc, we will call it from our own
219 std::ignore = luaG_getfield(L_, kIdxTop, "__gc"); // L_: DPC proxy metatable __gc 219 std::ignore = luaG_rawgetfield(L_, kIdxTop, "__gc"); // L_: DPC proxy metatable __gc
220 } else { 220 } else {
221 // keepers need a minimal metatable that only contains our own __gc 221 // keepers need a minimal metatable that only contains our own __gc
222 lua_createtable(L_, 0, 1); // L_: DPC proxy metatable 222 lua_createtable(L_, 0, 1); // L_: DPC proxy metatable