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:
@@ -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),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user