#include "luastack.hpp" #include "globaldb.hpp" LuaDefine(globaldb_enable, "c") { LuaVar globaldb; LuaStack LS(L, globaldb); LS.getfield(globaldb, LuaRegistry, "globaldb"); if (!LS.istable(globaldb)) { LS.newtable(globaldb); LS.setfield(LuaRegistry, "globaldb", globaldb); } return LS.result(); } // 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); // Get a pointer to the globaldb. LS.getfield(globaldb, LuaRegistry, "globaldb"); if (!LS.istable(globaldb)) { luaL_error(L, "globaldb is not enabled"); } LS.checkstring(globalname); // 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(); }