rhai/src/fn_func.rs

117 lines
4.6 KiB
Rust
Raw Normal View History

//! Module which defines the function registration mechanism.
#![cfg(not(feature = "no_function"))]
#![allow(non_snake_case)]
2020-10-28 15:18:44 +01:00
use crate::dynamic::Variant;
2020-04-09 04:38:33 +02:00
use crate::stdlib::{boxed::Box, string::ToString};
2020-11-16 16:25:19 +01:00
use crate::{Engine, EvalAltResult, ParseError, Scope, AST};
2020-04-09 04:38:33 +02:00
/// Trait to create a Rust closure from a script.
2020-04-09 04:38:33 +02:00
pub trait Func<ARGS, RET> {
type Output;
2020-11-20 09:52:28 +01:00
/// Create a Rust closure from an [`AST`].
/// The [`Engine`] and [`AST`] are consumed and basically embedded into the closure.
///
2020-10-27 04:30:38 +01:00
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
2020-04-09 04:38:33 +02:00
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_ast'
///
/// let engine = Engine::new(); // create a new 'Engine' just for this
///
/// let ast = engine.compile("fn calc(x, y) { x + len(y) < 42 }")?;
///
2020-04-09 04:38:33 +02:00
/// // Func takes two type parameters:
/// // 1) a tuple made up of the types of the script function's parameters
/// // 2) the return type of the script function
/// //
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
2020-04-09 04:38:33 +02:00
/// let func = Func::<(i64, String), bool>::create_from_ast(
/// // ^^^^^^^^^^^^^ function parameter types in tuple
///
/// engine, // the 'Engine' is consumed into the closure
/// ast, // the 'AST'
/// "calc" // the entry-point function name
/// );
///
/// func(123, "hello".to_string())? == false; // call the anonymous function
/// # Ok(())
/// # }
fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output;
/// Create a Rust closure from a script.
2020-11-20 09:52:28 +01:00
/// The [`Engine`] is consumed and basically embedded into the closure.
///
2020-10-27 04:30:38 +01:00
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
2020-04-09 04:38:33 +02:00
/// use rhai::{Engine, Func}; // use 'Func' for 'create_from_script'
///
/// let engine = Engine::new(); // create a new 'Engine' just for this
///
/// let script = "fn calc(x, y) { x + len(y) < 42 }";
///
2020-04-09 04:38:33 +02:00
/// // Func takes two type parameters:
/// // 1) a tuple made up of the types of the script function's parameters
/// // 2) the return type of the script function
/// //
/// // 'func' will have type Box<dyn Fn(i64, String) -> Result<bool, Box<EvalAltResult>>> and is callable!
2020-04-09 04:38:33 +02:00
/// let func = Func::<(i64, String), bool>::create_from_script(
/// // ^^^^^^^^^^^^^ function parameter types in tuple
///
/// engine, // the 'Engine' is consumed into the closure
/// script, // the script, notice number of parameters must match
/// "calc" // the entry-point function name
/// )?;
///
/// func(123, "hello".to_string())? == false; // call the anonymous function
/// # Ok(())
/// # }
/// ```
fn create_from_script(
self,
script: &str,
entry_point: &str,
) -> Result<Self::Output, ParseError>;
}
macro_rules! def_anonymous_fn {
() => {
def_anonymous_fn!(imp);
};
(imp $($par:ident),*) => {
2020-04-16 17:31:48 +02:00
impl<$($par: Variant + Clone,)* RET: Variant + Clone> Func<($($par,)*), RET> for Engine
{
#[cfg(feature = "sync")]
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>> + Send + Sync>;
#[cfg(not(feature = "sync"))]
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>>>;
2020-10-08 16:25:50 +02:00
#[inline(always)]
fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output {
2020-05-11 17:48:50 +02:00
let fn_name = entry_point.to_string();
Box::new(move |$($par: $par),*| {
2020-05-11 17:48:50 +02:00
self.call_fn(&mut Scope::new(), &ast, &fn_name, ($($par,)*))
})
}
2020-10-08 16:25:50 +02:00
#[inline(always)]
fn create_from_script(self, script: &str, entry_point: &str) -> Result<Self::Output, ParseError> {
let ast = self.compile(script)?;
2020-04-09 04:38:33 +02:00
Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point))
}
}
};
($p0:ident $(, $p:ident)*) => {
def_anonymous_fn!(imp $p0 $(, $p)*);
def_anonymous_fn!($($p),*);
};
}
def_anonymous_fn!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);