fix an issue that global table is not applied in lua's main thread

This commit is contained in:
falsycat 2022-09-26 12:27:49 +09:00
parent f77a60831c
commit 5ef347fa2e
2 changed files with 18 additions and 13 deletions

View File

@ -23,8 +23,6 @@ lua_State* Thread::Init(lua_State* L) noexcept {
assert(state_ == kInitial);
th_ = lua_newthread(L);
PushImmEnv(L);
lua_setfenv(L, -2);
th_ref_.emplace(ctx_, ljq_, L);
state_ = kPaused;
@ -36,24 +34,24 @@ void Thread::Resume(lua_State* L, int narg) noexcept {
if (state_ == kAborted) return;
assert(L == th_);
assert(state_ == kPaused);
(void) L;
static const auto kHook = [](auto L, auto) {
luaL_error(L, "reached instruction limit (<=1e7)");
};
lua_sethook(th_, kHook, LUA_MASKCOUNT, kInstructionLimit);
lua_sethook(L, kHook, LUA_MASKCOUNT, kInstructionLimit);
PushGlobalTable(th_);
PushWeakPtr(th_, weak_from_this());
PushMeta(th_);
lua_setmetatable(th_, -2);
lua_setfield(th_, -2, "nf7");
lua_pop(th_, 1);
// set global table
PushGlobalTable(L);
PushWeakPtr(L, weak_from_this());
PushMeta(L);
lua_setmetatable(L, -2);
lua_setfield(L, -2, "nf7");
lua_pop(L, 1);
state_ = kRunning;
k.unlock();
active_ = true;
const auto ret = lua_resume(th_, narg);
const auto ret = lua_resume(L, narg);
active_ = false;
k.lock();
if (state_ == kAborted) return;
@ -68,7 +66,7 @@ void Thread::Resume(lua_State* L, int narg) noexcept {
state_ = kAborted;
}
if (!std::exchange(skip_handle_, false)) {
handler_(*this, th_);
handler_(*this, L);
}
}
void Thread::Abort() noexcept {

View File

@ -8,6 +8,7 @@
#include "common/dir_item.hh"
#include "common/generic_context.hh"
#include "common/generic_type_info.hh"
#include "common/luajit.hh"
#include "common/luajit_queue.hh"
#include "common/ptr_selector.hh"
#include "common/queue.hh"
@ -79,7 +80,13 @@ class LuaContext::Queue final : public nf7::luajit::Queue,
Queue() = delete;
Queue(LuaContext& f) : L(luaL_newstate()), env_(&f.env()) {
if (!L) throw nf7::Exception("failed to create new Lua state");
if (!L) {
throw nf7::Exception("failed to create new Lua state");
}
lua_pushthread(L);
nf7::luajit::PushImmEnv(L);
lua_setfenv(L, -2);
lua_pop(L, 1);
}
~Queue() noexcept {
th_->Push(