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);
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;

View file

@ -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);

View file

@ -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);

View file

@ -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(

View file

@ -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,

View file

@ -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);

View file

@ -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);