Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/zapcode-core/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ impl Vm {
// Resource checks
self.tracker.check_time(&self.limits)?;

let frame = self.frames.last().unwrap();
let frame = self
.frames
.last()
.ok_or_else(|| ZapcodeError::RuntimeError("no active call frame".to_string()))?;
let instructions = match frame.func_index {
Some(idx) => &self.program.functions[idx].instructions,
None => &self.program.instructions,
Expand Down Expand Up @@ -547,7 +550,10 @@ impl Vm {
loop {
self.tracker.check_time(&self.limits)?;

let frame = self.frames.last().unwrap();
let frame = self
.frames
.last()
.ok_or_else(|| ZapcodeError::RuntimeError("no active call frame".to_string()))?;
let instructions = match frame.func_index {
Some(idx) => &self.program.functions[idx].instructions,
None => &self.program.instructions,
Expand Down Expand Up @@ -1031,7 +1037,10 @@ impl Vm {
let target_frame_depth = self.frames.len() - 1;
loop {
self.tracker.check_time(&self.limits)?;
let frame = self.frames.last().unwrap();
let frame = self
.frames
.last()
.ok_or_else(|| ZapcodeError::RuntimeError("no active call frame".to_string()))?;
let instructions = match frame.func_index {
Some(idx) => &self.program.functions[idx].instructions,
None => &self.program.instructions,
Expand Down
32 changes: 32 additions & 0 deletions crates/zapcode-core/tests/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,35 @@ fn test_try_no_error() {
.unwrap();
assert_eq!(result, Value::Int(42));
}

// Regression: a throw escaping a nested array callback (or a callback inside a
// class method) emptied the VM frame stack; execute() then hit
// frames.last().unwrap() and aborted the host process. These must surface an
// error to the caller, never panic. (The guest-level catch not observing the
// throw is a separate, pre-existing unwinding issue.)
#[test]
fn test_throw_from_nested_callback_does_not_panic() {
let result = eval_ts(
r#"
let out = 0;
try {
[1].map(a => [2].map(b => { throw "n"; }));
} catch (e) { out = 3; }
out
"#,
);
assert!(result.is_err());
}

#[test]
fn test_throw_from_class_method_callback_does_not_panic() {
let result = eval_ts(
r#"
class A { run() { return [1].map(x => { throw "m"; }); } }
let out = 0;
try { new A().run(); } catch (e) { out = 6; }
out
"#,
);
assert!(result.is_err());
}