diff --git a/src/lua.rs b/src/lua.rs index 44a1f6e..7ed2cf7 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -889,10 +889,8 @@ impl Lua { ffi::luaL_sandboxthread(state); } else { // Restore original `LUA_GLOBALSINDEX` - self.ref_thread_exec(|ref_thread| { - ffi::lua_xpush(ref_thread, state, ffi::LUA_GLOBALSINDEX); - ffi::lua_replace(state, ffi::LUA_GLOBALSINDEX); - }); + ffi::lua_xpush(self.ref_thread(), state, ffi::LUA_GLOBALSINDEX); + ffi::lua_replace(state, ffi::LUA_GLOBALSINDEX); ffi::luaL_sandbox(state, 0); } })?; @@ -2457,16 +2455,6 @@ impl Lua { } } - /// Executes the function provided on the ref thread - #[inline] - pub(crate) unsafe fn ref_thread_exec(&self, f: F) -> R - where - F: FnOnce(*mut ffi::lua_State) -> R, - { - let ref_thread = (*self.extra.get()).ref_thread; - f(ref_thread) - } - unsafe fn push_userdata_metatable(&self) -> Result<()> { let extra = &mut *self.extra.get(); @@ -2969,6 +2957,13 @@ impl Lua { } } +impl LuaInner { + #[inline(always)] + pub(crate) fn ref_thread(&self) -> *mut ffi::lua_State { + unsafe { (*self.extra.get()).ref_thread } + } +} + struct StateGuard<'a>(&'a mut LuaInner, *mut ffi::lua_State); impl<'a> StateGuard<'a> { diff --git a/src/string.rs b/src/string.rs index 1093740..6a14186 100644 --- a/src/string.rs +++ b/src/string.rs @@ -122,7 +122,7 @@ impl<'lua> String<'lua> { #[inline] pub fn to_pointer(&self) -> *const c_void { let lua = self.0.lua; - unsafe { lua.ref_thread_exec(|refthr| ffi::lua_topointer(refthr, self.0.index)) } + unsafe { ffi::lua_topointer(lua.ref_thread(), self.0.index) } } } diff --git a/src/table.rs b/src/table.rs index 5269cba..7ee9691 100644 --- a/src/table.rs +++ b/src/table.rs @@ -363,13 +363,12 @@ impl<'lua> Table<'lua> { pub fn set_readonly(&self, enabled: bool) { let lua = self.0.lua; unsafe { - lua.ref_thread_exec(|refthr| { - ffi::lua_setreadonly(refthr, self.0.index, enabled as _); - if !enabled { - // Reset "safeenv" flag - ffi::lua_setsafeenv(refthr, self.0.index, 0); - } - }); + let ref_thread = lua.ref_thread(); + ffi::lua_setreadonly(ref_thread, self.0.index, enabled as _); + if !enabled { + // Reset "safeenv" flag + ffi::lua_setsafeenv(ref_thread, self.0.index, 0); + } } } @@ -380,7 +379,7 @@ impl<'lua> Table<'lua> { #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn is_readonly(&self) -> bool { let lua = self.0.lua; - unsafe { lua.ref_thread_exec(|refthr| ffi::lua_getreadonly(refthr, self.0.index) != 0) } + unsafe { ffi::lua_getreadonly(lua.ref_thread(), self.0.index) != 0 } } /// Converts the table to a generic C pointer. @@ -392,7 +391,7 @@ impl<'lua> Table<'lua> { #[inline] pub fn to_pointer(&self) -> *const c_void { let lua = self.0.lua; - unsafe { lua.ref_thread_exec(|refthr| ffi::lua_topointer(refthr, self.0.index)) } + unsafe { ffi::lua_topointer(lua.ref_thread(), self.0.index) } } /// Consume this table and return an iterator over the pairs of the table. diff --git a/src/thread.rs b/src/thread.rs index dbe4f5c..c0a4260 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -118,8 +118,7 @@ impl<'lua> Thread<'lua> { let _sg = StackGuard::new(lua.state); check_stack(lua.state, cmp::max(nargs + 1, 3))?; - let thread_state = - lua.ref_thread_exec(|ref_thread| ffi::lua_tothread(ref_thread, self.0.index)); + let thread_state = ffi::lua_tothread(lua.ref_thread(), self.0.index); let status = ffi::lua_status(thread_state); if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 { @@ -160,8 +159,7 @@ impl<'lua> Thread<'lua> { pub fn status(&self) -> ThreadStatus { let lua = self.0.lua; unsafe { - let thread_state = - lua.ref_thread_exec(|ref_thread| ffi::lua_tothread(ref_thread, self.0.index)); + let thread_state = ffi::lua_tothread(lua.ref_thread(), self.0.index); let status = ffi::lua_status(thread_state); if status != ffi::LUA_OK && status != ffi::LUA_YIELD { @@ -326,7 +324,7 @@ impl<'lua> Thread<'lua> { pub fn sandbox(&self) -> Result<()> { let lua = self.0.lua; unsafe { - let thread = lua.ref_thread_exec(|t| ffi::lua_tothread(t, self.0.index)); + let thread = ffi::lua_tothread(lua.ref_thread(), self.0.index); check_stack(thread, 1)?; check_stack(lua.state, 3)?; // Inherit `LUA_GLOBALSINDEX` from the caller @@ -366,8 +364,7 @@ impl<'lua, R> Drop for AsyncThread<'lua, R> { if !lua.recycle_thread(&mut self.thread) { #[cfg(feature = "lua54")] if self.thread.status() == ThreadStatus::Error { - let thread_state = - lua.ref_thread_exec(|t| ffi::lua_tothread(t, self.thread.0.index)); + let thread_state = ffi::lua_tothread(lua.ref_thread(), self.thread.0.index); ffi::lua_resetthread(thread_state); } } diff --git a/src/value.rs b/src/value.rs index 2d7403f..7f74b13 100644 --- a/src/value.rs +++ b/src/value.rs @@ -111,11 +111,11 @@ impl<'lua> Value<'lua> { Value::LightUserData(ud) => ud.0, Value::Table(t) => t.to_pointer(), Value::String(s) => s.to_pointer(), - Value::Function(Function(v)) - | Value::Thread(Thread(v)) - | Value::UserData(AnyUserData(v)) => v - .lua - .ref_thread_exec(|refthr| ffi::lua_topointer(refthr, v.index)), + Value::Function(Function(r)) + | Value::Thread(Thread(r)) + | Value::UserData(AnyUserData(r)) => { + ffi::lua_topointer(r.lua.ref_thread(), r.index) + } _ => ptr::null(), } }