diff --git a/src/state/raw.rs b/src/state/raw.rs index 7fcb371..8732c55 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -694,7 +694,7 @@ impl RawLua { /// Pops a value from the Lua stack. /// - /// Uses 2 stack spaces, does not call `checkstack`. + /// Uses up to 1 stack spaces, does not call `checkstack`. pub(crate) unsafe fn pop_value(&self) -> Value { let value = self.stack_value(-1, None); ffi::lua_pop(self.state(), 1); @@ -702,6 +702,8 @@ impl RawLua { } /// Returns value at given stack index without popping it. + /// + /// Uses up to 1 stack spaces, does not call `checkstack`. pub(crate) unsafe fn stack_value(&self, idx: c_int, type_hint: Option) -> Value { let state = self.state(); match type_hint.unwrap_or_else(|| ffi::lua_type(state, idx)) { @@ -757,25 +759,21 @@ impl RawLua { } ffi::LUA_TUSERDATA => { - let ref_thread = self.ref_thread(); - ffi::lua_xpush(state, ref_thread, idx); - // If the userdata is `WrappedFailure`, process it as an error or panic. let failure_mt_ptr = (*self.extra.get()).wrapped_failure_mt_ptr; - match get_internal_userdata::(ref_thread, -1, failure_mt_ptr).as_mut() { - Some(WrappedFailure::Error(err)) => { - ffi::lua_pop(ref_thread, 1); - Value::Error(Box::new(err.clone())) - } + match get_internal_userdata::(state, idx, failure_mt_ptr).as_mut() { + Some(WrappedFailure::Error(err)) => Value::Error(Box::new(err.clone())), Some(WrappedFailure::Panic(panic)) => { - ffi::lua_pop(ref_thread, 1); if let Some(panic) = panic.take() { resume_unwind(panic); } // Previously resumed panic? Value::Nil } - _ => Value::UserData(AnyUserData(self.pop_ref_thread())), + _ => { + ffi::lua_xpush(state, self.ref_thread(), idx); + Value::UserData(AnyUserData(self.pop_ref_thread())) + } } } diff --git a/src/util/userdata.rs b/src/util/userdata.rs index 5ef93d5..e55fc8c 100644 --- a/src/util/userdata.rs +++ b/src/util/userdata.rs @@ -73,7 +73,7 @@ pub(crate) unsafe fn init_internal_metatable( Ok(()) } -// Uses 2 stack spaces, does not call checkstack +// Uses up to 1 stack space, does not call `checkstack` pub(crate) unsafe fn get_internal_userdata( state: *mut ffi::lua_State, index: c_int,