Lots of work on LuaStack

This commit is contained in:
2020-11-27 13:21:07 -05:00
parent f690fb147b
commit 455ba5df87
19 changed files with 715 additions and 527 deletions

View File

@@ -0,0 +1,43 @@
#include "globaldb.hpp"
#include "table.hpp"
// Get a table from the global database.
//
// GLOBALNAME
// if globalname is already present, and is a table, return it.
// if globalname is already present, and not a table, error.
// if globalname is not present, create and initialize it.
//
int lpx_globaldb_global(lua_State *L) {
LuaArg globalname;
LuaRet globaltab;
LuaVar globaldb;
LuaStack LS(L, globalname, globaltab, globaldb);
LS.checktype(globalname, LUA_TSTRING);
// Get a pointer to the globaldb.
LS.getfield(globaldb, LUA_REGISTRYINDEX, "globaldb");
if (!LS.istable(globaldb)) {
LS.newtable(globaldb);
LS.setfield(LUA_REGISTRYINDEX, "globaldb", globaldb);
}
// Get the globaltab from the globaldb, sanity check it.
LS.rawget(globaltab, globaldb, globalname);
if (LS.istable(globaltab)) {
return LS.result();
} else if (!LS.isnil(globaltab)) {
luaL_error(L, "%s is not a global", LS.tostring(globalname).c_str());
}
// Create a new globaltab and store it in the globaldb.
LS.newtable(globaltab);
LS.rawset(globaldb, globalname, globaltab);
LS.setfield(globaltab, "__global", globalname);
return LS.result();
}
void luaopen_lpx_globaldb(lua_State *L) {
LuaStack::reg(L, 0, "global", LuaArgCheck<1, lpx_globaldb_global>);
}