From 38a9cded72d663f48eb3154ed2eb495bbf3f80f3 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 6 Jun 2024 11:57:49 +0200 Subject: Fix deep_test for all flavors of Lua --- src/compat.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src') diff --git a/src/compat.h b/src/compat.h index 1789a8b..58af985 100644 --- a/src/compat.h +++ b/src/compat.h @@ -172,10 +172,22 @@ struct Wrap return ::lua_dump(L_, writer_, data_, strip_); } + template + static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) + { + lua_createtable(L_, 0, N - 1); + ::luaL_setfuncs(L_, funcs_, 0); + } + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int nup_) { ::luaL_setfuncs(L_, funcs_, nup_); } + + static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) + { + ::luaL_setmetatable(L_, tname_.data()); + } }; // ################################################################################################# @@ -188,10 +200,22 @@ struct Wrap::type> return ::lua_dump(L_, writer_, data_, strip_); } + template + static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) + { + lua_createtable(L_, 0, N - 1); + ::luaL_setfuncs(L_, funcs_, 0); + } + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) { ::luaL_setfuncs(L_, funcs_, nup_); } + + static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) + { + ::luaL_setmetatable(L_, tname_.data()); + } }; // ################################################################################################# @@ -204,10 +228,22 @@ struct Wrap::type> return ::lua_dump(L_, writer_, data_); } + template + static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) + { + lua_createtable(L_, 0, N - 1); + ::luaL_setfuncs(L_, funcs_, 0); + } + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) { ::luaL_setfuncs(L_, funcs_, nup_); } + + static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) + { + ::luaL_setmetatable(L_, tname_.data()); + } }; // ################################################################################################# @@ -220,10 +256,23 @@ struct Wrap::type> return ::lua_dump(L_, writer_, data_); } + template + static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) + { + lua_createtable(L_, 0, N - 1); + ::luaL_register(L_, nullptr, funcs_); + } + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], [[maybe_unused]] int const nup_) { ::luaL_register(L_, nullptr, funcs_); } + + static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) + { + luaL_getmetatable(L_, tname_.data()); + lua_setmetatable(L_, -2); + } }; // ################################################################################################# @@ -248,11 +297,26 @@ LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); // ------------------------------------------------------------------------------------------------- +template +inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) +{ + (Wrap::luaL_newlib)(L_, funcs_); +} + +// ------------------------------------------------------------------------------------------------- + inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) { Wrap::luaL_setfuncs(L_, funcs_, 0); } +// ------------------------------------------------------------------------------------------------- + +inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname_) +{ + return Wrap::luaL_setmetatable(L_, tname_); +} + // ################################################################################################# // must keep as a macro as long as we do constant string concatenations -- cgit v1.2.3-55-g6feb