Get rid of getfield/setfield

This commit is contained in:
2021-02-10 16:47:45 -05:00
parent 3883f86dee
commit eefe1bd58a
5 changed files with 63 additions and 61 deletions

View File

@@ -48,10 +48,10 @@ void LuaStack::register_all_userdata(lua_State *L) {
LS.set(classname, name); LS.set(classname, name);
if (mode.find('t') != std::string::npos) { // Register type if (mode.find('t') != std::string::npos) { // Register type
LS.newtable(tab); LS.newtable(tab);
LS.setfield(tab, "type", name); LS.rawset(tab, "type", name);
LS.setfield(tab, "__gc", collect_tagged_pointer); LS.rawset(tab, "__gc", collect_tagged_pointer);
LS.makeclass(classtab, classname); LS.makeclass(classtab, classname);
LS.setfield(tab, "__index", classtab); LS.rawset(tab, "__index", classtab);
LS.setlightuserdata(lud, (void *)tag); LS.setlightuserdata(lud, (void *)tag);
LS.rawset(LuaRegistry, lud, tab); LS.rawset(LuaRegistry, lud, tab);
} }
@@ -216,8 +216,8 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
} }
// Repair the special fields. // Repair the special fields.
setfield(classtab, "__index", classtab); rawset(classtab, "__index", classtab);
setfield(classtab, "__class", classname); rawset(classtab, "__class", classname);
} }
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const { void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
@@ -254,7 +254,7 @@ LuaDefine(system_type, "f") {
int type = LS.type(obj); int type = LS.type(obj);
if (type == LUA_TUSERDATA) { if (type == LUA_TUSERDATA) {
LS.getmetatable(mt, obj); LS.getmetatable(mt, obj);
LS.getfield(tname, mt, "type"); LS.rawget(tname, mt, "type");
} else { } else {
LS.set(tname, lua_typename(L, type)); LS.set(tname, lua_typename(L, type));
} }

View File

@@ -504,17 +504,17 @@ public:
lua_rawset(L_, tab); lua_rawset(L_, tab);
} }
template<typename VT> // template<typename VT>
void setfield(LuaSlot tab, const char *field, VT value) const { // void rawset(LuaSlot tab, const char *field, VT value) const {
push_any_value(value); // push_any_value(value);
lua_setfield(L_, tab, field); // lua_rawset(L_, tab, field);
} // }
template<typename RT> // template<typename RT>
void getfield(RT &target, LuaSlot tab, const char *field) const { // void rawget(RT &target, LuaSlot tab, const char *field) const {
lua_getfield(L_, tab, field); // lua_rawget(L_, tab, field);
pop_any_value(target); // pop_any_value(target);
} // }
// Call invokes any C function. It pushes the arguments on the stack, // Call invokes any C function. It pushes the arguments on the stack,
// calls the cfunction, verifies that the number of return values is as // calls the cfunction, verifies that the number of return values is as

View File

@@ -91,15 +91,15 @@ void SourceDB::initialize(lua_State *L) {
} }
} }
} }
LS.setfield(LuaRegistry, "source_snapshot_builtins", snapshot); LS.rawset(LuaRegistry, "source_snapshot_builtins", snapshot);
LS.result(); LS.result();
} }
static int source_updatefile(lua_State *L) { static int source_updatefile(lua_State *L) {
LuaArg source, fn; LuaArg source, fn;
LuaRet info; LuaRet info;
LuaVar fingerprint, null; LuaVar fingerprint, null, loadresult;
LuaStack LS(L, source, fn, info, fingerprint, null); LuaStack LS(L, source, fn, info, fingerprint, null, loadresult);
// Get the existing info table from the source DB. // Get the existing info table from the source DB.
if (LS.istable(source)) { if (LS.istable(source)) {
@@ -115,7 +115,7 @@ static int source_updatefile(lua_State *L) {
// these fields: code, fingerprint, closure, error // these fields: code, fingerprint, closure, error
// Otherwise, update nothing. // Otherwise, update nothing.
std::string cfn = LS.ckstring(fn); std::string cfn = LS.ckstring(fn);
LS.getfield(fingerprint, info, "fingerprint"); LS.rawget(fingerprint, info, "fingerprint");
std::string old_fingerprint; std::string old_fingerprint;
if (LS.isstring(fingerprint)) { if (LS.isstring(fingerprint)) {
old_fingerprint = LS.ckstring(fingerprint); old_fingerprint = LS.ckstring(fingerprint);
@@ -126,15 +126,16 @@ static int source_updatefile(lua_State *L) {
if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) { if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) {
std::cerr << "Rereading " << cfn << std::endl; std::cerr << "Rereading " << cfn << std::endl;
std::string ccode = util::get_file_contents("lua/" + cfn); std::string ccode = util::get_file_contents("lua/" + cfn);
LS.setfield(info, "name", fn); LS.rawset(info, "name", fn);
LS.setfield(info, "fingerprint", new_fingerprint); LS.rawset(info, "fingerprint", new_fingerprint);
LS.setfield(info, "code", ccode); LS.rawset(info, "code", ccode);
if ((new_fingerprint == "")||(ccode == "")) { if ((new_fingerprint == "")||(ccode == "")) {
LS.setfield(info, "loadresult", "cannot read source file"); LS.rawset(info, "loadresult", "cannot read source file");
} else { } else {
std::string chunk = "=" + cfn; std::string chunk = "=" + cfn;
luaL_loadbuffer(L, ccode.c_str(), ccode.size(), chunk.c_str()); luaL_loadbuffer(L, ccode.c_str(), ccode.size(), chunk.c_str());
lua_setfield(L, info.index(), "loadresult"); lua_replace(L, loadresult.index());
LS.rawset(info, "loadresult", loadresult);
} }
} }
return LS.result(); return LS.result();
@@ -146,7 +147,7 @@ void SourceDB::update() {
LuaStack LS(L, newdb, sourcedb, info, fn, seq); LuaStack LS(L, newdb, sourcedb, info, fn, seq);
// Get the (old) source database. // Get the (old) source database.
LS.getfield(sourcedb, LuaRegistry, "sourcedb"); LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (!LS.istable(sourcedb)) { if (!LS.istable(sourcedb)) {
LS.newtable(sourcedb); LS.newtable(sourcedb);
} }
@@ -167,12 +168,12 @@ void SourceDB::update() {
// Insert the sequence number and put finalized info into the new database. // Insert the sequence number and put finalized info into the new database.
LS.set(seq, i + 1); LS.set(seq, i + 1);
LS.setfield(info, "sequence", seq); LS.rawset(info, "sequence", seq);
LS.rawset(newdb, fn, info); LS.rawset(newdb, fn, info);
} }
// Store the new source db. // Store the new source db.
LS.setfield(LuaRegistry, "sourcedb", newdb); LS.rawset(LuaRegistry, "sourcedb", newdb);
LS.result(); LS.result();
} }
@@ -183,7 +184,7 @@ static void source_clear_globals(lua_State *L) {
LuaVar classname, classtab, key; LuaVar classname, classtab, key;
LuaStack LS(L, classname, classtab, key); LuaStack LS(L, classname, classtab, key);
LS.setfield(LuaGlobals, "_G", LuaNil); LS.rawset(LuaGlobals, "_G", LuaNil);
LS.set(classname, LuaNil); LS.set(classname, LuaNil);
while (LS.next(LuaGlobals, classname, classtab) != 0) { while (LS.next(LuaGlobals, classname, classtab) != 0) {
if (LS.istable(classtab)) { if (LS.istable(classtab)) {
@@ -192,7 +193,7 @@ static void source_clear_globals(lua_State *L) {
LS.rawset(LuaGlobals, classname, LuaNil); LS.rawset(LuaGlobals, classname, LuaNil);
} }
} }
LS.setfield(LuaGlobals, "_G", LuaGlobals); LS.rawset(LuaGlobals, "_G", LuaGlobals);
LS.result(); LS.result();
} }
@@ -202,8 +203,8 @@ static void source_restore_builtins(lua_State *L) {
LuaVar snapshot, key, value, skey, svalue, target; LuaVar snapshot, key, value, skey, svalue, target;
LuaStack LS(L, snapshot, key, value, skey, svalue, target); LuaStack LS(L, snapshot, key, value, skey, svalue, target);
LS.getfield(snapshot, LuaRegistry, "source_snapshot_builtins"); LS.rawget(snapshot, LuaRegistry, "source_snapshot_builtins");
LS.setfield(LuaGlobals, "_G", LuaGlobals); LS.rawset(LuaGlobals, "_G", LuaGlobals);
LS.set(key, LuaNil); LS.set(key, LuaNil);
while (LS.next(snapshot, key, value) != 0) { while (LS.next(snapshot, key, value) != 0) {
LS.checktable(value); LS.checktable(value);
@@ -258,17 +259,17 @@ static void source_load_lfunctions(lua_State *L) {
LuaStack LS(L, sourcedb, errors, key, info, seq, closure, err); LuaStack LS(L, sourcedb, errors, key, info, seq, closure, err);
// Get the source database. // Get the source database.
LS.getfield(sourcedb, LuaRegistry, "sourcedb"); LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (LS.type(sourcedb) != LUA_TTABLE) { if (LS.type(sourcedb) != LUA_TTABLE) {
LS.newtable(sourcedb); LS.newtable(sourcedb);
LS.setfield(LuaRegistry, "sourcedb", sourcedb); LS.rawset(LuaRegistry, "sourcedb", sourcedb);
} }
// Sort the keys by sequence number. // Sort the keys by sequence number.
std::map<int, std::string> indices; std::map<int, std::string> indices;
LS.set(key, LuaNil); LS.set(key, LuaNil);
while (LS.next(sourcedb, key, info) != 0) { while (LS.next(sourcedb, key, info) != 0) {
LS.getfield(seq, info, "sequence"); LS.rawget(seq, info, "sequence");
indices[LS.ckinteger(seq)] = LS.ckstring(key); indices[LS.ckinteger(seq)] = LS.ckstring(key);
} }
@@ -276,7 +277,7 @@ static void source_load_lfunctions(lua_State *L) {
std::stringstream errss; std::stringstream errss;
for (const auto &p : indices) { for (const auto &p : indices) {
LS.rawget(info, sourcedb, p.second); LS.rawget(info, sourcedb, p.second);
LS.getfield(closure, info, "loadresult"); LS.rawget(closure, info, "loadresult");
// If there's already an error in the sourcedb, collect it. // If there's already an error in the sourcedb, collect it.
if (!LS.isfunction(closure)) { if (!LS.isfunction(closure)) {
@@ -314,7 +315,7 @@ void SourceDB::run_unittests() {
LuaVar unittests, name, func, err; LuaVar unittests, name, func, err;
LuaStack LS(L, unittests, name, func, err); LuaStack LS(L, unittests, name, func, err);
LS.getfield(unittests, LuaGlobals, "unittests"); LS.rawget(unittests, LuaGlobals, "unittests");
// Sort the unit test names. // Sort the unit test names.
std::set<std::string> names; std::set<std::string> names;

View File

@@ -143,8 +143,8 @@ LuaDefine(queue_create, "c") {
LuaStack LS(L, queue); LuaStack LS(L, queue);
LS.newtable(queue); LS.newtable(queue);
LS.setfield(queue, "head", 1000000); LS.rawset(queue, "head", 1000000);
LS.setfield(queue, "tail", 1000000); LS.rawset(queue, "tail", 1000000);
return LS.result(); return LS.result();
} }
@@ -153,9 +153,9 @@ LuaDefine(queue_push, "c") {
lua_Integer head; lua_Integer head;
LuaStack LS(L, queue, elt); LuaStack LS(L, queue, elt);
LS.getfield(head, queue, "head"); LS.rawget(head, queue, "head");
LS.rawset(queue, head, elt); LS.rawset(queue, head, elt);
LS.setfield(queue, "head", head+1); LS.rawset(queue, "head", head+1);
return LS.result(); return LS.result();
} }
@@ -165,14 +165,14 @@ LuaDefine(queue_pop, "c") {
lua_Integer head, tail; lua_Integer head, tail;
LuaStack LS(L, queue, elt); LuaStack LS(L, queue, elt);
LS.getfield(tail, queue, "tail"); LS.rawget(tail, queue, "tail");
LS.getfield(head, queue, "head"); LS.rawget(head, queue, "head");
if (head == tail) { if (head == tail) {
LS.set(elt, LuaNil); LS.set(elt, LuaNil);
} else { } else {
LS.rawget(elt, queue, tail); LS.rawget(elt, queue, tail);
LS.rawset(queue, tail, LuaNil); LS.rawset(queue, tail, LuaNil);
LS.setfield(queue, "tail", tail + 1); LS.rawset(queue, "tail", tail + 1);
} }
return LS.result(); return LS.result();
} }
@@ -183,8 +183,8 @@ LuaDefine(queue_size, "c") {
lua_Number head, tail; lua_Number head, tail;
LuaStack LS(L, queue, size); LuaStack LS(L, queue, size);
LS.getfield(head, queue, "head"); LS.rawget(head, queue, "head");
LS.getfield(tail, queue, "tail"); LS.rawget(tail, queue, "tail");
LS.set(size, head - tail); LS.set(size, head - tail);
return LS.result(); return LS.result();
} }
@@ -196,8 +196,8 @@ LuaDefine(queue_nth, "c") {
LuaStack LS(L, queue, n, elt); LuaStack LS(L, queue, n, elt);
nth = LS.ckinteger(n) - 1; nth = LS.ckinteger(n) - 1;
LS.getfield(head, queue, "head"); LS.rawget(head, queue, "head");
LS.getfield(tail, queue, "tail"); LS.rawget(tail, queue, "tail");
if ((nth < 0) || (nth + tail >= head)) { if ((nth < 0) || (nth + tail >= head)) {
luaL_error(L, "index out of range"); luaL_error(L, "index out of range");
} }

View File

@@ -27,10 +27,10 @@ World::World() {
// Put the world pointer into the lua registry. // Put the world pointer into the lua registry.
LS.newpointer(world, this, false); LS.newpointer(world, this, false);
LS.setfield(LuaRegistry, "world", world); LS.rawset(LuaRegistry, "world", world);
// Create the tangibles table in the registry. // Create the tangibles table in the registry.
LS.setfield(LuaRegistry, "tangibles", LuaNewTable); LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
// Initialize the SourceDB // Initialize the SourceDB
source_db_.initialize(state()); source_db_.initialize(state());
@@ -51,10 +51,10 @@ void Tangible::be_a_player() {
LuaStack LS(world_->state(), classtab, mt, place, tangibles); LuaStack LS(world_->state(), classtab, mt, place, tangibles);
LS.makeclass(classtab, "player"); LS.makeclass(classtab, "player");
LS.getfield(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, anim_queue_.get_id()); LS.rawget(place, tangibles, anim_queue_.get_id());
LS.getmetatable(mt, place); LS.getmetatable(mt, place);
LS.setfield(mt, "__index", classtab); LS.rawset(mt, "__index", classtab);
LS.result(); LS.result();
} }
} }
@@ -113,14 +113,14 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
LS.setmetatable(database, metatab); LS.setmetatable(database, metatab);
// Store the database into the tangibles table. // Store the database into the tangibles table.
LS.getfield(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawset(tangibles, id, database); LS.rawset(tangibles, id, database);
// Populate the database and metatable with initial stuff. // Populate the database and metatable with initial stuff.
LS.setfield(database, "inventory", LuaNewTable); LS.rawset(database, "inventory", LuaNewTable);
LS.setfield(database, "id", id); LS.rawset(database, "id", id);
LS.setfield(metatab, "id", id); LS.rawset(metatab, "id", id);
// LS.setfield(metatab, "__metatable", LuaNil); // LS.rawset(metatab, "__metatable", LuaNil);
LS.result(); LS.result();
if (!pushdb) lua_pop(L, 1); if (!pushdb) lua_pop(L, 1);
@@ -130,7 +130,7 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
World *World::fetch(lua_State *L) { World *World::fetch(lua_State *L) {
LuaVar world; LuaVar world;
LuaStack LS(L, world); LuaStack LS(L, world);
LS.getfield(world, LuaRegistry, "world"); LS.rawget(world, LuaRegistry, "world");
World *w = LS.ckuserdata<World>(world); World *w = LS.ckuserdata<World>(world);
LS.result(); LS.result();
return w; return w;
@@ -144,7 +144,7 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
LuaStack LS(L, actor, place, ugui, func, tangibles); LuaStack LS(L, actor, place, ugui, func, tangibles);
// Get the actor and place. // Get the actor and place.
LS.getfield(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(actor, tangibles, actor_id); LS.rawget(actor, tangibles, actor_id);
LS.rawget(place, tangibles, place_id); LS.rawget(place, tangibles, place_id);
if (!LS.istable(actor) || !LS.istable(place)) { if (!LS.istable(actor) || !LS.istable(place)) {
@@ -153,7 +153,8 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
} }
// Get the interface closure. // Get the interface closure.
LS.getfield(func, place, "interface"); // Use of 'gettable' instead of 'rawget' is deliberate.
LS.gettable(func, place, "interface");
if (!LS.isfunction(func)) { if (!LS.isfunction(func)) {
LS.result(); LS.result();
return; return;
@@ -188,7 +189,7 @@ LuaDefine(tangible_get, "c") {
LuaVar tangibles; LuaVar tangibles;
LuaStack LS(L, id, database, tangibles); LuaStack LS(L, id, database, tangibles);
LS.getfield(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(database, tangibles, id); LS.rawget(database, tangibles, id);
return LS.result(); return LS.result();
} }