Add docs for luaopen_base

This commit is contained in:
2021-12-17 14:04:34 -05:00
parent 9b956f00e7
commit 0a1480d85c
5 changed files with 27 additions and 35 deletions

View File

@@ -360,31 +360,6 @@ int LuaStack::rawlen(LuaSlot obj) const {
return lua_rawlen(L_, obj.index());
}
std::string LuaStack::get_function_name(LuaSlot fn) {
LuaVar globals, key, val, skey, sval;
LuaStack LS(L_, globals, key, val, skey, sval);
LS.getglobaltable(globals);
LS.set(key, LuaNil);
while (LS.next(globals, key, val)) {
if (LS.isstring(key) && !LS.rawequal(globals, val)) {
if (LS.rawequal(val, fn)) {
return LS.ckstring(key);
}
if (LS.istable(val)) {
LS.set(skey, LuaNil);
while (LS.next(val, skey, sval)) {
if (LS.isstring(skey) && LS.rawequal(sval, fn)) {
std::string n1 = LS.ckstring(key);
std::string n2 = LS.ckstring(skey);
return n1 + "." + n2;
}
}
}
}
}
return "";
}
int LuaStack::gettabletype(LuaSlot tab) const {
uint16_t bits = lua_getflagbits(L_, tab.index());
return LUA_TT_GENERAL + (bits & 0x000F);

View File

@@ -449,9 +449,6 @@ public:
push_any_value(value);
lua_rawseti(L_, tab, key);
}
// Get function name
std::string get_function_name(LuaSlot ls);
// Lua flagbits manipulation: Table types.
int gettabletype(LuaSlot tab) const;

View File

@@ -33,12 +33,7 @@ void atomic_print(LuaStack &LS, LuaSlot val, bool quote, std::ostream *os) {
(*os) << (LS.ckboolean(val) ? "true" : "false");
return;
case LUA_TFUNCTION: {
std::string name = LS.get_function_name(val);
if (name.empty()) {
(*os) << "<fn>";
} else {
(*os) << "<fn." << name << ">";
}
(*os) << "<function>";
return;
}
default:

View File

@@ -103,10 +103,14 @@ static void source_install_builtins(lua_State *L) {
// Nuke a few of the builtin functions for sandboxing reasons.
LS.getglobaltable(globtab);
erase_builtin(LS, globtab, "", "dofile");
erase_builtin(LS, globtab, "", "collectgarbage");
erase_builtin(LS, globtab, "", "loadfile");
erase_builtin(LS, globtab, "", "load");
erase_builtin(LS, globtab, "", "loadstring");
erase_builtin(LS, globtab, "", "print");
erase_builtin(LS, globtab, "", "xpcall");
erase_builtin(LS, globtab, "string", "dump");
// Set the metatable for strings.
// Normally, this would be done by luaopen_string, but we're
@@ -386,6 +390,7 @@ static std::string source_load_lfunctions(lua_State *L) {
return errss.str();
}
std::string SourceDB::rebuild() {
lua_State *L = lua_state_;
source_clear_globals(L);
@@ -513,6 +518,7 @@ void SourceDB::register_lua_builtins() {
lua_close(L);
}
std::string SourceDB::function_docs(const LuaStack &LS0, LuaSlot fn) {
lua_State *L = LS0.state();
LuaVar sourcedb, fname, finfo, code;
@@ -702,3 +708,21 @@ LuaDefineBuiltin(string_gmatch, "str, pattern", "iterate over pattern-matched su
LuaDefineBuiltin(string_gsub, "str, pattern, replace", "global replace pattern in string");
LuaDefineBuiltin(string_match, "str, pattern", "return start and end of pattern in string");
LuaDefineBuiltin(string_sub, "str, pos1, pos2", "return substring of str from pos1 to pos2");
LuaDefineBuiltin(assert, "flag [,message]", "assert that flag is true, if not, raise error");
LuaDefineBuiltin(error, "message", "raise an error");
LuaDefineBuiltin(getmetatable, "table", "get the metatable of a table");
LuaDefineBuiltin(ipairs, "vector", "meant to be used in loops, iterates over a vector");
LuaDefineBuiltin(next, "table, k", "returns the key after k and the corresponding value");
LuaDefineBuiltin(pairs, "table", "meant to be used in loops, iterates over a table");
LuaDefineBuiltin(rawpairs, "table", "meant to be used in loops, iterates over a table");
LuaDefineBuiltin(pcall, "function, arg1, arg2, ...", "call the function, catching any errors");
LuaDefineBuiltin(rawequal, "val1, val2", "return true if the two values are the same object");
LuaDefineBuiltin(rawlen, "obj", "return the length of a vector or string");
LuaDefineBuiltin(rawget, "table, key", "get a table entry, ignoring metamethods");
LuaDefineBuiltin(rawset, "table, key, value", "set a table entry, ignoring metamethods");
LuaDefineBuiltin(select, "n, arg1, arg2, ...", "return the nth argument");
LuaDefineBuiltin(setmetatable, "table, meta", "set the metatable of the specified table");
LuaDefineBuiltin(tonumber, "str", "convert a string to a number");
LuaDefineBuiltin(type, "obj", "return the type of obj as a string");

View File

@@ -120,6 +120,7 @@
#ifndef SOURCE_HPP
#define SOURCE_HPP
#include "util.hpp"
#include "luastack.hpp"
#include "streambuffer.hpp"
#include "debugcollector.hpp"