(Luau) Set thread globals to main thread globals when resetting the thread

This commit is contained in:
Alex Orlenko 2023-06-02 21:13:23 +01:00
parent a1089dbf95
commit c905a34b1d
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
2 changed files with 9 additions and 6 deletions

View File

@ -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 }

View File

@ -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))
}
}