rhai/src/fn_register.rs

180 lines
8.3 KiB
Rust
Raw Normal View History

2020-03-08 12:54:02 +01:00
//! Module which defines the function registration mechanism.
#![allow(non_snake_case)]
2020-11-16 16:10:14 +01:00
use crate::dynamic::{DynamicWriteLock, Variant};
use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync};
use crate::r#unsafe::unsafe_try_cast;
2020-08-04 12:39:24 +02:00
use crate::stdlib::{any::TypeId, boxed::Box, mem, string::String};
2021-03-08 08:30:32 +01:00
use crate::{Dynamic, Engine, FnAccess, FnNamespace, NativeCallContext, RhaiResult};
2016-02-29 22:43:45 +01:00
2020-03-25 04:24:06 +01:00
// These types are used to build a unique _marker_ tuple type for each combination
// of function parameter types in order to make each trait implementation unique.
// That is because stable Rust currently does not allow distinguishing implementations
2020-11-25 02:36:06 +01:00
// based purely on parameter types of traits (`Fn`, `FnOnce` and `FnMut`).
2020-03-25 04:24:06 +01:00
//
// For example:
//
// `NativeFunction<(Mut<A>, B, Ref<C>), R>`
2020-03-25 04:24:06 +01:00
//
// will have the function prototype constraint to:
//
// `FN: (&mut A, B, &C) -> R`
//
// These types are not actually used anywhere.
pub struct Mut<T>(T);
//pub struct Ref<T>(T);
/// Dereference into DynamicWriteLock
2020-05-11 07:36:50 +02:00
#[inline(always)]
pub fn by_ref<T: Variant + Clone>(data: &mut Dynamic) -> DynamicWriteLock<T> {
// Directly cast the &mut Dynamic into DynamicWriteLock to access the underlying data.
data.write_lock::<T>().unwrap()
2020-03-24 09:57:35 +01:00
}
/// Dereference into value.
2020-05-11 07:36:50 +02:00
#[inline(always)]
2020-05-12 10:32:22 +02:00
pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
if TypeId::of::<T>() == TypeId::of::<&str>() {
// If T is `&str`, data must be `ImmutableString`, so map directly to it
data.flatten_in_place();
let ref_str = data
.as_str_ref()
.expect("argument passed by value should not be shared");
let ref_t = unsafe { mem::transmute::<_, &T>(&ref_str) };
ref_t.clone()
} else if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is `String`, data must be `ImmutableString`, so map directly to it
unsafe_try_cast(mem::take(data).take_string().unwrap()).unwrap()
} else {
// We consume the argument and then replace it with () - the argument is not supposed to be used again.
// This way, we avoid having to clone the argument again, because it is already a clone when passed here.
mem::take(data).cast::<T>()
}
}
/// Trait to register custom functions with an [`Engine`].
pub trait RegisterNativeFunction<Args, Result> {
/// Register the function with an [`Engine`].
fn register_into(self, engine: &mut Engine, name: &str);
}
2017-12-20 12:16:14 +01:00
macro_rules! def_register {
() => {
2020-05-19 16:25:57 +02:00
def_register!(imp from_pure :);
2017-12-20 12:16:14 +01:00
};
(imp $abi:ident : $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => {
// ^ function ABI type
// ^ function parameter generic type name (A, B, C etc.)
2020-10-02 08:55:02 +02:00
// ^ call argument(like A, *B, &mut C etc)
// ^ function parameter marker type (T, Ref<T> or Mut<T>)
// ^ function parameter actual type (T, &T or &mut T)
// ^ argument let statement
impl<
FN: Fn($($param),*) -> RET + SendSync + 'static,
$($par: Variant + Clone,)*
2020-04-12 17:00:06 +02:00
RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), ()> for FN {
2021-03-04 11:13:47 +01:00
#[inline(always)]
fn register_into(self, engine: &mut Engine, name: &str) {
engine.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
2021-03-08 08:30:32 +01:00
&[$(TypeId::of::<$par>()),*],
CallableFunction::$abi(Box::new(move |_: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().unwrap()); )*
// Call the function with each argument value
let r = self($($arg),*);
// Map the result
Ok(r.into_dynamic())
}) as Box<FnAny>)
2020-05-13 13:21:42 +02:00
);
}
}
impl<
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
2020-04-12 17:00:06 +02:00
$($par: Variant + Clone,)*
RET: Variant + Clone
> RegisterNativeFunction<(NativeCallContext<'static>, $($mark,)*), ()> for FN {
#[inline(always)]
fn register_into(self, engine: &mut Engine, name: &str) {
engine.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
&[$(TypeId::of::<$par>()),*],
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().unwrap()); )*
// Call the function with each argument value
let r = self(ctx, $($arg),*);
// Map the result
Ok(r.into_dynamic())
}) as Box<FnAny>)
);
}
}
impl<
2021-03-02 08:02:28 +01:00
FN: Fn($($param),*) -> RhaiResult + SendSync + 'static,
$($par: Variant + Clone,)*
> RegisterNativeFunction<($($mark,)*), RhaiResult> for FN {
#[inline(always)]
fn register_into(self, engine: &mut Engine, name: &str) {
engine.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
&[$(TypeId::of::<$par>()),*],
CallableFunction::$abi(Box::new(move |_: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().unwrap()); )*
// Call the function with each argument value
self($($arg),*)
}) as Box<FnAny>)
);
}
}
impl<
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RhaiResult + SendSync + 'static,
$($par: Variant + Clone,)*
> RegisterNativeFunction<(NativeCallContext<'static>, $($mark,)*), RhaiResult> for FN {
2021-03-04 11:13:47 +01:00
#[inline(always)]
fn register_into(self, engine: &mut Engine, name: &str) {
engine.global_namespace.set_fn(name, FnNamespace::Global, FnAccess::Public, None,
2021-03-08 08:30:32 +01:00
&[$(TypeId::of::<$par>()),*],
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().unwrap()); )*
// Call the function with each argument value
self(ctx, $($arg),*)
}) as Box<FnAny>)
2020-05-13 13:21:42 +02:00
);
}
}
2017-12-20 12:16:14 +01:00
//def_register!(imp_pop $($par => $mark => $param),*);
};
($p0:ident $(, $p:ident)*) => {
def_register!(imp from_pure : $p0 => $p0 => $p0 => $p0 => let $p0 => by_value $(, $p => $p => $p => $p => let $p => by_value)*);
2021-03-07 15:10:54 +01:00
def_register!(imp from_method : $p0 => &mut $p0 => Mut<$p0> => &mut $p0 => let mut $p0 => by_ref $(, $p => $p => $p => $p => let $p => by_value)*);
// ^ CallableFunction constructor
// ^ first parameter passed through
// ^ others passed by value (by_value)
2017-12-20 12:16:14 +01:00
2020-04-09 12:45:49 +02:00
// Currently does not support first argument which is a reference, as there will be
// conflicting implementations since &T: Any and T: Any cannot be distinguished
//def_register!(imp $p0 => Ref<$p0> => &$p0 => by_ref $(, $p => $p => $p => by_value)*);
2017-12-20 12:16:14 +01:00
def_register!($($p),*);
};
}
def_register!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);