| @@ -20,8 +20,8 @@ |
| 20 | 20 | use std::sync::Arc; |
| 21 | 21 | |
| 22 | 22 | use feel_core::{ |
| 23 | | - compile as core_compile, CompiledExpr as CoreCompiled, Context, EvalEnv, Foreign, |
| 24 | | - ForeignObject, Value, |
| 23 | + compile as core_compile, Args, CompiledExpr as CoreCompiled, Context, EvalEnv, Foreign, |
| 24 | + ForeignFunction, ForeignObject, Func, FunctionRegistry, Value, |
| 25 | 25 | }; |
| 26 | 26 | use serde::Serialize; |
| 27 | 27 | use wasm_bindgen::prelude::*; |
| @@ -35,8 +35,8 @@ |
| 35 | 35 | /// returned as plain `null` (the spec-shaped result); see `evaluate_strict` to |
| 36 | 36 | /// surface such errors as a thrown exception instead. |
| 37 | 37 | #[wasm_bindgen] |
| 38 | | -pub fn evaluate(expr: &str, context: JsValue) -> Result<JsValue, JsError> { |
| 39 | | - run(&compile_core(expr)?, context, false) |
| 38 | +pub fn evaluate(expr: &str, context: JsValue, functions: JsValue) -> Result<JsValue, JsError> { |
| 39 | + run(&compile_core(expr)?, context, functions, false) |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /// Evaluate a FEEL expression (strict boundary policy): if the result is, or |
| @@ -44,8 +44,12 @@ |
| 44 | 44 | /// the error pass silently as `null` (ADR-14). A plain (value) null is still |
| 45 | 45 | /// returned as `null`. |
| 46 | 46 | #[wasm_bindgen] |
| 47 | | -pub fn evaluate_strict(expr: &str, context: JsValue) -> Result<JsValue, JsError> { |
| 48 | | - run(&compile_core(expr)?, context, true) |
| 47 | +pub fn evaluate_strict( |
| 48 | + expr: &str, |
| 49 | + context: JsValue, |
| 50 | + functions: JsValue, |
| 51 | +) -> Result<JsValue, JsError> { |
| 52 | + run(&compile_core(expr)?, context, functions, true) |
| 49 | 53 | } |
| 50 | 54 | |
| 51 | 55 | /// A parsed FEEL expression, reusable across many evaluations. |
| @@ -72,14 +76,18 @@ |
| 72 | 76 | #[wasm_bindgen] |
| 73 | 77 | impl CompiledExpr { |
| 74 | 78 | /// Evaluate this compiled expression against a context (lenient policy). |
| 75 | | - pub fn evaluate(&self, context: JsValue) -> Result<JsValue, JsError> { |
| 76 | | - run(&self.inner, context, false) |
| 79 | + pub fn evaluate(&self, context: JsValue, functions: JsValue) -> Result<JsValue, JsError> { |
| 80 | + run(&self.inner, context, functions, false) |
| 77 | 81 | } |
| 78 | 82 | |
| 79 | 83 | /// Evaluate this compiled expression against a context (strict policy; see |
| 80 | 84 | /// the free `evaluate_strict`). |
| 81 | | - pub fn evaluate_strict(&self, context: JsValue) -> Result<JsValue, JsError> { |
| 82 | | - run(&self.inner, context, true) |
| 85 | + pub fn evaluate_strict( |
| 86 | + &self, |
| 87 | + context: JsValue, |
| 88 | + functions: JsValue, |
| 89 | + ) -> Result<JsValue, JsError> { |
| 90 | + run(&self.inner, context, functions, true) |
| 83 | 91 | } |
| 84 | 92 | } |
| 85 | 93 | |
| @@ -91,9 +99,17 @@ |
| 91 | 99 | /// objects), evaluate the compiled program against the host regex provider and |
| 92 | 100 | /// the `Date.now()` instant, optionally surface error-nulls strictly, map the |
| 93 | 101 | /// result back out (date/date-and-time as native `Date` objects). |
| 94 | | -fn run(compiled: &CoreCompiled, context: JsValue, strict: bool) -> Result<JsValue, JsError> { |
| 102 | +fn run( |
| 103 | + compiled: &CoreCompiled, |
| 104 | + context: JsValue, |
| 105 | + functions: JsValue, |
| 106 | + strict: bool, |
| 107 | +) -> Result<JsValue, JsError> { |
| 95 | 108 | let ctx = context_from_js(&context)?; |
| 96 | | - let env = EvalEnv::new(®EX, now_zoned()); |
| 109 | + // Ambient host functions (ADR-23): a JS object of `name -> function`, consulted |
| 110 | + // after context bindings and built-ins. `undefined`/`null` -> no-op registry. |
| 111 | + let registry = JsFunctionRegistry { funcs: functions }; |
| 112 | + let env = EvalEnv::with_functions(®EX, now_zoned(), ®istry); |
| 97 | 113 | let value = compiled |
| 98 | 114 | .evaluate_with_env(&ctx, &env) |
| 99 | 115 | .map_err(|e| JsError::new(&e.to_string()))?; |
| @@ -160,6 +176,16 @@ |
| 160 | 176 | Some(jf) => Ok(jf.obj.clone()), |
| 161 | 177 | None => value_to_js(&f.0.force()), |
| 162 | 178 | }, |
| 179 | + // A function value (ADR-23): a host callable is handed back as the original |
| 180 | + // JS function (identity pass-through); a native FEEL closure has no JS |
| 181 | + // representation and crosses out as `null`. |
| 182 | + Value::Function(f) => match &**f { |
| 183 | + Func::Foreign(host_fn) => match host_fn.as_any().downcast_ref::<JsForeignFn>() { |
| 184 | + Some(jf) => Ok(jf.obj.clone()), |
| 185 | + None => Ok(JsValue::NULL), |
| 186 | + }, |
| 187 | + Func::Closure(_) => Ok(JsValue::NULL), |
| 188 | + }, |
| 163 | 189 | // Scalars (number/bool/string/null) go through the JSON-compatible path so |
| 164 | 190 | // integral numbers stay JS numbers (`1 + 1` -> `2`, not `2.0`). |
| 165 | 191 | _ => to_js(&value_to_json(value)), |
| @@ -252,6 +278,12 @@ |
| 252 | 278 | let arr: &js_sys::Array = value.unchecked_ref(); |
| 253 | 279 | return Value::List(arr.iter().map(|x| shallow_convert_js(&x)).collect()); |
| 254 | 280 | } |
| 281 | + // A JS function becomes a FEEL function value the host can invoke (ADR-23). |
| 282 | + // Checked before `is_object` (a function has `typeof === "function"`, not |
| 283 | + // `"object"`, but be explicit about ordering). |
| 284 | + if value.is_function() { |
| 285 | + return Func::Foreign(Arc::new(JsForeignFn { obj: value.clone() })).into_value(); |
| 286 | + } |
| 255 | 287 | if value.is_object() { |
| 256 | 288 | return Value::Foreign(Foreign(Arc::new(JsForeign { obj: value.clone() }))); |
| 257 | 289 | } |
| @@ -308,6 +340,76 @@ |
| 308 | 340 | |
| 309 | 341 | fn as_any(&self) -> &dyn std::any::Any { |
| 310 | 342 | self |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +// --------------------------------------------------------------------------- // |
| 347 | +// Host callables (ADR-23): a JS function invoked from FEEL |
| 348 | +// --------------------------------------------------------------------------- // |
| 349 | + |
| 350 | +/// A JS function behind feel-core's [`ForeignFunction`] seam. `obj` is the original |
| 351 | +/// `JsValue` (a function), kept whole for identity pass-through on output. |
| 352 | +#[derive(Debug)] |
| 353 | +struct JsForeignFn { |
| 354 | + obj: JsValue, |
| 355 | +} |
| 356 | + |
| 357 | +impl ForeignFunction for JsForeignFn { |
| 358 | + fn call(&self, args: Args) -> Value { |
| 359 | + let func: &js_sys::Function = self.obj.unchecked_ref(); |
| 360 | + match js_call(func, args) { |
| 361 | + Ok(v) => shallow_convert_js(&v), |
| 362 | + Err(_) => Value::Error, // a thrown JS exception -> error-null (ADR-14) |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + fn as_any(&self) -> &dyn std::any::Any { |
| 367 | + self |
| 368 | + } |
| 369 | +} |
| 370 | + |
| 371 | +/// Invoke a JS function with FEEL arguments (ADR-23). JS has no keyword arguments, |
| 372 | +/// so positional FEEL args become positional JS args and — if any named args are |
| 373 | +/// present — a single trailing options object `{name: value, …}` is appended (the |
| 374 | +/// idiomatic JS convention). A thrown exception propagates as `Err`. |
| 375 | +fn js_call(func: &js_sys::Function, args: Args) -> Result<JsValue, JsValue> { |
| 376 | + let arr = js_sys::Array::new(); |
| 377 | + for v in &args.positional { |
| 378 | + arr.push(&value_to_js(v).map_err(|_| JsValue::NULL)?); |
| 379 | + } |
| 380 | + if !args.named.is_empty() { |
| 381 | + let opts = js_sys::Object::new(); |
| 382 | + for (name, v) in &args.named { |
| 383 | + js_sys::Reflect::set( |
| 384 | + &opts, |
| 385 | + &JsValue::from_str(name), |
| 386 | + &value_to_js(v).map_err(|_| JsValue::NULL)?, |
| 387 | + )?; |
| 388 | + } |
| 389 | + arr.push(&opts.into()); |
| 390 | + } |
| 391 | + func.apply(&JsValue::NULL, &arr) |
| 392 | +} |
| 393 | + |
| 394 | +/// A host function registry backed by a JS object `name -> function` (the third |
| 395 | +/// `functions` argument; ADR-23). The ambient-function path, beside binding a |
| 396 | +/// function directly in the context. `undefined`/`null` means no registry. |
| 397 | +#[derive(Debug)] |
| 398 | +struct JsFunctionRegistry { |
| 399 | + funcs: JsValue, |
| 400 | +} |
| 401 | + |
| 402 | +impl FunctionRegistry for JsFunctionRegistry { |
| 403 | + fn get(&self, name: &str) -> Option<Arc<dyn ForeignFunction>> { |
| 404 | + if self.funcs.is_undefined() || self.funcs.is_null() { |
| 405 | + return None; |
| 406 | + } |
| 407 | + let item = js_sys::Reflect::get(&self.funcs, &JsValue::from_str(name)).ok()?; |
| 408 | + if item.is_function() { |
| 409 | + Some(Arc::new(JsForeignFn { obj: item })) |
| 410 | + } else { |
| 411 | + None |
| 412 | + } |
| 311 | 413 | } |
| 312 | 414 | } |
| 313 | 415 | |