json.encode and json.decode finished. Also lots of refactoring.

This commit is contained in:
2022-06-06 23:03:26 -04:00
parent f03a48b0a6
commit 779d9e20b8
11 changed files with 1292 additions and 109 deletions

View File

@@ -56,8 +56,7 @@ LuaDefine(classname, "classtable", "get the class name from a class table") {
return LS.result();
}
static void get_reg_name(const LuaFunctionReg *reg, std::string_view &classname, std::string_view &funcname) {
std::string_view name(reg->get_name());
static void get_reg_name(std::string_view name, std::string_view &classname, std::string_view &funcname) {
size_t upos = name.find('_');
if (upos == std::string_view::npos) {
funcname = name;
@@ -280,7 +279,7 @@ static void source_load_cfunctions(lua_State *L) {
if ((func != nullptr) && (!r->get_sandbox())) {
std::string_view classname;
std::string_view funcname;
get_reg_name(r, classname, funcname);
get_reg_name(r->get_name(), classname, funcname);
if (classname.empty()) {
LS.getglobaltable(classobj);
LS.rawset(classobj, funcname, func);
@@ -293,6 +292,31 @@ static void source_load_cfunctions(lua_State *L) {
LS.result();
}
// Load all the 'LuaConstant' constants into the lua state.
//
static void source_load_cconstants(lua_State *L) {
LuaVar classobj, value;
LuaStack LS(L, classobj, value);
for (auto r = LuaConstantReg::All; r != nullptr; r=r->next()) {
if (r->get_tokenvalue().empty()) {
LS.set(value, r->get_numbervalue());
} else {
LS.set(value, r->get_tokenvalue());
}
std::string_view classname;
std::string_view funcname;
get_reg_name(r->get_name(), classname, funcname);
if (classname.empty()) {
LS.getglobaltable(classobj);
LS.rawset(classobj, funcname, value);
} else {
LS.makeclass(classobj, classname);
LS.rawset(classobj, funcname, value);
}
}
LS.result();
}
// Run all the closures from the source database.
//
static eng::string source_load_lfunctions(lua_State *L) {
@@ -341,18 +365,12 @@ static eng::string source_load_lfunctions(lua_State *L) {
eng::string SourceDB::rebuild() {
lua_State *L = lua_state_;
LuaVar mathclass;
LuaStack LS(L, mathclass);
LuaVar mathclass, httpclass, jsonnull;
LuaStack LS(L, mathclass, httpclass, jsonnull);
source_clear_globals(L);
source_load_cfunctions(L);
source_load_cconstants(L);
eng::string errs = source_load_lfunctions(L);
// A few builtin constants. These are hardwired.
LS.makeclass(mathclass, "math");
LS.rawset(mathclass, "pi", M_PI);
LS.rawset(mathclass, "huge", HUGE_VAL);
LS.rawset(mathclass, "maxint", LuaStack::MAXINT);
LS.result();
return errs;
}
@@ -466,7 +484,7 @@ void SourceDB::register_lua_builtins() {
for (auto reg = LuaFunctionReg::All; reg != nullptr; reg=reg->next()) {
std::string_view funcname;
std::string_view classname;
get_reg_name(reg, classname, funcname);
get_reg_name(reg->get_name(), classname, funcname);
if (classname.empty()) {
LS.getglobaltable(classtab);
} else {
@@ -524,7 +542,7 @@ eng::string SourceDB::function_docs(const LuaStack &LS0, LuaSlot fn) {
}
std::string_view classname;
std::string_view funcname;
get_reg_name(reg, classname, funcname);
get_reg_name(reg->get_name(), classname, funcname);
eng::ostringstream oss;
util::StringVec docs = util::split_docstring(reg->get_docs());
oss << "function ";
@@ -747,6 +765,11 @@ LuaDefineBuiltin(math_sqrt, "x", "return the square root of x");
LuaDefineBuiltin(math_tan, "x", "return the tangent of x in radians");
LuaDefineBuiltin(math_tanh, "x", "return the hyperbolic tangent of x in radians");
LuaSandboxBuiltin(math_log10, "", "");
LuaNumberConstant(math_pi, M_PI, "");
LuaNumberConstant(math_huge, HUGE_VAL, "");
LuaNumberConstant(math_nan, NAN, "");
LuaNumberConstant(math_maxint, LuaStack::MAXINT, "");
// math.random and math.randomseed are in world-accessor.cpp, because
// generating random numbers must manipulate global state which is
// stored in the world model.