Inverted control flow, engine as library

This commit is contained in:
2021-10-04 17:45:18 -04:00
parent e0fdb2d42f
commit bc22dc89af
15 changed files with 387 additions and 136 deletions

View File

@@ -15,24 +15,6 @@
#include "source.hpp"
#include "luasnap.hpp"
// 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;
}
LuaDefine(source_makeclass, "f") {
LuaArg classname;
LuaRet classtab;
@@ -155,17 +137,7 @@ static void source_updatefile(LuaStack &LS0, LuaSlot source, LuaSlot fn, LuaSlot
old_fingerprint = LS.ckstring(fingerprint);
}
// std::cerr << "Probing " << cfn << std::endl;
std::string new_fingerprint = util::get_file_fingerprint("lua/" + cfn);
if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) {
std::cerr << "Rereading " << cfn << std::endl;
std::string code;
if (new_fingerprint != "") code = util::get_file_contents("lua/" + cfn);
LS.rawset(info, "name", fn);
LS.rawset(info, "fingerprint", new_fingerprint);
LS.rawset(info, "code", code);
LS.rawset(info, "hash", util::hash_to_hex(util::hash_string(code)));
calculate_loadresult(LS, info, cfn, code);
}
LS.result();
}
@@ -295,39 +267,31 @@ std::string SourceDB::get(const std::string &fn) {
return oss.str();
}
void SourceDB::update() {
void SourceDB::update(const util::LuaSource &source) {
lua_State *L = lua_state_;
LuaVar sourcedb, newdb, info, fn, seq;
LuaStack LS(L, newdb, sourcedb, info, fn, seq);
LuaVar sourcedb, info;
LuaStack LS(L, sourcedb, info);
// Get the (old) source database.
// Get and clear the source database.
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (!LS.istable(sourcedb)) {
LS.newtable(sourcedb);
LS.rawset(LuaRegistry, "sourcedb", sourcedb);
}
LS.cleartable(sourcedb);
// Read the list of filenames.
util::StringVec filenames = read_control_lst("lua/control.lst");
if (filenames.empty()) {
luaL_error(L, "cannot read source database control.lst");
for (int i = 0; i < int(source.size()); i++) {
const std::string &file = source[i].first;
const std::string &code = source[i].second;
std::cerr << "Compiling " << file << std::endl;
LS.newtable(info);
LS.rawset(info, "name", file);
LS.rawset(info, "code", code);
LS.rawset(info, "hash", util::hash_to_hex(util::hash_string(code)));
LS.rawset(info, "sequence", i + 1);
calculate_loadresult(LS, info, file, code);
LS.rawset(sourcedb, file, info);
}
// Process the files one by one.
LS.newtable(newdb);
for (int i = 0; i < int(filenames.size()); i++) {
LS.set(fn, filenames[i]);
// Call source_updatefile to get the updated info for one file.
source_updatefile(LS, sourcedb, fn, info);
// Insert the sequence number and put finalized info into the new database.
LS.set(seq, i + 1);
LS.rawset(info, "sequence", seq);
LS.rawset(newdb, fn, info);
}
// Store the new source db.
LS.rawset(LuaRegistry, "sourcedb", newdb);
LS.result();
}