Add LUA_SetCFunctionField

This commit is contained in:
LJ Sonic 2023-10-28 11:06:30 +02:00
parent d1dbdee016
commit c3f79a112f
7 changed files with 24 additions and 38 deletions

View file

@ -771,8 +771,7 @@ int LUA_SOCLib(lua_State *L)
lua_register(L,"getActionName",lib_getActionName); lua_register(L,"getActionName",lib_getActionName);
luaL_newmetatable(L, META_ACTION); luaL_newmetatable(L, META_ACTION);
lua_pushcfunction(L, action_call); LUA_SetCFunctionField(L, "__call", action_call);
lua_setfield(L, -2, "__call");
lua_pop(L, 1); lua_pop(L, 1);
return 0; return 0;

View file

@ -4300,8 +4300,7 @@ int LUA_BaseLib(lua_State *L)
// Set metatable for string // Set metatable for string
lua_pushliteral(L, ""); // dummy string lua_pushliteral(L, ""); // dummy string
lua_getmetatable(L, -1); // get string metatable lua_getmetatable(L, -1); // get string metatable
lua_pushcfunction(L,lib_concat); // push concatination function LUA_SetCFunctionField(L, "__add", lib_concat);
lua_setfield(L,-2,"__add"); // ... store it as mathematical addition
lua_pop(L, 2); // pop metatable and dummy string lua_pop(L, 2); // pop metatable and dummy string
lua_newtable(L); lua_newtable(L);

View file

@ -2898,10 +2898,8 @@ int LUA_MapLib(lua_State *L)
node_fields_ref = Lua_CreateFieldTable(L, node_opt); node_fields_ref = Lua_CreateFieldTable(L, node_opt);
luaL_newmetatable(L, META_NODEBBOX); luaL_newmetatable(L, META_NODEBBOX);
//lua_pushcfunction(L, nodebbox_get); //LUA_SetCFunctionField(L, "__index", nodebbox_get);
//lua_setfield(L, -2, "__index"); LUA_SetCFunctionField(L, "__call", nodebbox_call);
lua_pushcfunction(L, nodebbox_call);
lua_setfield(L, -2, "__call");
lua_pop(L, 1); lua_pop(L, 1);
LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs); LUA_RegisterGlobalUserdata(L, "segs", lib_getSeg, NULL, lib_numsegs);

View file

@ -578,8 +578,7 @@ static void LUA_ClearState(void)
// lock the global namespace // lock the global namespace
lua_getmetatable(L, LUA_GLOBALSINDEX); lua_getmetatable(L, LUA_GLOBALSINDEX);
lua_pushcfunction(L, setglobals); LUA_SetCFunctionField(L, "__newindex", setglobals);
lua_setfield(L, -2, "__newindex");
lua_newtable(L); lua_newtable(L);
lua_setfield(L, -2, "__metatable"); lua_setfield(L, -2, "__metatable");
lua_pop(L, 1); lua_pop(L, 1);
@ -1815,20 +1814,17 @@ void LUA_PushTaggableObjectArray
lua_newuserdata(L, 0); lua_newuserdata(L, 0);
lua_createtable(L, 0, 2); lua_createtable(L, 0, 2);
lua_createtable(L, 0, 2); lua_createtable(L, 0, 2);
lua_pushcfunction(L, iterator); LUA_SetCFunctionField(L, "iterate", iterator);
lua_setfield(L, -2, "iterate");
LUA_InsertTaggroupIterator(L, garray, LUA_InsertTaggroupIterator(L, garray,
max_elements, element_array, sizeof_element, meta); max_elements, element_array, sizeof_element, meta);
lua_createtable(L, 0, 1); lua_createtable(L, 0, 1);
lua_pushcfunction(L, indexer); LUA_SetCFunctionField(L, "__index", indexer);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, counter); LUA_SetCFunctionField(L, "__len", counter);
lua_setfield(L, -2, "__len");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_setglobal(L, field); lua_setglobal(L, field);
} }
@ -1841,20 +1837,17 @@ static void SetBasicMetamethods(
) )
{ {
if (get) if (get)
{ LUA_SetCFunctionField(L, "__index", get);
lua_pushcfunction(L, get);
lua_setfield(L, -2, "__index");
}
if (set) if (set)
{ LUA_SetCFunctionField(L, "__newindex", set);
lua_pushcfunction(L, set);
lua_setfield(L, -2, "__newindex");
}
if (len) if (len)
{ LUA_SetCFunctionField(L, "__len", len);
lua_pushcfunction(L, len);
lua_setfield(L, -2, "__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( void LUA_RegisterUserdataMetatable(

View file

@ -73,6 +73,8 @@ void LUA_PushTaggableObjectArray
size_t sizeof_element, size_t sizeof_element,
const char *meta); const char *meta);
void LUA_SetCFunctionField(lua_State *L, const char *name, lua_CFunction value);
void LUA_RegisterUserdataMetatable( void LUA_RegisterUserdataMetatable(
lua_State *L, lua_State *L,
const char *name, const char *name,

View file

@ -372,8 +372,7 @@ void LUA_InsertTaggroupIterator
lua_pushcclosure(L, lib_numTaggroupElements, 2); lua_pushcclosure(L, lib_numTaggroupElements, 2);
lua_setfield(L, -2, "__len"); lua_setfield(L, -2, "__len");
lua_pushcfunction(L, element_iterator); LUA_SetCFunctionField(L, "__call", element_iterator);
lua_setfield(L, -2, "__call");
lua_pushcclosure(L, lib_getTaggroup, 1); lua_pushcclosure(L, lib_getTaggroup, 1);
lua_setfield(L, -2, "tagged"); lua_setfield(L, -2, "tagged");
} }
@ -414,11 +413,9 @@ set_taglist_metatable(lua_State *L, const char *meta)
lua_setfenv(L, -2); lua_setfenv(L, -2);
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, taglist_len); LUA_SetCFunctionField(L, "__len", taglist_len);
lua_setfield(L, -2, "__len");
lua_pushcfunction(L, taglist_equal); LUA_SetCFunctionField(L, "__eq", taglist_equal);
lua_setfield(L, -2, "__eq");
#ifdef MUTABLE_TAGS #ifdef MUTABLE_TAGS
return luaL_ref(L, LUA_REGISTRYINDEX); return luaL_ref(L, LUA_REGISTRYINDEX);
#endif #endif
@ -428,8 +425,7 @@ int LUA_TagLib(lua_State *L)
{ {
LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true); LUA_CreateAndSetUserdataField(L, LUA_GLOBALSINDEX, "tags", NULL, NULL, lib_numTags, true);
lua_createtable(L, 0, 1); lua_createtable(L, 0, 1);
lua_pushcfunction(L, lib_iterateTags); LUA_SetCFunctionField(L, "iterate", lib_iterateTags);
lua_setfield(L, -2, "iterate");
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
lua_pop(L, 2); lua_pop(L, 2);

View file

@ -127,8 +127,7 @@ static int lib_startIterate(lua_State *L)
int LUA_ThinkerLib(lua_State *L) int LUA_ThinkerLib(lua_State *L)
{ {
luaL_newmetatable(L, META_ITERATIONSTATE); luaL_newmetatable(L, META_ITERATIONSTATE);
lua_pushcfunction(L, iterationState_gc); LUA_SetCFunctionField(L, "__gc", iterationState_gc);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1); lua_pop(L, 1);
lua_createtable(L, 0, 1); lua_createtable(L, 0, 1);