@@ -51,6 +51,8 @@ using namespace DFHack::LuaWrapper;
5151 */
5252void LuaWrapper::field_error (lua_State *state, int index, const char *err, const char *mode)
5353{
54+ index = lua_absindex (state, index);
55+
5456 if (lua_islightuserdata (state, UPVAL_METATABLE))
5557 lua_pushstring (state, " (global)" );
5658 else
@@ -495,6 +497,8 @@ const type_identity *LuaWrapper::get_object_identity(lua_State *state, int objid
495497 const char *ctx, bool allow_type,
496498 bool keep_metatable)
497499{
500+ objidx = lua_absindex (state, objidx);
501+
498502 if (allow_type && !keep_metatable && lua_isstring (state, objidx))
499503 {
500504 int idx = luaL_checkoption (state, objidx, NULL , primitive_types);
@@ -777,6 +781,8 @@ static int meta_reinterpret_cast(lua_State *state)
777781
778782static void invoke_resize (lua_State *state, int table, lua_Integer size)
779783{
784+ table = lua_absindex (state, table);
785+
780786 lua_getfield (state, table, " resize" );
781787 lua_pushvalue (state, table);
782788 lua_pushinteger (state, size);
@@ -785,6 +791,8 @@ static void invoke_resize(lua_State *state, int table, lua_Integer size)
785791
786792static void copy_table (lua_State *state, int dest, int src, int skipbase)
787793{
794+ src = lua_absindex (state, src);
795+ dest = lua_absindex (state, dest);
788796 // stack: (skipbase) skipkey skipkey |
789797
790798 int top = lua_gettop (state);
@@ -982,6 +990,8 @@ static int meta_delete(lua_State *state)
982990 */
983991uint8_t *LuaWrapper::get_object_addr (lua_State *state, int obj, int field, const char *mode)
984992{
993+ field = lua_absindex (state, field);
994+
985995 if (!lua_isuserdata (state, obj) ||
986996 !lua_getmetatable (state, obj))
987997 field_error (state, field, " invalid object" , mode);
@@ -1170,6 +1180,8 @@ void LuaWrapper::MakeMetatable(lua_State *state, const type_identity *type, cons
11701180 */
11711181void LuaWrapper::EnableMetaField (lua_State *state, int ftable_idx, const char *name, void *id)
11721182{
1183+ ftable_idx = lua_absindex (state, ftable_idx);
1184+
11731185 lua_pushlightuserdata (state, id);
11741186 lua_setfield (state, ftable_idx, name);
11751187}
@@ -1179,6 +1191,9 @@ void LuaWrapper::EnableMetaField(lua_State *state, int ftable_idx, const char *n
11791191 */
11801192void LuaWrapper::SetPtrMethods (lua_State *state, int meta_idx, int read_idx)
11811193{
1194+ meta_idx = lua_absindex (state, meta_idx);
1195+ read_idx = lua_absindex (state, read_idx);
1196+
11821197 lua_getfield (state, LUA_REGISTRYINDEX, DFHACK_COMPARE_NAME);
11831198 lua_setfield (state, meta_idx, " __eq" );
11841199
@@ -1218,6 +1233,8 @@ void LuaWrapper::SetPtrMethods(lua_State *state, int meta_idx, int read_idx)
12181233 */
12191234void LuaWrapper::SetPairsMethod (lua_State *state, int meta_idx, const char *name)
12201235{
1236+ meta_idx = lua_absindex (state, meta_idx);
1237+
12211238 if (lua_isnil (state, -1 ))
12221239 {
12231240 lua_pop (state, 1 );
@@ -1234,6 +1251,9 @@ void LuaWrapper::SetPairsMethod(lua_State *state, int meta_idx, const char *name
12341251void LuaWrapper::PushStructMethod (lua_State *state, int meta_idx, int ftable_idx,
12351252 lua_CFunction function)
12361253{
1254+ meta_idx = lua_absindex (state, meta_idx);
1255+ ftable_idx = lua_absindex (state, ftable_idx);
1256+
12371257 lua_rawgetp (state, LUA_REGISTRYINDEX, &DFHACK_TYPETABLE_TOKEN);
12381258 lua_pushvalue (state, meta_idx);
12391259 lua_pushvalue (state, ftable_idx);
@@ -1246,6 +1266,9 @@ void LuaWrapper::PushStructMethod(lua_State *state, int meta_idx, int ftable_idx
12461266void LuaWrapper::SetStructMethod (lua_State *state, int meta_idx, int ftable_idx,
12471267 lua_CFunction function, const char *name)
12481268{
1269+ meta_idx = lua_absindex (state, meta_idx);
1270+ ftable_idx = lua_absindex (state, ftable_idx);
1271+
12491272 PushStructMethod (state, meta_idx, ftable_idx, function);
12501273 lua_setfield (state, meta_idx, name);
12511274}
@@ -1257,6 +1280,9 @@ void LuaWrapper::PushContainerMethod(lua_State *state, int meta_idx, int ftable_
12571280 lua_CFunction function,
12581281 const type_identity *container, const type_identity *item, int count)
12591282{
1283+ meta_idx = lua_absindex (state, meta_idx);
1284+ ftable_idx = lua_absindex (state, ftable_idx);
1285+
12601286 lua_rawgetp (state, LUA_REGISTRYINDEX, &DFHACK_TYPETABLE_TOKEN);
12611287 lua_pushvalue (state, meta_idx);
12621288 lua_pushvalue (state, ftable_idx);
@@ -1288,6 +1314,9 @@ void LuaWrapper::SetContainerMethod(lua_State *state, int meta_idx, int ftable_i
12881314 */
12891315void LuaWrapper::AttachEnumKeys (lua_State *state, int meta_idx, int ftable_idx, const type_identity *ienum)
12901316{
1317+ meta_idx = lua_absindex (state, meta_idx);
1318+ ftable_idx = lua_absindex (state, ftable_idx);
1319+
12911320 EnableMetaField (state, ftable_idx, " _enum" );
12921321
12931322 LookupInTable (state, const_cast <type_identity*>(ienum), &DFHACK_TYPEID_TABLE_TOKEN);
@@ -1449,6 +1478,8 @@ static void RenderTypeChildren(lua_State *state, const std::vector<const compoun
14491478
14501479void LuaWrapper::AssociateId (lua_State *state, int table, int val, const char *name)
14511480{
1481+ table = lua_absindex (state, table);
1482+
14521483 lua_pushinteger (state, val);
14531484 lua_pushstring (state, name);
14541485 lua_dup (state);
@@ -1460,6 +1491,9 @@ void LuaWrapper::AssociateId(lua_State *state, int table, int val, const char *n
14601491
14611492static void FillEnumKeys (lua_State *state, int ix_meta, int ftable, enum_identity *eid)
14621493{
1494+ ix_meta = lua_absindex (state, ix_meta);
1495+ ftable = lua_absindex (state, ftable);
1496+
14631497 const char *const *keys = eid->getKeys ();
14641498
14651499 // Create a new table attached to ftable as __index
@@ -1554,6 +1588,9 @@ static void FillEnumKeys(lua_State *state, int ix_meta, int ftable, enum_identit
15541588
15551589static void FillBitfieldKeys (lua_State *state, int ix_meta, int ftable, bitfield_identity *eid)
15561590{
1591+ ix_meta = lua_absindex (state, ix_meta);
1592+ ftable = lua_absindex (state, ftable);
1593+
15571594 // Create a new table attached to ftable as __index
15581595 lua_newtable (state);
15591596 int base = lua_gettop (state);
0 commit comments