Makeclass cleaned up

This commit is contained in:
2021-02-10 16:22:24 -05:00
parent e838b4ac97
commit 3883f86dee
13 changed files with 120 additions and 54 deletions

View File

@@ -189,33 +189,17 @@ int LuaStack::next(LuaSlot tab, LuaSlot key, LuaSlot value) const {
return ret;
}
bool LuaStack::isemptytable(LuaSlot tab) const {
if (lua_istable(L_, tab)) {
lua_pushnil(L_);
if (lua_next(L_, tab) == 0) {
return true;
}
lua_pop(L_, 2);
}
return false;
}
void LuaStack::newtable(LuaSlot target) const {
lua_newtable(L_);
lua_replace(L_, target);
}
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
int top = lua_gettop(L_);
checkstring(classname);
// Special case: if the classname is _G, return global env.
lua_pushstring(L_, "_G");
int eqlg = lua_equal(L_, -1, classname.index());
lua_settop(L_, top);
if (eqlg) {
set(classtab, LuaGlobals);
return;
// Special case: if the classname is _G, detect and error.
if (equal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
}
// Get the classtab from the global environment.
@@ -236,6 +220,19 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
setfield(classtab, "__class", classname);
}
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
rawget(sub, tab, name);
if (istable(sub)) {
return;
} else if (isnil(sub)) {
newtable(sub);
rawset(tab, name, sub);
return;
} else {
luaL_error(L_, "%s is not a table", name);
}
}
void LuaStack::setlightuserdata(LuaSlot target, void *p) const {
lua_pushlightuserdata(L_, p);
lua_replace(L_, target);