From 4e374294b6d2801b85398096c0f59f84237837e8 Mon Sep 17 00:00:00 2001 From: jyelon Date: Mon, 12 Jan 2026 13:45:04 -0500 Subject: [PATCH] A small refactor to SourceDB to prepare for doc-search functionality --- luprex/cpp/core/source.cpp | 57 ++++++++++++++++-------------- luprex/cpp/core/source.hpp | 8 ++++- luprex/cpp/core/world-accessor.cpp | 2 +- luprex/cpp/core/world.hpp | 4 +++ 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/luprex/cpp/core/source.cpp b/luprex/cpp/core/source.cpp index ffc8b734..a6f3114d 100644 --- a/luprex/cpp/core/source.cpp +++ b/luprex/cpp/core/source.cpp @@ -17,6 +17,7 @@ #include #include + 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 SourceDB::modules() { eng::vector 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 ""; diff --git a/luprex/cpp/core/source.hpp b/luprex/cpp/core/source.hpp index 63dd1bbf..d3ba462a 100644 --- a/luprex/cpp/core/source.hpp +++ b/luprex/cpp/core/source.hpp @@ -148,6 +148,12 @@ public: // eng::vector 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. // diff --git a/luprex/cpp/core/world-accessor.cpp b/luprex/cpp/core/world-accessor.cpp index d23c5996..75795c58 100644 --- a/luprex/cpp/core/world-accessor.cpp +++ b/luprex/cpp/core/world-accessor.cpp @@ -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; } diff --git a/luprex/cpp/core/world.hpp b/luprex/cpp/core/world.hpp index edd96a2a..3e85c094 100644 --- a/luprex/cpp/core/world.hpp +++ b/luprex/cpp/core/world.hpp @@ -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.