Skip to content

Commit

Permalink
Fix VM reset after runtime error, closes #207
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp authored and David Peter committed Oct 11, 2023
1 parent 7f4ea15 commit 3334dad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion numbat/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ mod tests {
@aliases(Hz: short)
unit hertz: Frequency = 1 / second
fn sin(x: Scalar) -> Scalar
fn atan2<D>(y: D, x: D) -> Scalar
fn mean<D>(xs: D…) -> D
Expand Down
3 changes: 2 additions & 1 deletion numbat/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,15 @@ impl Vm {
}

pub fn run(&mut self, ctx: &mut ExecutionContext) -> Result<InterpreterResult> {
let old_stack = self.stack.clone();
let result = self.run_without_cleanup(ctx);
if result.is_err() {
// Perform cleanup: clear the stack and move IP to the end.
// This is useful for the REPL.
//
// TODO(minor): is this really enough? Shouldn't we also remove
// the bytecode?
self.stack.clear();
self.stack = old_stack;

// Reset the call stack
// TODO: move the following to a function?
Expand Down
12 changes: 12 additions & 0 deletions numbat/tests/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ fn test_implicit_conversion() {
expect_failure("x2", "Unknown identifier 'x2'");
}

#[test]
fn test_reset_after_runtime_error() {
let mut ctx = get_test_context();

let _ = ctx.interpret("let x = 1", CodeSource::Internal).unwrap();
let res = ctx.interpret("1/0", CodeSource::Internal);

assert!(res.is_err());

expect_output_with_context(&mut ctx, "x", "1");
}

#[test]
fn test_function_inverses() {
expect_output("sin(asin(0.1234))", "0.1234");
Expand Down

0 comments on commit 3334dad

Please sign in to comment.