Change how class tables are created and recreated for greater robustness
This commit is contained in:
@@ -75,7 +75,20 @@ static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
lua_State *LuaStack::newstate (lua_Alloc allocf) {
|
lua_State *LuaStack::newstate (lua_Alloc allocf) {
|
||||||
if (allocf == nullptr) allocf = l_alloc;
|
if (allocf == nullptr) allocf = l_alloc;
|
||||||
lua_State *L = lua_newstate(allocf, NULL);
|
lua_State *L = lua_newstate(allocf, NULL);
|
||||||
if (L) lua_atpanic(L, &panicf);
|
assert(L != nullptr);
|
||||||
|
lua_atpanic(L, &panicf);
|
||||||
|
|
||||||
|
// We want all states to have a classes table and
|
||||||
|
// a tangibles table so that LS.makeclass and LS.maketan
|
||||||
|
// work out-of-the-box.
|
||||||
|
|
||||||
|
lua_pushstring(L, "classes");
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||||
|
lua_pushstring(L, "tangibles");
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||||
|
|
||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,27 +269,35 @@ bool LuaStack::validclassname(LuaSlot slot) const {
|
|||||||
|
|
||||||
eng::string LuaStack::classname(LuaSlot tab) const {
|
eng::string LuaStack::classname(LuaSlot tab) const {
|
||||||
eng::string result;
|
eng::string result;
|
||||||
if (istable(tab)) {
|
if ((istable(tab)) && (gettabletype(tab) == LUA_TT_CLASS)) {
|
||||||
lua_pushstring(L_, "__class");
|
LuaVar classes, name, dup;
|
||||||
lua_rawget(L_, tab);
|
LuaStack LS(L_, classes, name, dup);
|
||||||
if (lua_type(L_, -1) == LUA_TSTRING) {
|
// Get the classes table from the registry.
|
||||||
lua_pushglobaltable(L_); // cname table
|
LS.rawget(classes, LuaRegistry, "classes");
|
||||||
lua_pushvalue(L_, -2); // cname table cname
|
|
||||||
lua_rawget(L_, -2); // cname table ctab
|
// Try the efficient approach: get the class name from
|
||||||
if (lua_rawequal(L_, -1, tab)) {
|
// the class, and confirm it by checking the classes table.
|
||||||
size_t len;
|
LS.rawget(name, tab, "__class");
|
||||||
const char *s = lua_tolstring(L_, -3, &len);
|
if (LS.isstring(name)) {
|
||||||
result = eng::string(s, len);
|
LS.rawget(dup, classes, name);
|
||||||
if (!validclassname(result)) {
|
if (LS.rawequal(dup, tab)) {
|
||||||
result = "";
|
result = LS.ckstring(name);
|
||||||
}
|
LS.result();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do it the brute force way: scan the classes table.
|
||||||
|
LS.set(name, LuaNil);
|
||||||
|
while (LS.next(classes, name, dup)) {
|
||||||
|
if (LS.rawequal(dup, tab)) {
|
||||||
|
result = LS.ckstring(name);
|
||||||
|
LS.result();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
lua_pop(L_, 3);
|
|
||||||
} else {
|
|
||||||
lua_pop(L_, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
eng::string LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
|
eng::string LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
|
||||||
@@ -342,24 +363,31 @@ eng::string LuaStack::getclass(LuaSlot tab, std::string_view name) const {
|
|||||||
|
|
||||||
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||||
lua_checkstack(L_, 20);
|
lua_checkstack(L_, 20);
|
||||||
LuaVar globtab, cname;
|
LuaVar classes, globtab, cname;
|
||||||
LuaStack LS(L_, globtab, cname);
|
LuaStack LS(L_, classes, globtab, cname);
|
||||||
|
|
||||||
// Validate the class name.
|
// Validate the class name.
|
||||||
assert(LS.validclassname(classname));
|
assert(LS.validclassname(classname));
|
||||||
|
|
||||||
|
// Fetch the classes table from the registry.
|
||||||
|
LS.rawget(classes, LuaRegistry, "classes");
|
||||||
|
assert(LS.istable(classes));
|
||||||
|
|
||||||
// Fetch the global environment from the registry.
|
// Fetch the global environment from the registry.
|
||||||
LS.getglobaltable(globtab);
|
LS.getglobaltable(globtab);
|
||||||
|
|
||||||
// Get the classtab from the global environment.
|
// Get the classtab from the classes table.
|
||||||
LS.rawget(classtab, globtab, classname);
|
LS.rawget(classtab, classes, classname);
|
||||||
|
|
||||||
// Make a new table if necessary.
|
// Make a new table if necessary.
|
||||||
if (!LS.istable(classtab)) {
|
if (!LS.istable(classtab)) {
|
||||||
LS.newtable(classtab);
|
LS.newtable(classtab);
|
||||||
LS.rawset(globtab, classname, classtab);
|
LS.rawset(classes, classname, classtab);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put the table into the global environment.
|
||||||
|
LS.rawset(globtab, classname, classtab);
|
||||||
|
|
||||||
// Repair the special fields.
|
// Repair the special fields.
|
||||||
LS.settabletype(classtab, LUA_TT_CLASS);
|
LS.settabletype(classtab, LUA_TT_CLASS);
|
||||||
LS.rawset(classtab, "__class", classname);
|
LS.rawset(classtab, "__class", classname);
|
||||||
|
|||||||
@@ -458,9 +458,11 @@ public:
|
|||||||
void makeclass(LuaSlot tab, std::string_view name) const;
|
void makeclass(LuaSlot tab, std::string_view name) const;
|
||||||
|
|
||||||
// Create a tangible, or look up an existing tangible.
|
// Create a tangible, or look up an existing tangible.
|
||||||
// This doesn't do the entire process of tangible creation. It
|
// If the tangible doesn't exist yet, this creates a tangible stub.
|
||||||
// just creates an empty table, marks it as a tangible, creates
|
// It is possible to use World::tangible_make to transform a tangible
|
||||||
// the metatable, stores the tangible ID, and stores it in the tangible table.
|
// stub into a full blown tangible, and World::tangible_delete to turn
|
||||||
|
// a full-blown tangible back into a stub. A stub doesn't have a
|
||||||
|
// class or a thread table.
|
||||||
void maketan(LuaSlot tab, int64_t id) const;
|
void maketan(LuaSlot tab, int64_t id) const;
|
||||||
|
|
||||||
// Return true if a tangible is empty (deleted or not yet created).
|
// Return true if a tangible is empty (deleted or not yet created).
|
||||||
|
|||||||
@@ -245,26 +245,24 @@ void SourceDB::update(const util::LuaSourceVec &source) {
|
|||||||
LS.result();
|
LS.result();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete everything from the global environment except
|
// Delete everything from the global environment.
|
||||||
// the class tables.
|
// Clear all the classes in the registry classes table.
|
||||||
//
|
//
|
||||||
static void source_clear_globals(lua_State *L) {
|
static void source_clear_globals(lua_State *L) {
|
||||||
LuaVar classname, classtab, key, globtab;
|
LuaVar classname, classtab, key, globtab, classes;
|
||||||
LuaStack LS(L, classname, classtab, key, globtab);
|
LuaStack LS(L, classname, classtab, key, globtab, classes);
|
||||||
|
|
||||||
LS.getglobaltable(globtab);
|
LS.getglobaltable(globtab);
|
||||||
LS.rawset(globtab, "_G", LuaNil);
|
LS.cleartable(globtab, true);
|
||||||
LS.set(classname, LuaNil);
|
|
||||||
while (LS.next(globtab, classname, classtab) != 0) {
|
|
||||||
if (LS.rawequal(globtab, classtab)) {
|
|
||||||
LS.rawset(globtab, classname, LuaNil);
|
|
||||||
} else if (LS.istable(classtab)) {
|
|
||||||
LS.cleartable(classtab, true);
|
|
||||||
} else {
|
|
||||||
LS.rawset(globtab, classname, LuaNil);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LS.rawset(globtab, "_G", globtab);
|
LS.rawset(globtab, "_G", globtab);
|
||||||
|
|
||||||
|
LS.rawget(classes, LuaRegistry, "classes");
|
||||||
|
assert(LS.istable(classes));
|
||||||
|
LS.set(classname, LuaNil);
|
||||||
|
while (LS.next(classes, classname, classtab) != 0) {
|
||||||
|
assert(LS.istable(classtab));
|
||||||
|
LS.cleartable(classtab, true);
|
||||||
|
}
|
||||||
LS.result();
|
LS.result();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,9 +60,6 @@ World::World(WorldType wt) {
|
|||||||
LS.getglobaltable(globtab);
|
LS.getglobaltable(globtab);
|
||||||
LS.settabletype(globtab, LUA_TT_GLOBALENV);
|
LS.settabletype(globtab, LUA_TT_GLOBALENV);
|
||||||
|
|
||||||
// Create the tangibles table in the registry.
|
|
||||||
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
|
|
||||||
|
|
||||||
// Store the world type in the registry.
|
// Store the world type in the registry.
|
||||||
LS.rawset(LuaRegistry, "worldtype", wt);
|
LS.rawset(LuaRegistry, "worldtype", wt);
|
||||||
|
|
||||||
@@ -592,7 +589,7 @@ void World::invoke_flush_prints(int64_t actor_id, int64_t place_id, const eng::s
|
|||||||
void World::invoke_lua(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
void World::invoke_lua(int64_t actor_id, int64_t place_id, const eng::string &action, const InvocationData &data) {
|
||||||
assert(stack_is_clear());
|
assert(stack_is_clear());
|
||||||
|
|
||||||
// Make sure that actor and place exist.
|
// Make sure that actor and place exist and are not stubs.
|
||||||
Tangible *tactor = tangible_get(actor_id);
|
Tangible *tactor = tangible_get(actor_id);
|
||||||
Tangible *tplace = tangible_get(place_id);
|
Tangible *tplace = tangible_get(place_id);
|
||||||
if ((tactor == nullptr) || (tplace == nullptr)) {
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user