beyond-all-reason / spring

A powerful free cross-platform RTS game engine
https://beyond-all-reason.github.io/spring/
Other
178 stars 95 forks source link

Add named constants for magic -1/-2 for Lua table key/value stack indices #1530

Open sprunk opened 4 weeks ago

sprunk commented 4 weeks ago

All over Lua code that iterates over tables there's these magic -1 and -2 constants used as stack indices. E.g. https://github.com/beyond-all-reason/spring/blob/747f18b2222860ae264853aa062c5b3d9b164e25/rts/Lua/LuaSyncedRead.cpp#L2703-L2704 https://github.com/beyond-all-reason/spring/blob/747f18b2222860ae264853aa062c5b3d9b164e25/rts/Lua/LuaSyncedCtrl.cpp#L425-L426

Add (and use) some sort of named constants for them for self-documentation, e.g.

static constexpr int LUA_TABLE_VALUE_INDEX = -1;
static constexpr int LUA_TABLE_KEY_INDEX   = -2;