Added new tangible ops, including tangible.delete

This commit is contained in:
2021-03-19 13:41:41 -04:00
parent 05052f846e
commit e06bae63f7
5 changed files with 138 additions and 41 deletions

View File

@@ -153,19 +153,55 @@ lua_State *LuaStack::newthread(LuaSlot target) const {
return result;
}
void LuaStack::getclass(LuaSlot classtab, LuaSlot classname) const {
lua_checkstack(L_, 20);
LuaVar globtab, cname;
LuaStack LS(L_, globtab, cname);
// Convert class name to a table.
if (LS.isstring(classname)) {
if (LS.rawequal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
}
LS.getglobaltable(globtab);
LS.rawget(classtab, globtab, classname);
if (!LS.istable(classtab)) {
luaL_error(L_, "Not a class: %s", lua_tostring(L_, classname));
}
LS.rawget(cname, classtab, "__class");
if (!LS.rawequal(cname, classname)) {
luaL_error(L_, "Not a class: %s", lua_tostring(L_, classname));
}
} else {
LS.set(classtab, classname);
if (!LS.istable(classtab)) {
luaL_error(L_, "parameter must be a class");
}
LS.rawget(cname, classtab, "__class");
if (!LS.isstring(cname)) {
luaL_error(L_, "table is not a class");
}
}
// OK, we're done.
LS.result();
}
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
lua_checkstack(L_, 20);
LuaVar globtab;
LuaStack LS(L_, globtab);
// Validate the class name.
LS.checkstring(classname);
if (!LS.isstring(classname)) {
luaL_error(L_, "Not a class name: %s", lua_tostring(L_, classname));
}
if (LS.rawequal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
}
// Fetch the global environment from the registry.
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
LS.getglobaltable(globtab);
// Get the classtab from the global environment.
// Create it if it doesn't exist.
@@ -201,6 +237,17 @@ void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
}
}
void LuaStack::cleartable(LuaSlot tab) const {
checktable(tab);
lua_pushnil(L_);
while (lua_next(L_, tab.index()) != 0) {
lua_pop(L_, 1); // Pop the old value.
lua_pushvalue(L_, -1); // Clone the key
lua_pushnil(L_); // Push the new value.
lua_settable(L_, tab.index());
}
}
void LuaStack::check_nret(int xnret, int otop, int nret) const {
int ntop = lua_gettop(L_);
if ((nret != xnret)||(ntop != otop + xnret)) {