From 2d06d308cff9b949acd3cea6139d5f14bf719dc7 Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 17 Jul 2023 18:29:07 +0300 Subject: [PATCH] Improve field lookup for player_t and mobj_t a bit more --- src/lua_mobjlib.c | 10 ++++------ src/lua_playerlib.c | 10 ++++------ src/lua_udatalib.c | 24 ++++++++++++------------ src/lua_udatalib.h | 4 ++++ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c index 34471bc75..d0afa89ff 100644 --- a/src/lua_mobjlib.c +++ b/src/lua_mobjlib.c @@ -559,13 +559,12 @@ static int mobj_get(lua_State *L) lua_getmetatable(L, 1); lua_pushvalue(L, 2); // Push field name - lua_pushliteral(L, ".get"); // Push suffix - lua_concat(L, 2); // Concat into "field_name.get" - lua_rawget(L, -2); // Get getter (lel) from metatable + lua_rawget(L, -2); // Get getter/setter table from metatable // If field exists, run getter for it if (!lua_isnil(L, -1)) { //CONS_Printf("Running getter for field %s\n", field); + lua_rawgeti(L, -1, UDATALIB_GETTER); lua_pushlightuserdata(L, mo); lua_call(L, 1, 1); //CONS_Printf("Getter returned %s\n", lua_typename(L, lua_type(L, -1))); @@ -614,13 +613,12 @@ static int mobj_set(lua_State *L) lua_getmetatable(L, 1); // Push metatable lua_pushvalue(L, 2); // Push field name - lua_pushliteral(L, ".set"); // Push suffix - lua_concat(L, 2); // Concat into "field_name.set" - lua_rawget(L, -2); // Get setter from metatable + lua_rawget(L, -2); // Get getter/setter table from metatable // If field exists, run setter for it if (!lua_isnil(L, -1)) { //CONS_Printf("Running setter for field %s\n", luaL_checkstring(L, 2)); + lua_rawgeti(L, -1, UDATALIB_SETTER); lua_pushlightuserdata(L, mo); lua_pushvalue(L, 3); lua_call(L, 2, 0); diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 229d741ea..263a9c5cd 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -492,13 +492,12 @@ static int player_get(lua_State *L) lua_getmetatable(L, 1); lua_pushvalue(L, 2); // Push field name - lua_pushliteral(L, ".get"); // Push suffix - lua_concat(L, 2); // Concat into "field_name.get" - lua_rawget(L, -2); // Get getter (lel) from metatable + lua_rawget(L, -2); // Get getter/setter table from metatable // If field exists, run getter for it if (!lua_isnil(L, -1)) { //CONS_Printf("Running getter for field %s\n", field); + lua_rawgeti(L, -1, UDATALIB_GETTER); lua_pushlightuserdata(L, plr); lua_call(L, 1, 1); //CONS_Printf("Getter returned %s\n", lua_typename(L, lua_type(L, -1))); @@ -538,13 +537,12 @@ static int player_set(lua_State *L) lua_getmetatable(L, 1); // Push metatable lua_pushvalue(L, 2); // Push field name - lua_pushliteral(L, ".set"); // Push suffix - lua_concat(L, 2); // Concat into "field_name.set" - lua_rawget(L, -2); // Get setter from metatable + lua_rawget(L, -2); // Get getter/setter table from metatable // If field exists, run setter for it if (!lua_isnil(L, -1)) { //CONS_Printf("Running setter for field %s\n", luaL_checkstring(L, 2)); + lua_rawgeti(L, -1, UDATALIB_SETTER); lua_pushlightuserdata(L, plr); lua_pushvalue(L, 3); lua_call(L, 2, 0); diff --git a/src/lua_udatalib.c b/src/lua_udatalib.c index 13a27436b..9dada464c 100644 --- a/src/lua_udatalib.c +++ b/src/lua_udatalib.c @@ -93,23 +93,23 @@ void udatalib_addfield(lua_State *L, int mt, udata_field_t field) { int idx = abs_index(L, mt); - lua_pushstring(L, field.name); - lua_pushliteral(L, ".get"); - lua_concat(L, 2); + lua_createtable(L, 2, 0); // Push table - lua_pushinteger(L, field.offset); - lua_pushcclosure(L, field.getter, 1); + lua_pushinteger(L, field.offset); // Push offset + lua_pushcclosure(L, field.getter, 1); // Pop offset, push closure - lua_settable(L, idx); + lua_rawseti(L, -2, UDATALIB_GETTER); // Pop closure, add to table - lua_pushstring(L, field.name); - lua_pushliteral(L, ".set"); - lua_concat(L, 2); + lua_pushinteger(L, field.offset); // Push offset + lua_pushcclosure(L, field.setter, 1); // Pop offset, push closure - lua_pushinteger(L, field.offset); - lua_pushcclosure(L, field.setter, 1); + lua_rawseti(L, -2, UDATALIB_SETTER); // Pop closure, add to table - lua_settable(L, idx); + lua_pushstring(L, field.name); // Push field name + lua_pushvalue(L, -2); // Copy table + lua_rawset(L, idx); // Pop field name and table, add to metatable + + lua_pop(L, 1); // Pop field table } void udatalib_addfields(lua_State *L, int mt, const udata_field_t fields[]) diff --git a/src/lua_udatalib.h b/src/lua_udatalib.h index b8eb6e4f1..1b73113eb 100644 --- a/src/lua_udatalib.h +++ b/src/lua_udatalib.h @@ -10,6 +10,10 @@ #include "lua_libs.h" #include "fastcmp.h" +// Indices in getter/setter tables for fields +#define UDATALIB_GETTER 1 +#define UDATALIB_SETTER 2 + typedef struct udata_field_s { const char *name; intptr_t offset;