fix: peek returns nth element from the top of the stack (#5262)

This commit is contained in:
Matthias Seitz
2023-11-01 18:46:54 +01:00
committed by GitHub
parent 23c4828e5a
commit cf438d91bd

View File

@ -280,6 +280,7 @@ impl StackObj {
.length(0) .length(0)
.build(); .build();
// peek returns the nth-from-the-top element of the stack.
let peek = FunctionObjectBuilder::new( let peek = FunctionObjectBuilder::new(
context, context,
NativeFunction::from_copy_closure_with_captures( NativeFunction::from_copy_closure_with_captures(
@ -293,6 +294,9 @@ impl StackObj {
), ),
))) )))
} }
// idx is from the top of the stack, so we need to reverse it
// SAFETY: bounds checked above
let idx = len - idx - 1;
stack_arr.get(idx as u64, ctx) stack_arr.get(idx as u64, ctx)
}, },
stack_arr, stack_arr,