A small refactor to SourceDB to prepare for doc-search functionality
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
LuaDefine(makeclass, "classname", "create a class if it doesn't already exist") {
|
LuaDefine(makeclass, "classname", "create a class if it doesn't already exist") {
|
||||||
LuaArg classname;
|
LuaArg classname;
|
||||||
LuaRet classtab;
|
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> SourceDB::modules() {
|
||||||
eng::vector<eng::string> result;
|
eng::vector<eng::string> result;
|
||||||
LuaVar sourcedb, key, info, seq;
|
LuaVar sourcedb, key, info, seq;
|
||||||
@@ -522,12 +545,8 @@ void SourceDB::register_lua_builtins() {
|
|||||||
lua_close(L);
|
lua_close(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eng::string SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn) {
|
||||||
eng::string SourceDB::function_docs(const LuaCoreStack &LS0, LuaSlot fn) {
|
lua_State *L = LS.state();
|
||||||
lua_State *L = LS0.state();
|
|
||||||
LuaVar sourcedb, fname, finfo, code;
|
|
||||||
LuaExtStack LS(L, sourcedb, fname, finfo, code);
|
|
||||||
|
|
||||||
if (LS.iscfunction(fn)) {
|
if (LS.iscfunction(fn)) {
|
||||||
lua_CFunction cfn = lua_tocfunction(L, fn.index());
|
lua_CFunction cfn = lua_tocfunction(L, fn.index());
|
||||||
const LuaFunctionReg *reg = LuaFunctionReg::lookup(cfn);
|
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);
|
int status = lua_getinfo(L, ">S", &ar);
|
||||||
if (status == 0) return "";
|
if (status == 0) return "";
|
||||||
|
|
||||||
// Get the source database.
|
// Get the source code.
|
||||||
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
|
util::StringVec lines = util::split_lines(get_source(eng::string(ar.short_src)));
|
||||||
if (!LS.istable(sourcedb)) {
|
if (lines.empty()) return "";
|
||||||
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 "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split the code into lines.
|
// Find the line of code containing the function prototype.
|
||||||
util::StringVec lines = util::split_lines(LS.ckstring(code));
|
// Lua numbers source lines from 1, but we number lines from 0,
|
||||||
|
// so we have to subtract one.
|
||||||
int linehi = ar.linedefined - 1;
|
int linehi = ar.linedefined - 1;
|
||||||
if ((linehi < 0) || (linehi >= int(lines.size()))) {
|
if ((linehi < 0) || (linehi >= int(lines.size()))) {
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
@@ -148,6 +148,12 @@ public:
|
|||||||
//
|
//
|
||||||
eng::vector<eng::string> modules();
|
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
|
// rebuild_module
|
||||||
//
|
//
|
||||||
// To rebuild the lua environment, fetch the module list, then
|
// To rebuild the lua environment, fetch the module list, then
|
||||||
@@ -196,7 +202,7 @@ public:
|
|||||||
|
|
||||||
// Get function documentation.
|
// 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.
|
// Serialize and unserialize a source vector.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -933,7 +933,7 @@ LuaDefine(doc, "function",
|
|||||||
std::ostream *ostream = w->lthread_print_stream();
|
std::ostream *ostream = w->lthread_print_stream();
|
||||||
LuaArg func;
|
LuaArg func;
|
||||||
LuaDefStack LS(L, 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 == "") {
|
if (doc == "") {
|
||||||
(*ostream) << "no doc found" << std::endl;
|
(*ostream) << "no doc found" << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,6 +265,10 @@ public:
|
|||||||
//
|
//
|
||||||
const PrintBuffer *get_printbuffer(int64_t actor_id);
|
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.
|
// Rebuild the global environment from the source database.
|
||||||
//
|
//
|
||||||
// Returns true if the rebuild goes without errors.
|
// Returns true if the rebuild goes without errors.
|
||||||
|
|||||||
Reference in New Issue
Block a user