41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#include "luastack.hpp"
|
|
#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.
|
|
//
|
|
LuaDefine(globaldb_global, "f") {
|
|
LuaArg globalname;
|
|
LuaRet globaltab;
|
|
LuaVar globaldb;
|
|
LuaStack LS(L, globalname, globaltab, globaldb);
|
|
|
|
LS.checkstring(globalname);
|
|
|
|
// Get a pointer to the globaldb.
|
|
LS.getfield(globaldb, LuaRegistry, "globaldb");
|
|
if (!LS.istable(globaldb)) {
|
|
LS.newtable(globaldb);
|
|
LS.setfield(LuaRegistry, "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.ckstring(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();
|
|
}
|