diff options
Diffstat (limited to 'src/compat.h')
-rw-r--r-- | src/compat.h | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/src/compat.h b/src/compat.h index 24a105f..8963ef7 100644 --- a/src/compat.h +++ b/src/compat.h | |||
@@ -11,6 +11,8 @@ extern "C" | |||
11 | } | 11 | } |
12 | #endif // __cplusplus | 12 | #endif // __cplusplus |
13 | 13 | ||
14 | #include "debug.h" | ||
15 | |||
14 | // try to detect if we are building against LuaJIT or MoonJIT | 16 | // try to detect if we are building against LuaJIT or MoonJIT |
15 | #if defined(LUA_JITLIBNAME) | 17 | #if defined(LUA_JITLIBNAME) |
16 | #include "luajit.h" | 18 | #include "luajit.h" |
@@ -302,6 +304,14 @@ inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) | |||
302 | 304 | ||
303 | // ------------------------------------------------------------------------------------------------- | 305 | // ------------------------------------------------------------------------------------------------- |
304 | 306 | ||
307 | template <typename T> | ||
308 | [[nodiscard]] T* luaG_newuserdatauv(lua_State* L_, int nuvalue_) | ||
309 | { | ||
310 | return static_cast<T*>(lua_newuserdatauv(L_, sizeof(T), nuvalue_)); | ||
311 | } | ||
312 | |||
313 | // ------------------------------------------------------------------------------------------------- | ||
314 | |||
305 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) | 315 | inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) |
306 | { | 316 | { |
307 | Wrap<LUA_VERSION_NUM>::luaL_setfuncs(L_, funcs_, 0); | 317 | Wrap<LUA_VERSION_NUM>::luaL_setfuncs(L_, funcs_, 0); |
@@ -314,27 +324,50 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname | |||
314 | return Wrap<LUA_VERSION_NUM>::luaL_setmetatable(L_, tname_); | 324 | return Wrap<LUA_VERSION_NUM>::luaL_setmetatable(L_, tname_); |
315 | } | 325 | } |
316 | 326 | ||
327 | // ------------------------------------------------------------------------------------------------- | ||
328 | |||
329 | // a small helper to extract a full userdata pointer from the stack in a safe way | ||
330 | template <typename T> | ||
331 | [[nodiscard]] T* luaG_tofulluserdata(lua_State* L_, int index_) | ||
332 | { | ||
333 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); | ||
334 | return static_cast<T*>(lua_touserdata(L_, index_)); | ||
335 | } | ||
336 | |||
337 | // ------------------------------------------------------------------------------------------------- | ||
338 | |||
339 | template <typename T> | ||
340 | [[nodiscard]] auto luaG_tolightuserdata(lua_State* L_, int index_) | ||
341 | { | ||
342 | LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); | ||
343 | if constexpr (std::is_pointer_v<T>) { | ||
344 | return static_cast<T>(lua_touserdata(L_, index_)); | ||
345 | } else { | ||
346 | return static_cast<T*>(lua_touserdata(L_, index_)); | ||
347 | } | ||
348 | } | ||
349 | |||
317 | // ################################################################################################# | 350 | // ################################################################################################# |
318 | 351 | ||
319 | // must keep as a macro as long as we do constant string concatenations | 352 | // must keep as a macro as long as we do constant string concatenations |
320 | #define STRINGVIEW_FMT "%.*s" | 353 | #define STRINGVIEW_FMT "%.*s" |
321 | 354 | ||
322 | // a replacement of lua_tolstring | 355 | // a replacement of lua_tolstring |
323 | [[nodiscard]] inline std::string_view luaG_tostringview(lua_State* const L_, int const idx_) | 356 | [[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, int const idx_) |
324 | { | 357 | { |
325 | size_t _len{ 0 }; | 358 | size_t _len{ 0 }; |
326 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; | 359 | char const* _str{ lua_tolstring(L_, idx_, &_len) }; |
327 | return std::string_view{ _str, _len }; | 360 | return std::string_view{ _str, _len }; |
328 | } | 361 | } |
329 | 362 | ||
330 | [[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* const L_, int const idx_) | 363 | [[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, int const idx_) |
331 | { | 364 | { |
332 | size_t _len{ 0 }; | 365 | size_t _len{ 0 }; |
333 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; | 366 | char const* _str{ luaL_checklstring(L_, idx_, &_len) }; |
334 | return std::string_view{ _str, _len }; | 367 | return std::string_view{ _str, _len }; |
335 | } | 368 | } |
336 | 369 | ||
337 | [[nodiscard]] inline std::string_view luaG_optstringview(lua_State* const L_, int const idx_, std::string_view const& default_) | 370 | [[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, int const idx_, std::string_view const& default_) |
338 | { | 371 | { |
339 | if (lua_isnoneornil(L_, idx_)) { | 372 | if (lua_isnoneornil(L_, idx_)) { |
340 | return default_; | 373 | return default_; |
@@ -345,13 +378,13 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname | |||
345 | } | 378 | } |
346 | 379 | ||
347 | template<typename ...EXTRA> | 380 | template<typename ...EXTRA> |
348 | [[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_) | 381 | [[nodiscard]] inline std::string_view luaG_pushstring(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_) |
349 | { | 382 | { |
350 | if constexpr (sizeof...(EXTRA) == 0) { | 383 | if constexpr (sizeof...(EXTRA) == 0) { |
351 | if constexpr (LUA_VERSION_NUM == 501) { | 384 | if constexpr (LUA_VERSION_NUM == 501) { |
352 | // lua_pushlstring doesn't return a value in Lua 5.1 | 385 | // lua_pushlstring doesn't return a value in Lua 5.1 |
353 | lua_pushlstring(L_, str_.data(), str_.size()); | 386 | lua_pushlstring(L_, str_.data(), str_.size()); |
354 | return luaG_tostringview(L_, -1); | 387 | return luaG_tostring(L_, -1); |
355 | } else { | 388 | } else { |
356 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; | 389 | return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; |
357 | } | 390 | } |