Changed calling conventions again
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include "util.hpp"
|
||||
#include "table.hpp"
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "luastack.hpp"
|
||||
#ifdef WIN32
|
||||
#define stat _stat
|
||||
#endif
|
||||
#include "table.hpp"
|
||||
#include "source.hpp"
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
@@ -23,7 +19,7 @@
|
||||
// to file info. A file info is a lua table containing:
|
||||
//
|
||||
// name: filename as a string
|
||||
// mtime: file modification time as human-readable 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
|
||||
@@ -39,29 +35,11 @@
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
static std::string get_mtime(const std::string &fn) {
|
||||
struct stat result;
|
||||
if(stat(fn.c_str(), &result)==0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << result.st_mtime;
|
||||
return ss.str();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
static std::string get_code(const std::string &fn) {
|
||||
std::ifstream fs(fn);
|
||||
std::stringstream buffer;
|
||||
buffer << fs.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
int lpx_source_updatefile(lua_State *L) {
|
||||
LuaDefineHidden(source_updatefile) {
|
||||
LuaArg source, fn;
|
||||
LuaRet info;
|
||||
LuaVar mtime, null;
|
||||
LuaStack LS(L, source, fn, info, mtime, null);
|
||||
LuaVar fingerprint, null;
|
||||
LuaStack LS(L, source, fn, info, fingerprint, null);
|
||||
|
||||
// Get the existing info table from the source DB.
|
||||
if (LS.istable(source)) {
|
||||
@@ -74,42 +52,43 @@ int lpx_source_updatefile(lua_State *L) {
|
||||
}
|
||||
|
||||
// If the file modification is wrong, update
|
||||
// these fields: code, mtime, closure, error
|
||||
// these fields: code, fingerprint, closure, error
|
||||
// Otherwise, update nothing.
|
||||
std::string cfn = LS.tostring(fn);
|
||||
LS.getfield(mtime, info, "mtime");
|
||||
std::string old_mtime;
|
||||
if (LS.isstring(mtime)) {
|
||||
old_mtime = LS.tostring(mtime);
|
||||
LS.getfield(fingerprint, info, "fingerprint");
|
||||
std::string old_fingerprint;
|
||||
if (LS.isstring(fingerprint)) {
|
||||
old_fingerprint = LS.tostring(fingerprint);
|
||||
}
|
||||
std::cerr << "Probing " << cfn << std::endl;
|
||||
std::string new_mtime = get_mtime("syslua/" + cfn);
|
||||
LS.setnil(null);
|
||||
if ((old_mtime == "") || (old_mtime != new_mtime)) {
|
||||
std::string new_fingerprint = util::get_file_fingerprint("syslua/" + cfn);
|
||||
LS.set(null, LuaNil);
|
||||
if ((old_fingerprint == "") || (old_fingerprint != new_fingerprint)) {
|
||||
std::cerr << "Rereading " << cfn << std::endl;
|
||||
std::string ccode = get_code("syslua/" + cfn);
|
||||
std::string ccode = util::get_file_contents("syslua/" + cfn);
|
||||
LS.setfield(info, "name", fn);
|
||||
LS.setfield(info, "mtime", new_mtime);
|
||||
LS.setfield(info, "fingerprint", new_fingerprint);
|
||||
LS.setfield(info, "code", ccode);
|
||||
if ((new_mtime == "")||(ccode == "")) {
|
||||
LS.setfield(info, "error", "cannot read source file");
|
||||
LS.setfield(info, "closure", null);
|
||||
} else if (luaL_loadbuffer(L, ccode.c_str(), ccode.size(), cfn.c_str()) == 0) {
|
||||
lua_setfield(L, info.index(), "closure");
|
||||
LS.setfield(info, "error", null);
|
||||
if ((new_fingerprint == "")||(ccode == "")) {
|
||||
LS.setfield(info, "loadresult", "cannot read source file");
|
||||
} else {
|
||||
lua_setfield(L, info.index(), "error");
|
||||
LS.setfield(info, "closure", null);
|
||||
std::string chunk = "=" + cfn;
|
||||
luaL_loadbuffer(L, ccode.c_str(), ccode.size(), chunk.c_str());
|
||||
lua_setfield(L, info.index(), "loadresult");
|
||||
}
|
||||
}
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
int lpx_source_updateall(lua_State *L) {
|
||||
LuaArg source;
|
||||
LuaRet newdb;
|
||||
LuaVar info, fn, seq;
|
||||
LuaStack LS(L, source, newdb, info, fn, seq);
|
||||
LuaDefineHidden(source_update) {
|
||||
LuaVar sourcedb, newdb, info, fn, seq;
|
||||
LuaStack LS(L, newdb, sourcedb, info, fn, seq);
|
||||
|
||||
// Get the (old) source database.
|
||||
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (!LS.istable(sourcedb)) {
|
||||
LS.newtable(sourcedb);
|
||||
}
|
||||
|
||||
// Read the list of filenames.
|
||||
std::string ctrl = "syslua/control.lst";
|
||||
@@ -120,20 +99,20 @@ int lpx_source_updateall(lua_State *L) {
|
||||
|
||||
// Process the files one by one.
|
||||
LS.newtable(newdb);
|
||||
for (int i = 0; i < filenames.size(); i++) {
|
||||
LS.setstring(fn, filenames[i]);
|
||||
for (int i = 0; i < int(filenames.size()); i++) {
|
||||
LS.set(fn, filenames[i]);
|
||||
|
||||
// Call source_updatefile to get the updated info for one file.
|
||||
lua_pushvalue(L, source.index());
|
||||
lua_pushvalue(L, fn.index());
|
||||
lpx_source_updatefile(L);
|
||||
lua_replace(L, info.index());
|
||||
LS.call(info, source_updatefile, sourcedb, fn);
|
||||
|
||||
// Insert the sequence number and put finalized info into the new database.
|
||||
LS.setnumber(seq, i + 1);
|
||||
LS.set(seq, i + 1);
|
||||
LS.setfield(info, "sequence", seq);
|
||||
LS.rawset(newdb, fn, info);
|
||||
}
|
||||
|
||||
// Store the new source db.
|
||||
LS.setfield(LuaRegistry, "sourcedb", newdb);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
@@ -144,7 +123,7 @@ int lpx_source_updateall(lua_State *L) {
|
||||
// if classname is already present, and not a table, error.
|
||||
// if classname is not present, create and initialize it.
|
||||
//
|
||||
int lpx_source_class(lua_State *L) {
|
||||
LuaDefineGlobalFunction(source_class) {
|
||||
LuaArg classname;
|
||||
LuaRet classtab;
|
||||
LuaVar classdb, action;
|
||||
@@ -153,10 +132,10 @@ int lpx_source_class(lua_State *L) {
|
||||
LS.checktype(classname, LUA_TSTRING);
|
||||
|
||||
// Get a pointer to the classdb.
|
||||
LS.getfield(classdb, LUA_REGISTRYINDEX, "classdb");
|
||||
LS.getfield(classdb, LuaRegistry, "classdb");
|
||||
if (!LS.istable(classdb)) {
|
||||
LS.newtable(classdb);
|
||||
LS.setfield(LUA_REGISTRYINDEX, "classdb", classdb);
|
||||
LS.setfield(LuaRegistry, "classdb", classdb);
|
||||
}
|
||||
|
||||
// Get the classtab from the classdb, sanity check it.
|
||||
@@ -183,31 +162,29 @@ int lpx_source_class(lua_State *L) {
|
||||
// it simply deletes all keys. That way, if somebody has a pointer
|
||||
// to a class, the pointer is not invalidated.
|
||||
//
|
||||
int lpx_source_resetclasses(lua_State *L) {
|
||||
LuaDefineHidden(source_reset_classes) {
|
||||
LuaVar classdb, classname, classtab, action, key;
|
||||
LuaStack LS(L, classname, classtab, classdb, action, key);
|
||||
|
||||
// Get a pointer to the classdb.
|
||||
LS.getfield(classdb, LUA_REGISTRYINDEX, "classdb");
|
||||
LS.getfield(classdb, LuaRegistry, "classdb");
|
||||
if (!LS.istable(classdb)) {
|
||||
LS.newtable(classdb);
|
||||
LS.setfield(LUA_REGISTRYINDEX, "classdb", classdb);
|
||||
LS.setfield(LuaRegistry, "classdb", classdb);
|
||||
}
|
||||
|
||||
// Iterate over the classdb, clearing it.
|
||||
LS.setnil(classname);
|
||||
LS.set(classname, LuaNil);
|
||||
while (LS.next(classdb, classname, classtab) != 0) {
|
||||
if (LS.istable(classtab)) {
|
||||
LS.setstring(key, "action");
|
||||
LS.set(key, "action");
|
||||
LS.rawget(action, classtab, key);
|
||||
if (LS.istable(action)) {
|
||||
lua_pushvalue(L, action.index());
|
||||
lpx_table_clear(L);
|
||||
LS.call(table_clear, action);
|
||||
} else {
|
||||
LS.newtable(action);
|
||||
}
|
||||
lua_pushvalue(L, classtab.index());
|
||||
lpx_table_clear(L);
|
||||
LS.call(table_clear, classtab);
|
||||
LS.setfield(classtab, "__index", classtab);
|
||||
LS.setfield(classtab, "__class", classname);
|
||||
LS.setfield(classtab, "action", action);
|
||||
@@ -216,10 +193,120 @@ int lpx_source_resetclasses(lua_State *L) {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
void luaopen_lpx_source(lua_State *L) {
|
||||
LuaStack::reg(L, "source", "updatefile", LuaArgCheck<2, lpx_source_updatefile>);
|
||||
LuaStack::reg(L, "source", "updateall", LuaArgCheck<1, lpx_source_updateall>);
|
||||
LuaStack::reg(L, "source", "class", LuaArgCheck<1, lpx_source_class>);
|
||||
LuaStack::reg(L, "source", "resetclasses", LuaArgCheck<0, lpx_source_resetclasses>);
|
||||
LuaStack::reg(L, 0 , "class", LuaArgCheck<1, lpx_source_class>);
|
||||
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.
|
||||
return 0;
|
||||
}
|
||||
|
||||
LuaDefineHidden(source_load_cfunctions) {
|
||||
auto regs = LuaFunctionReg::all();
|
||||
for (const LuaFunctionReg *r : regs) {
|
||||
const std::string &name = r->get_name();
|
||||
size_t upos = name.find('_');
|
||||
std::string classname;
|
||||
std::string funcname;
|
||||
if (upos == std::string::npos) {
|
||||
continue;
|
||||
} else {
|
||||
funcname = name.substr(upos + 1);
|
||||
classname = name.substr(0, upos);
|
||||
}
|
||||
lua_CFunction func = r->get_func();
|
||||
int mode = r->get_mode();
|
||||
if (mode == 3) { // Class Method
|
||||
lua_pushlstring(L, classname.c_str(), classname.size());
|
||||
source_class(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
|
||||
lua_pushcfunction(L, func);
|
||||
lua_setglobal(L, funcname.c_str());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Fetches the source database and runs all the loadresults.
|
||||
//
|
||||
// Returns a single string, which is a bunch of concatenated error
|
||||
// messages.
|
||||
//
|
||||
LuaDefineHidden(source_load_lfunctions) {
|
||||
LuaRet errors;
|
||||
LuaVar sourcedb, key, info, seq, closure, err;
|
||||
LuaStack LS(L, sourcedb, errors, key, info, seq, closure, err);
|
||||
|
||||
// Get the source database.
|
||||
LS.getfield(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (LS.type(sourcedb) != LUA_TTABLE) {
|
||||
LS.newtable(sourcedb);
|
||||
LS.setfield(LuaRegistry, "sourcedb", sourcedb);
|
||||
}
|
||||
|
||||
// Sort the keys by sequence number.
|
||||
std::map<int, std::string> indices;
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(sourcedb, key, info) != 0) {
|
||||
LS.getfield(seq, info, "sequence");
|
||||
indices[LS.tointeger(seq)] = LS.tostring(key);
|
||||
}
|
||||
|
||||
// Now call the closures in the proper order.
|
||||
std::stringstream errss;
|
||||
for (const auto &p : indices) {
|
||||
LS.rawget(info, sourcedb, p.second);
|
||||
LS.getfield(closure, info, "loadresult");
|
||||
|
||||
// If there's already an error in the sourcedb, collect it.
|
||||
if (!LS.isfunction(closure)) {
|
||||
errss << LS.tostring(closure);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Call the closure. If there's an error, collect it.
|
||||
lua_pushvalue(L, closure.index());
|
||||
if (lua_pcall(L, 0, 0, 0) != 0) {
|
||||
lua_replace(L, err.index());
|
||||
errss << LS.tostring(err);
|
||||
}
|
||||
}
|
||||
LS.set(errors, errss.str());
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
LuaDefineGlobalMethod(source_rebuild) {
|
||||
LuaVar errs;
|
||||
LuaStack LS(L, errs);
|
||||
source_reset_classes(L);
|
||||
source_load_builtins(L);
|
||||
source_load_cfunctions(L);
|
||||
source_load_lfunctions(L);
|
||||
lua_replace(L, errs.index());
|
||||
std::string errstr = LS.tostring(errs);
|
||||
std::cerr << errstr;
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user