Stub out the new global.set and global.get operators

This commit is contained in:
2023-03-01 17:35:06 -05:00
parent 9ce34d950b
commit db234c2934
6 changed files with 115 additions and 88 deletions

View File

@@ -1,72 +1,74 @@
#include "luastack.hpp"
#include "globaldb.hpp"
LuaDefine(global_once, "name", "for a given string, returns true exactly once") {
LuaDefine(global_set, "varname, value",
"|Store data in the global data table."
"|"
"|You can store global data using global.set, then you can"
"|retrieve it using global.get. You can also retrieve data using"
"|gv.varname, which is just shorthand for global.get. You may not"
"|store data using gv.varname=value, this yields the error 'Use "
"|global.set to store data in the global data table.'"
"|"
"|Values stored using global.set are difference transmitted to all"
"|connected clients immediately. When a new client connects,"
"|he will receive all the global data."
"|"
"|The global data table is not the same thing as the lua "
"|environment table. Trying to store data in the lua environment"
"|table will seem to work, at first, but the data will not get"
"|difference transmitted, and will eventually be cleared and lost."
"|Therefore, it is essential that global data be stored in the"
"|global data table (using global.set) instead of in the lua"
"|environment table."
"|"
"|There are certain restrictions on the values that you store."
"|The value can contain strings, numbers, simple tables, and"
"|tangibles. Nothing else is allowed. Simple tables are tables"
"|that don't have metatables, and that aren't special tables such"
"|as classes, the lua environment table, the registry, or the like."
"|"
"|When you store the value, a recursive copy is made and stored."
"|When you call global.get, you obtain the copy. Any attempt to"
"|mutate the copy will fail with this lua error message: 'Tables"
"|returned by global.get are immutable.' This rule prevents'"
"|aliasing between global data and other data structures."
"|") {
return 0;
}
LuaDefine(global_get, "varname",
"|Get data stored using global.set"
"|"
"|See doc(global.set) for information on how to store global data."
"|") {
return 0;
}
LuaDefine(global_once, "name",
"|For a given string, returns true exactly once"
"|"
"|The semantics and difference transmission behavior of global.once"
"|are identical to the semantics of global.set, since global.once"
"|uses global.set under the covers."
"|") {
LuaArg name;
LuaRet flag;
LuaVar oncedb, val;
LuaStack LS(L, name, flag, oncedb, val);
LS.guard_nopredict("global.once");
// 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();
return 0;
}
LuaDefine(global_clearonce, "name", "reset the specified once-flag") {
LuaDefine(global_clearonce, "name",
"|Reset the specified once-flag"
"|"
"|The semantics and difference transmission behavior of global.clearonce"
"|are identical to the semantics of global.set, since global.once"
"|uses global.set under the covers."
"|") {
LuaArg name;
LuaVar oncedb;
LuaStack LS(L, name, oncedb);
LS.guard_nopredict("global.clearonce");
// 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") {
LuaArg globalname;
LuaRet globaltab;
LuaVar globaldb;
LuaStack LS(L, globalname, globaltab, globaldb);
LS.checkstring(globalname, "globalname");
// Get a pointer to the globaldb.
LS.rawget(globaldb, LuaRegistry, "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.rawset(globaltab, "__global", globalname);
LS.settabletype(globaltab, LUA_TT_GLOBALDB);
return LS.result();
return 0;
}