A small refactor to SourceDB to prepare for doc-search functionality

This commit is contained in:
2026-01-12 13:45:04 -05:00
parent bd53ba09a4
commit 4e374294b6
4 changed files with 43 additions and 28 deletions

View File

@@ -17,6 +17,7 @@
#include <algorithm>
#include <fstream>
LuaDefine(makeclass, "classname", "create a class if it doesn't already exist") {
LuaArg classname;
LuaRet classtab;
@@ -297,6 +298,28 @@ static void source_load_cconstants(lua_State *L) {
}
}
eng::string SourceDB::get_source(const eng::string &fn)
{
LuaVar sourcedb, fname, finfo, code;
LuaExtStack LS(lua_state_, sourcedb, fname, finfo, code);
// Get the source database.
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (!LS.istable(sourcedb)) return "";
// Get the finfo table from the source db.
LS.set(fname, fn);
LS.rawget(finfo, sourcedb, fname);
if (!LS.istable(finfo)) return "";
// Get the code from the finfo table.
LS.rawget(code, finfo, "code");
if (!LS.isstring(code)) return "";
return LS.ckstring(code);
}
eng::vector<eng::string> SourceDB::modules() {
eng::vector<eng::string> result;
LuaVar sourcedb, key, info, seq;
@@ -522,12 +545,8 @@ void SourceDB::register_lua_builtins() {
lua_close(L);
}
eng::string SourceDB::function_docs(const LuaCoreStack &LS0, LuaSlot fn) {
lua_State *L = LS0.state();
LuaVar sourcedb, fname, finfo, code;
LuaExtStack LS(L, sourcedb, fname, finfo, code);
eng::string SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn) {
lua_State *L = LS.state();
if (LS.iscfunction(fn)) {
lua_CFunction cfn = lua_tocfunction(L, fn.index());
const LuaFunctionReg *reg = LuaFunctionReg::lookup(cfn);
@@ -556,27 +575,13 @@ eng::string SourceDB::function_docs(const LuaCoreStack &LS0, LuaSlot fn) {
int status = lua_getinfo(L, ">S", &ar);
if (status == 0) return "";
// Get the source database.
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
if (!LS.istable(sourcedb)) {
return "";
}
// Get the finfo table from the source db.
LS.set(fname, eng::string(ar.short_src));
LS.rawget(finfo, sourcedb, fname);
if (!LS.istable(finfo)) {
return "";
}
// Get the code from the finfo table.
LS.rawget(code, finfo, "code");
if (!LS.isstring(code)) {
return "";
}
// Get the source code.
util::StringVec lines = util::split_lines(get_source(eng::string(ar.short_src)));
if (lines.empty()) return "";
// Split the code into lines.
util::StringVec lines = util::split_lines(LS.ckstring(code));
// Find the line of code containing the function prototype.
// Lua numbers source lines from 1, but we number lines from 0,
// so we have to subtract one.
int linehi = ar.linedefined - 1;
if ((linehi < 0) || (linehi >= int(lines.size()))) {
return "";

View File

@@ -148,6 +148,12 @@ public:
//
eng::vector<eng::string> modules();
// get_source
//
// Get the source code for a given module.
//
eng::string get_source(const eng::string &fn);
// rebuild_module
//
// To rebuild the lua environment, fetch the module list, then
@@ -196,7 +202,7 @@ public:
// Get function documentation.
//
static eng::string function_docs(const LuaCoreStack &LS, LuaSlot slot);
eng::string function_docs(const LuaCoreStack &LS, LuaSlot slot);
// Serialize and unserialize a source vector.
//

View File

@@ -933,7 +933,7 @@ LuaDefine(doc, "function",
std::ostream *ostream = w->lthread_print_stream();
LuaArg func;
LuaDefStack LS(L, func);
eng::string doc = SourceDB::function_docs(LS, func);
eng::string doc = w->get_source().function_docs(LS, func);
if (doc == "") {
(*ostream) << "no doc found" << std::endl;
}

View File

@@ -265,6 +265,10 @@ public:
//
const PrintBuffer *get_printbuffer(int64_t actor_id);
// Get the source database.
//
SourceDB &get_source() { return source_db_; }
// Rebuild the global environment from the source database.
//
// Returns true if the rebuild goes without errors.