Initial revision of animqueue

This commit is contained in:
2021-01-12 14:14:38 -05:00
parent 78f8610eb8
commit 25b9b4cb5d
18 changed files with 785 additions and 308 deletions

View File

@@ -14,6 +14,93 @@
#include "traceback.hpp"
LuaDefine(source_makeclass, "f") {
LuaArg classname;
LuaVar action, gname;
LuaRet classtab;
LuaStack LS(L, classname, classtab, action, gname);
LS.checkstring(classname);
// Special case: if the classname is _G, return global env.
LS.set(gname, "_G");
if (LS.equal(classname, gname)) {
LS.set(classtab, LuaGlobals);
return LS.result();
}
// Get the classtab from the global environment.
// Create it if it doesn't exist.
LS.rawget(classtab, LuaGlobals, classname);
if (LS.isnil(classtab)) {
LS.newtable(classtab);
LS.rawset(LuaGlobals, classname, classtab);
}
// If the name isn't bound to a table, abort.
if (!LS.istable(classtab)) {
luaL_error(L, "%s is not a class", LS.ckstring(classname).c_str());
}
// Repair the special fields.
LS.setfield(classtab, "__index", classtab);
LS.setfield(classtab, "__class", classname);
// Repair the action table.
LS.getfield(action, classtab, "action");
if (!LS.istable(action)) {
LS.setfield(classtab, "action", LuaNewTable);
}
return LS.result();
}
// Load the builtins.
static void load_builtin(lua_State *L, const char *name, lua_CFunction func) {
lua_pushcfunction(L, func);
lua_pushstring(L, name);
lua_call(L, 1, 0);
}
LuaDefine(source_install_builtins, "") {
LuaStack LS(L);
load_builtin(L, "base", luaopen_base);
load_builtin(L, "table", luaopen_table);
load_builtin(L, "string", luaopen_string);
load_builtin(L, "math", luaopen_math);
load_builtin(L, "bit", luaopen_math);
load_builtin(L, "debug", luaopen_debug);
// Do not load: package, io, os, debug, jit
return 0;
}
LuaDefine(source_install_and_snapshot_builtins, "") {
LuaVar key, value, skey, svalue, snapshot, ssnapshot;
LuaStack LS(L, snapshot, key, value, skey, svalue, ssnapshot);
// Note: the global environment contains _G. This routine
// perceives this as a subtable, so it backs up the top level
// as well.
source_install_builtins(L);
LS.newtable(snapshot);
LS.set(key, LuaNil);
while (LS.next(LuaGlobals, key, value) != 0) {
if (LS.istable(value)) {
LS.newtable(ssnapshot);
LS.rawset(snapshot, key, ssnapshot);
LS.set(skey, LuaNil);
while (LS.next(value, skey, svalue) != 0) {
if (LS.isfunction(svalue) || LS.isstring(svalue) || LS.isnumber(svalue)) {
LS.rawset(ssnapshot, skey, svalue);
}
}
}
}
LS.setfield(LuaRegistry, "source_snapshot_builtins", snapshot);
return LS.result();
}
LuaDefine(source_updatefile, "") {
LuaArg source, fn;
LuaRet info;
@@ -95,58 +182,8 @@ LuaDefine(source_update, "c") {
return LS.result();
}
// Make a class. A class is a table in the global environment.
//
// The global environment is protected, but this function can
// override that protection.
//
LuaDefine(source_makeclass, "f") {
LuaArg classname;
LuaVar action, gname;
LuaRet classtab;
LuaStack LS(L, classname, classtab, action, gname);
LS.checkstring(classname);
// Special case: if the classname is _G, return global env.
LS.set(gname, "_G");
if (LS.equal(classname, gname)) {
LS.set(classtab, LuaGlobals);
return LS.result();
}
// Get the classtab from the global environment.
// Create it if it doesn't exist.
LS.rawget(classtab, LuaGlobals, classname);
if (LS.isnil(classtab)) {
LS.newtable(classtab);
LS.rawset(LuaGlobals, classname, classtab);
}
// If the name isn't bound to a table, abort.
if (!LS.istable(classtab)) {
luaL_error(L, "%s is not a class", LS.ckstring(classname).c_str());
}
// Repair the special fields.
LS.setfield(classtab, "__index", classtab);
LS.setfield(classtab, "__class", classname);
// Repair the action table.
LS.getfield(action, classtab, "action");
if (!LS.istable(action)) {
LS.setfield(classtab, "action", LuaNewTable);
}
return LS.result();
}
// Clear the global environment.
//
// Clears out almost everything from the global
// environment. However, does not delete class tables.
//
// This is used during source_rebuild operations.
// Delete everything from the global environment except
// the class tables and the class action tables.
//
LuaDefine(source_clear_globals, "") {
LuaVar classname, classtab, action, key;
@@ -156,13 +193,16 @@ LuaDefine(source_clear_globals, "") {
LS.set(classname, LuaNil);
while (LS.next(LuaGlobals, classname, classtab) != 0) {
if (LS.istable(classtab)) {
bool keep_action = false;
LS.getfield(action, classtab, "action");
if (LS.istable(action)) {
LS.call(table_clear, action);
} else {
LS.newtable(action);
keep_action = true;
}
LS.call(table_clear, classtab);
if (keep_action) {
LS.setfield(classtab, "action", action);
}
} else {
LS.rawset(LuaGlobals, classname, LuaNil);
}
@@ -173,11 +213,7 @@ LuaDefine(source_clear_globals, "") {
// Restore the lua builtins from the backup snapshot.
//
// The global environment is expected to be clean, but
// the subtables are expected to still be present, in
// accordance with how 'source_clear_globals' works.
//
LuaDefine(source_restore_builtins, "g") {
LuaDefine(source_restore_builtins, "") {
LuaVar snapshot, key, value, skey, svalue, subglobal;
LuaStack LS(L, snapshot, key, value, skey, svalue, subglobal);
@@ -195,57 +231,8 @@ LuaDefine(source_restore_builtins, "g") {
return LS.result();
}
// Snapshot all the lua builtin functions.
// Load all the 'LuaDefine' C functions into the lua state.
//
// Precondition: this is meant to be used on a pristine lua
// intepreter, before the user dumps a bunch of crap into the
// global environment. It won't work if the global environment
// contains anything other than the lua builtins.
//
// Note: the global environment contains _G. This routine
// perceives this as a subtable, so it backs up the top level
// as well.
//
LuaDefine(source_snapshot_builtins, "c") {
LuaVar key, value, skey, svalue, snapshot, ssnapshot;
LuaStack LS(L, snapshot, key, value, skey, svalue, ssnapshot);
LS.newtable(snapshot);
LS.set(key, LuaNil);
while (LS.next(LuaGlobals, key, value) != 0) {
if (LS.istable(value)) {
LS.newtable(ssnapshot);
LS.rawset(snapshot, key, ssnapshot);
LS.set(skey, LuaNil);
while (LS.next(value, skey, svalue) != 0) {
if (LS.isfunction(svalue) || LS.isstring(svalue) || LS.isnumber(svalue)) {
LS.rawset(ssnapshot, skey, svalue);
}
}
}
}
LS.setfield(LuaRegistry, "source_snapshot_builtins", snapshot);
return LS.result();
}
static void load_builtin(lua_State *L, const char *name, lua_CFunction func) {
lua_pushcfunction(L, func);
lua_pushstring(L, name);
lua_call(L, 1, 0);
}
LuaDefine(source_load_builtins, "") {
LuaStack LS(L);
load_builtin(L, "base", luaopen_base);
load_builtin(L, "table", luaopen_table);
load_builtin(L, "string", luaopen_string);
load_builtin(L, "math", luaopen_math);
load_builtin(L, "bit", luaopen_math);
load_builtin(L, "debug", luaopen_debug);
// Do not load: package, io, os, debug, jit
return 0;
}
LuaDefine(source_load_cfunctions, "") {
auto regs = LuaFunctionReg::all();
for (const LuaFunctionReg *r : regs) {
@@ -275,10 +262,7 @@ LuaDefine(source_load_cfunctions, "") {
return 0;
}
// Fetches the source database and runs all the loadresults.
//
// Returns a single string, which is a bunch of concatenated error
// messages.
// Run all the closures from the source database.
//
LuaDefine(source_load_lfunctions, "") {
LuaRet errors;
@@ -336,13 +320,3 @@ LuaDefine(source_rebuild, "c") {
return LS.result();
}
LuaDefine(source_autoinit, "") {
auto regs = LuaFunctionReg::all();
for (const LuaFunctionReg *r : regs) {
std::string mode = r->get_mode();
if (mode.find('a') != std::string::npos) {
r->get_func()(L);
}
}
return 0;
}