God knows what's modified
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@@ -10,32 +11,10 @@
|
||||
#include "luastack.hpp"
|
||||
#include "table.hpp"
|
||||
#include "source.hpp"
|
||||
#include "traceback.hpp"
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// The source database is a lua table that maps filenames
|
||||
// to file info. A file info is a lua table containing:
|
||||
//
|
||||
// name: filename as a string
|
||||
// fingerprint: file modification time as human-readable string
|
||||
// code: the entire contents of the source file as a string
|
||||
// error: a syntax error message, or nil
|
||||
// hash: 128-bit hash of the code
|
||||
// closure: a lua closure, the compiled file
|
||||
// sequence: the position of the file in control.lst
|
||||
//
|
||||
// Operations on a source DB:
|
||||
//
|
||||
// source.peek()
|
||||
// - peek at the source database, for inspection purposes.
|
||||
// source.read()
|
||||
// - return a new source, reusing info from old.
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
LuaDefineHidden(source_updatefile) {
|
||||
LuaDefine(source_updatefile, "") {
|
||||
LuaArg source, fn;
|
||||
LuaRet info;
|
||||
LuaVar fingerprint, null;
|
||||
@@ -54,11 +33,11 @@ LuaDefineHidden(source_updatefile) {
|
||||
// If the file modification is wrong, update
|
||||
// these fields: code, fingerprint, closure, error
|
||||
// Otherwise, update nothing.
|
||||
std::string cfn = LS.tostring(fn);
|
||||
std::string cfn = LS.ckstring(fn);
|
||||
LS.getfield(fingerprint, info, "fingerprint");
|
||||
std::string old_fingerprint;
|
||||
if (LS.isstring(fingerprint)) {
|
||||
old_fingerprint = LS.tostring(fingerprint);
|
||||
old_fingerprint = LS.ckstring(fingerprint);
|
||||
}
|
||||
std::cerr << "Probing " << cfn << std::endl;
|
||||
std::string new_fingerprint = util::get_file_fingerprint("syslua/" + cfn);
|
||||
@@ -80,7 +59,7 @@ LuaDefineHidden(source_updatefile) {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefineHidden(source_update) {
|
||||
LuaDefine(source_update, "c") {
|
||||
LuaVar sourcedb, newdb, info, fn, seq;
|
||||
LuaStack LS(L, newdb, sourcedb, info, fn, seq);
|
||||
|
||||
@@ -116,98 +95,158 @@ LuaDefineHidden(source_update) {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
// Get a class from the class database.
|
||||
// Make a class. A class is a table in the global environment.
|
||||
//
|
||||
// CLASSNAME
|
||||
// if classname is already present, and is a table, return it.
|
||||
// if classname is already present, and not a table, error.
|
||||
// if classname is not present, create and initialize it.
|
||||
// The global environment is protected, but this function can
|
||||
// override that protection.
|
||||
//
|
||||
LuaDefineGlobalFunction(source_class) {
|
||||
LuaDefine(source_makeclass, "f") {
|
||||
LuaArg classname;
|
||||
LuaVar action, gname;
|
||||
LuaRet classtab;
|
||||
LuaVar classdb, action;
|
||||
LuaStack LS(L, classname, classtab, classdb, action);
|
||||
LuaStack LS(L, classname, classtab, action, gname);
|
||||
|
||||
LS.checktype(classname, LUA_TSTRING);
|
||||
LS.checkstring(classname);
|
||||
|
||||
// Get a pointer to the classdb.
|
||||
LS.getfield(classdb, LuaRegistry, "classdb");
|
||||
if (!LS.istable(classdb)) {
|
||||
LS.newtable(classdb);
|
||||
LS.setfield(LuaRegistry, "classdb", classdb);
|
||||
}
|
||||
|
||||
// Get the classtab from the classdb, sanity check it.
|
||||
LS.rawget(classtab, classdb, classname);
|
||||
if (LS.istable(classtab)) {
|
||||
// Special case: if the classname is _G, return global env.
|
||||
LS.set(gname, "_G");
|
||||
if (LS.equal(classname, gname)) {
|
||||
LS.set(classtab, LuaGlobals);
|
||||
return LS.result();
|
||||
} else if (!LS.isnil(classtab)) {
|
||||
luaL_error(L, "%s is not a class", LS.tostring(classname).c_str());
|
||||
}
|
||||
|
||||
// Get the classtab from the global environment.
|
||||
// Create it if it doesn't exist.
|
||||
LS.rawget(classtab, LuaGlobals, classname);
|
||||
if (LS.isnil(classtab)) {
|
||||
LS.newtable(classtab);
|
||||
LS.rawset(LuaGlobals, classname, classtab);
|
||||
}
|
||||
|
||||
// Create a new classtab and store it in the classdb.
|
||||
LS.newtable(classtab);
|
||||
LS.rawset(classdb, classname, classtab);
|
||||
// If the name isn't bound to a table, abort.
|
||||
if (!LS.istable(classtab)) {
|
||||
luaL_error(L, "%s is not a class", LS.ckstring(classname).c_str());
|
||||
}
|
||||
|
||||
// Repair the special fields.
|
||||
LS.setfield(classtab, "__index", classtab);
|
||||
LS.setfield(classtab, "__class", classname);
|
||||
LS.newtable(action);
|
||||
LS.setfield(classtab, "action", action);
|
||||
|
||||
// Repair the action table.
|
||||
LS.getfield(action, classtab, "action");
|
||||
if (!LS.istable(action)) {
|
||||
LS.setfield(classtab, "action", LuaNewTable);
|
||||
}
|
||||
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
// Reset a class database:
|
||||
// Clear the global environment.
|
||||
//
|
||||
// Clear out all classes. Instead of replacing the class tables,
|
||||
// it simply deletes all keys. That way, if somebody has a pointer
|
||||
// to a class, the pointer is not invalidated.
|
||||
// Clears out almost everything from the global
|
||||
// environment. However, does not delete class tables.
|
||||
//
|
||||
LuaDefineHidden(source_reset_classes) {
|
||||
LuaVar classdb, classname, classtab, action, key;
|
||||
LuaStack LS(L, classname, classtab, classdb, action, key);
|
||||
// This is used during source_rebuild operations.
|
||||
//
|
||||
LuaDefine(source_clear_globals, "") {
|
||||
LuaVar classname, classtab, action, key;
|
||||
LuaStack LS(L, classname, classtab, action, key);
|
||||
|
||||
// Get a pointer to the classdb.
|
||||
LS.getfield(classdb, LuaRegistry, "classdb");
|
||||
if (!LS.istable(classdb)) {
|
||||
LS.newtable(classdb);
|
||||
LS.setfield(LuaRegistry, "classdb", classdb);
|
||||
}
|
||||
|
||||
// Iterate over the classdb, clearing it.
|
||||
LS.setfield(LuaGlobals, "_G", LuaNil);
|
||||
LS.set(classname, LuaNil);
|
||||
while (LS.next(classdb, classname, classtab) != 0) {
|
||||
while (LS.next(LuaGlobals, classname, classtab) != 0) {
|
||||
if (LS.istable(classtab)) {
|
||||
LS.set(key, "action");
|
||||
LS.rawget(action, classtab, key);
|
||||
LS.getfield(action, classtab, "action");
|
||||
if (LS.istable(action)) {
|
||||
LS.call(table_clear, action);
|
||||
} else {
|
||||
LS.newtable(action);
|
||||
}
|
||||
LS.call(table_clear, classtab);
|
||||
LS.setfield(classtab, "__index", classtab);
|
||||
LS.setfield(classtab, "__class", classname);
|
||||
LS.setfield(classtab, "action", action);
|
||||
} else {
|
||||
LS.rawset(LuaGlobals, classname, LuaNil);
|
||||
}
|
||||
}
|
||||
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
// Restore the lua builtins from the backup snapshot.
|
||||
//
|
||||
// The global environment is expected to be clean, but
|
||||
// the subtables are expected to still be present, in
|
||||
// accordance with how 'source_clear_globals' works.
|
||||
//
|
||||
LuaDefine(source_restore_builtins, "g") {
|
||||
LuaVar snapshot, key, value, skey, svalue, subglobal;
|
||||
LuaStack LS(L, snapshot, key, value, skey, svalue, subglobal);
|
||||
|
||||
LS.getfield(snapshot, LuaRegistry, "source_snapshot_builtins");
|
||||
LS.setfield(LuaGlobals, "_G", LuaGlobals);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(snapshot, key, value) != 0) {
|
||||
LS.checktable(value);
|
||||
LS.call(subglobal, source_makeclass, key);
|
||||
LS.set(skey, LuaNil);
|
||||
while (LS.next(value, skey, svalue) != 0) {
|
||||
LS.rawset(subglobal, skey, svalue);
|
||||
}
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefineHidden(source_load_builtins) {
|
||||
luaopen_base(L);
|
||||
luaopen_table(L);
|
||||
luaopen_string(L);
|
||||
luaopen_math(L);
|
||||
luaopen_bit(L);
|
||||
// luaopen_package(L); // Omitted because we use our own package system.
|
||||
// luaopen_io(L); // Not safe for the sandbox.
|
||||
// luaopen_os(L); // Not safe for the sandbox.
|
||||
// luaopen_debug(L); // Not safe for the sandbox.
|
||||
// luaopen_jit(L); // Don't know what it's for.
|
||||
// Snapshot all the lua builtin functions.
|
||||
//
|
||||
// Precondition: this is meant to be used on a pristine lua
|
||||
// intepreter, before the user dumps a bunch of crap into the
|
||||
// global environment. It won't work if the global environment
|
||||
// contains anything other than the lua builtins.
|
||||
//
|
||||
// Note: the global environment contains _G. This routine
|
||||
// perceives this as a subtable, so it backs up the top level
|
||||
// as well.
|
||||
//
|
||||
LuaDefine(source_snapshot_builtins, "c") {
|
||||
LuaVar key, value, skey, svalue, snapshot, ssnapshot;
|
||||
LuaStack LS(L, snapshot, key, value, skey, svalue, ssnapshot);
|
||||
|
||||
LS.newtable(snapshot);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(LuaGlobals, key, value) != 0) {
|
||||
if (LS.istable(value)) {
|
||||
LS.newtable(ssnapshot);
|
||||
LS.rawset(snapshot, key, ssnapshot);
|
||||
LS.set(skey, LuaNil);
|
||||
while (LS.next(value, skey, svalue) != 0) {
|
||||
if (LS.isfunction(svalue) || LS.isstring(svalue) || LS.isnumber(svalue)) {
|
||||
LS.rawset(ssnapshot, skey, svalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LS.setfield(LuaRegistry, "source_snapshot_builtins", snapshot);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
static void load_builtin(lua_State *L, const char *name, lua_CFunction func) {
|
||||
lua_pushcfunction(L, func);
|
||||
lua_pushstring(L, name);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
|
||||
LuaDefine(source_load_builtins, "") {
|
||||
LuaStack LS(L);
|
||||
load_builtin(L, "base", luaopen_base);
|
||||
load_builtin(L, "table", luaopen_table);
|
||||
load_builtin(L, "string", luaopen_string);
|
||||
load_builtin(L, "math", luaopen_math);
|
||||
load_builtin(L, "bit", luaopen_math);
|
||||
load_builtin(L, "debug", luaopen_debug);
|
||||
// Do not load: package, io, os, debug, jit
|
||||
return 0;
|
||||
}
|
||||
|
||||
LuaDefineHidden(source_load_cfunctions) {
|
||||
LuaDefine(source_load_cfunctions, "") {
|
||||
auto regs = LuaFunctionReg::all();
|
||||
for (const LuaFunctionReg *r : regs) {
|
||||
const std::string &name = r->get_name();
|
||||
@@ -221,27 +260,14 @@ LuaDefineHidden(source_load_cfunctions) {
|
||||
classname = name.substr(0, upos);
|
||||
}
|
||||
lua_CFunction func = r->get_func();
|
||||
int mode = r->get_mode();
|
||||
if (mode == 3) { // Class Method
|
||||
std::string mode = r->get_mode();
|
||||
if (mode.find('c') != std::string::npos) { // Insert into class
|
||||
lua_pushlstring(L, classname.c_str(), classname.size());
|
||||
source_class(L);
|
||||
source_makeclass(L);
|
||||
lua_pushcfunction(L, func);
|
||||
lua_setfield(L, -2, funcname.c_str());
|
||||
}
|
||||
if ((mode == 1) || (mode == 2)) { // Global function or global method
|
||||
int top = lua_gettop(L);
|
||||
lua_getglobal(L, classname.c_str());
|
||||
if (!lua_istable(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
lua_newtable(L);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setglobal(L, classname.c_str());
|
||||
}
|
||||
lua_pushcfunction(L, func);
|
||||
lua_setfield(L, -2, funcname.c_str());
|
||||
lua_settop(L, top);
|
||||
}
|
||||
if (mode == 1) { // Global function
|
||||
if (mode.find('f') != std::string::npos) { // Make global function
|
||||
lua_pushcfunction(L, func);
|
||||
lua_setglobal(L, funcname.c_str());
|
||||
}
|
||||
@@ -254,7 +280,7 @@ LuaDefineHidden(source_load_cfunctions) {
|
||||
// Returns a single string, which is a bunch of concatenated error
|
||||
// messages.
|
||||
//
|
||||
LuaDefineHidden(source_load_lfunctions) {
|
||||
LuaDefine(source_load_lfunctions, "") {
|
||||
LuaRet errors;
|
||||
LuaVar sourcedb, key, info, seq, closure, err;
|
||||
LuaStack LS(L, sourcedb, errors, key, info, seq, closure, err);
|
||||
@@ -271,7 +297,7 @@ LuaDefineHidden(source_load_lfunctions) {
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(sourcedb, key, info) != 0) {
|
||||
LS.getfield(seq, info, "sequence");
|
||||
indices[LS.tointeger(seq)] = LS.tostring(key);
|
||||
indices[LS.ckinteger(seq)] = LS.ckstring(key);
|
||||
}
|
||||
|
||||
// Now call the closures in the proper order.
|
||||
@@ -282,31 +308,41 @@ LuaDefineHidden(source_load_lfunctions) {
|
||||
|
||||
// If there's already an error in the sourcedb, collect it.
|
||||
if (!LS.isfunction(closure)) {
|
||||
errss << LS.tostring(closure);
|
||||
errss << LS.ckstring(closure) << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// Call the closure. If there's an error, collect it.
|
||||
lua_pushvalue(L, closure.index());
|
||||
if (lua_pcall(L, 0, 0, 0) != 0) {
|
||||
if (traceback_pcall(L, 0, 0) != 0) {
|
||||
lua_replace(L, err.index());
|
||||
errss << LS.tostring(err);
|
||||
errss << LS.ckstring(err);
|
||||
}
|
||||
}
|
||||
LS.set(errors, errss.str());
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefineGlobalMethod(source_rebuild) {
|
||||
LuaDefine(source_rebuild, "c") {
|
||||
LuaVar errs;
|
||||
LuaStack LS(L, errs);
|
||||
source_reset_classes(L);
|
||||
source_load_builtins(L);
|
||||
source_clear_globals(L);
|
||||
source_restore_builtins(L);
|
||||
source_load_cfunctions(L);
|
||||
source_load_lfunctions(L);
|
||||
lua_replace(L, errs.index());
|
||||
std::string errstr = LS.tostring(errs);
|
||||
std::string errstr = LS.ckstring(errs);
|
||||
std::cerr << errstr;
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefine(source_autoinit, "") {
|
||||
auto regs = LuaFunctionReg::all();
|
||||
for (const LuaFunctionReg *r : regs) {
|
||||
std::string mode = r->get_mode();
|
||||
if (mode.find('a') != std::string::npos) {
|
||||
r->get_func()(L);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user