Change how class tables are created and recreated for greater robustness

This commit is contained in:
2023-03-17 13:31:10 -04:00
parent a8c780563f
commit 5ede3f507c
4 changed files with 71 additions and 46 deletions

View File

@@ -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) {
if (allocf == nullptr) allocf = l_alloc;
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;
}
@@ -256,27 +269,35 @@ bool LuaStack::validclassname(LuaSlot slot) const {
eng::string LuaStack::classname(LuaSlot tab) const {
eng::string result;
if (istable(tab)) {
lua_pushstring(L_, "__class");
lua_rawget(L_, tab);
if (lua_type(L_, -1) == LUA_TSTRING) {
lua_pushglobaltable(L_); // cname table
lua_pushvalue(L_, -2); // cname table cname
lua_rawget(L_, -2); // cname table ctab
if (lua_rawequal(L_, -1, tab)) {
size_t len;
const char *s = lua_tolstring(L_, -3, &len);
result = eng::string(s, len);
if (!validclassname(result)) {
result = "";
}
if ((istable(tab)) && (gettabletype(tab) == LUA_TT_CLASS)) {
LuaVar classes, name, dup;
LuaStack LS(L_, classes, name, dup);
// Get the classes table from the registry.
LS.rawget(classes, LuaRegistry, "classes");
// Try the efficient approach: get the class name from
// the class, and confirm it by checking the classes table.
LS.rawget(name, tab, "__class");
if (LS.isstring(name)) {
LS.rawget(dup, classes, name);
if (LS.rawequal(dup, tab)) {
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 {
@@ -342,24 +363,31 @@ eng::string LuaStack::getclass(LuaSlot tab, std::string_view name) const {
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
lua_checkstack(L_, 20);
LuaVar globtab, cname;
LuaStack LS(L_, globtab, cname);
LuaVar classes, globtab, cname;
LuaStack LS(L_, classes, globtab, cname);
// Validate the class name.
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.
LS.getglobaltable(globtab);
// Get the classtab from the global environment.
LS.rawget(classtab, globtab, classname);
// Get the classtab from the classes table.
LS.rawget(classtab, classes, classname);
// Make a new table if necessary.
if (!LS.istable(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.
LS.settabletype(classtab, LUA_TT_CLASS);
LS.rawset(classtab, "__class", classname);