Merge pull request #141 from urbit/eamsden/stack_frame_wrapper

2stackz: add function to wrap a closure in frame push and pop
This commit is contained in:
Alex Shelkovnykov 2023-11-28 11:58:46 -06:00 committed by GitHub
commit b78ccf25e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -656,6 +656,21 @@ impl NockStack {
self.pc = false; self.pc = false;
} }
/** Run a closure inside a frame, popping regardless of the value returned by the closure.
* This is useful for writing fallible computations with the `?` operator.
*
* Note that results allocated on the stack *must* be `preserve()`d by the closure.
*/
pub unsafe fn with_frame<F, O>(&mut self, num_locals: usize, f: F) -> O
where
F: FnOnce() -> O,
{
self.frame_push(num_locals);
let ret = f();
self.frame_pop();
ret
}
/** Lightweight stack. /** Lightweight stack.
* The lightweight stack is a stack data structure present in each stack * The lightweight stack is a stack data structure present in each stack
* frame, often used for noun traversal. During normal operation (self.pc * frame, often used for noun traversal. During normal operation (self.pc