Refactor VM evaluation to use macro

The `eval` and `eval_observed` methods in the `VM` struct were very
similar, with the primary difference being the call to the `VMObserver`
trait. This commit refactors these methods into a single macro,
`dispatch_eval!`, which reduces code duplication and improves
maintainability.

The `Value::TailCallRequest` variant was also updated to use `Box` for
its contents, which helps keep the `Value` enum size consistent.

Finally, the benchmarks in the `examples` directory have been updated to
reflect minor performance changes resulting from these code
modifications.
This commit is contained in:
Michael Schimmel
2026-02-18 02:01:27 +01:00
parent 9b16bc47be
commit 98deb8f3fe
7 changed files with 111 additions and 97 deletions
+3 -3
View File
@@ -70,7 +70,7 @@ pub enum Value {
Function(Rc<dyn Fn(Vec<Value>) -> Value>),
Object(Rc<dyn Object>), // For compiled Closures and other opaque types
Cell(Rc<RefCell<Value>>), // Boxed value for captures
TailCallRequest(Rc<dyn Object>, Vec<Value>), // Internal: For TCO
TailCallRequest(Box<(Rc<dyn Object>, Vec<Value>)>), // Internal: For TCO (Boxed to keep Value small)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -146,7 +146,7 @@ impl Value {
Value::Function { .. } => StaticType::Any, // Dynamic function
Value::Object(o) => StaticType::Object(o.type_name()),
Value::Cell(c) => c.borrow().static_type(),
Value::TailCallRequest(_, _) => panic!("Internal VM state leaked: TailCallRequest"),
Value::TailCallRequest(_) => panic!("Internal VM state leaked: TailCallRequest"),
}
}
}
@@ -181,7 +181,7 @@ impl fmt::Display for Value {
Value::Function(_) => write!(f, "<native fn>"),
Value::Object(o) => write!(f, "<{}>", o.type_name()),
Value::Cell(c) => write!(f, "{}", c.borrow()),
Value::TailCallRequest(_, _) => panic!("Internal VM state leaked: TailCallRequest"),
Value::TailCallRequest(_) => panic!("Internal VM state leaked: TailCallRequest"),
}
}
}