2021-01-02 13:31:18 -05:00
|
|
|
|
2020-11-27 13:21:07 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
2020-12-05 18:57:53 -05:00
|
|
|
#include <algorithm>
|
2020-11-27 13:21:07 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "util.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
|
2020-11-27 13:21:07 -05:00
|
|
|
#include "luastack.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
#include "table.hpp"
|
|
|
|
|
#include "source.hpp"
|
2021-01-02 13:31:18 -05:00
|
|
|
#include "traceback.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
|
2020-11-27 13:21:07 -05:00
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(source_updatefile, "") {
|
2020-11-27 13:21:07 -05:00
|
|
|
LuaArg source, fn;
|
|
|
|
|
LuaRet info;
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaVar fingerprint, null;
|
|
|
|
|
LuaStack LS(L, source, fn, info, fingerprint, null);
|
2020-11-27 13:21:07 -05:00
|
|
|
|
|
|
|
|
// Get the existing info table from the source DB.
|
|
|
|
|
if (LS.istable(source)) {
|
|
|
|
|
LS.rawget(info, source, fn);
|
|
|
|
|
if (!LS.istable(info)) {
|
|
|
|
|
LS.newtable(info);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LS.newtable(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the file modification is wrong, update
|
2020-12-05 18:57:53 -05:00
|
|
|
// these fields: code, fingerprint, closure, error
|
2020-11-27 13:21:07 -05:00
|
|
|
// Otherwise, update nothing.
|
2021-01-02 13:31:18 -05:00
|
|
|
std::string cfn = LS.ckstring(fn);
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.getfield(fingerprint, info, "fingerprint");
|
|
|
|
|
std::string old_fingerprint;
|
|
|
|
|
if (LS.isstring(fingerprint)) {
|
2021-01-02 13:31:18 -05:00
|
|
|
old_fingerprint = LS.ckstring(fingerprint);
|
2020-11-27 13:21:07 -05:00
|
|
|
}
|
|
|
|
|
std::cerr << "Probing " << cfn << std::endl;
|
2020-12-05 18:57:53 -05:00
|
|
|
std::string new_fingerprint = util::get_file_fingerprint("syslua/" + cfn);
|
|
|
|
|
LS.set(null, LuaNil);
|
|
|
|
|
if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) {
|
2020-11-27 13:21:07 -05:00
|
|
|
std::cerr << "Rereading " << cfn << std::endl;
|
2020-12-05 18:57:53 -05:00
|
|
|
std::string ccode = util::get_file_contents("syslua/" + cfn);
|
2020-11-27 13:21:07 -05:00
|
|
|
LS.setfield(info, "name", fn);
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.setfield(info, "fingerprint", new_fingerprint);
|
2020-11-27 13:21:07 -05:00
|
|
|
LS.setfield(info, "code", ccode);
|
2020-12-05 18:57:53 -05:00
|
|
|
if ((new_fingerprint == "")||(ccode == "")) {
|
|
|
|
|
LS.setfield(info, "loadresult", "cannot read source file");
|
2020-11-27 13:21:07 -05:00
|
|
|
} else {
|
2020-12-05 18:57:53 -05:00
|
|
|
std::string chunk = "=" + cfn;
|
|
|
|
|
luaL_loadbuffer(L, ccode.c_str(), ccode.size(), chunk.c_str());
|
|
|
|
|
lua_setfield(L, info.index(), "loadresult");
|
2020-11-27 13:21:07 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(source_update, "c") {
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaVar sourcedb, newdb, info, fn, seq;
|
|
|
|
|
LuaStack LS(L, newdb, sourcedb, info, fn, seq);
|
|
|
|
|
|
|
|
|
|
// Get the (old) source database.
|
|
|
|
|
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
|
|
|
|
if (!LS.istable(sourcedb)) {
|
|
|
|
|
LS.newtable(sourcedb);
|
|
|
|
|
}
|
2020-11-27 13:21:07 -05:00
|
|
|
|
|
|
|
|
// Read the list of filenames.
|
|
|
|
|
std::string ctrl = "syslua/control.lst";
|
|
|
|
|
util::stringvec filenames = util::trim_and_uncomment(util::read_lines(ctrl));
|
|
|
|
|
if (filenames.empty()) {
|
|
|
|
|
luaL_error(L, "cannot read source database control.lst");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process the files one by one.
|
|
|
|
|
LS.newtable(newdb);
|
2020-12-05 18:57:53 -05:00
|
|
|
for (int i = 0; i < int(filenames.size()); i++) {
|
|
|
|
|
LS.set(fn, filenames[i]);
|
2020-11-27 13:21:07 -05:00
|
|
|
|
|
|
|
|
// Call source_updatefile to get the updated info for one file.
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.call(info, source_updatefile, sourcedb, fn);
|
2020-11-27 13:21:07 -05:00
|
|
|
|
|
|
|
|
// Insert the sequence number and put finalized info into the new database.
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.set(seq, i + 1);
|
2020-11-27 13:21:07 -05:00
|
|
|
LS.setfield(info, "sequence", seq);
|
|
|
|
|
LS.rawset(newdb, fn, info);
|
|
|
|
|
}
|
2020-12-05 18:57:53 -05:00
|
|
|
|
|
|
|
|
// Store the new source db.
|
|
|
|
|
LS.setfield(LuaRegistry, "sourcedb", newdb);
|
2020-11-27 13:21:07 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
// Delete everything from the global environment except
|
|
|
|
|
// the class tables and the class action tables.
|
2021-01-02 13:31:18 -05:00
|
|
|
//
|
|
|
|
|
LuaDefine(source_clear_globals, "") {
|
|
|
|
|
LuaVar classname, classtab, action, key;
|
|
|
|
|
LuaStack LS(L, classname, classtab, action, key);
|
|
|
|
|
|
|
|
|
|
LS.setfield(LuaGlobals, "_G", LuaNil);
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.set(classname, LuaNil);
|
2021-01-02 13:31:18 -05:00
|
|
|
while (LS.next(LuaGlobals, classname, classtab) != 0) {
|
2020-11-27 13:21:07 -05:00
|
|
|
if (LS.istable(classtab)) {
|
2021-01-12 14:14:38 -05:00
|
|
|
bool keep_action = false;
|
2021-01-02 13:31:18 -05:00
|
|
|
LS.getfield(action, classtab, "action");
|
2020-11-27 13:21:07 -05:00
|
|
|
if (LS.istable(action)) {
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.call(table_clear, action);
|
2021-01-12 14:14:38 -05:00
|
|
|
keep_action = true;
|
2020-11-27 13:21:07 -05:00
|
|
|
}
|
2020-12-05 18:57:53 -05:00
|
|
|
LS.call(table_clear, classtab);
|
2021-01-12 14:14:38 -05:00
|
|
|
if (keep_action) {
|
|
|
|
|
LS.setfield(classtab, "action", action);
|
|
|
|
|
}
|
2021-01-02 13:31:18 -05:00
|
|
|
} else {
|
|
|
|
|
LS.rawset(LuaGlobals, classname, LuaNil);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore the lua builtins from the backup snapshot.
|
|
|
|
|
//
|
2021-01-12 14:14:38 -05:00
|
|
|
LuaDefine(source_restore_builtins, "") {
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaVar snapshot, key, value, skey, svalue, subglobal;
|
|
|
|
|
LuaStack LS(L, snapshot, key, value, skey, svalue, subglobal);
|
|
|
|
|
|
|
|
|
|
LS.getfield(snapshot, LuaRegistry, "source_snapshot_builtins");
|
|
|
|
|
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(snapshot, key, value) != 0) {
|
|
|
|
|
LS.checktable(value);
|
|
|
|
|
LS.call(subglobal, source_makeclass, key);
|
|
|
|
|
LS.set(skey, LuaNil);
|
|
|
|
|
while (LS.next(value, skey, svalue) != 0) {
|
|
|
|
|
LS.rawset(subglobal, skey, svalue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
// Load all the 'LuaDefine' C functions into the lua state.
|
2021-01-02 13:31:18 -05:00
|
|
|
//
|
|
|
|
|
LuaDefine(source_load_cfunctions, "") {
|
2020-12-05 18:57:53 -05:00
|
|
|
auto regs = LuaFunctionReg::all();
|
|
|
|
|
for (const LuaFunctionReg *r : regs) {
|
|
|
|
|
const std::string &name = r->get_name();
|
|
|
|
|
size_t upos = name.find('_');
|
|
|
|
|
std::string classname;
|
|
|
|
|
std::string funcname;
|
|
|
|
|
if (upos == std::string::npos) {
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
funcname = name.substr(upos + 1);
|
|
|
|
|
classname = name.substr(0, upos);
|
|
|
|
|
}
|
|
|
|
|
lua_CFunction func = r->get_func();
|
2021-01-02 13:31:18 -05:00
|
|
|
std::string mode = r->get_mode();
|
|
|
|
|
if (mode.find('c') != std::string::npos) { // Insert into class
|
2020-12-05 18:57:53 -05:00
|
|
|
lua_pushlstring(L, classname.c_str(), classname.size());
|
2021-01-02 13:31:18 -05:00
|
|
|
source_makeclass(L);
|
2020-12-05 18:57:53 -05:00
|
|
|
lua_pushcfunction(L, func);
|
|
|
|
|
lua_setfield(L, -2, funcname.c_str());
|
|
|
|
|
}
|
2021-01-02 13:31:18 -05:00
|
|
|
if (mode.find('f') != std::string::npos) { // Make global function
|
2020-12-05 18:57:53 -05:00
|
|
|
lua_pushcfunction(L, func);
|
|
|
|
|
lua_setglobal(L, funcname.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
// Run all the closures from the source database.
|
2020-12-05 18:57:53 -05:00
|
|
|
//
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(source_load_lfunctions, "") {
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaRet errors;
|
|
|
|
|
LuaVar sourcedb, key, info, seq, closure, err;
|
|
|
|
|
LuaStack LS(L, sourcedb, errors, key, info, seq, closure, err);
|
|
|
|
|
|
|
|
|
|
// Get the source database.
|
|
|
|
|
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
|
|
|
|
if (LS.type(sourcedb) != LUA_TTABLE) {
|
|
|
|
|
LS.newtable(sourcedb);
|
|
|
|
|
LS.setfield(LuaRegistry, "sourcedb", sourcedb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort the keys by sequence number.
|
|
|
|
|
std::map<int, std::string> indices;
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(sourcedb, key, info) != 0) {
|
|
|
|
|
LS.getfield(seq, info, "sequence");
|
2021-01-02 13:31:18 -05:00
|
|
|
indices[LS.ckinteger(seq)] = LS.ckstring(key);
|
2020-12-05 18:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now call the closures in the proper order.
|
|
|
|
|
std::stringstream errss;
|
|
|
|
|
for (const auto &p : indices) {
|
|
|
|
|
LS.rawget(info, sourcedb, p.second);
|
|
|
|
|
LS.getfield(closure, info, "loadresult");
|
|
|
|
|
|
|
|
|
|
// If there's already an error in the sourcedb, collect it.
|
|
|
|
|
if (!LS.isfunction(closure)) {
|
2021-01-02 13:31:18 -05:00
|
|
|
errss << LS.ckstring(closure) << "\n";
|
2020-12-05 18:57:53 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the closure. If there's an error, collect it.
|
|
|
|
|
lua_pushvalue(L, closure.index());
|
2021-01-02 13:31:18 -05:00
|
|
|
if (traceback_pcall(L, 0, 0) != 0) {
|
2020-12-05 18:57:53 -05:00
|
|
|
lua_replace(L, err.index());
|
2021-01-02 13:31:18 -05:00
|
|
|
errss << LS.ckstring(err);
|
2020-12-05 18:57:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LS.set(errors, errss.str());
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(source_rebuild, "c") {
|
2020-12-05 18:57:53 -05:00
|
|
|
LuaVar errs;
|
|
|
|
|
LuaStack LS(L, errs);
|
2021-01-02 13:31:18 -05:00
|
|
|
source_clear_globals(L);
|
|
|
|
|
source_restore_builtins(L);
|
2020-12-05 18:57:53 -05:00
|
|
|
source_load_cfunctions(L);
|
|
|
|
|
source_load_lfunctions(L);
|
|
|
|
|
lua_replace(L, errs.index());
|
2021-01-02 13:31:18 -05:00
|
|
|
std::string errstr = LS.ckstring(errs);
|
2020-12-05 18:57:53 -05:00
|
|
|
std::cerr << errstr;
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|