Skip to main content

HostCall

Struct HostCall 

Source
pub struct HostCall<'a> { /* private fields */ }
Expand description

The context of a call to a HostFunction.

Implementations§

Source§

impl HostCall<'_>

Source

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.

Source

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)));

Auto Trait Implementations§

§

impl<'a> !Freeze for HostCall<'a>

§

impl<'a> !RefUnwindSafe for HostCall<'a>

§

impl<'a> !Send for HostCall<'a>

§

impl<'a> !Sync for HostCall<'a>

§

impl<'a> !UnwindSafe for HostCall<'a>

§

impl<'a> Unpin for HostCall<'a>

§

impl<'a> UnsafeUnpin for HostCall<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.