diff options
Diffstat (limited to 'src/compat.h')
-rw-r--r-- | src/compat.h | 145 |
1 files changed, 97 insertions, 48 deletions
diff --git a/src/compat.h b/src/compat.h index 43f8922..6307d86 100644 --- a/src/compat.h +++ b/src/compat.h | |||
@@ -47,11 +47,11 @@ enum class LuaType | |||
47 | CDATA = 10 // LuaJIT CDATA | 47 | CDATA = 10 // LuaJIT CDATA |
48 | }; | 48 | }; |
49 | 49 | ||
50 | inline LuaType lua_type_as_enum(lua_State* L_, int idx_) | 50 | inline LuaType luaG_type(lua_State* L_, int idx_) |
51 | { | 51 | { |
52 | return static_cast<LuaType>(lua_type(L_, idx_)); | 52 | return static_cast<LuaType>(lua_type(L_, idx_)); |
53 | } | 53 | } |
54 | inline char const* lua_typename(lua_State* L_, LuaType t_) | 54 | inline char const* luaG_typename(lua_State* L_, LuaType t_) |
55 | { | 55 | { |
56 | return lua_typename(L_, static_cast<int>(t_)); | 56 | return lua_typename(L_, static_cast<int>(t_)); |
57 | } | 57 | } |
@@ -79,18 +79,10 @@ inline size_t lua_rawlen(lua_State* L_, int idx_) | |||
79 | { | 79 | { |
80 | return lua_objlen(L_, idx_); | 80 | return lua_objlen(L_, idx_); |
81 | } | 81 | } |
82 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
83 | { | ||
84 | luaL_register(L_, nullptr, funcs_); | ||
85 | } | ||
86 | // keep as macros to be consistent with Lua headers | 82 | // keep as macros to be consistent with Lua headers |
87 | #define LUA_OK 0 | 83 | #define LUA_OK 0 |
88 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value | 84 | #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value |
89 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources | 85 | void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources |
90 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) | ||
91 | { | ||
92 | return lua_dump(L_, writer_, data_); | ||
93 | } | ||
94 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 | 86 | #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 |
95 | 87 | ||
96 | int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); | 88 | int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); |
@@ -102,14 +94,6 @@ int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); | |||
102 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way | 94 | // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way |
103 | #if LUA_VERSION_NUM == 502 | 95 | #if LUA_VERSION_NUM == 502 |
104 | 96 | ||
105 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
106 | { | ||
107 | luaL_setfuncs(L_, funcs_, 0); | ||
108 | } | ||
109 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) | ||
110 | { | ||
111 | return lua_dump(L_, writer_, data_); | ||
112 | } | ||
113 | #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 |
114 | 98 | ||
115 | #endif // LUA_VERSION_NUM == 502 | 99 | #endif // LUA_VERSION_NUM == 502 |
@@ -121,7 +105,7 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u | |||
121 | // starting with Lua 5.3, lua_getfield returns the type of the value it found | 105 | // starting with Lua 5.3, lua_getfield returns the type of the value it found |
122 | #if LUA_VERSION_NUM < 503 | 106 | #if LUA_VERSION_NUM < 503 |
123 | lua_getfield(L_, idx_, k_.data()); | 107 | lua_getfield(L_, idx_, k_.data()); |
124 | return lua_type_as_enum(L_, -1); | 108 | return luaG_type(L_, -1); |
125 | #else // LUA_VERSION_NUM >= 503 | 109 | #else // LUA_VERSION_NUM >= 503 |
126 | return static_cast<LuaType>(lua_getfield(L_, idx_, k_.data())); | 110 | return static_cast<LuaType>(lua_getfield(L_, idx_, k_.data())); |
127 | #endif // LUA_VERSION_NUM >= 503 | 111 | #endif // LUA_VERSION_NUM >= 503 |
@@ -132,14 +116,6 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u | |||
132 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way | 116 | // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way |
133 | #if LUA_VERSION_NUM == 503 | 117 | #if LUA_VERSION_NUM == 503 |
134 | 118 | ||
135 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
136 | { | ||
137 | luaL_setfuncs(L_, funcs_, 0); | ||
138 | } | ||
139 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
140 | { | ||
141 | return lua_dump(L_, writer_, data_, strip_); | ||
142 | } | ||
143 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | 119 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) |
144 | { | 120 | { |
145 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | 121 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); |
@@ -164,14 +140,6 @@ int lua_setiuservalue(lua_State* L_, int idx_, int n_); | |||
164 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way | 140 | // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way |
165 | #if LUA_VERSION_NUM == 504 | 141 | #if LUA_VERSION_NUM == 504 |
166 | 142 | ||
167 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
168 | { | ||
169 | luaL_setfuncs(L_, funcs_, 0); | ||
170 | } | ||
171 | inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
172 | { | ||
173 | return lua_dump(L_, writer_, data_, strip_); | ||
174 | } | ||
175 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | 143 | inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) |
176 | { | 144 | { |
177 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); | 145 | return static_cast<int>(luaL_optinteger(L_, n_, d_)); |
@@ -182,14 +150,6 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | |||
182 | 150 | ||
183 | // ################################################################################################# | 151 | // ################################################################################################# |
184 | 152 | ||
185 | // use this in place of lua_absindex to save a function call | ||
186 | inline int luaG_absindex(lua_State* L_, int idx_) | ||
187 | { | ||
188 | return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); | ||
189 | } | ||
190 | |||
191 | // ################################################################################################# | ||
192 | |||
193 | // a strong-typed wrapper over lua error codes to see them easier in a debugger | 153 | // a strong-typed wrapper over lua error codes to see them easier in a debugger |
194 | enum class LuaError | 154 | enum class LuaError |
195 | { | 155 | { |
@@ -211,28 +171,117 @@ inline constexpr LuaError ToLuaError(int rc_) | |||
211 | 171 | ||
212 | // ################################################################################################# | 172 | // ################################################################################################# |
213 | 173 | ||
174 | // Default matches Lua 5.4 as of now | ||
175 | template <int VERSION, typename SPECIALIZE = void> | ||
176 | struct Wrap | ||
177 | { | ||
178 | static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) | ||
179 | { | ||
180 | return ::lua_dump(L_, writer_, data_, strip_); | ||
181 | } | ||
182 | |||
183 | static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int nup_) | ||
184 | { | ||
185 | ::luaL_setfuncs(L_, funcs_, nup_); | ||
186 | } | ||
187 | }; | ||
188 | |||
189 | // ################################################################################################# | ||
190 | |||
191 | template <int VERSION> | ||
192 | struct Wrap<VERSION, typename std::enable_if<VERSION == 503>::type> | ||
193 | { | ||
194 | static inline int lua_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
195 | { | ||
196 | return ::lua_dump(L_, writer_, data_, strip_); | ||
197 | } | ||
198 | |||
199 | static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) | ||
200 | { | ||
201 | ::luaL_setfuncs(L_, funcs_, nup_); | ||
202 | } | ||
203 | }; | ||
204 | |||
205 | // ################################################################################################# | ||
206 | |||
207 | template <int VERSION> | ||
208 | struct Wrap<VERSION, typename std::enable_if<VERSION == 502>::type> | ||
209 | { | ||
210 | static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) | ||
211 | { | ||
212 | return ::lua_dump(L_, writer_, data_); | ||
213 | } | ||
214 | |||
215 | static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) | ||
216 | { | ||
217 | ::luaL_setfuncs(L_, funcs_, nup_); | ||
218 | } | ||
219 | }; | ||
220 | |||
221 | // ################################################################################################# | ||
222 | |||
223 | template <int VERSION> | ||
224 | struct Wrap<VERSION, typename std::enable_if<VERSION == 501>::type> | ||
225 | { | ||
226 | static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) | ||
227 | { | ||
228 | return ::lua_dump(L_, writer_, data_); | ||
229 | } | ||
230 | |||
231 | static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], [[maybe_unused]] int const nup_) | ||
232 | { | ||
233 | ::luaL_register(L_, nullptr, funcs_); | ||
234 | } | ||
235 | }; | ||
236 | |||
237 | // ################################################################################################# | ||
238 | // All the compatibility wrappers we expose start with luaG_ | ||
239 | |||
240 | // use this in place of lua_absindex to save a function call | ||
241 | inline int luaG_absindex(lua_State* L_, int idx_) | ||
242 | { | ||
243 | return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); | ||
244 | } | ||
245 | |||
246 | // ------------------------------------------------------------------------------------------------- | ||
247 | |||
248 | inline int luaG_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) | ||
249 | { | ||
250 | return Wrap<LUA_VERSION_NUM>::lua_dump(L_, writer_, data_, strip_); | ||
251 | } | ||
252 | |||
253 | // ------------------------------------------------------------------------------------------------- | ||
254 | |||
214 | LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); | 255 | LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); |
215 | 256 | ||
257 | // ------------------------------------------------------------------------------------------------- | ||
258 | |||
259 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | ||
260 | { | ||
261 | Wrap<LUA_VERSION_NUM>::luaL_setfuncs(L_, funcs_, 0); | ||
262 | } | ||
263 | |||
216 | // ################################################################################################# | 264 | // ################################################################################################# |
217 | 265 | ||
266 | // must keep as a macro as long as we do constant string concatenations | ||
218 | #define STRINGVIEW_FMT "%.*s" | 267 | #define STRINGVIEW_FMT "%.*s" |
219 | 268 | ||
220 | // a replacement of lua_tolstring | 269 | // a replacement of lua_tolstring |
221 | [[nodiscard]] inline std::string_view lua_tostringview(lua_State* L_, int idx_) | 270 | [[nodiscard]] inline std::string_view luaG_tostringview(lua_State* L_, int idx_) |
222 | { | 271 | { |
223 | size_t _len{ 0 }; | 272 | size_t _len{ 0 }; |
224 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; | 273 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; |
225 | return std::string_view{ _str, _len }; | 274 | return std::string_view{ _str, _len }; |
226 | } | 275 | } |
227 | 276 | ||
228 | [[nodiscard]] inline std::string_view luaL_checkstringview(lua_State* L_, int idx_) | 277 | [[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* L_, int idx_) |
229 | { | 278 | { |
230 | size_t _len{ 0 }; | 279 | size_t _len{ 0 }; |
231 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; | 280 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; |
232 | return std::string_view{ _str, _len }; | 281 | return std::string_view{ _str, _len }; |
233 | } | 282 | } |
234 | 283 | ||
235 | [[nodiscard]] inline std::string_view luaL_optstringview(lua_State* L_, int idx_, std::string_view const& default_) | 284 | [[nodiscard]] inline std::string_view luaG_optstringview(lua_State* L_, int idx_, std::string_view const& default_) |
236 | { | 285 | { |
237 | if (lua_isnoneornil(L_, idx_)) { | 286 | if (lua_isnoneornil(L_, idx_)) { |
238 | return default_; | 287 | return default_; |
@@ -242,12 +291,12 @@ LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); | |||
242 | return std::string_view{ _str, _len }; | 291 | return std::string_view{ _str, _len }; |
243 | } | 292 | } |
244 | 293 | ||
245 | [[nodiscard]] inline std::string_view lua_pushstringview(lua_State* L_, std::string_view const& str_) | 294 | [[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* L_, std::string_view const& str_) |
246 | { | 295 | { |
247 | #if LUA_VERSION_NUM == 501 | 296 | #if LUA_VERSION_NUM == 501 |
248 | // lua_pushlstring doesn't return a value in Lua 5.1 | 297 | // lua_pushlstring doesn't return a value in Lua 5.1 |
249 | lua_pushlstring(L_, str_.data(), str_.size()); | 298 | lua_pushlstring(L_, str_.data(), str_.size()); |
250 | return lua_tostringview(L_, -1); | 299 | return luaG_tostringview(L_, -1); |
251 | #else | 300 | #else |
252 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; | 301 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; |
253 | #endif // LUA_VERSION_NUM > 501 | 302 | #endif // LUA_VERSION_NUM > 501 |