macro_rules! wrap_fn_with_context {
(0, $function:expr) => { ... };
(1, $function:expr) => { ... };
(2, $function:expr) => { ... };
(3, $function:expr) => { ... };
(4, $function:expr) => { ... };
(5, $function:expr) => { ... };
(6, $function:expr) => { ... };
(7, $function:expr) => { ... };
(8, $function:expr) => { ... };
(9, $function:expr) => { ... };
(10, $function:expr) => { ... };
}
Expand description
Analogue of wrap_fn
macro that injects the CallContext
as the first argument. This can be used to call functions within the implementation.
As with wrap_fn
, this macro must be called with 2 args: the arity of the function
(excluding CallContext
), and then the function / closure itself.
ยงExamples
fn map_array<'a>(
context: &mut CallContext<'_, 'a, f32>,
array: Vec<Value<'a, f32>>,
map_fn: Function<'a, f32>,
) -> Result<Vec<Value<'a, f32>>, Error<'a>> {
array
.into_iter()
.map(|value| {
let arg = context.apply_call_span(value);
map_fn.evaluate(vec![arg], context)
})
.collect()
}
let program = "(1, 2, 3).map(|x| x + 3) == (4, 5, 6)";
let program = Untyped::<F32Grammar>::parse_statements(program)?;
let module = Environment::new()
.insert_native_fn("map", wrap_fn_with_context!(2, map_array))
.compile_module("test_map", &program)?;
assert_eq!(module.run()?, Value::Bool(true));