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
+8 -8
View File
@@ -22,7 +22,7 @@ pub fn register(env: &Environment) {
params: StaticType::Tuple(vec![StaticType::Float]),
ret: StaticType::Float,
}));
env.register_native(name, ty, Purity::Pure, move |args| {
env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let Some(x) = to_f64(&args[0]) {
Value::Float(f(x))
} else {
@@ -37,7 +37,7 @@ pub fn register(env: &Environment) {
params: StaticType::Tuple(vec![StaticType::Float]),
ret: StaticType::Int,
}));
env.register_native(name, ty, Purity::Pure, move |args| {
env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let Some(x) = to_f64(&args[0]) {
Value::Int(f(x) as i64)
} else {
@@ -70,7 +70,7 @@ pub fn register(env: &Environment) {
params: StaticType::Tuple(vec![StaticType::Float, StaticType::Float]),
ret: StaticType::Float,
}));
env.register_native(name, ty, Purity::Pure, move |args| {
env.register_native_fn(name, ty, Purity::Pure, move |args| {
if let (Some(a), Some(b)) = (to_f64(&args[0]), to_f64(&args[1])) {
Value::Float(f(a, b))
} else {
@@ -88,7 +88,7 @@ pub fn register(env: &Environment) {
params: StaticType::Tuple(vec![StaticType::Any]),
ret: StaticType::Any,
}));
env.register_native("abs", abs_ty, Purity::Pure, |args| {
env.register_native_fn("abs", abs_ty, Purity::Pure, |args| {
if args.len() != 1 {
return Value::Void;
}
@@ -105,7 +105,7 @@ pub fn register(env: &Environment) {
ret: StaticType::Any,
}));
env.register_native("min", variadic_ty.clone(), Purity::Pure, |args| {
env.register_native_fn("min", variadic_ty.clone(), Purity::Pure, |args| {
if args.is_empty() {
return Value::Void;
}
@@ -120,7 +120,7 @@ pub fn register(env: &Environment) {
best
});
env.register_native("max", variadic_ty, Purity::Pure, |args| {
env.register_native_fn("max", variadic_ty, Purity::Pure, |args| {
if args.is_empty() {
return Value::Void;
}
@@ -142,7 +142,7 @@ pub fn register(env: &Environment) {
}));
let prng = env.prng.clone();
env.register_native("random", random_ty, Purity::SideEffectFree, move |_| {
env.register_native_fn("random", random_ty, Purity::SideEffectFree, move |_| {
Value::Float(prng.borrow_mut().f64())
});
@@ -152,7 +152,7 @@ pub fn register(env: &Environment) {
}));
let prng_seed = env.prng.clone();
env.register_native("seed!", seed_ty, Purity::Impure, move |args| {
env.register_native_fn("seed!", seed_ty, Purity::Impure, move |args| {
if let Value::Int(s) = args[0] {
prng_seed.borrow_mut().seed(s as u64);
}