Skip to content

Commit bf10fde

Browse files
committed
Allow negative indicies in more lua functions
1 parent e4f3cd9 commit bf10fde

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

library/LuaApi.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ void dfhack_printerr(lua_State *S, const string &str);
134134

135135
static df::coord2d CheckCoordXY(lua_State *state, int base, bool vararg = false)
136136
{
137+
base = lua_absindex(state, base);
138+
137139
df::coord2d p;
138140
if (vararg && lua_gettop(state) <= base)
139141
Lua::CheckDFAssign(state, &p, base);
@@ -149,6 +151,8 @@ static df::coord2d CheckCoordXY(lua_State *state, int base, bool vararg = false)
149151

150152
static df::coord CheckCoordXYZ(lua_State *state, int base, bool vararg = false)
151153
{
154+
base = lua_absindex(state, base);
155+
152156
df::coord p;
153157
if (vararg && lua_gettop(state) <= base)
154158
Lua::CheckDFAssign(state, &p, base);
@@ -628,6 +632,8 @@ void Lua::Push(lua_State *L, const Screen::Pen &info)
628632

629633
static Pen *check_pen_native(lua_State *L, int index)
630634
{
635+
index = lua_absindex(L, index);
636+
631637
lua_rawgetp(L, LUA_REGISTRYINDEX, &DFHACK_PEN_TOKEN);
632638

633639
if (!lua_getmetatable(L, index) || !lua_rawequal(L, -1, -2))
@@ -770,6 +776,8 @@ static void make_pen_table(lua_State *L, Pen &pen)
770776

771777
static void get_pen_mirror(lua_State *L, int idx)
772778
{
779+
idx = lua_absindex(L, idx);
780+
773781
lua_getuservalue(L, idx);
774782

775783
if (lua_isnil(L, -1))
@@ -957,6 +965,8 @@ using Screen::PenArray;
957965

958966
static PenArray *check_penarray_native(lua_State *L, int index)
959967
{
968+
index = lua_absindex(L, index);
969+
960970
lua_rawgetp(L, LUA_REGISTRYINDEX, &DFHACK_PENARRAY_TOKEN);
961971

962972
if (!lua_getmetatable(L, index) || !lua_rawequal(L, -1, -2))
@@ -1066,6 +1076,8 @@ static int DFHACK_RANDOM_TOKEN = 0;
10661076

10671077
static MersenneRNG *check_random_native(lua_State *L, int index)
10681078
{
1079+
index = lua_absindex(L, index);
1080+
10691081
lua_rawgetp(L, LUA_REGISTRYINDEX, &DFHACK_RANDOM_TOKEN);
10701082

10711083
if (!lua_getmetatable(L, index) || !lua_rawequal(L, -1, -2))

library/LuaTools.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ void DFHack::Lua::Push(lua_State *state, const df::coord2d &pos)
122122

123123
void DFHack::Lua::GetVector(lua_State *state, std::vector<std::string> &pvec, int idx)
124124
{
125+
idx = lua_absindex(state, idx);
126+
125127
luaL_checktype(state, idx, LUA_TTABLE);
126128
lua_pushnil(state); // first key
127129
while (lua_next(state, idx) != 0)
@@ -2063,6 +2065,8 @@ void DFHack::Lua::Core::onStateChange(color_ostream &out, int code) {
20632065
static void run_timers(color_ostream &out, lua_State *L,
20642066
std::multimap<int,int> &timers, int table, int bound)
20652067
{
2068+
table = lua_absindex(L, table);
2069+
20662070
while (!timers.empty() && timers.begin()->first <= bound)
20672071
{
20682072
int id = timers.begin()->second;

library/LuaWrapper.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ using namespace DFHack::LuaWrapper;
5151
*/
5252
void 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

778782
static 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

786792
static 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
*/
983991
uint8_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
*/
11711181
void 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
*/
11801192
void 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
*/
12191234
void 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
12341251
void 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
12461266
void 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
*/
12891315
void 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

14501479
void 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

14611492
static 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

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

library/include/LuaTools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ namespace DFHack {namespace Lua {
420420
template<typename T>
421421
requires std::is_arithmetic_v<T>
422422
void GetVector(lua_State *state, std::vector<T> &pvec, int idx = 1) {
423+
idx = lua_absindex(state, idx);
424+
423425
lua_pushnil(state); // first key
424426
while (lua_next(state, idx) != 0)
425427
{

0 commit comments

Comments
 (0)