From 4a9a83daa5e40cf8f1cd493ffcdc699434fe90f6 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 12 Aug 2023 19:28:02 -0400 Subject: [PATCH] Expose more fields, refactor --- library/LuaTypes.cpp | 55 +++++++++++++++++++++---------- test/structures/struct_fields.lua | 8 +++++ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp index 1cc83be31..0d81886db 100644 --- a/library/LuaTypes.cpp +++ b/library/LuaTypes.cpp @@ -1365,6 +1365,42 @@ static void IndexFields(lua_State *state, int base, struct_identity *pstruct, in } } +static void PushFieldInfoSubTable(lua_State *state, const struct_field_info *field) +{ + lua_newtable(state); // new field info + Lua::TableInsert(state, "mode", field->mode); + Lua::TableInsert(state, "name", field->name); + Lua::TableInsert(state, "offset", field->offset); + Lua::TableInsert(state, "count", field->count); + + if (field->type) { + Lua::TableInsert(state, "type_name", field->type->getFullName()); + + lua_pushlightuserdata(state, field->type); + lua_setfield(state, -2, "type_identity"); + + lua_pushstring(state, "type"); + lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_TYPEID_TABLE_TOKEN); + lua_rawgetp(state, -1, field->type); + lua_remove(state, -2); // TYPEID_TABLE + lua_settable(state, -3); + } + + if (field->extra) { + // TODO: index_enum, ref_target + if (field->extra->union_tag_field) { + Lua::TableInsert(state, "union_tag_field", field->extra->union_tag_field); + } + if (field->extra->union_tag_attr) { + Lua::TableInsert(state, "union_tag_attr", field->extra->union_tag_attr); + } + if (field->extra->original_name) { + Lua::TableInsert(state, "original_name", field->extra->original_name); + } + } + // freeze_table(state); // TODO: make pairs() work +} + static void AddFieldInfoTable(lua_State *state, int ftable_idx, struct_identity *pstruct) { Lua::StackUnwinder base{state}; @@ -1412,24 +1448,9 @@ static void AddFieldInfoTable(lua_State *state, int ftable_idx, struct_identity while (lua_next(state, ix_fieldinfo)) { auto field = static_cast(lua_touserdata(state, -1)); lua_pushvalue(state, -2); // field name - lua_newtable(state); // new field info - Lua::TableInsert(state, "name", field->name); - Lua::TableInsert(state, "offset", field->offset); - - if (field->type) { - Lua::TableInsert(state, "type_name", field->type->getFullName()); - - lua_pushstring(state, "type"); - lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_TYPEID_TABLE_TOKEN); - lua_rawgetp(state, -1, field->type); - lua_remove(state, -2); // TYPEID_TABLE - lua_settable(state, -3); - } - - // freeze_table(state); // TODO: make pairs() work + PushFieldInfoSubTable(state, field); lua_settable(state, ix_fields); - - lua_pop(state, 1); // field name + lua_pop(state, 1); // struct_field_info } // lua_pushvalue(state, ix_fields); diff --git a/test/structures/struct_fields.lua b/test/structures/struct_fields.lua index e94c9fbc5..5ad513dcc 100644 --- a/test/structures/struct_fields.lua +++ b/test/structures/struct_fields.lua @@ -21,6 +21,14 @@ function test.access() expect.eq(fields[name].name, name, name) expect.eq(fields[name].type_name, expected.type_name, name) expect.eq(type(fields[name].offset), 'number', name) + expect.eq(type(fields[name].mode), 'number', name) + expect.eq(type(fields[name].count), 'number', name) + end +end + +function test.globals_original_name() + for name, info in pairs(df.global._fields) do + expect.eq(type(info.original_name), 'string', name) end end