Completed port to eris lua 5.2
This commit is contained in:
@@ -50,54 +50,40 @@ LuaDefine(source_maketangible, "f") {
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
// Load the builtins.
|
||||
|
||||
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);
|
||||
// the 'luaopen' function creates a new table.
|
||||
//
|
||||
// We don't want to create new tables during reload, so we call luaopen,
|
||||
// then we copy the contents of the new table into the existing 'makeclass'
|
||||
// table.
|
||||
//
|
||||
static void load_builtin_class(lua_State *L, const char *name, lua_CFunction func) {
|
||||
LuaVar sourcetab, classtab, key, value;
|
||||
LuaStack LS(L, sourcetab, classtab, key, value);
|
||||
LS.makeclass(classtab, name);
|
||||
func(L);
|
||||
lua_replace(L, sourcetab.index());
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(sourcetab, key, value) != 0) {
|
||||
LS.rawset(classtab, key, value);
|
||||
}
|
||||
}
|
||||
|
||||
static void source_install_builtins(lua_State *L) {
|
||||
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);
|
||||
}
|
||||
LuaVar nullstring, stringclass;
|
||||
LuaStack LS(L, nullstring, stringclass);
|
||||
luaopen_base(L);
|
||||
load_builtin_class(L, "table", luaopen_table);
|
||||
load_builtin_class(L, "string", luaopen_string);
|
||||
load_builtin_class(L, "math", luaopen_math);
|
||||
load_builtin_class(L, "debug", luaopen_debug);
|
||||
|
||||
void SourceDB::initialize(lua_State *L) {
|
||||
lua_state_ = L;
|
||||
LuaVar key, value, skey, svalue, snapshot, ssnapshot, globtab;
|
||||
LuaStack LS(L, snapshot, key, value, skey, svalue, ssnapshot, globtab);
|
||||
|
||||
// Install the builtins.
|
||||
source_install_builtins(L);
|
||||
|
||||
// Get the global environment.
|
||||
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
|
||||
|
||||
// Note: the global environment contains _G. This routine
|
||||
// perceives this as a subtable, so it backs up the top level
|
||||
// as well.
|
||||
LS.newtable(snapshot);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(globtab, 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.rawset(LuaRegistry, "source_snapshot_builtins", snapshot);
|
||||
LS.result();
|
||||
// Set the metatable for strings.
|
||||
// Normally, this would be done by luaopen_string, but we're
|
||||
// messing with the tables so we have to redo it.
|
||||
LS.makeclass(stringclass, "string");
|
||||
LS.set(nullstring, "");
|
||||
LS.setmetatable(nullstring, stringclass);
|
||||
}
|
||||
|
||||
static int source_updatefile(lua_State *L) {
|
||||
@@ -189,9 +175,7 @@ static void source_clear_globals(lua_State *L) {
|
||||
LuaVar classname, classtab, key, globtab;
|
||||
LuaStack LS(L, classname, classtab, key, globtab);
|
||||
|
||||
// Get the global environment.
|
||||
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
|
||||
|
||||
LS.getglobaltable(globtab);
|
||||
LS.rawset(globtab, "_G", LuaNil);
|
||||
LS.set(classname, LuaNil);
|
||||
while (LS.next(globtab, classname, classtab) != 0) {
|
||||
@@ -205,32 +189,6 @@ static void source_clear_globals(lua_State *L) {
|
||||
LS.result();
|
||||
}
|
||||
|
||||
// Restore the lua builtins from the backup snapshot.
|
||||
//
|
||||
static void source_restore_builtins(lua_State *L) {
|
||||
LuaVar snapshot, key, value, skey, svalue, target, globtab;
|
||||
LuaStack LS(L, snapshot, key, value, skey, svalue, target, globtab);
|
||||
|
||||
// Get the global environment table.
|
||||
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
|
||||
|
||||
LS.rawget(snapshot, LuaRegistry, "source_snapshot_builtins");
|
||||
LS.rawset(globtab, "_G", globtab);
|
||||
LS.set(key, LuaNil);
|
||||
while (LS.next(snapshot, key, value) != 0) {
|
||||
LS.checktable(value);
|
||||
if (LS.rawequal(key, "_G")) {
|
||||
LS.set(target, globtab);
|
||||
} else {
|
||||
LS.makeclass(target, key);
|
||||
}
|
||||
LS.set(skey, LuaNil);
|
||||
while (LS.next(value, skey, svalue) != 0) {
|
||||
LS.rawset(target, skey, svalue);
|
||||
}
|
||||
}
|
||||
LS.result();
|
||||
}
|
||||
|
||||
// Load all the 'LuaDefine' C functions into the lua state.
|
||||
//
|
||||
@@ -312,7 +270,7 @@ void SourceDB::rebuild() {
|
||||
LuaVar errs;
|
||||
LuaStack LS(L, errs);
|
||||
source_clear_globals(L);
|
||||
source_restore_builtins(L);
|
||||
source_install_builtins(L);
|
||||
source_load_cfunctions(L);
|
||||
source_load_lfunctions(L);
|
||||
lua_replace(L, errs.index());
|
||||
@@ -326,7 +284,7 @@ void SourceDB::run_unittests() {
|
||||
LuaVar unittests, name, func, err, globtab;
|
||||
LuaStack LS(L, unittests, name, func, err, globtab);
|
||||
|
||||
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
|
||||
LS.getglobaltable(globtab);
|
||||
LS.rawget(unittests, globtab, "unittests");
|
||||
|
||||
// Sort the unit test names.
|
||||
|
||||
Reference in New Issue
Block a user