Get rid of getfield/setfield
This commit is contained in:
@@ -48,10 +48,10 @@ void LuaStack::register_all_userdata(lua_State *L) {
|
||||
LS.set(classname, name);
|
||||
if (mode.find('t') != std::string::npos) { // Register type
|
||||
LS.newtable(tab);
|
||||
LS.setfield(tab, "type", name);
|
||||
LS.setfield(tab, "__gc", collect_tagged_pointer);
|
||||
LS.rawset(tab, "type", name);
|
||||
LS.rawset(tab, "__gc", collect_tagged_pointer);
|
||||
LS.makeclass(classtab, classname);
|
||||
LS.setfield(tab, "__index", classtab);
|
||||
LS.rawset(tab, "__index", classtab);
|
||||
LS.setlightuserdata(lud, (void *)tag);
|
||||
LS.rawset(LuaRegistry, lud, tab);
|
||||
}
|
||||
@@ -216,8 +216,8 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||
}
|
||||
|
||||
// Repair the special fields.
|
||||
setfield(classtab, "__index", classtab);
|
||||
setfield(classtab, "__class", classname);
|
||||
rawset(classtab, "__index", classtab);
|
||||
rawset(classtab, "__class", classname);
|
||||
}
|
||||
|
||||
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
|
||||
@@ -254,7 +254,7 @@ LuaDefine(system_type, "f") {
|
||||
int type = LS.type(obj);
|
||||
if (type == LUA_TUSERDATA) {
|
||||
LS.getmetatable(mt, obj);
|
||||
LS.getfield(tname, mt, "type");
|
||||
LS.rawget(tname, mt, "type");
|
||||
} else {
|
||||
LS.set(tname, lua_typename(L, type));
|
||||
}
|
||||
|
||||
@@ -504,17 +504,17 @@ public:
|
||||
lua_rawset(L_, tab);
|
||||
}
|
||||
|
||||
template<typename VT>
|
||||
void setfield(LuaSlot tab, const char *field, VT value) const {
|
||||
push_any_value(value);
|
||||
lua_setfield(L_, tab, field);
|
||||
}
|
||||
// template<typename VT>
|
||||
// void rawset(LuaSlot tab, const char *field, VT value) const {
|
||||
// push_any_value(value);
|
||||
// lua_rawset(L_, tab, field);
|
||||
// }
|
||||
|
||||
template<typename RT>
|
||||
void getfield(RT &target, LuaSlot tab, const char *field) const {
|
||||
lua_getfield(L_, tab, field);
|
||||
pop_any_value(target);
|
||||
}
|
||||
// template<typename RT>
|
||||
// void rawget(RT &target, LuaSlot tab, const char *field) const {
|
||||
// lua_rawget(L_, tab, field);
|
||||
// pop_any_value(target);
|
||||
// }
|
||||
|
||||
// Call invokes any C function. It pushes the arguments on the stack,
|
||||
// calls the cfunction, verifies that the number of return values is as
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
static int source_updatefile(lua_State *L) {
|
||||
LuaArg source, fn;
|
||||
LuaRet info;
|
||||
LuaVar fingerprint, null;
|
||||
LuaStack LS(L, source, fn, info, fingerprint, null);
|
||||
LuaVar fingerprint, null, loadresult;
|
||||
LuaStack LS(L, source, fn, info, fingerprint, null, loadresult);
|
||||
|
||||
// Get the existing info table from the source DB.
|
||||
if (LS.istable(source)) {
|
||||
@@ -115,7 +115,7 @@ static int source_updatefile(lua_State *L) {
|
||||
// these fields: code, fingerprint, closure, error
|
||||
// Otherwise, update nothing.
|
||||
std::string cfn = LS.ckstring(fn);
|
||||
LS.getfield(fingerprint, info, "fingerprint");
|
||||
LS.rawget(fingerprint, info, "fingerprint");
|
||||
std::string old_fingerprint;
|
||||
if (LS.isstring(fingerprint)) {
|
||||
old_fingerprint = LS.ckstring(fingerprint);
|
||||
@@ -126,15 +126,16 @@ static int source_updatefile(lua_State *L) {
|
||||
if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) {
|
||||
std::cerr << "Rereading " << cfn << std::endl;
|
||||
std::string ccode = util::get_file_contents("lua/" + cfn);
|
||||
LS.setfield(info, "name", fn);
|
||||
LS.setfield(info, "fingerprint", new_fingerprint);
|
||||
LS.setfield(info, "code", ccode);
|
||||
LS.rawset(info, "name", fn);
|
||||
LS.rawset(info, "fingerprint", new_fingerprint);
|
||||
LS.rawset(info, "code", ccode);
|
||||
if ((new_fingerprint == "")||(ccode == "")) {
|
||||
LS.setfield(info, "loadresult", "cannot read source file");
|
||||
LS.rawset(info, "loadresult", "cannot read source file");
|
||||
} else {
|
||||
std::string chunk = "=" + cfn;
|
||||
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();
|
||||
@@ -146,7 +147,7 @@ void SourceDB::update() {
|
||||
LuaStack LS(L, newdb, sourcedb, info, fn, seq);
|
||||
|
||||
// Get the (old) source database.
|
||||
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
||||
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (!LS.istable(sourcedb)) {
|
||||
LS.newtable(sourcedb);
|
||||
}
|
||||
@@ -167,12 +168,12 @@ void SourceDB::update() {
|
||||
|
||||
// Insert the sequence number and put finalized info into the new database.
|
||||
LS.set(seq, i + 1);
|
||||
LS.setfield(info, "sequence", seq);
|
||||
LS.rawset(info, "sequence", seq);
|
||||
LS.rawset(newdb, fn, info);
|
||||
}
|
||||
|
||||
// Store the new source db.
|
||||
LS.setfield(LuaRegistry, "sourcedb", newdb);
|
||||
LS.rawset(LuaRegistry, "sourcedb", newdb);
|
||||
LS.result();
|
||||
}
|
||||
|
||||
@@ -183,7 +184,7 @@ static void source_clear_globals(lua_State *L) {
|
||||
LuaVar classname, classtab, key;
|
||||
LuaStack LS(L, classname, classtab, key);
|
||||
|
||||
LS.setfield(LuaGlobals, "_G", LuaNil);
|
||||
LS.rawset(LuaGlobals, "_G", LuaNil);
|
||||
LS.set(classname, LuaNil);
|
||||
while (LS.next(LuaGlobals, classname, classtab) != 0) {
|
||||
if (LS.istable(classtab)) {
|
||||
@@ -192,7 +193,7 @@ static void source_clear_globals(lua_State *L) {
|
||||
LS.rawset(LuaGlobals, classname, LuaNil);
|
||||
}
|
||||
}
|
||||
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
||||
LS.rawset(LuaGlobals, "_G", LuaGlobals);
|
||||
LS.result();
|
||||
}
|
||||
|
||||
@@ -202,8 +203,8 @@ static void source_restore_builtins(lua_State *L) {
|
||||
LuaVar snapshot, key, value, skey, svalue, target;
|
||||
LuaStack LS(L, snapshot, key, value, skey, svalue, target);
|
||||
|
||||
LS.getfield(snapshot, LuaRegistry, "source_snapshot_builtins");
|
||||
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
||||
LS.rawget(snapshot, LuaRegistry, "source_snapshot_builtins");
|
||||
LS.rawset(LuaGlobals, "_G", LuaGlobals);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(snapshot, key, value) != 0) {
|
||||
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);
|
||||
|
||||
// Get the source database.
|
||||
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
||||
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (LS.type(sourcedb) != LUA_TTABLE) {
|
||||
LS.newtable(sourcedb);
|
||||
LS.setfield(LuaRegistry, "sourcedb", sourcedb);
|
||||
LS.rawset(LuaRegistry, "sourcedb", sourcedb);
|
||||
}
|
||||
|
||||
// Sort the keys by sequence number.
|
||||
std::map<int, std::string> indices;
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(sourcedb, key, info) != 0) {
|
||||
LS.getfield(seq, info, "sequence");
|
||||
LS.rawget(seq, info, "sequence");
|
||||
indices[LS.ckinteger(seq)] = LS.ckstring(key);
|
||||
}
|
||||
|
||||
@@ -276,7 +277,7 @@ static void source_load_lfunctions(lua_State *L) {
|
||||
std::stringstream errss;
|
||||
for (const auto &p : indices) {
|
||||
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 (!LS.isfunction(closure)) {
|
||||
@@ -314,7 +315,7 @@ void SourceDB::run_unittests() {
|
||||
LuaVar 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.
|
||||
std::set<std::string> names;
|
||||
|
||||
@@ -143,8 +143,8 @@ LuaDefine(queue_create, "c") {
|
||||
LuaStack LS(L, queue);
|
||||
|
||||
LS.newtable(queue);
|
||||
LS.setfield(queue, "head", 1000000);
|
||||
LS.setfield(queue, "tail", 1000000);
|
||||
LS.rawset(queue, "head", 1000000);
|
||||
LS.rawset(queue, "tail", 1000000);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
@@ -153,9 +153,9 @@ LuaDefine(queue_push, "c") {
|
||||
lua_Integer head;
|
||||
LuaStack LS(L, queue, elt);
|
||||
|
||||
LS.getfield(head, queue, "head");
|
||||
LS.rawget(head, queue, "head");
|
||||
LS.rawset(queue, head, elt);
|
||||
LS.setfield(queue, "head", head+1);
|
||||
LS.rawset(queue, "head", head+1);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
@@ -165,14 +165,14 @@ LuaDefine(queue_pop, "c") {
|
||||
lua_Integer head, tail;
|
||||
LuaStack LS(L, queue, elt);
|
||||
|
||||
LS.getfield(tail, queue, "tail");
|
||||
LS.getfield(head, queue, "head");
|
||||
LS.rawget(tail, queue, "tail");
|
||||
LS.rawget(head, queue, "head");
|
||||
if (head == tail) {
|
||||
LS.set(elt, LuaNil);
|
||||
} else {
|
||||
LS.rawget(elt, queue, tail);
|
||||
LS.rawset(queue, tail, LuaNil);
|
||||
LS.setfield(queue, "tail", tail + 1);
|
||||
LS.rawset(queue, "tail", tail + 1);
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
@@ -183,8 +183,8 @@ LuaDefine(queue_size, "c") {
|
||||
lua_Number head, tail;
|
||||
LuaStack LS(L, queue, size);
|
||||
|
||||
LS.getfield(head, queue, "head");
|
||||
LS.getfield(tail, queue, "tail");
|
||||
LS.rawget(head, queue, "head");
|
||||
LS.rawget(tail, queue, "tail");
|
||||
LS.set(size, head - tail);
|
||||
return LS.result();
|
||||
}
|
||||
@@ -196,8 +196,8 @@ LuaDefine(queue_nth, "c") {
|
||||
LuaStack LS(L, queue, n, elt);
|
||||
|
||||
nth = LS.ckinteger(n) - 1;
|
||||
LS.getfield(head, queue, "head");
|
||||
LS.getfield(tail, queue, "tail");
|
||||
LS.rawget(head, queue, "head");
|
||||
LS.rawget(tail, queue, "tail");
|
||||
if ((nth < 0) || (nth + tail >= head)) {
|
||||
luaL_error(L, "index out of range");
|
||||
}
|
||||
|
||||
@@ -27,10 +27,10 @@ World::World() {
|
||||
|
||||
// Put the world pointer into the lua registry.
|
||||
LS.newpointer(world, this, false);
|
||||
LS.setfield(LuaRegistry, "world", world);
|
||||
LS.rawset(LuaRegistry, "world", world);
|
||||
|
||||
// Create the tangibles table in the registry.
|
||||
LS.setfield(LuaRegistry, "tangibles", LuaNewTable);
|
||||
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
|
||||
|
||||
// Initialize the SourceDB
|
||||
source_db_.initialize(state());
|
||||
@@ -51,10 +51,10 @@ void Tangible::be_a_player() {
|
||||
LuaStack LS(world_->state(), classtab, mt, place, tangibles);
|
||||
|
||||
LS.makeclass(classtab, "player");
|
||||
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(place, tangibles, anim_queue_.get_id());
|
||||
LS.getmetatable(mt, place);
|
||||
LS.setfield(mt, "__index", classtab);
|
||||
LS.rawset(mt, "__index", classtab);
|
||||
LS.result();
|
||||
}
|
||||
}
|
||||
@@ -113,14 +113,14 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
|
||||
LS.setmetatable(database, metatab);
|
||||
|
||||
// Store the database into the tangibles table.
|
||||
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawset(tangibles, id, database);
|
||||
|
||||
// Populate the database and metatable with initial stuff.
|
||||
LS.setfield(database, "inventory", LuaNewTable);
|
||||
LS.setfield(database, "id", id);
|
||||
LS.setfield(metatab, "id", id);
|
||||
// LS.setfield(metatab, "__metatable", LuaNil);
|
||||
LS.rawset(database, "inventory", LuaNewTable);
|
||||
LS.rawset(database, "id", id);
|
||||
LS.rawset(metatab, "id", id);
|
||||
// LS.rawset(metatab, "__metatable", LuaNil);
|
||||
|
||||
LS.result();
|
||||
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) {
|
||||
LuaVar world;
|
||||
LuaStack LS(L, world);
|
||||
LS.getfield(world, LuaRegistry, "world");
|
||||
LS.rawget(world, LuaRegistry, "world");
|
||||
World *w = LS.ckuserdata<World>(world);
|
||||
LS.result();
|
||||
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);
|
||||
|
||||
// Get the actor and place.
|
||||
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(actor, tangibles, actor_id);
|
||||
LS.rawget(place, tangibles, place_id);
|
||||
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.
|
||||
LS.getfield(func, place, "interface");
|
||||
// Use of 'gettable' instead of 'rawget' is deliberate.
|
||||
LS.gettable(func, place, "interface");
|
||||
if (!LS.isfunction(func)) {
|
||||
LS.result();
|
||||
return;
|
||||
@@ -188,7 +189,7 @@ LuaDefine(tangible_get, "c") {
|
||||
LuaVar tangibles;
|
||||
LuaStack LS(L, id, database, tangibles);
|
||||
|
||||
LS.getfield(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
||||
LS.rawget(database, tangibles, id);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user