From 541139b944fbddfd79e3d517aed79de3b716e4d7 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sat, 8 Jul 2023 12:52:14 +0100 Subject: [PATCH] Change Lua ref types Debug print from `Ref{index}` to `Ref{pointer}` The index is always uniq per instance, but pointer can exactly tell when several values reference to the same Lua internal value. --- src/string.rs | 3 +-- src/table.rs | 3 +-- src/types.rs | 25 +++++++++++++------------ src/value.rs | 20 ++++++++------------ 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/string.rs b/src/string.rs index 9a75cf5..d35f618 100644 --- a/src/string.rs +++ b/src/string.rs @@ -139,8 +139,7 @@ impl<'lua> String<'lua> { /// Typically this function is used only for hashing and debug information. #[inline] pub fn to_pointer(&self) -> *const c_void { - let ref_thread = self.0.lua.ref_thread(); - unsafe { ffi::lua_topointer(ref_thread, self.0.index) } + self.0.to_pointer() } /// Convert this handle to owned version. diff --git a/src/table.rs b/src/table.rs index 6faaae7..3ffc859 100644 --- a/src/table.rs +++ b/src/table.rs @@ -617,8 +617,7 @@ impl<'lua> Table<'lua> { /// Typically this function is used only for hashing and debug information. #[inline] pub fn to_pointer(&self) -> *const c_void { - let ref_thread = self.0.lua.ref_thread(); - unsafe { ffi::lua_topointer(ref_thread, self.0.index) } + self.0.to_pointer() } /// Convert this handle to owned version. diff --git a/src/types.rs b/src/types.rs index a272249..25f3bc2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -20,7 +20,6 @@ use crate::error::Result; #[cfg(not(feature = "luau"))] use crate::hook::Debug; use crate::lua::{ExtraData, Lua}; -use crate::util::{assert_stack, StackGuard}; use crate::value::MultiValue; #[cfg(feature = "unstable")] @@ -288,6 +287,11 @@ impl<'lua> LuaRef<'lua> { } } + #[inline] + pub(crate) fn to_pointer(&self) -> *const c_void { + unsafe { ffi::lua_topointer(self.lua.ref_thread(), self.index) } + } + #[cfg(feature = "unstable")] #[inline] pub(crate) fn into_owned(self) -> LuaOwnedRef { @@ -300,7 +304,7 @@ impl<'lua> LuaRef<'lua> { impl<'lua> fmt::Debug for LuaRef<'lua> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Ref({})", self.index) + write!(f, "Ref({:p})", self.to_pointer()) } } @@ -320,15 +324,12 @@ impl<'lua> Drop for LuaRef<'lua> { impl<'lua> PartialEq for LuaRef<'lua> { fn eq(&self, other: &Self) -> bool { - let lua = self.lua; - let state = lua.state(); - unsafe { - let _sg = StackGuard::new(state); - assert_stack(state, 2); - lua.push_ref(self); - lua.push_ref(other); - ffi::lua_rawequal(state, -1, -2) == 1 - } + let ref_thread = self.lua.ref_thread(); + assert!( + ref_thread == other.lua.ref_thread(), + "Lua instance passed Value created from a different main Lua state" + ); + unsafe { ffi::lua_rawequal(ref_thread, self.index, other.index) == 1 } } } @@ -342,7 +343,7 @@ pub(crate) struct LuaOwnedRef { #[cfg(feature = "unstable")] impl fmt::Debug for LuaOwnedRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "OwnedRef({})", self.index) + write!(f, "OwnedRef({:p})", self.to_ref().to_pointer()) } } diff --git a/src/value.rs b/src/value.rs index f363eb5..f263e76 100644 --- a/src/value.rs +++ b/src/value.rs @@ -116,18 +116,14 @@ impl<'lua> Value<'lua> { /// Typically this function is used only for hashing and debug information. #[inline] pub fn to_pointer(&self) -> *const c_void { - unsafe { - match self { - Value::LightUserData(ud) => ud.0, - Value::Table(t) => t.to_pointer(), - Value::String(s) => s.to_pointer(), - Value::Function(Function(r)) - | Value::Thread(Thread(r)) - | Value::UserData(AnyUserData(r)) => { - ffi::lua_topointer(r.lua.ref_thread(), r.index) - } - _ => ptr::null(), - } + match self { + Value::LightUserData(ud) => ud.0, + Value::String(String(r)) + | Value::Table(Table(r)) + | Value::Function(Function(r)) + | Value::Thread(Thread(r)) + | Value::UserData(AnyUserData(r)) => r.to_pointer(), + _ => ptr::null(), } }