2020-03-08 12:54:02 +01:00
|
|
|
//! Module which defines the function registration mechanism.
|
2020-07-23 12:40:42 +02:00
|
|
|
|
2020-03-18 05:04:26 +01:00
|
|
|
#![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};
|
2021-03-15 04:36:30 +01:00
|
|
|
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.
|
2020-03-25 10:21:58 +01:00
|
|
|
// 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:
|
|
|
|
//
|
2021-03-15 04:36:30 +01:00
|
|
|
// `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.
|
2020-03-25 10:21:58 +01:00
|
|
|
pub struct Mut<T>(T);
|
|
|
|
//pub struct Ref<T>(T);
|
2016-04-17 04:32:18 +02:00
|
|
|
|
2020-07-27 06:30:09 +02:00
|
|
|
/// Dereference into DynamicWriteLock
|
2020-05-11 07:36:50 +02:00
|
|
|
#[inline(always)]
|
2020-07-27 06:30:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-03 10:54:24 +02: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 {
|
2020-06-07 11:54:33 +02:00
|
|
|
if TypeId::of::<T>() == TypeId::of::<&str>() {
|
2021-03-06 03:44:55 +01:00
|
|
|
// If T is `&str`, data must be `ImmutableString`, so map directly to it
|
2021-03-06 14:25:49 +01:00
|
|
|
data.flatten_in_place();
|
2021-03-06 03:44:55 +01:00
|
|
|
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()
|
2020-07-13 07:40:51 +02:00
|
|
|
} else if TypeId::of::<T>() == TypeId::of::<String>() {
|
2021-03-06 03:44:55 +01:00
|
|
|
// If T is `String`, data must be `ImmutableString`, so map directly to it
|
2021-03-15 04:36:30 +01:00
|
|
|
unsafe_try_cast(mem::take(data).take_string().unwrap()).unwrap()
|
2020-06-07 11:54:33 +02:00
|
|
|
} 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>()
|
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 04:36:30 +01:00
|
|
|
/// 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);
|
2020-04-19 16:58:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
2020-07-27 06:30:09 +02:00
|
|
|
(imp $abi:ident : $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => {
|
2020-05-11 12:55:58 +02:00
|
|
|
// ^ function ABI type
|
2020-05-22 07:09:17 +02:00
|
|
|
// ^ 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)
|
2021-03-15 04:36:30 +01:00
|
|
|
// ^ function parameter marker type (T, Ref<T> or Mut<T>)
|
2020-07-27 06:30:09 +02:00
|
|
|
// ^ function parameter actual type (T, &T or &mut T)
|
|
|
|
// ^ argument let statement
|
2021-03-15 04:36:30 +01:00
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
impl<
|
2020-06-08 08:10:16 +02:00
|
|
|
FN: Fn($($param),*) -> RET + SendSync + 'static,
|
2021-03-15 04:36:30 +01:00
|
|
|
$($par: Variant + Clone,)*
|
2020-04-12 17:00:06 +02:00
|
|
|
RET: Variant + Clone
|
2021-03-15 04:36:30 +01:00
|
|
|
> RegisterNativeFunction<($($mark,)*), ()> for FN {
|
2021-03-04 11:13:47 +01:00
|
|
|
#[inline(always)]
|
2021-03-15 04:36:30 +01:00
|
|
|
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>()),*],
|
2021-03-15 04:36:30 +01:00
|
|
|
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
|
|
|
);
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 08:02:27 +01:00
|
|
|
impl<
|
2021-03-15 04:36:30 +01:00
|
|
|
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
|
2020-04-12 17:00:06 +02:00
|
|
|
$($par: Variant + Clone,)*
|
2021-03-15 04:36:30 +01:00
|
|
|
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,
|
2021-03-15 04:36:30 +01:00
|
|
|
$($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)]
|
2021-03-15 04:36:30 +01:00
|
|
|
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>()),*],
|
2021-03-15 04:36:30 +01:00
|
|
|
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
|
|
|
);
|
2020-03-08 15:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
//def_register!(imp_pop $($par => $mark => $param),*);
|
|
|
|
};
|
|
|
|
($p0:ident $(, $p:ident)*) => {
|
2020-07-27 06:30:09 +02:00
|
|
|
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
|
2020-07-27 06:30:09 +02:00
|
|
|
// ^ 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
|
2020-05-03 10:54:24 +02:00
|
|
|
//def_register!(imp $p0 => Ref<$p0> => &$p0 => by_ref $(, $p => $p => $p => by_value)*);
|
2020-03-25 10:21:58 +01:00
|
|
|
|
2017-12-20 12:16:14 +01:00
|
|
|
def_register!($($p),*);
|
|
|
|
};
|
2016-03-02 16:49:24 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 10:21:58 +01:00
|
|
|
def_register!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);
|