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

73 lines
2.0 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"
LuaDefine(global_once, "name", "for a given string, returns true exactly once") {
LuaArg name;
LuaRet flag;
LuaVar oncedb, val;
LuaStack LS(L, name, flag, oncedb, val);
2021-01-12 14:14:38 -05:00
// Get a pointer to the oncedb.
LS.rawget(oncedb, LuaRegistry, "oncedb");
if (!LS.istable(oncedb)) {
LS.set(flag, false);
return LS.result();
}
LS.checkstring(name, "name");
LS.rawget(val, oncedb, name);
if (!LS.isnil(val)) {
LS.set(flag, false);
return LS.result();
}
LS.rawset(oncedb, name, true);
LS.set(flag, true);
return LS.result();
}
LuaDefine(global_clearonce, "name", "reset the specified once-flag") {
LuaArg name;
LuaVar oncedb;
LuaStack LS(L, name, oncedb);
// Get a pointer to the oncedb.
LS.rawget(oncedb, LuaRegistry, "oncedb");
if (!LS.istable(oncedb)) {
return LS.result();
}
LS.checkstring(name, "name");
LS.rawset(oncedb, name, LuaNil);
return LS.result();
}
LuaDefine(global_table, "globalname", "get a table where global data can be stored") {
2020-11-27 13:21:07 -05:00
LuaArg globalname;
LuaRet globaltab;
LuaVar globaldb;
LuaStack LS(L, globalname, globaltab, globaldb);
LS.checkstring(globalname, "globalname");
2020-11-27 13:21:07 -05:00
// Get a pointer to the globaldb.
2021-02-10 16:22:24 -05:00
LS.rawget(globaldb, LuaRegistry, "globaldb");
2020-11-27 13:21:07 -05:00
// nopredict
if (lua_isyieldable(L) && (!LS.istable(globaldb))) {
return lua_yield(L, 0);
}
2021-01-12 14:14:38 -05:00
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);
2021-02-10 16:22:24 -05:00
LS.rawset(globaltab, "__global", globalname);
2021-08-23 23:34:30 -04:00
LS.settabletype(globaltab, LUA_TT_GLOBALDB);
2020-11-27 13:21:07 -05:00
return LS.result();
}