From c905a34b1d62f556d9660bf0f4882341cf022692 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Fri, 2 Jun 2023 21:13:23 +0100 Subject: [PATCH] (Luau) Set thread globals to main thread globals when resetting the thread --- src/lua.rs | 6 ++++++ src/thread.rs | 9 +++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 0480501..4fe89e2 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -3006,6 +3006,12 @@ impl LuaInner { self.state.load(Ordering::Relaxed) } + #[cfg(feature = "luau")] + #[inline(always)] + pub(crate) fn main_state(&self) -> *mut ffi::lua_State { + self.main_state + } + #[inline(always)] pub(crate) fn ref_thread(&self) -> *mut ffi::lua_State { unsafe { (*self.extra.get()).ref_thread } diff --git a/src/thread.rs b/src/thread.rs index 3d238fc..44fa860 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -251,8 +251,8 @@ impl<'lua> Thread<'lua> { #[cfg(feature = "luau")] { - // Inherit `LUA_GLOBALSINDEX` from the caller - ffi::lua_xpush(state, thread_state, ffi::LUA_GLOBALSINDEX); + // Inherit `LUA_GLOBALSINDEX` from the main thread + ffi::lua_xpush(lua.main_state(), thread_state, ffi::LUA_GLOBALSINDEX); ffi::lua_replace(thread_state, ffi::LUA_GLOBALSINDEX); } @@ -360,11 +360,8 @@ impl<'lua> Thread<'lua> { let state = lua.state(); unsafe { let thread = ffi::lua_tothread(lua.ref_thread(), self.0.index); - check_stack(thread, 1)?; + check_stack(thread, 3)?; check_stack(state, 3)?; - // Inherit `LUA_GLOBALSINDEX` from the caller - ffi::lua_xpush(state, thread, ffi::LUA_GLOBALSINDEX); - ffi::lua_replace(thread, ffi::LUA_GLOBALSINDEX); protect_lua!(state, 0, 0, |_| ffi::luaL_sandboxthread(thread)) } }