2017-12-20 12:16:14 +01:00
|
|
|
use std::any::TypeId;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
use crate::any::{Any, Dynamic};
|
2020-03-04 15:00:01 +01:00
|
|
|
use crate::engine::{Engine, FnCallArgs};
|
2020-03-02 15:13:14 +01:00
|
|
|
use crate::parser::Position;
|
2020-03-04 15:00:01 +01:00
|
|
|
use crate::result::EvalAltResult;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
/// A trait to register custom functions with the `Engine`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rhai::{Engine, RegisterFn};
|
|
|
|
///
|
|
|
|
/// // Normal function
|
|
|
|
/// fn add(x: i64, y: i64) -> i64 {
|
|
|
|
/// x + y
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let mut engine = Engine::new();
|
|
|
|
///
|
|
|
|
/// // You must use the trait rhai::RegisterFn to get this method.
|
|
|
|
/// engine.register_fn("add", add);
|
|
|
|
///
|
|
|
|
/// if let Ok(result) = engine.eval::<i64>("add(40, 2)") {
|
|
|
|
/// println!("Answer: {}", result); // prints 42
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-12-20 12:16:14 +01:00
|
|
|
pub trait RegisterFn<FN, ARGS, RET> {
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Register a custom function with the `Engine`.
|
2017-12-20 12:16:14 +01:00
|
|
|
fn register_fn(&mut self, name: &str, f: FN);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2020-03-04 15:00:01 +01:00
|
|
|
|
|
|
|
/// A trait to register custom functions that return `Dynamic` values with the `Engine`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rhai::{Engine, RegisterDynamicFn, Dynamic};
|
|
|
|
///
|
|
|
|
/// // Function that returns a Dynamic value
|
|
|
|
/// fn get_an_any(x: i64) -> Dynamic {
|
|
|
|
/// Box::new(x)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let mut engine = Engine::new();
|
|
|
|
///
|
|
|
|
/// // You must use the trait rhai::RegisterDynamicFn to get this method.
|
|
|
|
/// engine.register_dynamic_fn("get_an_any", get_an_any);
|
|
|
|
///
|
|
|
|
/// if let Ok(result) = engine.eval::<i64>("get_an_any(42)") {
|
|
|
|
/// println!("Answer: {}", result); // prints 42
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-02-28 16:38:39 +01:00
|
|
|
pub trait RegisterDynamicFn<FN, ARGS> {
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Register a custom function returning `Dynamic` values with the `Engine`.
|
2020-02-25 08:02:27 +01:00
|
|
|
fn register_dynamic_fn(&mut self, name: &str, f: FN);
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
pub struct Ref<A>(A);
|
|
|
|
pub struct Mut<A>(A);
|
2016-04-17 04:32:18 +02:00
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
macro_rules! count_args {
|
|
|
|
() => {0usize};
|
|
|
|
($head:ident $($tail:ident)*) => {1usize + count_args!($($tail)*)};
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
macro_rules! def_register {
|
|
|
|
() => {
|
|
|
|
def_register!(imp);
|
|
|
|
};
|
|
|
|
(imp $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => {
|
2020-02-25 08:02:27 +01:00
|
|
|
impl<
|
2017-12-20 12:16:14 +01:00
|
|
|
$($par: Any + Clone,)*
|
|
|
|
FN: Fn($($param),*) -> RET + 'static,
|
2020-02-25 08:02:27 +01:00
|
|
|
RET: Any
|
2020-03-04 15:00:01 +01:00
|
|
|
> RegisterFn<FN, ($($mark,)*), RET> for Engine<'_>
|
2017-12-20 12:16:14 +01:00
|
|
|
{
|
|
|
|
fn register_fn(&mut self, name: &str, f: FN) {
|
2020-03-02 05:08:03 +01:00
|
|
|
let fn_name = name.to_string();
|
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
let fun = move |mut args: FnCallArgs, pos: Position| {
|
2017-12-20 12:16:14 +01:00
|
|
|
// Check for length at the beginning to avoid
|
|
|
|
// per-element bound checks.
|
2020-03-02 05:08:03 +01:00
|
|
|
const NUM_ARGS: usize = count_args!($($par)*);
|
|
|
|
|
|
|
|
if args.len() != NUM_ARGS {
|
2020-03-03 16:31:16 +01:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, args.len(), pos))
|
2020-03-03 09:24:03 +01:00
|
|
|
} else {
|
|
|
|
#[allow(unused_variables, unused_mut)]
|
|
|
|
let mut drain = args.drain(..);
|
|
|
|
$(
|
|
|
|
// Downcast every element, return in case of a type mismatch
|
|
|
|
let $par = (drain.next().unwrap().downcast_mut() as Option<&mut $par>).unwrap();
|
|
|
|
)*
|
|
|
|
|
|
|
|
// Call the user-supplied function using ($clone) to
|
|
|
|
// potentially clone the value, otherwise pass the reference.
|
|
|
|
let r = f($(($clone)($par)),*);
|
|
|
|
Ok(Box::new(r) as Dynamic)
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
2020-02-23 15:48:46 +01:00
|
|
|
};
|
2020-03-04 15:00:01 +01:00
|
|
|
self.register_fn_raw(name, Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
impl<
|
2020-02-23 15:48:46 +01:00
|
|
|
$($par: Any + Clone,)*
|
2020-02-25 08:02:27 +01:00
|
|
|
FN: Fn($($param),*) -> Dynamic + 'static,
|
2020-03-04 15:00:01 +01:00
|
|
|
> RegisterDynamicFn<FN, ($($mark,)*)> for Engine<'_>
|
2020-02-23 15:48:46 +01:00
|
|
|
{
|
2020-02-25 08:02:27 +01:00
|
|
|
fn register_dynamic_fn(&mut self, name: &str, f: FN) {
|
2020-03-02 05:08:03 +01:00
|
|
|
let fn_name = name.to_string();
|
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
let fun = move |mut args: FnCallArgs, pos: Position| {
|
2020-02-23 15:48:46 +01:00
|
|
|
// Check for length at the beginning to avoid
|
|
|
|
// per-element bound checks.
|
2020-03-02 05:08:03 +01:00
|
|
|
const NUM_ARGS: usize = count_args!($($par)*);
|
|
|
|
|
|
|
|
if args.len() != NUM_ARGS {
|
2020-03-03 16:31:16 +01:00
|
|
|
Err(EvalAltResult::ErrorFunctionArgsMismatch(fn_name.clone(), NUM_ARGS, args.len(), pos))
|
2020-03-03 09:24:03 +01:00
|
|
|
} else {
|
|
|
|
#[allow(unused_variables, unused_mut)]
|
|
|
|
let mut drain = args.drain(..);
|
|
|
|
$(
|
|
|
|
// Downcast every element, return in case of a type mismatch
|
|
|
|
let $par = (drain.next().unwrap().downcast_mut() as Option<&mut $par>).unwrap();
|
|
|
|
)*
|
|
|
|
|
|
|
|
// Call the user-supplied function using ($clone) to
|
|
|
|
// potentially clone the value, otherwise pass the reference.
|
|
|
|
Ok(f($(($clone)($par)),*))
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
2017-12-20 12:16:14 +01:00
|
|
|
};
|
2020-03-04 15:00:01 +01:00
|
|
|
self.register_fn_raw(name, Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun));
|
2017-12-20 12:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//def_register!(imp_pop $($par => $mark => $param),*);
|
|
|
|
};
|
|
|
|
($p0:ident $(, $p:ident)*) => {
|
|
|
|
def_register!(imp $p0 => $p0 => $p0 => Clone::clone $(, $p => $p => $p => Clone::clone)*);
|
|
|
|
def_register!(imp $p0 => Ref<$p0> => &$p0 => |x| { x } $(, $p => $p => $p => Clone::clone)*);
|
|
|
|
def_register!(imp $p0 => Mut<$p0> => &mut $p0 => |x| { x } $(, $p => $p => $p => Clone::clone)*);
|
|
|
|
|
|
|
|
def_register!($($p),*);
|
|
|
|
};
|
|
|
|
// (imp_pop) => {};
|
|
|
|
// (imp_pop $head:ident => $head_mark:ty => $head_param:ty $(,$tail:ident => $tail_mark:ty => $tp:ty)*) => {
|
|
|
|
// def_register!(imp $($tail => $tail_mark => $tp),*);
|
|
|
|
// };
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
2020-03-03 16:17:42 +01:00
|
|
|
def_register!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
|