From 95e5a999b8bd12c0d11497d33cc74ffbffe71ddf Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Sun, 28 Feb 2021 15:05:45 -0500 Subject: [PATCH] Compiles with 5.2, but doesn't work. --- luprex/core/cpp/luasnap.cpp | 35 +++++--------------------- luprex/core/cpp/luastack.hpp | 1 + luprex/core/cpp/source.cpp | 48 ++++++++++++++++++++++-------------- luprex/core/cpp/world.cpp | 2 +- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/luprex/core/cpp/luasnap.cpp b/luprex/core/cpp/luasnap.cpp index 0335daf8..2b3dfb90 100644 --- a/luprex/core/cpp/luasnap.cpp +++ b/luprex/core/cpp/luasnap.cpp @@ -3,27 +3,6 @@ #include #include -extern "C" { -void *lj_alloc_create(); -void *lj_alloc_f(void *, void *, size_t, size_t); -lua_State *lj_state_newstate(lua_Alloc f, void *ud); -} - -static void *lsnap_create() { - return lj_alloc_create(); -} -static lua_State *lsnap_newstate(lua_Alloc f, void *ud) { - return lj_state_newstate(f, ud); -} -static void *lsnap_malloc(void *mptr, size_t nsize) { - return lj_alloc_f(mptr, 0, 0, nsize); -} -static void *lsnap_realloc(void *mptr, void *ptr, size_t osize, size_t nsize) { - return lj_alloc_f(mptr, ptr, osize, nsize); -} -static void *lsnap_free(void *mptr, size_t osize, void *ptr) { - return lj_alloc_f(mptr, ptr, osize, 0); -} struct BlockHeader { BlockHeader *prev_; @@ -50,7 +29,6 @@ class LuaSnapData { public: lua_State *state_; bool have_snapshot_; - void *lj_mstate_; BlockHeader sentinel_; LuaSnapData(); @@ -74,9 +52,8 @@ LuaSnapData::LuaSnapData() { sentinel_.size_ = 0; sentinel_.in_use_ = false; sentinel_.snapshot_ = 0; - lj_mstate_ = lsnap_create(); have_snapshot_ = false; - state_ = lsnap_newstate(lsd_alloc_f, (void*)this); + state_ = lua_newstate(lsd_alloc_f, (void*)this); } LuaSnapData::~LuaSnapData() { @@ -106,7 +83,7 @@ void LuaSnapData::rollback() { } else { assert(blk->in_use_); unlink(blk); - lsnap_free(lj_mstate_, blocksize(blk->size_), blk); + free(blk); } blk = next; } @@ -137,13 +114,13 @@ void *LuaSnapData::allocf(void *ptr, size_t osize, size_t nsize) { assert(blk->in_use_); if (blk->snapshot_ == nullptr) { unlink(blk); - lsnap_free(lj_mstate_, blocksize(blk->size_), blk); + free(blk); } else { blk->in_use_ = false; } return nullptr; } else if (ptr == NULL) { - BlockHeader *blk = (BlockHeader*)lsnap_malloc(lj_mstate_, blocksize(nsize)); + BlockHeader *blk = (BlockHeader*)malloc(blocksize(nsize)); link(blk); blk->size_ = nsize; blk->in_use_ = true; @@ -155,12 +132,12 @@ void *LuaSnapData::allocf(void *ptr, size_t osize, size_t nsize) { assert(blk->in_use_); if (blk->snapshot_ == nullptr) { unlink(blk); - blk = (BlockHeader*)lsnap_realloc(lj_mstate_, blk, blocksize(blk->size_), blocksize(nsize)); + blk = (BlockHeader*)realloc(blk, blocksize(nsize)); blk->size_ = nsize; link(blk); return header_to_pointer(blk); } else { - BlockHeader *nblk = (BlockHeader*)lsnap_malloc(lj_mstate_, blocksize(nsize)); + BlockHeader *nblk = (BlockHeader*)malloc(blocksize(nsize)); memcpy(nblk, blk, (blk->size_ < nsize) ? blk->size_ : nsize); blk->in_use_ = false; link(nblk); diff --git a/luprex/core/cpp/luastack.hpp b/luprex/core/cpp/luastack.hpp index f4c4d380..e934b85e 100644 --- a/luprex/core/cpp/luastack.hpp +++ b/luprex/core/cpp/luastack.hpp @@ -157,6 +157,7 @@ extern "C" { #include "lua.h" #include "lauxlib.h" +#include "lualib.h" } #include diff --git a/luprex/core/cpp/source.cpp b/luprex/core/cpp/source.cpp index 4a52ac0c..d6093016 100644 --- a/luprex/core/cpp/source.cpp +++ b/luprex/core/cpp/source.cpp @@ -70,16 +70,21 @@ static void source_install_builtins(lua_State *L) { void SourceDB::initialize(lua_State *L) { lua_state_ = L; - LuaVar key, value, skey, svalue, snapshot, ssnapshot; - LuaStack LS(L, snapshot, key, value, skey, svalue, ssnapshot); + 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. - source_install_builtins(L); LS.newtable(snapshot); LS.set(key, LuaNil); - while (LS.next(LuaGlobals, key, value) != 0) { + while (LS.next(globtab, key, value) != 0) { if (LS.istable(value)) { LS.newtable(ssnapshot); LS.rawset(snapshot, key, ssnapshot); @@ -181,35 +186,41 @@ void SourceDB::update() { // the class tables. // static void source_clear_globals(lua_State *L) { - LuaVar classname, classtab, key; - LuaStack LS(L, classname, classtab, key); + LuaVar classname, classtab, key, globtab; + LuaStack LS(L, classname, classtab, key, globtab); - LS.rawset(LuaGlobals, "_G", LuaNil); + // Get the global environment. + LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS); + + LS.rawset(globtab, "_G", LuaNil); LS.set(classname, LuaNil); - while (LS.next(LuaGlobals, classname, classtab) != 0) { + while (LS.next(globtab, classname, classtab) != 0) { if (LS.istable(classtab)) { LS.call(table_clear, classtab); } else { - LS.rawset(LuaGlobals, classname, LuaNil); + LS.rawset(globtab, classname, LuaNil); } } - LS.rawset(LuaGlobals, "_G", LuaGlobals); + LS.rawset(globtab, "_G", globtab); 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; - LuaStack LS(L, snapshot, key, value, skey, svalue, target); + 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(LuaGlobals, "_G", LuaGlobals); + LS.rawset(globtab, "_G", globtab); LS.set(key, LuaNil); while (LS.next(snapshot, key, value) != 0) { LS.checktable(value); - if (LS.equal(key, "_G")) { - LS.set(target, LuaGlobals); + if (LS.rawequal(key, "_G")) { + LS.set(target, globtab); } else { LS.makeclass(target, key); } @@ -312,10 +323,11 @@ void SourceDB::rebuild() { void SourceDB::run_unittests() { lua_State *L = lua_state_; - LuaVar unittests, name, func, err; - LuaStack LS(L, unittests, name, func, err); + LuaVar unittests, name, func, err, globtab; + LuaStack LS(L, unittests, name, func, err, globtab); - LS.rawget(unittests, LuaGlobals, "unittests"); + LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS); + LS.rawget(unittests, globtab, "unittests"); // Sort the unit test names. std::set names; diff --git a/luprex/core/cpp/world.cpp b/luprex/core/cpp/world.cpp index e272aa26..93b1ac87 100644 --- a/luprex/core/cpp/world.cpp +++ b/luprex/core/cpp/world.cpp @@ -323,7 +323,7 @@ void World::run_scheduled_threads(int64_t clk) { // Resume the coroutine. lua_State *CO = LS.ckthread(thread); int top = lua_gettop(CO); - int status = lua_resume(CO, (top > 0) ? (top - 1) : 0); + int status = lua_resume(CO, nullptr, (top > 0) ? (top - 1) : 0); // Three possible outcomes: finished, yielded, or errored. if (status == LUA_YIELD) {