Work on SourceDB:rebuild

This commit is contained in:
2023-04-10 17:58:44 -04:00
parent 005ce0629e
commit 972f0095c6
5 changed files with 94 additions and 60 deletions

View File

@@ -302,17 +302,17 @@ static void source_load_cconstants(lua_State *L) {
}
}
// Run all the closures from the source database.
//
static eng::string source_load_lfunctions(lua_State *L) {
LuaVar sourcedb, key, info, seq, closure, err;
LuaExtStack LS(L, sourcedb, key, info, seq, closure, err);
eng::vector<eng::string> SourceDB::modules() {
eng::vector<eng::string> result;
LuaVar sourcedb, key, info, seq;
LuaExtStack LS(lua_state_, sourcedb, key, info, seq);
result.push_back("CORE");
// Get the source database.
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (LS.type(sourcedb) != LUA_TTABLE) {
LS.newtable(sourcedb);
LS.rawset(LuaRegistry, "sourcedb", sourcedb);
if (!LS.istable(sourcedb)) {
return result;
}
// Sort the keys by sequence number.
@@ -323,38 +323,40 @@ static eng::string source_load_lfunctions(lua_State *L) {
indices[LS.ckinteger(seq)] = LS.ckstring(key);
}
// Now call the closures in the proper order.
eng::ostringstream errss;
for (const auto &p : indices) {
LS.rawget(info, sourcedb, p.second);
LS.rawget(closure, info, "loadresult");
// If there's already an error in the sourcedb, collect it.
if (!LS.isfunction(closure)) {
errss << LS.ckstring(closure) << "\n";
continue;
}
// Call the closure. If there's an error, collect it.
lua_pushvalue(L, closure.index());
eng::string msg = traceback_pcall(L, 0, 0);
if (!msg.empty()) {
LS.set(err, msg);
errss << msg << std::endl;
}
for (const auto &pair : indices) {
result.push_back(pair.second);
}
return errss.str();
return result;
}
void SourceDB::rebuild_core() {
source_clear_globals(lua_state_);
source_load_cfunctions(lua_state_);
source_load_cconstants(lua_state_);
}
eng::string SourceDB::rebuild() {
lua_State *L = lua_state_;
LuaExtStack LS(L);
source_clear_globals(L);
source_load_cfunctions(L);
source_load_cconstants(L);
eng::string errs = source_load_lfunctions(L);
return errs;
eng::string SourceDB::rebuild_module(const eng::string &mod) {
if (mod == "CORE") {
rebuild_core();
return "";
} else {
LuaVar sourcedb, info, closure;
LuaExtStack LS(lua_state_, sourcedb, info, closure);
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (!LS.istable(sourcedb)) {
return "SourceDB not initialized";
}
LS.rawget(info, sourcedb, mod);
if (!LS.istable(info)) {
return util::ss("No such module: ", mod);
}
LS.rawget(closure, info, "loadresult");
if (!LS.isfunction(closure)) {
return util::ss(mod, ":", LS.ckstring(closure));
}
lua_pushvalue(lua_state_, closure.index());
return traceback_pcall(lua_state_, 0, 0);
}
}
void SourceDB::run_unittests() {
@@ -407,8 +409,8 @@ void SourceDB::init(lua_State *L) {
LS.setmetatable(nullstring, classtab);
// Rebuild the global environment.
rebuild();
rebuild_core();
// We need to register all C functions with the eris permanents tables.
LS.rawget(persist, LuaRegistry, "persist");
LS.rawget(unpersist, LuaRegistry, "unpersist");