diff options
Diffstat (limited to '')
-rw-r--r-- | src/compat.h | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/src/compat.h b/src/compat.h index 8b3eb98..90e72a3 100644 --- a/src/compat.h +++ b/src/compat.h | |||
@@ -32,6 +32,10 @@ | |||
32 | 32 | ||
33 | // ################################################################################################# | 33 | // ################################################################################################# |
34 | 34 | ||
35 | static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; | ||
36 | |||
37 | // ################################################################################################# | ||
38 | |||
35 | // a strong-typed wrapper over lua types to see them easier in a debugger | 39 | // a strong-typed wrapper over lua types to see them easier in a debugger |
36 | enum class LuaType | 40 | enum class LuaType |
37 | { | 41 | { |
@@ -53,13 +57,13 @@ enum class LuaType | |||
53 | // add some Lua 5.3-style API when building for Lua 5.1 | 57 | // add some Lua 5.3-style API when building for Lua 5.1 |
54 | #if LUA_VERSION_NUM == 501 | 58 | #if LUA_VERSION_NUM == 501 |
55 | 59 | ||
56 | inline size_t lua_rawlen(lua_State* L_, int idx_) | 60 | inline size_t lua_rawlen(lua_State* L_, StackIndex idx_) |
57 | { | 61 | { |
58 | return lua_objlen(L_, idx_); | 62 | return lua_objlen(L_, idx_); |
59 | } | 63 | } |
60 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources | 64 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources |
61 | 65 | ||
62 | int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); | 66 | int luaL_getsubtable(lua_State* L_, StackIndex idx_, const char* fname_); |
63 | 67 | ||
64 | #endif // LUA_VERSION_NUM == 501 | 68 | #endif // LUA_VERSION_NUM == 501 |
65 | 69 | ||
@@ -80,8 +84,8 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | |||
80 | #if LUA_VERSION_NUM < 504 | 84 | #if LUA_VERSION_NUM < 504 |
81 | 85 | ||
82 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_); | 86 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_); |
83 | int lua_getiuservalue(lua_State* L_, int idx_, int n_); | 87 | int lua_getiuservalue(lua_State* L_, StackIndex idx_, int n_); |
84 | int lua_setiuservalue(lua_State* L_, int idx_, int n_); | 88 | int lua_setiuservalue(lua_State* L_, StackIndex idx_, int n_); |
85 | 89 | ||
86 | #define LUA_GNAME "_G" | 90 | #define LUA_GNAME "_G" |
87 | 91 | ||
@@ -92,7 +96,7 @@ int lua_setiuservalue(lua_State* L_, int idx_, int n_); | |||
92 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way | 96 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way |
93 | #if LUA_VERSION_NUM == 504 | 97 | #if LUA_VERSION_NUM == 504 |
94 | 98 | ||
95 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | 99 | inline int luaL_optint(lua_State* L_, StackIndex n_, lua_Integer d_) |
96 | { | 100 | { |
97 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | 101 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); |
98 | } | 102 | } |
@@ -123,7 +127,7 @@ inline constexpr LuaError ToLuaError(int const rc_) | |||
123 | // ################################################################################################# | 127 | // ################################################################################################# |
124 | 128 | ||
125 | // break lexical order for that one because it's needed below | 129 | // break lexical order for that one because it's needed below |
126 | inline LuaType luaG_type(lua_State* const L_, int const idx_) | 130 | inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_) |
127 | { | 131 | { |
128 | return static_cast<LuaType>(lua_type(L_, idx_)); | 132 | return static_cast<LuaType>(lua_type(L_, idx_)); |
129 | } | 133 | } |
@@ -135,9 +139,9 @@ inline LuaType luaG_type(lua_State* const L_, int const idx_) | |||
135 | // ################################################################################################# | 139 | // ################################################################################################# |
136 | 140 | ||
137 | // use this in place of lua_absindex to save a function call | 141 | // use this in place of lua_absindex to save a function call |
138 | inline int luaG_absindex(lua_State* L_, int idx_) | 142 | inline StackIndex luaG_absindex(lua_State* const L_, StackIndex const idx_) |
139 | { | 143 | { |
140 | return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); | 144 | return StackIndex{ (idx_ >= 0 || idx_ <= kIdxRegistry) ? idx_ : lua_gettop(L_) + idx_ + 1 }; |
141 | } | 145 | } |
142 | 146 | ||
143 | // ################################################################################################# | 147 | // ################################################################################################# |
@@ -171,7 +175,7 @@ static inline int luaG_dump(lua_State* const L_, lua_Writer const writer_, void* | |||
171 | 175 | ||
172 | // ################################################################################################# | 176 | // ################################################################################################# |
173 | 177 | ||
174 | int luaG_getalluservalues(lua_State* L_, int idx_); | 178 | int luaG_getalluservalues(lua_State* L_, StackIndex idx_); |
175 | 179 | ||
176 | // ################################################################################################# | 180 | // ################################################################################################# |
177 | 181 | ||
@@ -184,7 +188,7 @@ concept RequiresOldLuaGetfield = requires(LUA_GETFIELD f_) | |||
184 | }; | 188 | }; |
185 | 189 | ||
186 | template <RequiresOldLuaGetfield LUA_GETFIELD> | 190 | template <RequiresOldLuaGetfield LUA_GETFIELD> |
187 | static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, int const idx_, std::string_view const& name_) | 191 | static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, StackIndex const idx_, std::string_view const& name_) |
188 | { | 192 | { |
189 | f_(L_, idx_, name_.data()); | 193 | f_(L_, idx_, name_.data()); |
190 | return lua_type(L_, -1); | 194 | return lua_type(L_, -1); |
@@ -201,14 +205,14 @@ concept RequiresNewLuaGetfield = requires(LUA_GETFIELD f_) | |||
201 | }; | 205 | }; |
202 | 206 | ||
203 | template <RequiresNewLuaGetfield LUA_GETFIELD> | 207 | template <RequiresNewLuaGetfield LUA_GETFIELD> |
204 | static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, int const idx_, std::string_view const& name_) | 208 | static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, StackIndex const idx_, std::string_view const& name_) |
205 | { | 209 | { |
206 | return f_(L_, idx_, name_.data()); | 210 | return f_(L_, idx_, name_.data()); |
207 | } | 211 | } |
208 | 212 | ||
209 | // ------------------------------------------------------------------------------------------------- | 213 | // ------------------------------------------------------------------------------------------------- |
210 | 214 | ||
211 | static inline LuaType luaG_getfield(lua_State* const L_, int const idx_, std::string_view const& name_) | 215 | static inline LuaType luaG_getfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_) |
212 | { | 216 | { |
213 | return static_cast<LuaType>(WrapLuaGetField(lua_getfield, L_, idx_, name_)); | 217 | return static_cast<LuaType>(WrapLuaGetField(lua_getfield, L_, idx_, name_)); |
214 | } | 218 | } |
@@ -306,14 +310,14 @@ inline void luaG_pushglobaltable(lua_State* const L_) | |||
306 | #ifdef LUA_GLOBALSINDEX // All flavors of Lua 5.1 | 310 | #ifdef LUA_GLOBALSINDEX // All flavors of Lua 5.1 |
307 | ::lua_pushvalue(L_, LUA_GLOBALSINDEX); | 311 | ::lua_pushvalue(L_, LUA_GLOBALSINDEX); |
308 | #else // LUA_GLOBALSINDEX | 312 | #else // LUA_GLOBALSINDEX |
309 | ::lua_rawgeti(L_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); | 313 | ::lua_rawgeti(L_, kIdxRegistry, LUA_RIDX_GLOBALS); |
310 | #endif // LUA_GLOBALSINDEX | 314 | #endif // LUA_GLOBALSINDEX |
311 | } | 315 | } |
312 | 316 | ||
313 | // ################################################################################################# | 317 | // ################################################################################################# |
314 | 318 | ||
315 | inline void luaG_setfield(lua_State* const L_, int const idx_, char const* k_) = delete; | 319 | inline void luaG_setfield(lua_State* const L_, StackIndex const idx_, char const* k_) = delete; |
316 | inline void luaG_setfield(lua_State* const L_, int const idx_, std::string_view const& k_) | 320 | inline void luaG_setfield(lua_State* const L_, StackIndex const idx_, std::string_view const& k_) |
317 | { | 321 | { |
318 | lua_setfield(L_, idx_, k_.data()); | 322 | lua_setfield(L_, idx_, k_.data()); |
319 | } | 323 | } |
@@ -336,7 +340,7 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname | |||
336 | 340 | ||
337 | // a small helper to extract a full userdata pointer from the stack in a safe way | 341 | // a small helper to extract a full userdata pointer from the stack in a safe way |
338 | template <typename T> | 342 | template <typename T> |
339 | [[nodiscard]] T* luaG_tofulluserdata(lua_State* const L_, int const index_) | 343 | [[nodiscard]] T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_) |
340 | { | 344 | { |
341 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); | 345 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); |
342 | return static_cast<T*>(lua_touserdata(L_, index_)); | 346 | return static_cast<T*>(lua_touserdata(L_, index_)); |
@@ -345,7 +349,7 @@ template <typename T> | |||
345 | // ------------------------------------------------------------------------------------------------- | 349 | // ------------------------------------------------------------------------------------------------- |
346 | 350 | ||
347 | template <typename T> | 351 | template <typename T> |
348 | [[nodiscard]] auto luaG_tolightuserdata(lua_State* const L_, int const index_) | 352 | [[nodiscard]] auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_) |
349 | { | 353 | { |
350 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); | 354 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); |
351 | if constexpr (std::is_pointer_v<T>) { | 355 | if constexpr (std::is_pointer_v<T>) { |
@@ -364,7 +368,7 @@ template <typename T> | |||
364 | 368 | ||
365 | // ------------------------------------------------------------------------------------------------- | 369 | // ------------------------------------------------------------------------------------------------- |
366 | 370 | ||
367 | [[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, int const idx_) | 371 | [[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_) |
368 | { | 372 | { |
369 | return luaG_typename(L_, luaG_type(L_, idx_)); | 373 | return luaG_typename(L_, luaG_type(L_, idx_)); |
370 | } | 374 | } |
@@ -375,21 +379,21 @@ template <typename T> | |||
375 | #define STRINGVIEW_FMT "%.*s" | 379 | #define STRINGVIEW_FMT "%.*s" |
376 | 380 | ||
377 | // a replacement of lua_tolstring | 381 | // a replacement of lua_tolstring |
378 | [[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, int const idx_) | 382 | [[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_) |
379 | { | 383 | { |
380 | size_t _len{ 0 }; | 384 | size_t _len{ 0 }; |
381 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; | 385 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; |
382 | return _str ? std::string_view{ _str, _len } : ""; | 386 | return _str ? std::string_view{ _str, _len } : ""; |
383 | } | 387 | } |
384 | 388 | ||
385 | [[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, int const idx_) | 389 | [[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_) |
386 | { | 390 | { |
387 | size_t _len{ 0 }; | 391 | size_t _len{ 0 }; |
388 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; | 392 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; |
389 | return std::string_view{ _str, _len }; | 393 | return std::string_view{ _str, _len }; |
390 | } | 394 | } |
391 | 395 | ||
392 | [[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, int const idx_, std::string_view const& default_) | 396 | [[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_) |
393 | { | 397 | { |
394 | if (lua_isnoneornil(L_, idx_)) { | 398 | if (lua_isnoneornil(L_, idx_)) { |
395 | return default_; | 399 | return default_; |
@@ -406,7 +410,7 @@ inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view co | |||
406 | if constexpr (LUA_VERSION_NUM == 501) { | 410 | if constexpr (LUA_VERSION_NUM == 501) { |
407 | // lua_pushlstring doesn't return a value in Lua 5.1 | 411 | // lua_pushlstring doesn't return a value in Lua 5.1 |
408 | lua_pushlstring(L_, str_.data(), str_.size()); | 412 | lua_pushlstring(L_, str_.data(), str_.size()); |
409 | return luaG_tostring(L_, -1); | 413 | return luaG_tostring(L_, kIdxTop); |
410 | } else { | 414 | } else { |
411 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; | 415 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; |
412 | } | 416 | } |