diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2026-04-26 15:16:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-26 15:16:36 +0100 |
| commit | 5ba33f846129f3e6c0505b19f94634b24505e828 (patch) | |
| tree | aaacabe678006040c342935481a86607c66607fd /src/config/lua/objects/LuaObjectHelpers.hpp | |
| parent | 329b2b356dd92ac07f4e2aa69cbce82ee9ba9d19 (diff) | |
config/lua: init lua config manager, use lua if available (#13817)
Adds a new lua config manager, which will automatically be used if there is a lua config present.
Diffstat (limited to 'src/config/lua/objects/LuaObjectHelpers.hpp')
| -rw-r--r-- | src/config/lua/objects/LuaObjectHelpers.hpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/config/lua/objects/LuaObjectHelpers.hpp b/src/config/lua/objects/LuaObjectHelpers.hpp new file mode 100644 index 00000000..69c284ef --- /dev/null +++ b/src/config/lua/objects/LuaObjectHelpers.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include "../bindings/LuaBindingsInternal.hpp" + +#include "../../../helpers/memory/Memory.hpp" + +extern "C" { +#include <lauxlib.h> +} + +namespace Config::Lua::Objects { + + template <typename T> + inline int gcRef(lua_State* L) { + sc<T*>(lua_touserdata(L, 1))->~T(); + return 0; + } + + inline int readOnlyNewIndex(lua_State* L) { + return Config::Lua::Bindings::Internal::configError(L, "attempt to modify read-only hl object"); + } + + inline void registerMetatable(lua_State* L, const char* name, lua_CFunction indexFn, lua_CFunction gcFn, lua_CFunction eqFn = nullptr, lua_CFunction toStringFn = nullptr) { + luaL_newmetatable(L, name); + lua_pushcfunction(L, indexFn); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, readOnlyNewIndex); + lua_setfield(L, -2, "__newindex"); + lua_pushcfunction(L, gcFn); + lua_setfield(L, -2, "__gc"); + + if (eqFn) { + lua_pushcfunction(L, eqFn); + lua_setfield(L, -2, "__eq"); + } + + if (toStringFn) { + lua_pushcfunction(L, toStringFn); + lua_setfield(L, -2, "__tostring"); + } + + lua_pop(L, 1); + } + + class ILuaObjectWrapper { + public: + virtual ~ILuaObjectWrapper() = default; + virtual void setup(lua_State* L) = 0; + }; + +} |
