aboutsummaryrefslogtreecommitdiff
path: root/src/compat.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat.hpp')
-rw-r--r--src/compat.hpp138
1 files changed, 81 insertions, 57 deletions
diff --git a/src/compat.hpp b/src/compat.hpp
index 9a8dedf..4dc7433 100644
--- a/src/compat.hpp
+++ b/src/compat.hpp
@@ -5,7 +5,6 @@
5 5
6// try to detect if we are building against LuaJIT or MoonJIT 6// try to detect if we are building against LuaJIT or MoonJIT
7#if defined(LUA_JITLIBNAME) 7#if defined(LUA_JITLIBNAME)
8#include "luajit.h"
9#if (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__)) 8#if (defined(__x86_64__) || defined(_M_X64) || defined(__LP64__))
10#define LUAJIT_FLAVOR() 64 9#define LUAJIT_FLAVOR() 64
11#else // 64 bits 10#else // 64 bits
@@ -21,7 +20,7 @@
21#endif // LUA_OK 20#endif // LUA_OK
22 21
23#ifndef LUA_ERRGCMM 22#ifndef LUA_ERRGCMM
24#define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1 and Lua 5.4, we don't care about the actual value 23#define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1 and Lua 5.4/5.5, we don't care about the actual value
25#endif // LUA_ERRGCMM 24#endif // LUA_ERRGCMM
26 25
27 26
@@ -29,7 +28,7 @@
29#define LUA_LOADED_TABLE "_LOADED" // doesn't exist before Lua 5.3 28#define LUA_LOADED_TABLE "_LOADED" // doesn't exist before Lua 5.3
30#endif // LUA_LOADED_TABLE 29#endif // LUA_LOADED_TABLE
31 30
32// code is now preferring Lua 5.4 API 31// code is now preferring Lua 5.5 API
33 32
34// ################################################################################################# 33// #################################################################################################
35 34
@@ -76,18 +75,6 @@ int luaL_getsubtable(lua_State* L_, StackIndex idx_, char const* fname_);
76 75
77// ################################################################################################# 76// #################################################################################################
78 77
79// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way
80#if LUA_VERSION_NUM == 503
81
82inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_)
83{
84 return static_cast<int>(luaL_optinteger(L_, n_, d_));
85}
86
87#endif // LUA_VERSION_NUM == 503
88
89// #################################################################################################
90
91#if LUA_VERSION_NUM < 504 78#if LUA_VERSION_NUM < 504
92 79
93void* lua_newuserdatauv(lua_State* L_, size_t sz_, UserValueCount nuvalue_); 80void* lua_newuserdatauv(lua_State* L_, size_t sz_, UserValueCount nuvalue_);
@@ -100,15 +87,11 @@ int lua_setiuservalue(lua_State* L_, StackIndex idx_, UserValueIndex n_);
100 87
101// ################################################################################################# 88// #################################################################################################
102 89
103// wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way 90#if LUA_VERSION_NUM < 505
104#if LUA_VERSION_NUM == 504
105 91
106inline int luaL_optint(lua_State* L_, StackIndex n_, lua_Integer d_) 92unsigned int luaL_makeseed(lua_State*);
107{
108 return static_cast<int>(luaL_optinteger(L_, n_, d_));
109}
110 93
111#endif // LUA_VERSION_NUM == 504 94#endif // LUA_VERSION_NUM < 505
112 95
113// ################################################################################################# 96// #################################################################################################
114 97
@@ -136,14 +119,14 @@ inline constexpr LuaError ToLuaError(int const rc_)
136 119
137// break lexical order for that one because it's needed below 120// break lexical order for that one because it's needed below
138[[nodiscard]] 121[[nodiscard]]
139inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_) 122inline LuaType luaW_type(lua_State* const L_, StackIndex const idx_)
140{ 123{
141 return static_cast<LuaType>(lua_type(L_, idx_)); 124 return static_cast<LuaType>(lua_type(L_, idx_));
142} 125}
143 126
144// ################################################################################################# 127// #################################################################################################
145// ################################################################################################# 128// #################################################################################################
146// All the compatibility wrappers we expose start with luaG_ 129// All the compatibility wrappers we expose start with luaW_
147// ################################################################################################# 130// #################################################################################################
148// ################################################################################################# 131// #################################################################################################
149 132
@@ -152,7 +135,7 @@ inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_)
152 135
153// a replacement of lua_tolstring 136// a replacement of lua_tolstring
154[[nodiscard]] 137[[nodiscard]]
155inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_) 138inline std::string_view luaW_tostring(lua_State* const L_, StackIndex const idx_)
156{ 139{
157 size_t _len{ 0 }; 140 size_t _len{ 0 };
158 char const* _str{ lua_tolstring(L_, idx_, &_len) }; 141 char const* _str{ lua_tolstring(L_, idx_, &_len) };
@@ -160,7 +143,7 @@ inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_
160} 143}
161 144
162[[nodiscard]] 145[[nodiscard]]
163inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_) 146inline std::string_view luaW_checkstring(lua_State* const L_, StackIndex const idx_)
164{ 147{
165 size_t _len{ 0 }; 148 size_t _len{ 0 };
166 char const* _str{ luaL_checklstring(L_, idx_, &_len) }; 149 char const* _str{ luaL_checklstring(L_, idx_, &_len) };
@@ -168,7 +151,7 @@ inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const i
168} 151}
169 152
170[[nodiscard]] 153[[nodiscard]]
171inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_) 154inline std::string_view luaW_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_)
172{ 155{
173 if (lua_isnoneornil(L_, idx_)) { 156 if (lua_isnoneornil(L_, idx_)) {
174 return default_; 157 return default_;
@@ -179,13 +162,13 @@ inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx
179} 162}
180 163
181template <typename... EXTRA> 164template <typename... EXTRA>
182inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_) 165inline std::string_view luaW_pushstring(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_)
183{ 166{
184 if constexpr (sizeof...(EXTRA) == 0) { 167 if constexpr (sizeof...(EXTRA) == 0) {
185 if constexpr (LUA_VERSION_NUM == 501) { 168 if constexpr (LUA_VERSION_NUM == 501) {
186 // lua_pushlstring doesn't return a value in Lua 5.1 169 // lua_pushlstring doesn't return a value in Lua 5.1
187 lua_pushlstring(L_, str_.data(), str_.size()); 170 lua_pushlstring(L_, str_.data(), str_.size());
188 return luaG_tostring(L_, kIdxTop); 171 return luaW_tostring(L_, kIdxTop);
189 } else { 172 } else {
190 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; 173 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
191 } 174 }
@@ -198,7 +181,7 @@ inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view co
198// ################################################################################################# 181// #################################################################################################
199 182
200// use this in place of lua_absindex to save a function call 183// use this in place of lua_absindex to save a function call
201inline StackIndex luaG_absindex(lua_State* const L_, StackIndex const idx_) 184inline StackIndex luaW_absindex(lua_State* const L_, StackIndex const idx_)
202{ 185{
203 return StackIndex{ (idx_ >= 0 || idx_ <= kIdxRegistry) ? idx_ : StackIndex{ lua_gettop(L_) + idx_ + 1 } }; 186 return StackIndex{ (idx_ >= 0 || idx_ <= kIdxRegistry) ? idx_ : StackIndex{ lua_gettop(L_) + idx_ + 1 } };
204} 187}
@@ -227,14 +210,14 @@ static inline int WrapLuaDump(LUA_DUMP f_, lua_State* const L_, lua_Writer const
227 210
228// ------------------------------------------------------------------------------------------------- 211// -------------------------------------------------------------------------------------------------
229 212
230static inline int luaG_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) 213static inline int luaW_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_)
231{ 214{
232 return WrapLuaDump(lua_dump, L_, writer_, data_, strip_); 215 return WrapLuaDump(lua_dump, L_, writer_, data_, strip_);
233} 216}
234 217
235// ################################################################################################# 218// #################################################################################################
236 219
237UserValueCount luaG_getalluservalues(lua_State* L_, StackIndex idx_); 220UserValueCount luaW_getalluservalues(lua_State* L_, StackIndex idx_);
238 221
239// ################################################################################################# 222// #################################################################################################
240 223
@@ -272,7 +255,7 @@ static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, StackInd
272// ------------------------------------------------------------------------------------------------- 255// -------------------------------------------------------------------------------------------------
273 256
274[[nodiscard]] 257[[nodiscard]]
275static inline LuaType luaG_getfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_) 258static inline LuaType luaW_getfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_)
276{ 259{
277 return static_cast<LuaType>(WrapLuaGetField(lua_getfield, L_, idx_, name_)); 260 return static_cast<LuaType>(WrapLuaGetField(lua_getfield, L_, idx_, name_));
278} 261}
@@ -280,21 +263,62 @@ static inline LuaType luaG_getfield(lua_State* const L_, StackIndex const idx_,
280// ################################################################################################# 263// #################################################################################################
281 264
282[[nodiscard]] 265[[nodiscard]]
283LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); 266LuaType luaW_getmodule(lua_State* L_, std::string_view const& name_);
267
268// #################################################################################################
269
270template <typename LUA_NEWSTATE>
271concept RequiresOldLuaNewState = requires(LUA_NEWSTATE f_)
272{
273 {
274 f_(nullptr, 0)
275 } -> std::same_as<lua_State*>;
276};
277
278template <RequiresOldLuaNewState LUA_NEWSTATE>
279static inline lua_State* WrapLuaNewState(LUA_NEWSTATE const lua_newstate_, lua_Alloc const allocf_, void* const ud_, [[maybe_unused]] unsigned int const seed_)
280{
281 // until Lua 5.5, lua_newstate has only 2 parameters
282 return lua_newstate_(allocf_, ud_);
283}
284
285// -------------------------------------------------------------------------------------------------
286
287template <typename LUA_NEWSTATE>
288concept RequiresNewLuaNewState = requires(LUA_NEWSTATE f_)
289{
290 {
291 f_(nullptr, nullptr, 0)
292 } -> std::same_as<lua_State*>;
293};
294
295template <RequiresNewLuaNewState LUA_NEWSTATE>
296static inline lua_State* WrapLuaNewState(LUA_NEWSTATE const lua_newstate_, lua_Alloc const allocf_, void* const ud_, unsigned int const seed_)
297{
298 // starting with Lua 5.5, lua_newstate has 3 parameters
299 return lua_newstate_(allocf_, ud_, seed_);
300}
301
302// -------------------------------------------------------------------------------------------------
303
304static inline lua_State* luaW_newstate(lua_Alloc const allocf_, void* const ud_, unsigned int const seed_)
305{
306 return WrapLuaNewState(lua_newstate, allocf_, ud_, seed_);
307}
284 308
285// ################################################################################################# 309// #################################################################################################
286 310
287template<typename ENUM> 311template<typename ENUM>
288requires std::is_enum_v<ENUM> 312requires std::is_enum_v<ENUM>
289[[nodiscard]] 313[[nodiscard]]
290ENUM luaG_optenum(lua_State* const L_, StackIndex const idx_, ENUM const def_) 314ENUM luaW_optenum(lua_State* const L_, StackIndex const idx_, ENUM const def_)
291{ 315{
292 return static_cast<ENUM>(luaL_optinteger(L_, idx_, static_cast<std::underlying_type_t<ENUM>>(def_))); 316 return static_cast<ENUM>(luaL_optinteger(L_, idx_, static_cast<std::underlying_type_t<ENUM>>(def_)));
293} 317}
294 318
295// ################################################################################################# 319// #################################################################################################
296 320
297inline void luaG_registerlibfuncs(lua_State* const L_, luaL_Reg const* funcs_) 321inline void luaW_registerlibfuncs(lua_State* const L_, luaL_Reg const* funcs_)
298{ 322{
299 // fake externs to make clang happy... 323 // fake externs to make clang happy...
300 extern void luaL_register(lua_State*, char const*, luaL_Reg const*); // Lua 5.1 324 extern void luaL_register(lua_State*, char const*, luaL_Reg const*); // Lua 5.1
@@ -353,7 +377,7 @@ static inline int WrapLuaResume(LUA_RESUME const lua_resume_, lua_State* const L
353// ------------------------------------------------------------------------------------------------- 377// -------------------------------------------------------------------------------------------------
354 378
355[[nodiscard]] 379[[nodiscard]]
356static inline LuaError luaG_resume(lua_State* const L_, lua_State* const from_, int const nargs_, int* const nresults_) 380static inline LuaError luaW_resume(lua_State* const L_, lua_State* const from_, int const nargs_, int* const nresults_)
357{ 381{
358 return ToLuaError(WrapLuaResume(lua_resume, L_, from_, nargs_, nresults_)); 382 return ToLuaError(WrapLuaResume(lua_resume, L_, from_, nargs_, nresults_));
359} 383}
@@ -364,11 +388,11 @@ template <typename LUA_RAWGET>
364concept RequiresOldLuaRawget = requires(LUA_RAWGET f_) { { f_(nullptr, 0) } -> std::same_as<void>; }; 388concept RequiresOldLuaRawget = requires(LUA_RAWGET f_) { { f_(nullptr, 0) } -> std::same_as<void>; };
365 389
366template <RequiresOldLuaRawget LUA_RAWGET> 390template <RequiresOldLuaRawget LUA_RAWGET>
367static inline LuaType WrapLuaRawget(LUA_RAWGET lua_rawget_, lua_State* const L_, StackIndex const idx_) 391static inline LuaType WrapLuaRawget(LUA_RAWGET const lua_rawget_, lua_State* const L_, StackIndex const idx_)
368{ 392{
369 // until Lua 5.3, lua_rawget -> void 393 // until Lua 5.3, lua_rawget -> void
370 lua_rawget_(L_, idx_); 394 lua_rawget_(L_, idx_);
371 return luaG_type(L_, kIdxTop); 395 return luaW_type(L_, kIdxTop);
372} 396}
373 397
374// ------------------------------------------------------------------------------------------------- 398// -------------------------------------------------------------------------------------------------
@@ -377,7 +401,7 @@ template <typename LUA_RAWGET>
377concept RequiresNewLuaRawget = requires(LUA_RAWGET f_) { { f_(nullptr, 0) } -> std::same_as<int>; }; 401concept RequiresNewLuaRawget = requires(LUA_RAWGET f_) { { f_(nullptr, 0) } -> std::same_as<int>; };
378 402
379template <RequiresNewLuaRawget LUA_RAWGET> 403template <RequiresNewLuaRawget LUA_RAWGET>
380static inline LuaType WrapLuaRawget(LUA_RAWGET lua_rawget_, lua_State* const L_, StackIndex const idx_) 404static inline LuaType WrapLuaRawget(LUA_RAWGET const lua_rawget_, lua_State* const L_, StackIndex const idx_)
381{ 405{
382 // starting with Lua 5.3, lua_rawget -> int (the type of the extracted value) 406 // starting with Lua 5.3, lua_rawget -> int (the type of the extracted value)
383 return static_cast<LuaType>(lua_rawget_(L_, idx_)); 407 return static_cast<LuaType>(lua_rawget_(L_, idx_));
@@ -385,42 +409,42 @@ static inline LuaType WrapLuaRawget(LUA_RAWGET lua_rawget_, lua_State* const L_,
385 409
386// ------------------------------------------------------------------------------------------------- 410// -------------------------------------------------------------------------------------------------
387 411
388static inline LuaType luaG_rawget(lua_State* const L_, StackIndex const idx_) 412static inline LuaType luaW_rawget(lua_State* const L_, StackIndex const idx_)
389{ 413{
390 return WrapLuaRawget(lua_rawget, L_, idx_); 414 return WrapLuaRawget(lua_rawget, L_, idx_);
391} 415}
392 416
393// ################################################################################################# 417// #################################################################################################
394 418
395static inline LuaType luaG_rawgetfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_) 419static inline LuaType luaW_rawgetfield(lua_State* const L_, StackIndex const idx_, std::string_view const& name_)
396{ 420{
397 auto const _absIdx{ luaG_absindex(L_, idx_) }; 421 auto const _absIdx{ luaW_absindex(L_, idx_) };
398 luaG_pushstring(L_, name_); // L_: ... t ... name_ 422 luaW_pushstring(L_, name_); // L_: ... t ... name_
399 lua_rawget(L_, _absIdx); // L_: ... t ... <field> 423 lua_rawget(L_, _absIdx); // L_: ... t ... <field>
400 return luaG_type(L_, kIdxTop); 424 return luaW_type(L_, kIdxTop);
401} 425}
402 426
403// ################################################################################################# 427// #################################################################################################
404 428
405template <size_t N> 429template <size_t N>
406static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) 430static inline void luaW_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N])
407{ 431{
408 lua_createtable(L_, 0, N - 1); 432 lua_createtable(L_, 0, N - 1);
409 luaG_registerlibfuncs(L_, funcs_); 433 luaW_registerlibfuncs(L_, funcs_);
410} 434}
411 435
412// ################################################################################################# 436// #################################################################################################
413 437
414template <typename T> 438template <typename T>
415[[nodiscard]] 439[[nodiscard]]
416T* luaG_newuserdatauv(lua_State* const L_, UserValueCount const nuvalue_) 440T* luaW_newuserdatauv(lua_State* const L_, UserValueCount const nuvalue_)
417{ 441{
418 return static_cast<T*>(lua_newuserdatauv(L_, sizeof(T), nuvalue_)); 442 return static_cast<T*>(lua_newuserdatauv(L_, sizeof(T), nuvalue_));
419} 443}
420 444
421// ################################################################################################# 445// #################################################################################################
422 446
423inline void luaG_pushglobaltable(lua_State* const L_) 447inline void luaW_pushglobaltable(lua_State* const L_)
424{ 448{
425#ifdef LUA_GLOBALSINDEX // All flavors of Lua 5.1 449#ifdef LUA_GLOBALSINDEX // All flavors of Lua 5.1
426 ::lua_pushvalue(L_, LUA_GLOBALSINDEX); 450 ::lua_pushvalue(L_, LUA_GLOBALSINDEX);
@@ -431,15 +455,15 @@ inline void luaG_pushglobaltable(lua_State* const L_)
431 455
432// ################################################################################################# 456// #################################################################################################
433 457
434inline void luaG_setfield(lua_State* const L_, StackIndex const idx_, char const* const k_) = delete; 458inline void luaW_setfield(lua_State* const L_, StackIndex const idx_, char const* const k_) = delete;
435inline void luaG_setfield(lua_State* const L_, StackIndex const idx_, std::string_view const& k_) 459inline void luaW_setfield(lua_State* const L_, StackIndex const idx_, std::string_view const& k_)
436{ 460{
437 lua_setfield(L_, idx_, k_.data()); 461 lua_setfield(L_, idx_, k_.data());
438} 462}
439 463
440// ################################################################################################# 464// #################################################################################################
441 465
442inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname_) 466inline void luaW_setmetatable(lua_State* const L_, std::string_view const& tname_)
443{ 467{
444 // fake externs to make clang happy... 468 // fake externs to make clang happy...
445 if constexpr (LUA_VERSION_NUM > 501) { 469 if constexpr (LUA_VERSION_NUM > 501) {
@@ -456,7 +480,7 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname
456// a small helper to extract a full userdata pointer from the stack in a safe way 480// a small helper to extract a full userdata pointer from the stack in a safe way
457template <typename T> 481template <typename T>
458[[nodiscard]] 482[[nodiscard]]
459T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_) 483T* luaW_tofulluserdata(lua_State* const L_, StackIndex const index_)
460{ 484{
461 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); 485 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA);
462 return static_cast<T*>(lua_touserdata(L_, index_)); 486 return static_cast<T*>(lua_touserdata(L_, index_));
@@ -466,7 +490,7 @@ T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_)
466 490
467template <typename T> 491template <typename T>
468[[nodiscard]] 492[[nodiscard]]
469auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_) 493auto luaW_tolightuserdata(lua_State* const L_, StackIndex const index_)
470{ 494{
471 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); 495 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_));
472 if constexpr (std::is_pointer_v<T>) { 496 if constexpr (std::is_pointer_v<T>) {
@@ -479,7 +503,7 @@ auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_)
479// ------------------------------------------------------------------------------------------------- 503// -------------------------------------------------------------------------------------------------
480 504
481[[nodiscard]] 505[[nodiscard]]
482inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_) 506inline std::string_view luaW_typename(lua_State* const L_, LuaType const t_)
483{ 507{
484 return lua_typename(L_, static_cast<int>(t_)); 508 return lua_typename(L_, static_cast<int>(t_));
485} 509}
@@ -487,7 +511,7 @@ inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_)
487// ------------------------------------------------------------------------------------------------- 511// -------------------------------------------------------------------------------------------------
488 512
489[[nodiscard]] 513[[nodiscard]]
490inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_) 514inline std::string_view luaW_typename(lua_State* const L_, StackIndex const idx_)
491{ 515{
492 return luaG_typename(L_, luaG_type(L_, idx_)); 516 return luaW_typename(L_, luaW_type(L_, idx_));
493} 517}