Eliminate LuaStack::rawgeti, it's not safe

This commit is contained in:
2021-11-17 15:11:55 -05:00
parent fb043e9971
commit 0881e33c6f
5 changed files with 32 additions and 32 deletions

View File

@@ -429,7 +429,7 @@ public:
}
template<typename RT>
void rawgeti(RT &target, LuaSlot tab, int key) const {
void rawget(RT &target, LuaSlot tab, int key) const {
lua_rawgeti(L_, tab, key);
pop_any_value(target);
}
@@ -442,7 +442,7 @@ public:
}
template<typename VT>
void rawseti(LuaSlot tab, int key, VT value) const {
void rawset(LuaSlot tab, int key, VT value) const {
push_any_value(value);
lua_rawseti(L_, tab, key);
}

View File

@@ -175,9 +175,9 @@ static void pprint_r(Inspector &insp, int level, LuaSlot root) {
// Output the table keys.
table_getpairs(LS, root, pairs, true);
for (int i = 2; ; i+=2) {
LS.rawgeti(key, pairs, i);
LS.rawget(key, pairs, i);
if (LS.isnil(key)) break;
LS.rawgeti(val, pairs, i+1);
LS.rawget(val, pairs, i+1);
if (needcomma) (*insp.stream) << ",";
needcomma = true;
if (LS.isnumber(key) && (LS.ckint(key) == nextseq)) {

View File

@@ -244,11 +244,11 @@ LuaDefine(deque_create, "c") {
LuaStack LS(L, rdeque, classobj);
const int imax = 4;
LS.createtable(rdeque, DEQUE_BASE + imax - 1, 0);
LS.rawseti(rdeque, DEQUE_LEFT, 0);
LS.rawseti(rdeque, DEQUE_FILL, 0);
LS.rawseti(rdeque, DEQUE_MAX, imax);
LS.rawset(rdeque, DEQUE_LEFT, 0);
LS.rawset(rdeque, DEQUE_FILL, 0);
LS.rawset(rdeque, DEQUE_MAX, imax);
for (int i = 0; i < imax; i++) {
LS.rawseti(rdeque, DEQUE_BASE + i, 0);
LS.rawset(rdeque, DEQUE_BASE + i, 0);
}
LS.makeclass(classobj, "deque");
LS.setmetatable(rdeque, classobj);
@@ -262,7 +262,7 @@ LuaDefine(deque_pushl, "c") {
deque_get_info(L, deque.index(), &left, &fill, &max);
max = deque_make_room(L, deque.index(), left, fill, max);
int target = (left - 1) & (max-1);
LS.rawseti(deque, DEQUE_BASE + target, elt);
LS.rawset(deque, DEQUE_BASE + target, elt);
fill += 1;
left = (left - 1) & (max - 1);
deque_put_info(L, deque.index(), &left, &fill, NULL);
@@ -276,7 +276,7 @@ LuaDefine(deque_pushr, "c") {
deque_get_info(L, deque.index(), &left, &fill, &max);
max = deque_make_room(L, deque.index(), left, fill, max);
int target = (left + fill) & (max-1);
LS.rawseti(deque, DEQUE_BASE + target, elt);
LS.rawset(deque, DEQUE_BASE + target, elt);
fill += 1;
deque_put_info(L, deque.index(), NULL, &fill, NULL);
return LS.result();
@@ -292,8 +292,8 @@ LuaDefine(deque_popl, "c") {
LS.set(result, LuaNil);
return LS.result();
}
LS.rawgeti(result, deque, DEQUE_BASE + left);
LS.rawseti(deque, DEQUE_BASE + left, 0);
LS.rawget(result, deque, DEQUE_BASE + left);
LS.rawset(deque, DEQUE_BASE + left, 0);
left = (left + 1) & (max - 1);
fill -= 1;
deque_put_info(L, deque.index(), &left, &fill, NULL);
@@ -311,8 +311,8 @@ LuaDefine(deque_popr, "c") {
return LS.result();
}
int target = (left + fill - 1) & (max - 1);
LS.rawgeti(result, deque, DEQUE_BASE + target);
LS.rawseti(deque, DEQUE_BASE + target, 0);
LS.rawget(result, deque, DEQUE_BASE + target);
LS.rawset(deque, DEQUE_BASE + target, 0);
fill -= 1;
deque_put_info(L, deque.index(), NULL, &fill, NULL);
return LS.result();
@@ -330,7 +330,7 @@ LuaDefine(deque_nthl, "c") {
return LS.result();
}
int target = (left + n - 1) & (max - 1);
LS.rawgeti(result, deque, DEQUE_BASE + target);
LS.rawget(result, deque, DEQUE_BASE + target);
return LS.result();
}
@@ -346,7 +346,7 @@ LuaDefine(deque_nthr, "c") {
return LS.result();
}
int target = (left + fill - n) & (max - 1);
LS.rawgeti(result, deque, DEQUE_BASE + target);
LS.rawget(result, deque, DEQUE_BASE + target);
return LS.result();
}
@@ -361,7 +361,7 @@ LuaDefine(deque_setl, "c") {
return LS.result();
}
int target = (left + n - 1) & (max - 1);
LS.rawseti(deque, DEQUE_BASE + target, val);
LS.rawset(deque, DEQUE_BASE + target, val);
return LS.result();
}
@@ -376,7 +376,7 @@ LuaDefine(deque_setr, "c") {
return LS.result();
}
int target = (left + fill - n) & (max - 1);
LS.rawseti(deque, DEQUE_BASE + target, val);
LS.rawset(deque, DEQUE_BASE + target, val);
return LS.result();
}
@@ -389,7 +389,7 @@ LuaDefine(deque_findl, "c") {
deque_get_info(L, deque.index(), &left, &fill, &max);
for (int i = 0; i < fill; i++) {
int index = (left + i) & (max - 1);
LS.rawgeti(check, deque, DEQUE_BASE + index);
LS.rawget(check, deque, DEQUE_BASE + index);
if (LS.rawequal(check, val)) {
LS.set(pos, i + 1);
return LS.result();
@@ -409,7 +409,7 @@ LuaDefine(deque_findr, "c") {
int base = left + fill - 1;
for (int i = 0; i < fill; i++) {
int index = (base - i) & (max - 1);
LS.rawgeti(check, deque, DEQUE_BASE + index);
LS.rawget(check, deque, DEQUE_BASE + index);
if (LS.rawequal(check, val)) {
LS.set(pos, i + 1);
return LS.result();
@@ -424,7 +424,7 @@ LuaDefine(deque_size, "c") {
LuaRet size;
LuaStack LS(L, deque, size);
LS.checktable(deque);
LS.rawgeti(size, deque, DEQUE_FILL);
LS.rawget(size, deque, DEQUE_FILL);
LS.checknumber(size);
return LS.result();
}
@@ -548,7 +548,7 @@ bool table_getpairs(LuaStack &LS0, LuaSlot tab, LuaSlot pairs, bool sort) {
// Create the table, store the initial 1.
int total = lua_nkeys(L, tab.index());
LS.createtable(pairs, total * 2 + 1, 0);
LS.rawseti(pairs, 1, 1);
LS.rawset(pairs, 1, 1);
// Transfer the pairs into the sequence.
lua_pushnil(L);
int offset = 2;

View File

@@ -263,7 +263,7 @@ static void set_transmitted_value(LuaStack &LS, LuaSlot tangibles, LuaSlot ntmap
return;
}
case LUA_TT_GENERAL: {
LS.rawgeti(target, ntmap, sb->read_int32());
LS.rawget(target, ntmap, sb->read_int32());
return;
}
case LUA_TT_CLASS: {
@@ -272,7 +272,7 @@ static void set_transmitted_value(LuaStack &LS, LuaSlot tangibles, LuaSlot ntmap
}
case LUA_TT_TANGIBLE: {
int64_t id = sb->read_int64();
LS.rawgeti(target, tangibles, id);
LS.rawget(target, tangibles, id);
if (LS.isnil(target)) {
World *w = World::fetch_global_pointer(LS.state());
w->tangible_make(LS.state(), id, "nowhere", true);
@@ -321,7 +321,7 @@ void World::patch_numbered_tables(StreamBuffer *sb) {
int nmodified = sb->read_int32();
for (int i = 0; i < nmodified; i++) {
int index = sb->read_int32();
LS.rawgeti(tab, ntmap, index);
LS.rawget(tab, ntmap, index);
assert(LS.istable(tab));
patch_table(LS, tangibles, ntmap, tab, sb);
}
@@ -347,9 +347,9 @@ void World::diff_numbered_tables(lua_State *master, StreamBuffer *sb) {
int s_top = lua_gettop(synch);
int m_top = lua_gettop(master);
for (int id = 1; id <= m_ntables; id++) {
MLS.rawgeti(mtab, mntmap, id);
MLS.rawget(mtab, mntmap, id);
if (MLS.istable(mtab)) {
SLS.rawgeti(stab, sntmap, id);
SLS.rawget(stab, sntmap, id);
assert(SLS.istable(stab));
int tw = sb->total_writes();
sb->write_int32(id);
@@ -379,7 +379,7 @@ void World::patch_tangible_databases(StreamBuffer *sb) {
int nmodified = sb->read_int32();
for (int i = 0; i < nmodified; i++) {
int64_t id = sb->read_int64();
LS.rawgeti(tab, tangibles, id);
LS.rawget(tab, tangibles, id);
assert(LS.istable(tab));
patch_table(LS, tangibles, ntmap, tab, sb);
}

View File

@@ -45,7 +45,7 @@ std::string World::tangible_pprint(int64_t id) const {
LuaVar tangibles, tan, meta;
LuaStack LS(L, tangibles, tan, meta);
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawgeti(tan, tangibles, id);
LS.rawget(tan, tangibles, id);
std::ostringstream oss;
if (LS.istable(tan)) {
LS.getmetatable(meta, tan);
@@ -72,7 +72,7 @@ std::string World::numbered_tables_debug_string() const {
// Iterate over the map. For each table, if it has
// a TID, output that. Otherwise, output "unknown".
for (int i = 1; i < 10000; i++) {
LS.rawgeti(tab, ntmap, i);
LS.rawget(tab, ntmap, i);
if (!LS.istable(tab)) break;
LS.rawget(tid, tab, "TID");
if (LS.isstring(tid)) {
@@ -201,7 +201,7 @@ void World::tangible_set_class(int64_t id, const std::string &c) const {
LuaVar tangibles, tan, meta, sclass;
LuaStack LS(state(), tangibles, tan, meta, sclass);
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawgeti(tan, tangibles, id);
LS.rawget(tan, tangibles, id);
assert(LS.istable(tan));
LS.getmetatable(meta, tan);
if (c == "") {
@@ -217,7 +217,7 @@ std::string World::tangible_get_class(int64_t id) const {
LuaVar tangibles, tan, meta, sclass;
LuaStack LS(state(), tangibles, tan, meta, sclass);
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawgeti(tan, tangibles, id);
LS.rawget(tan, tangibles, id);
assert(LS.istable(tan));
LS.getmetatable(meta, tan);
LS.rawget(sclass, meta, "__index");