Files
integration/luprex/core/cpp/globaldb.cpp

50 lines
1.4 KiB
C++
Raw Normal View History

2020-12-05 18:57:53 -05:00
#include "luastack.hpp"
2020-11-27 13:21:07 -05:00
#include "globaldb.hpp"
2021-01-12 14:14:38 -05:00
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();
}
2020-11-27 13:21:07 -05:00
// 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.
//
2021-01-02 13:31:18 -05:00
LuaDefine(globaldb_global, "f") {
2020-11-27 13:21:07 -05:00
LuaArg globalname;
LuaRet globaltab;
LuaVar globaldb;
LuaStack LS(L, globalname, globaltab, globaldb);
2021-01-12 14:14:38 -05:00
// Get a pointer to the globaldb.
2020-12-05 18:57:53 -05:00
LS.getfield(globaldb, LuaRegistry, "globaldb");
2020-11-27 13:21:07 -05:00
if (!LS.istable(globaldb)) {
2021-01-12 14:14:38 -05:00
luaL_error(L, "globaldb is not enabled");
2020-11-27 13:21:07 -05:00
}
2021-01-12 14:14:38 -05:00
LS.checkstring(globalname);
2020-11-27 13:21:07 -05:00
// 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)) {
2021-01-02 13:31:18 -05:00
luaL_error(L, "%s is not a global", LS.ckstring(globalname).c_str());
2020-11-27 13:21:07 -05:00
}
// 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();
}