Skip to main content

HostFunction

Struct HostFunction 

Source
pub struct HostFunction { /* private fields */ }
Expand description

A function implemented by the host application, bound to an expression through Bindings::function and called like a built-in.

Arguments and results cross the boundary as JSON: None stands for undefined, and a function passed as an argument arrives as None; HostCall::function gives a handle to call it (ADR 0011). An error returned by the function stops the evaluation; its position and token are set to the call site when it has none.

use jlens_core::{Bindings, Expression, HostFunction};
use serde_json::json;

let tax = HostFunction::with_signature("<n-n?:n>", |args, _call| {
    let amount = args[0].as_ref().and_then(|v| v.as_f64()).unwrap_or(0.0);
    let rate = args[1].as_ref().and_then(|v| v.as_f64()).unwrap_or(0.25);
    Ok(Some(json!(amount * rate)))
})
.unwrap();
let bindings = Bindings::new().function("tax", tax);
// `price.$tax()` takes its amount from the context, as the `-` in the signature allows.
let expr = Expression::compile("$tax(200) + price.$tax()").unwrap();
let result = expr.evaluate(Some(&json!({"price": 8})), &bindings).unwrap();
assert_eq!(result, Some(json!(52)));

Implementations§

Source§

impl HostFunction

Source

pub fn new<F>(arity: usize, imp: F) -> Self
where F: Fn(&[Option<JsonValue>], &HostCall<'_>) -> Result<Option<JsonValue>> + Send + Sync + 'static,

A function without a signature, declaring arity parameters.

The arity plays the part of a JavaScript function’s length. Higher-order functions use it to decide which arguments to pass ($map passes the index only to functions of two parameters or more), and partial application ($f(?, 2)) to know how many parameters there are. Arguments are passed as given, however many the call has: index them with get.

Source

pub fn with_signature<F>(signature: &str, imp: F) -> Result<Self>
where F: Fn(&[Option<JsonValue>], &HostCall<'_>) -> Result<Option<JsonValue>> + Send + Sync + 'static,

A function with a signature such as <n-n?:n>, which arguments are checked against before each call, as for built-in functions (T0410, T0412). The function then receives at least one argument per parameter in the signature (None for a missing optional one), and the context value where a parameter has the - modifier. The arity is the number of parameters in the signature.

Fails with the signature’s syntax error (S0401, S0402) if it is invalid.

Source

pub fn signature(&self) -> Option<&Signature>

The signature, if the function has one.

Source

pub fn arity(&self) -> usize

The number of declared parameters.

Trait Implementations§

Source§

impl Clone for HostFunction

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HostFunction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.