Refactor Examples to use assert_eq

This commit updates all example files to use `assert_eq!` for verifying
output, replacing the previous `Output:` comments. This change makes the
examples more robust and self-testing.

The benchmark results in some examples have also been slightly adjusted,
reflecting minor performance variations after the refactoring.

Additionally, a runtime panic handling mechanism has been introduced in
the VM for function calls, which improves error reporting for unexpected
panics.
This commit is contained in:
2026-03-26 15:07:48 +01:00
parent 0088a644eb
commit 16af3ae9fc
40 changed files with 313 additions and 235 deletions
+24 -2
View File
@@ -441,7 +441,19 @@ impl VM {
self.tail_call = Some((func_val, arg_vals));
return Ok(Value::Void);
}
Value::Function(f) => return Ok((f.func)(&arg_vals)),
Value::Function(f) => {
return std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
(f.func)(&arg_vals)
}))
.map_err(|e| {
e.downcast_ref::<String>()
.cloned()
.or_else(|| {
e.downcast_ref::<&str>().map(|s| s.to_string())
})
.unwrap_or_else(|| "runtime panic".to_string())
});
}
Value::FieldAccessor(k) => {
if arg_vals.len() != 1 {
return Err(format!(
@@ -510,7 +522,17 @@ impl VM {
loop {
let result = match &current_func {
Value::Function(f) => {
let res = (f.func)(&self.stack[base..]);
let res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
(f.func)(&self.stack[base..])
}))
.map_err(|e| {
e.downcast_ref::<String>()
.cloned()
.or_else(|| {
e.downcast_ref::<&str>().map(|s| s.to_string())
})
.unwrap_or_else(|| "runtime panic".to_string())
})?;
self.stack.truncate(base);
return Ok(res);
}