Skip to content

Commit 2e0e86d

Browse files
committed
Update stack_value helper.
It uses zero stack spaces in Luau, and ref thread for `WrappedError` check in ther Lua versions. Close #597
1 parent 0ac7aeb commit 2e0e86d

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/state/raw.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,6 @@ impl RawLua {
702702
}
703703

704704
/// Returns value at given stack index without popping it.
705-
///
706-
/// Uses 2 stack spaces, does not call checkstack.
707705
pub(crate) unsafe fn stack_value(&self, idx: c_int, type_hint: Option<c_int>) -> Value {
708706
let state = self.state();
709707
match type_hint.unwrap_or_else(|| ffi::lua_type(state, idx)) {
@@ -759,21 +757,25 @@ impl RawLua {
759757
}
760758

761759
ffi::LUA_TUSERDATA => {
760+
let ref_thread = self.ref_thread();
761+
ffi::lua_xpush(state, ref_thread, idx);
762+
762763
// If the userdata is `WrappedFailure`, process it as an error or panic.
763764
let failure_mt_ptr = (*self.extra.get()).wrapped_failure_mt_ptr;
764-
match get_internal_userdata::<WrappedFailure>(state, idx, failure_mt_ptr).as_mut() {
765-
Some(WrappedFailure::Error(err)) => Value::Error(Box::new(err.clone())),
765+
match get_internal_userdata::<WrappedFailure>(ref_thread, -1, failure_mt_ptr).as_mut() {
766+
Some(WrappedFailure::Error(err)) => {
767+
ffi::lua_pop(ref_thread, 1);
768+
Value::Error(Box::new(err.clone()))
769+
},
766770
Some(WrappedFailure::Panic(panic)) => {
771+
ffi::lua_pop(ref_thread, 1);
767772
if let Some(panic) = panic.take() {
768773
resume_unwind(panic);
769774
}
770775
// Previously resumed panic?
771776
Value::Nil
772777
}
773-
_ => {
774-
ffi::lua_xpush(state, self.ref_thread(), idx);
775-
Value::UserData(AnyUserData(self.pop_ref_thread()))
776-
}
778+
_ => Value::UserData(AnyUserData(self.pop_ref_thread())),
777779
}
778780
}
779781

0 commit comments

Comments
 (0)