44 lines
1.3 KiB
C++
44 lines
1.3 KiB
C++
|
|
#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>);
|
||
|
|
}
|