diff options
Diffstat (limited to 'src/compat.h')
-rw-r--r-- | src/compat.h | 132 |
1 files changed, 103 insertions, 29 deletions
diff --git a/src/compat.h b/src/compat.h index a7ad1f3..4816cb7 100644 --- a/src/compat.h +++ b/src/compat.h | |||
@@ -25,76 +25,150 @@ extern "C" | |||
25 | 25 | ||
26 | // code is now preferring Lua 5.4 API | 26 | // code is now preferring Lua 5.4 API |
27 | 27 | ||
28 | // ################################################################################################# | ||
29 | |||
28 | // add some Lua 5.3-style API when building for Lua 5.1 | 30 | // add some Lua 5.3-style API when building for Lua 5.1 |
29 | #if LUA_VERSION_NUM == 501 | 31 | #if LUA_VERSION_NUM == 501 |
30 | 32 | ||
31 | #define lua501_equal lua_equal | 33 | #define lua501_equal lua_equal |
32 | #define lua_absindex(L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) + 1) | 34 | inline int lua_absindex(lua_State* L_, int idx_) |
35 | { | ||
36 | return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); | ||
37 | } | ||
33 | #if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap | 38 | #if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap |
34 | #define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) | 39 | inline void lua_pushglobaltable(lua_State* L_) |
40 | { | ||
41 | lua_pushvalue(L_, LUA_GLOBALSINDEX); | ||
42 | } | ||
35 | #endif // LUAJIT_VERSION_NUM | 43 | #endif // LUAJIT_VERSION_NUM |
36 | #define lua_setuservalue lua_setfenv | 44 | inline int lua_setuservalue(lua_State* L_, int idx_) |
37 | #define lua_getuservalue lua_getfenv | 45 | { |
38 | #define lua_rawlen lua_objlen | 46 | return lua_setfenv(L_, idx_); |
39 | #define luaG_registerlibfuncs(L_, _funcs) luaL_register(L_, nullptr, _funcs) | 47 | } |
48 | inline void lua_getuservalue(lua_State* L_, int idx_) | ||
49 | { | ||
50 | lua_getfenv(L_, idx_); | ||
51 | } | ||
52 | inline size_t lua_rawlen(lua_State* L_, int idx_) | ||
53 | { | ||
54 | return lua_objlen(L_, idx_); | ||
55 | } | ||
56 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
57 | { | ||
58 | luaL_register(L_, nullptr, funcs_); | ||
59 | } | ||
60 | // keep as macros to be consistent with Lua headers | ||
40 | #define LUA_OK 0 | 61 | #define LUA_OK 0 |
41 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value | 62 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value |
42 | void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources | 63 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources |
43 | #define lua504_dump(L_, writer_, data_, strip_) lua_dump(L_, writer_, data_) | 64 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) |
65 | { | ||
66 | return lua_dump(L_, writer_, data_); | ||
67 | } | ||
44 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 | 68 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 |
45 | 69 | ||
46 | #endif // LUA_VERSION_NUM == 501 | 70 | #endif // LUA_VERSION_NUM == 501 |
47 | 71 | ||
72 | // ################################################################################################# | ||
73 | |||
48 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way | 74 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way |
49 | #if LUA_VERSION_NUM == 502 | 75 | #if LUA_VERSION_NUM == 502 |
50 | 76 | ||
51 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 77 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
52 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 78 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
79 | { | ||
80 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
81 | } | ||
53 | #endif // lua501_equal | 82 | #endif // lua501_equal |
54 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 83 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
55 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 84 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
85 | { | ||
86 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
87 | } | ||
56 | #endif // lua_lessthan | 88 | #endif // lua_lessthan |
57 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 89 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
58 | #define lua504_dump(L, writer, data, strip) lua_dump(L, writer, data) | 90 | { |
91 | luaL_setfuncs(L_, funcs_, 0); | ||
92 | } | ||
93 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) | ||
94 | { | ||
95 | return lua_dump(L_, writer_, data_); | ||
96 | } | ||
59 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 | 97 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 |
60 | 98 | ||
61 | #endif // LUA_VERSION_NUM == 502 | 99 | #endif // LUA_VERSION_NUM == 502 |
62 | 100 | ||
101 | // ################################################################################################# | ||
102 | |||
63 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way | 103 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way |
64 | #if LUA_VERSION_NUM == 503 | 104 | #if LUA_VERSION_NUM == 503 |
65 | 105 | ||
66 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 106 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
67 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 107 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
108 | { | ||
109 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
110 | } | ||
68 | #endif // lua501_equal | 111 | #endif // lua501_equal |
69 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 112 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
70 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 113 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
114 | { | ||
115 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
116 | } | ||
71 | #endif // lua_lessthan | 117 | #endif // lua_lessthan |
72 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 118 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
73 | #define lua504_dump lua_dump | 119 | { |
74 | #define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) | 120 | luaL_setfuncs(L_, funcs_, 0); |
121 | } | ||
122 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
123 | { | ||
124 | return lua_dump(L_, writer_, data_, strip_); | ||
125 | } | ||
126 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | ||
127 | { | ||
128 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | ||
129 | } | ||
75 | 130 | ||
76 | #endif // LUA_VERSION_NUM == 503 | 131 | #endif // LUA_VERSION_NUM == 503 |
77 | 132 | ||
133 | // ################################################################################################# | ||
134 | |||
78 | #if LUA_VERSION_NUM < 504 | 135 | #if LUA_VERSION_NUM < 504 |
79 | 136 | ||
80 | void* lua_newuserdatauv(lua_State* L, size_t sz, int nuvalue); | 137 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_); |
81 | int lua_getiuservalue(lua_State* L, int idx, int n); | 138 | int lua_getiuservalue(lua_State* L_, int idx_, int n_); |
82 | int lua_setiuservalue(lua_State* L, int idx, int n); | 139 | int lua_setiuservalue(lua_State* L_, int idx_, int n_); |
83 | 140 | ||
84 | #endif // LUA_VERSION_NUM < 504 | 141 | #endif // LUA_VERSION_NUM < 504 |
85 | 142 | ||
143 | // ################################################################################################# | ||
144 | |||
86 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way | 145 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way |
87 | #if LUA_VERSION_NUM == 504 | 146 | #if LUA_VERSION_NUM == 504 |
88 | 147 | ||
89 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h | 148 | #ifndef lua501_equal // already defined when compatibility is active in luaconf.h |
90 | #define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) | 149 | inline int lua501_equal(lua_State* L_, int a_, int b_) |
150 | { | ||
151 | return lua_compare(L_, a_, b_, LUA_OPEQ); | ||
152 | } | ||
91 | #endif // lua501_equal | 153 | #endif // lua501_equal |
92 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h | 154 | #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h |
93 | #define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) | 155 | inline int lua_lessthan(lua_State* L_, int a_, int b_) |
156 | { | ||
157 | return lua_compare(L_, a_, b_, LUA_OPLT); | ||
158 | } | ||
94 | #endif // lua_lessthan | 159 | #endif // lua_lessthan |
95 | #define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) | 160 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
96 | #define lua504_dump lua_dump | 161 | { |
97 | #define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) | 162 | luaL_setfuncs(L_, funcs_, 0); |
163 | } | ||
164 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
165 | { | ||
166 | return lua_dump(L_, writer_, data_, strip_); | ||
167 | } | ||
168 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | ||
169 | { | ||
170 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | ||
171 | } | ||
98 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.4, we don't care about the actual value | 172 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.4, we don't care about the actual value |
99 | 173 | ||
100 | #endif // LUA_VERSION_NUM == 504 | 174 | #endif // LUA_VERSION_NUM == 504 |
@@ -117,11 +191,11 @@ enum class LuaType | |||
117 | CDATA = 10 // LuaJIT CDATA | 191 | CDATA = 10 // LuaJIT CDATA |
118 | }; | 192 | }; |
119 | 193 | ||
120 | inline LuaType lua_type_as_enum(lua_State* L, int idx_) | 194 | inline LuaType lua_type_as_enum(lua_State* L_, int idx_) |
121 | { | 195 | { |
122 | return static_cast<LuaType>(lua_type(L, idx_)); | 196 | return static_cast<LuaType>(lua_type(L_, idx_)); |
123 | } | 197 | } |
124 | inline char const* lua_typename(lua_State* L, LuaType t_) | 198 | inline char const* lua_typename(lua_State* L_, LuaType t_) |
125 | { | 199 | { |
126 | return lua_typename(L, static_cast<int>(t_)); | 200 | return lua_typename(L_, static_cast<int>(t_)); |
127 | } | 201 | } |