pub struct HostCall<'a> { /* private fields */ }Expand description
The context of a call to a HostFunction.
Implementations§
Source§impl HostCall<'_>
impl HostCall<'_>
Sourcepub fn input(&self) -> Option<JsonValue>
pub fn input(&self) -> Option<JsonValue>
The context value of the call: the input of the function call expression, such as the
current item in items.$f(). None when it is undefined, and for a function marked
without_input.
The value is converted to JSON when this method is called, so a function that does not
need it does not pay for converting it. A value larger than the size limit of a result
(Options::max_result_size), or nested more than 1,000 levels deep, is None, and the
evaluation then fails with U1001, whatever the function returns.
Sourcepub fn function(&self, index: usize) -> Option<Callable<'_>>
pub fn function(&self, index: usize) -> Option<Callable<'_>>
A handle to call argument index when it is a function, such as the lambda in
$retry(function() { $fetch() }, 3): a lambda, a built-in function, a partial
application, a chain or a host function. None when it is not a function. Arguments are
indexed as the function receives them, after the signature has been checked.
The function still receives None in place of the argument, as ADR 0006 decided; the
handle borrows the call, so it cannot be called once the function has returned.
use jlens_core::{Bindings, Expression, HostFunction};
use serde_json::json;
// `$twice($f, $x)` gives `$f($f($x))`.
let twice = HostFunction::with_signature("<fx:x>", |args, call| {
let f = call.function(0).expect("a function, as the signature requires");
let once = f.call(&[args[1].clone()])?;
f.call(&[once])
})
.unwrap();
let bindings = Bindings::new().function("twice", twice);
let expr = Expression::compile("$twice(function($n) { $n * 3 }, 2)").unwrap();
assert_eq!(expr.evaluate(None, &bindings).unwrap(), Some(json!(18)));