Improve field lookup for player_t and mobj_t a bit more

This commit is contained in:
indev committed 2023-07-17 18:29:07 +03:00
1 parent 83cfaa6212
commit 2d06d308cf
4 files changed
+24 -24

No files matched your search

+4 -6
View File
@@ -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);
+4 -6
View File
@@ -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);
+12 -12
View File
@@ -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[])
+4
View File
@@ -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;