From 7632af3c73fc4e4ae3ad4c98c90c39b47c3b8379 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Tue, 4 Oct 2022 08:31:36 -0400 Subject: Consolidate API object code (#12728) Co-authored-by: sfan5 --- src/script/lua_api/l_base.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/script/lua_api/l_base.cpp') diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp index f842671b8..03d8ee62d 100644 --- a/src/script/lua_api/l_base.cpp +++ b/src/script/lua_api/l_base.cpp @@ -100,6 +100,29 @@ bool ModApiBase::registerFunction(lua_State *L, const char *name, return true; } +void ModApiBase::registerClass(lua_State *L, const char *name, + const luaL_Reg *methods, + const luaL_Reg *metamethods) +{ + luaL_newmetatable(L, name); + luaL_register(L, NULL, metamethods); + int metatable = lua_gettop(L); + + lua_newtable(L); + luaL_register(L, NULL, methods); + int methodtable = lua_gettop(L); + + lua_pushvalue(L, methodtable); + lua_setfield(L, metatable, "__index"); + + // Protect the real metatable. + lua_pushvalue(L, methodtable); + lua_setfield(L, metatable, "__metatable"); + + // Pop methodtable and metatable. + lua_pop(L, 2); +} + int ModApiBase::l_deprecated_function(lua_State *L, const char *good, const char *bad, lua_CFunction func) { thread_local std::vector deprecated_logged; -- cgit v1.2.3