From b639c229e3fdef21cec4535284eeabbca361dad6 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 22 May 2024 14:43:07 +0200 Subject: lua503_getfield → strong typed luaG_getfield MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compat.h | 72 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) (limited to 'src/compat.h') diff --git a/src/compat.h b/src/compat.h index b5afe17..f097fb6 100644 --- a/src/compat.h +++ b/src/compat.h @@ -31,6 +31,33 @@ extern "C" // ################################################################################################# +// a strong-typed wrapper over lua types to see them easier in a debugger +enum class LuaType +{ + NONE = LUA_TNONE, + NIL = LUA_TNIL, + BOOLEAN = LUA_TBOOLEAN, + LIGHTUSERDATA = LUA_TLIGHTUSERDATA, + NUMBER = LUA_TNUMBER, + STRING = LUA_TSTRING, + TABLE = LUA_TTABLE, + FUNCTION = LUA_TFUNCTION, + USERDATA = LUA_TUSERDATA, + THREAD = LUA_TTHREAD, + CDATA = 10 // LuaJIT CDATA +}; + +inline LuaType lua_type_as_enum(lua_State* L_, int idx_) +{ + return static_cast(lua_type(L_, idx_)); +} +inline char const* lua_typename(lua_State* L_, LuaType t_) +{ + return lua_typename(L_, static_cast(t_)); +} + +// ################################################################################################# + // add some Lua 5.3-style API when building for Lua 5.1 #if LUA_VERSION_NUM == 501 @@ -106,22 +133,16 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u // ################################################################################################# -#if LUA_VERSION_NUM < 503 -// starting with Lua 5.3, lua_getfield returns the type of the value it found -inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) +[[nodiscard]] inline LuaType luaG_getfield(lua_State* L_, int idx_, char const* k_) { +// starting with Lua 5.3, lua_getfield returns the type of the value it found +#if LUA_VERSION_NUM < 503 lua_getfield(L_, idx_, k_); - return lua_type(L_, -1); -} - + return lua_type_as_enum(L_, -1); #else // LUA_VERSION_NUM >= 503 - -inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) -{ - return lua_getfield(L_, idx_, k_); -} - + return static_cast(lua_getfield(L_, idx_, k_)); #endif // LUA_VERSION_NUM >= 503 +} // ################################################################################################# @@ -202,33 +223,6 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) // ################################################################################################# -// a strong-typed wrapper over lua types to see them easier in a debugger -enum class LuaType -{ - NONE = LUA_TNONE, - NIL = LUA_TNIL, - BOOLEAN = LUA_TBOOLEAN, - LIGHTUSERDATA = LUA_TLIGHTUSERDATA, - NUMBER = LUA_TNUMBER, - STRING = LUA_TSTRING, - TABLE = LUA_TTABLE, - FUNCTION = LUA_TFUNCTION, - USERDATA = LUA_TUSERDATA, - THREAD = LUA_TTHREAD, - CDATA = 10 // LuaJIT CDATA -}; - -inline LuaType lua_type_as_enum(lua_State* L_, int idx_) -{ - return static_cast(lua_type(L_, idx_)); -} -inline char const* lua_typename(lua_State* L_, LuaType t_) -{ - return lua_typename(L_, static_cast(t_)); -} - -// ################################################################################################# - // a strong-typed wrapper over lua error codes to see them easier in a debugger enum class LuaError { -- cgit v1.2.3-55-g6feb