From c3f79a112f3ec5cff160f9040196d284ffd4f9d8 Mon Sep 17 00:00:00 2001 From: LJ Sonic Date: Sat, 28 Oct 2023 11:06:30 +0200 Subject: [PATCH] Add LUA_SetCFunctionField --- src/deh_lua.c | 3 +-- src/lua_baselib.c | 3 +-- src/lua_maplib.c | 6 ++---- src/lua_script.c | 33 +++++++++++++-------------------- src/lua_script.h | 2 ++ src/lua_taglib.c | 12 ++++-------- src/lua_thinkerlib.c | 3 +-- 7 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/deh_lua.c b/src/deh_lua.c index 0b789547b..7188d25e3 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -771,8 +771,7 @@ int LUA_SOCLib(lua_State *L) lua_register(L,"getActionName",lib_getActionName); luaL_newmetatable(L, META_ACTION); - lua_pushcfunction(L, action_call); - lua_setfield(L, -2, "__call"); + LUA_SetCFunctionField(L, "__call", action_call); lua_pop(L, 1); return 0; diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 1821550b8..f7ed763a9 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -4300,8 +4300,7 @@ int LUA_BaseLib(lua_State *L) // Set metatable for string lua_pushliteral(L, ""); // dummy string lua_getmetatable(L, -1); // get string metatable - lua_pushcfunction(L,lib_concat); // push concatination function - lua_setfield(L,-2,"__add"); // ... store it as mathematical addition + LUA_SetCFunctionField(L, "__add", lib_concat); lua_pop(L, 2); // pop metatable and dummy string lua_newtable(L); diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 2e1aab3eb..efa2edf20 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -2898,10 +2898,8 @@ int LUA_MapLib(lua_State *L) node_fields_ref = Lua_CreateFieldTable(L, node_opt); luaL_newmetatable(L, META_NODEBBOX); - //lua_pushcfunction(L, nodebbox_get); - //lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, nodebbox_call); - lua_setfield(L, -2, "__call"); + //LUA_SetCFunctionField(L, "__index", nodebbox_get); + LUA_SetCFunctionField(L, "__call", nodebbox_call); lua_pop(L, 1); LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs); diff --git a/src/lua_script.c b/src/lua_script.c index 9aea96ac7..5cacc3f01 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -578,8 +578,7 @@ static void LUA_ClearState(void) // lock the global namespace lua_getmetatable(L, LUA_GLOBALSINDEX); - lua_pushcfunction(L, setglobals); - lua_setfield(L, -2, "__newindex"); + LUA_SetCFunctionField(L, "__newindex", setglobals); lua_newtable(L); lua_setfield(L, -2, "__metatable"); lua_pop(L, 1); @@ -1815,20 +1814,17 @@ void LUA_PushTaggableObjectArray lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_createtable(L, 0, 2); - lua_pushcfunction(L, iterator); - lua_setfield(L, -2, "iterate"); + LUA_SetCFunctionField(L, "iterate", iterator); LUA_InsertTaggroupIterator(L, garray, max_elements, element_array, sizeof_element, meta); lua_createtable(L, 0, 1); - lua_pushcfunction(L, indexer); - lua_setfield(L, -2, "__index"); + LUA_SetCFunctionField(L, "__index", indexer); lua_setmetatable(L, -2); lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, counter); - lua_setfield(L, -2, "__len"); + LUA_SetCFunctionField(L, "__len", counter); lua_setmetatable(L, -2); lua_setglobal(L, field); } @@ -1841,20 +1837,17 @@ static void SetBasicMetamethods( ) { if (get) - { - lua_pushcfunction(L, get); - lua_setfield(L, -2, "__index"); - } + LUA_SetCFunctionField(L, "__index", get); if (set) - { - lua_pushcfunction(L, set); - lua_setfield(L, -2, "__newindex"); - } + LUA_SetCFunctionField(L, "__newindex", set); if (len) - { - lua_pushcfunction(L, len); - lua_setfield(L, -2, "__len"); - } + LUA_SetCFunctionField(L, "__len", len); +} + +void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value) +{ + lua_pushcfunction(L, value); + lua_setfield(L, -2, name); } void LUA_RegisterUserdataMetatable( diff --git a/src/lua_script.h b/src/lua_script.h index dd9a2568e..53d848f8e 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -73,6 +73,8 @@ void LUA_PushTaggableObjectArray size_t sizeof_element, const char *meta); +void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value); + void LUA_RegisterUserdataMetatable( lua_State *L, const char *name, diff --git a/src/lua_taglib.c b/src/lua_taglib.c index 5ed457534..9e73a050c 100644 --- a/src/lua_taglib.c +++ b/src/lua_taglib.c @@ -372,8 +372,7 @@ void LUA_InsertTaggroupIterator lua_pushcclosure(L, lib_numTaggroupElements, 2); lua_setfield(L, -2, "__len"); - lua_pushcfunction(L, element_iterator); - lua_setfield(L, -2, "__call"); + LUA_SetCFunctionField(L, "__call", element_iterator); lua_pushcclosure(L, lib_getTaggroup, 1); lua_setfield(L, -2, "tagged"); } @@ -414,11 +413,9 @@ set_taglist_metatable(lua_State *L, const char *meta) lua_setfenv(L, -2); lua_setfield(L, -2, "__index"); - lua_pushcfunction(L, taglist_len); - lua_setfield(L, -2, "__len"); + LUA_SetCFunctionField(L, "__len", taglist_len); - lua_pushcfunction(L, taglist_equal); - lua_setfield(L, -2, "__eq"); + LUA_SetCFunctionField(L, "__eq", taglist_equal); #ifdef MUTABLE_TAGS return luaL_ref(L, LUA_REGISTRYINDEX); #endif @@ -428,8 +425,7 @@ int LUA_TagLib(lua_State *L) { LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true); lua_createtable(L, 0, 1); - lua_pushcfunction(L, lib_iterateTags); - lua_setfield(L, -2, "iterate"); + LUA_SetCFunctionField(L, "iterate", lib_iterateTags); lua_setfield(L, -2, "__index"); lua_pop(L, 2); diff --git a/src/lua_thinkerlib.c b/src/lua_thinkerlib.c index cff92f34d..f1be8c789 100644 --- a/src/lua_thinkerlib.c +++ b/src/lua_thinkerlib.c @@ -127,8 +127,7 @@ static int lib_startIterate(lua_State *L) int LUA_ThinkerLib(lua_State *L) { luaL_newmetatable(L, META_ITERATIONSTATE); - lua_pushcfunction(L, iterationState_gc); - lua_setfield(L, -2, "__gc"); + LUA_SetCFunctionField(L, "__gc", iterationState_gc); lua_pop(L, 1); lua_createtable(L, 0, 1);