From 3214c7bd7e77fdd458d64ec101a8a67287b59ffa Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 1 Aug 2024 16:09:11 -0400 Subject: Add lua_push(), lua_to(), lua_[gs]etfieldv(), lua_raw[gs]etfield(). Leverage C++ overloads to allow use of generic function names disambiguated by argument type. This allows using templates for certain common operation sequences. --- indra/llcommon/lua_function.h | 162 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) (limited to 'indra/llcommon/lua_function.h') diff --git a/indra/llcommon/lua_function.h b/indra/llcommon/lua_function.h index 7f7a7566f3..7b59af30f5 100644 --- a/indra/llcommon/lua_function.h +++ b/indra/llcommon/lua_function.h @@ -18,6 +18,7 @@ #include "luau/lualib.h" #include "fsyspath.h" #include "llerror.h" +#include "llsd.h" #include "stringize.h" #include // std::uncaught_exceptions() #include // std::shared_ptr @@ -168,6 +169,167 @@ private: int mIndex; }; +/***************************************************************************** +* lua_push() wrappers for generic code +*****************************************************************************/ +inline +void lua_push(lua_State* L, bool b) +{ + lua_pushboolean(L, int(b)); +} + +inline +void lua_push(lua_State* L, lua_CFunction fn) +{ + lua_pushcfunction(L, fn, ""); +} + +inline +void lua_push(lua_State* L, lua_Integer n) +{ + lua_pushinteger(L, n); +} + +inline +void lua_push(lua_State* L, void* p) +{ + lua_pushlightuserdata(L, p); +} + +inline +void lua_push(lua_State* L, const LLSD& data) +{ + lua_pushllsd(L, data); +} + +inline +void lua_push(lua_State* L, const char* s, size_t len) +{ + lua_pushlstring(L, s, len); +} + +inline +void lua_push(lua_State* L) +{ + lua_pushnil(L); +} + +inline +void lua_push(lua_State* L, lua_Number n) +{ + lua_pushnumber(L, n); +} + +inline +void lua_push(lua_State* L, const std::string& s) +{ + lua_pushstdstring(L, s); +} + +inline +void lua_push(lua_State* L, const char* s) +{ + lua_pushstring(L, s); +} + +/***************************************************************************** +* lua_to() wrappers for generic code +*****************************************************************************/ +template +auto lua_to(lua_State* L, int index); + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_toboolean(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_tocfunction(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_tointeger(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_tollsd(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_tonumber(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_tostdstring(L, index); +} + +template <> +inline +auto lua_to(lua_State* L, int index) +{ + return lua_touserdata(L, index); +} + +/***************************************************************************** +* field operations +*****************************************************************************/ +// return to C++, from table at index, the value of field k +template +auto lua_getfieldv(lua_State* L, int index, const char* k) +{ + luaL_checkstack(L, 1, nullptr); + lua_getfield(L, index, k); + LuaPopper pop(L, 1); + return lua_to(L, -1); +} + +// set in table at index, as field k, the specified C++ value +template +auto lua_setfieldv(lua_State* L, int index, const char* k, const T& value) +{ + luaL_checkstack(L, 1, nullptr); + lua_push(L, value); + lua_setfield(L, index, k); +} + +// return to C++, from table at index, the value of field k (without metamethods) +template +auto lua_rawgetfield(lua_State* L, int index, const std::string_view& k) +{ + luaL_checkstack(L, 1, nullptr); + lua_pushlstring(L, k.data(), k.length()); + lua_rawget(L, index); + LuaPopper pop(L, 1); + return lua_to(L, -1); +} + +// set in table at index, as field k, the specified C++ value (without metamethods) +template +void lua_rawsetfield(lua_State* L, int index, const std::string_view& k, const T& value) +{ + luaL_checkstack(L, 2, nullptr); + lua_pushlstring(L, k.data(), k.length()); + lua_push(L, value); + lua_rawset(L, index); +} + /***************************************************************************** * lua_function (and helper class LuaFunction) *****************************************************************************/ -- cgit v1.2.3