Refactor native function registration and instantiation

Introduces `register_native` for direct registration of `NativeFunction`
and `register_native_fn` for convenience from closures. The
`Environment::run`
method is removed, and its functionality is now handled by
`Environment::instantiate`,
which packages the linked AST into an invokable `NativeFunction`. This
streamlines
the execution path and better separates compilation/linking from runtime
execution.
This commit is contained in:
Michael Schimmel
2026-02-22 11:56:46 +01:00
parent a726b79d8a
commit cb94f20c0b
7 changed files with 143 additions and 65 deletions
+3 -2
View File
@@ -71,10 +71,11 @@ mod tests {
let env = Environment::new();
let compiled = env.compile(source).expect("Failed to compile");
let linked = env.link(compiled);
let result = env.run(&linked);
let func = env.instantiate(linked);
let result: Result<Value, String> = Ok((func.func)(vec![]));
match result {
Ok(Value::Int(val)) => assert_eq!(val, 20, "Closure should modify outer variable"),
Ok(Value::Int(20)) => (),
Ok(val) => panic!("Expected Int(20), got {:?}", val),
Err(e) => panic!("VM Error: {}", e),
}