Files
integration/luprex/core/cpp/source.cpp

323 lines
9.6 KiB
C++
Raw Normal View History

2021-01-02 13:31:18 -05:00
2020-11-27 13:21:07 -05:00
#include <string>
#include <vector>
#include <map>
2021-01-12 15:49:05 -05:00
#include <set>
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"
2021-01-23 14:44:06 -05:00
#include "traceback.hpp"
2020-12-05 18:57:53 -05:00
#include "table.hpp"
#include "source.hpp"
2021-02-02 16:29:07 -05:00
// Read control.lst
//
// - trim all lines
// - remove blank lines
// - remove comment lines
//
util::stringvec read_control_lst(const std::string &path) {
util::stringvec lines = util::get_file_lines(path);
util::stringvec result;
for (int i = 0; i < int(lines.size()); i++) {
std::string trimmed = util::trim(lines[i]);
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
result.push_back(trimmed);
}
}
return result;
}
2020-11-27 13:21:07 -05:00
2021-01-12 14:14:38 -05:00
LuaDefine(source_makeclass, "f") {
LuaArg classname;
LuaRet classtab;
2021-02-09 17:15:54 -05:00
LuaStack LS(L, classname, classtab);
LS.makeclass(classtab, classname);
2021-01-12 14:14:38 -05:00
return LS.result();
}
2021-02-10 16:22:24 -05:00
LuaDefine(source_maketangible, "f") {
LuaArg classname;
LuaRet classtab;
LuaVar subtab;
LuaStack LS(L, classname, classtab, subtab);
LS.makeclass(classtab, classname);
LS.makesubtable(subtab, classtab, "action");
return LS.result();
}
2021-01-12 14:14:38 -05:00
2021-02-28 16:32:42 -05:00
// the 'luaopen' function creates a new table.
//
// We don't want to create new tables during reload, so we call luaopen,
// then we copy the contents of the new table into the existing 'makeclass'
// table.
//
static void load_builtin_class(lua_State *L, const char *name, lua_CFunction func) {
LuaVar sourcetab, classtab, key, value;
LuaStack LS(L, sourcetab, classtab, key, value);
LS.makeclass(classtab, name);
func(L);
lua_replace(L, sourcetab.index());
LS.set(key, LuaNil);
while (LS.next(sourcetab, key, value) != 0) {
LS.rawset(classtab, key, value);
}
2021-01-12 14:14:38 -05:00
}
2021-01-16 01:24:33 -05:00
static void source_install_builtins(lua_State *L) {
LuaVar nullstring, stringclass, globtab;
LuaStack LS(L, nullstring, stringclass, globtab);
2021-02-28 16:32:42 -05:00
luaopen_base(L);
load_builtin_class(L, "table", luaopen_table);
load_builtin_class(L, "string", luaopen_string);
load_builtin_class(L, "math", luaopen_math);
load_builtin_class(L, "debug", luaopen_debug);
// Nuke a few of the builtin functions for sandboxing reasons.
LS.getglobaltable(globtab);
LS.rawset(globtab, "loadfile", LuaNil);
2021-02-28 16:32:42 -05:00
// Set the metatable for strings.
// Normally, this would be done by luaopen_string, but we're
// messing with the tables so we have to redo it.
LS.makeclass(stringclass, "string");
LS.set(nullstring, "");
LS.setmetatable(nullstring, stringclass);
2021-01-12 14:14:38 -05:00
}
2021-01-16 01:24:33 -05:00
static int source_updatefile(lua_State *L) {
2020-11-27 13:21:07 -05:00
LuaArg source, fn;
LuaRet info;
2021-02-10 16:47:45 -05:00
LuaVar fingerprint, null, loadresult;
LuaStack LS(L, source, fn, info, fingerprint, null, loadresult);
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);
2021-02-10 16:47:45 -05:00
LS.rawget(fingerprint, info, "fingerprint");
2020-12-05 18:57:53 -05:00
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
}
2021-01-16 01:24:33 -05:00
// std::cerr << "Probing " << cfn << std::endl;
2021-01-23 16:10:29 -05:00
std::string new_fingerprint = util::get_file_fingerprint("lua/" + cfn);
2020-12-05 18:57:53 -05:00
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;
2021-01-23 16:10:29 -05:00
std::string ccode = util::get_file_contents("lua/" + cfn);
2021-02-10 16:47:45 -05:00
LS.rawset(info, "name", fn);
LS.rawset(info, "fingerprint", new_fingerprint);
LS.rawset(info, "code", ccode);
2020-12-05 18:57:53 -05:00
if ((new_fingerprint == "")||(ccode == "")) {
2021-02-10 16:47:45 -05:00
LS.rawset(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());
2021-02-10 16:47:45 -05:00
lua_replace(L, loadresult.index());
LS.rawset(info, "loadresult", loadresult);
2020-11-27 13:21:07 -05:00
}
}
return LS.result();
}
2021-01-16 01:24:33 -05:00
void SourceDB::update() {
lua_State *L = lua_state_;
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.
2021-02-10 16:47:45 -05:00
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
2020-12-05 18:57:53 -05:00
if (!LS.istable(sourcedb)) {
LS.newtable(sourcedb);
}
2020-11-27 13:21:07 -05:00
// Read the list of filenames.
2021-02-02 16:29:07 -05:00
util::stringvec filenames = read_control_lst("lua/control.lst");
2020-11-27 13:21:07 -05:00
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);
2021-02-10 16:47:45 -05:00
LS.rawset(info, "sequence", seq);
2020-11-27 13:21:07 -05:00
LS.rawset(newdb, fn, info);
}
2020-12-05 18:57:53 -05:00
// Store the new source db.
2021-02-10 16:47:45 -05:00
LS.rawset(LuaRegistry, "sourcedb", newdb);
2021-01-16 01:24:33 -05:00
LS.result();
2020-11-27 13:21:07 -05:00
}
2021-01-12 14:14:38 -05:00
// Delete everything from the global environment except
2021-02-09 17:15:54 -05:00
// the class tables.
2021-01-02 13:31:18 -05:00
//
2021-01-16 01:24:33 -05:00
static void source_clear_globals(lua_State *L) {
2021-02-28 15:05:45 -05:00
LuaVar classname, classtab, key, globtab;
LuaStack LS(L, classname, classtab, key, globtab);
2021-01-02 13:31:18 -05:00
2021-02-28 16:32:42 -05:00
LS.getglobaltable(globtab);
2021-02-28 15:05:45 -05:00
LS.rawset(globtab, "_G", LuaNil);
2020-12-05 18:57:53 -05:00
LS.set(classname, LuaNil);
2021-02-28 15:05:45 -05:00
while (LS.next(globtab, classname, classtab) != 0) {
2020-11-27 13:21:07 -05:00
if (LS.istable(classtab)) {
2020-12-05 18:57:53 -05:00
LS.call(table_clear, classtab);
2021-01-02 13:31:18 -05:00
} else {
2021-02-28 15:05:45 -05:00
LS.rawset(globtab, classname, LuaNil);
2021-01-02 13:31:18 -05:00
}
}
2021-02-28 15:05:45 -05:00
LS.rawset(globtab, "_G", globtab);
2021-01-16 01:24:33 -05:00
LS.result();
2021-01-02 13:31:18 -05:00
}
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
//
2021-01-16 01:24:33 -05:00
static void source_load_cfunctions(lua_State *L) {
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());
}
}
}
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-16 01:24:33 -05:00
static void source_load_lfunctions(lua_State *L) {
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.
2021-02-10 16:47:45 -05:00
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
2020-12-05 18:57:53 -05:00
if (LS.type(sourcedb) != LUA_TTABLE) {
LS.newtable(sourcedb);
2021-02-10 16:47:45 -05:00
LS.rawset(LuaRegistry, "sourcedb", sourcedb);
2020-12-05 18:57:53 -05:00
}
// Sort the keys by sequence number.
std::map<int, std::string> indices;
LS.set(key, LuaNil);
while (LS.next(sourcedb, key, info) != 0) {
2021-02-10 16:47:45 -05:00
LS.rawget(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);
2021-02-10 16:47:45 -05:00
LS.rawget(closure, info, "loadresult");
2020-12-05 18:57:53 -05:00
// 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());
2021-01-16 01:24:33 -05:00
LS.result();
2020-12-05 18:57:53 -05:00
}
2021-01-16 01:24:33 -05:00
void SourceDB::rebuild() {
lua_State *L = lua_state_;
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);
2021-02-28 16:32:42 -05:00
source_install_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;
2021-01-16 01:24:33 -05:00
LS.result();
2020-12-05 18:57:53 -05:00
}
2021-01-16 01:24:33 -05:00
void SourceDB::run_unittests() {
lua_State *L = lua_state_;
2021-02-28 15:05:45 -05:00
LuaVar unittests, name, func, err, globtab;
LuaStack LS(L, unittests, name, func, err, globtab);
2021-01-12 15:49:05 -05:00
2021-02-28 16:32:42 -05:00
LS.getglobaltable(globtab);
2021-02-28 15:05:45 -05:00
LS.rawget(unittests, globtab, "unittests");
2021-01-12 15:49:05 -05:00
// Sort the unit test names.
std::set<std::string> names;
LS.set(name, LuaNil);
while (LS.next(unittests, name, func) != 0) {
if (LS.isfunction(func) && LS.isstring(name)) {
names.insert(LS.ckstring(name));
}
}
// Run the functions in order
bool any = false;
for (const std::string &name : names) {
std::cerr << "Running unittests." << name << std::endl;
LS.rawget(func, unittests, name);
lua_pushvalue(L, func.index());
if (traceback_pcall(L, 0, 0) != 0) {
lua_replace(L, err.index());
std::cerr << LS.ckstring(err);
any = true;
}
}
2021-01-16 01:24:33 -05:00
if (any) {
exit(1);
}
LS.result();
2021-01-12 15:49:05 -05:00
}