Avoid allocations in metadata.
This commit is contained in:
parent
e10576abff
commit
e53be6e8ed
@ -117,10 +117,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
|
||||
/// Register a custom function.
|
||||
#[inline(always)]
|
||||
pub fn with_fn<A, R, S>(
|
||||
pub fn with_fn<A, const N: usize, R, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
method: impl RegisterNativeFunction<A, R, S>,
|
||||
method: impl RegisterNativeFunction<A, N, R, S>,
|
||||
) -> &mut Self {
|
||||
self.engine.register_fn(name, method);
|
||||
self
|
||||
@ -152,7 +152,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
pub fn with_get<V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S> + crate::func::SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_get(name, get_fn);
|
||||
self
|
||||
@ -165,7 +165,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
pub fn with_set<V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_set(name, set_fn);
|
||||
self
|
||||
@ -180,8 +180,8 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
pub fn with_get_set<V: Variant + Clone, S1, S2>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S1> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S2> + crate::func::SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_get_set(name, get_fn, set_fn);
|
||||
self
|
||||
@ -198,7 +198,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S> + crate::func::SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_indexer_get(get_fn);
|
||||
self
|
||||
@ -210,7 +210,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_indexer_set(set_fn);
|
||||
self
|
||||
@ -222,8 +222,8 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone, S1, S2>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S1> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S2> + crate::func::SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + crate::func::SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + crate::func::SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.engine.register_indexer_get_set(get_fn, set_fn);
|
||||
self
|
||||
|
@ -185,10 +185,10 @@ impl Engine {
|
||||
/// This method will be removed in the next major version.
|
||||
#[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
|
||||
#[inline(always)]
|
||||
pub fn register_result_fn<A, R, S>(
|
||||
pub fn register_result_fn<A, const N: usize, R, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
func: impl RegisterNativeFunction<A, R, RhaiResultOf<S>>,
|
||||
func: impl RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
|
||||
) -> &mut Self {
|
||||
self.register_fn(name, func)
|
||||
}
|
||||
@ -209,7 +209,7 @@ impl Engine {
|
||||
pub fn register_get_result<T: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, RhaiResultOf<S>>
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -230,7 +230,7 @@ impl Engine {
|
||||
pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), RhaiResultOf<S>>
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -257,7 +257,7 @@ impl Engine {
|
||||
S,
|
||||
>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, RhaiResultOf<S>>
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -282,7 +282,7 @@ impl Engine {
|
||||
S,
|
||||
>(
|
||||
&mut self,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), RhaiResultOf<S>>
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -516,10 +516,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
/// This method will be removed in the next major version.
|
||||
#[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
|
||||
#[inline(always)]
|
||||
pub fn with_result_fn<N, A, F, R, S>(&mut self, name: N, method: F) -> &mut Self
|
||||
pub fn with_result_fn<X, A, const N: usize, F, R, S>(&mut self, name: X, method: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
F: RegisterNativeFunction<A, R, RhaiResultOf<S>>,
|
||||
X: AsRef<str> + Into<Identifier>,
|
||||
F: RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
|
||||
{
|
||||
self.with_fn(name, method)
|
||||
}
|
||||
@ -541,7 +541,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
pub fn with_get_result<V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, RhaiResultOf<S>>
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -563,7 +563,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
pub fn with_set_result<V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), RhaiResultOf<S>>
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -586,7 +586,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, RhaiResultOf<S>>
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@ -607,7 +607,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), RhaiResultOf<S>>
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>>
|
||||
+ crate::func::SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
|
@ -56,7 +56,7 @@ impl Engine {
|
||||
/// # }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn register_fn<A, R, S, F: RegisterNativeFunction<A, R, S>>(
|
||||
pub fn register_fn<A, const N: usize, R, S, F: RegisterNativeFunction<A, N, R, S>>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
func: F,
|
||||
@ -302,7 +302,7 @@ impl Engine {
|
||||
pub fn register_get<T: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S> + SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
|
||||
}
|
||||
@ -352,7 +352,7 @@ impl Engine {
|
||||
pub fn register_set<T: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
|
||||
}
|
||||
@ -406,8 +406,8 @@ impl Engine {
|
||||
pub fn register_get_set<T: Variant + Clone, V: Variant + Clone, S1, S2>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S1> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S2> + SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.register_get(&name, get_fn).register_set(&name, set_fn)
|
||||
}
|
||||
@ -464,7 +464,7 @@ impl Engine {
|
||||
#[inline]
|
||||
pub fn register_indexer_get<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S> + SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
@ -539,7 +539,7 @@ impl Engine {
|
||||
#[inline]
|
||||
pub fn register_indexer_set<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>(
|
||||
&mut self,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
|
||||
@ -621,8 +621,8 @@ impl Engine {
|
||||
S2,
|
||||
>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S1> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S2> + SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.register_indexer_get(get_fn)
|
||||
.register_indexer_set(set_fn)
|
||||
|
@ -9,7 +9,10 @@ use crate::types::dynamic::{DynamicWriteLock, Variant};
|
||||
use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{any::TypeId, mem};
|
||||
use std::{
|
||||
any::{type_name, TypeId},
|
||||
mem,
|
||||
};
|
||||
|
||||
/// 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.
|
||||
@ -19,13 +22,13 @@ use std::{any::TypeId, mem};
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, ()>` = `Fn(&mut A, B, &C) -> R`
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, ()>` = `Fn(&mut A, B, &C) -> R`
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, NativeCallContext>` = `Fn(NativeCallContext, &mut A, B, &C) -> R`
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, NativeCallContext>` = `Fn(NativeCallContext, &mut A, B, &C) -> R`
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, RhaiResultOf<()>>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, RhaiResultOf<()>>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, RhaiResultOf<NativeCallContext>>` = `Fn(NativeCallContext, &mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, RhaiResultOf<NativeCallContext>>` = `Fn(NativeCallContext, &mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
|
||||
///
|
||||
/// These types are not actually used anywhere.
|
||||
pub struct Mut<T>(T);
|
||||
@ -66,18 +69,18 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
||||
///
|
||||
/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by `Mut<T>`.
|
||||
/// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type.
|
||||
pub trait RegisterNativeFunction<ARGS, RET, RESULT> {
|
||||
pub trait RegisterNativeFunction<ARGS, const N: usize, RET, RESULT> {
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
#[must_use]
|
||||
fn into_callable_function(self) -> CallableFunction;
|
||||
/// Get the type ID's of this function's parameters.
|
||||
#[must_use]
|
||||
fn param_types() -> Box<[TypeId]>;
|
||||
fn param_types() -> [TypeId; N];
|
||||
/// _(metadata)_ Get the type names of this function's parameters.
|
||||
/// Exported under the `metadata` feature only.
|
||||
#[cfg(feature = "metadata")]
|
||||
#[must_use]
|
||||
fn param_names() -> Box<[&'static str]>;
|
||||
fn param_names() -> [&'static str; N];
|
||||
/// _(metadata)_ Get the type ID of this function's return value.
|
||||
/// Exported under the `metadata` feature only.
|
||||
#[cfg(feature = "metadata")]
|
||||
@ -89,7 +92,7 @@ pub trait RegisterNativeFunction<ARGS, RET, RESULT> {
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
fn return_type_name() -> &'static str {
|
||||
std::any::type_name::<RET>()
|
||||
type_name::<RET>()
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,23 +127,24 @@ macro_rules! check_constant {
|
||||
|
||||
macro_rules! def_register {
|
||||
() => {
|
||||
def_register!(imp Pure :);
|
||||
def_register!(imp Pure : 0;);
|
||||
};
|
||||
(imp $abi:ident : $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => {
|
||||
(imp $abi:ident : $n:expr ; $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => {
|
||||
// ^ function ABI type
|
||||
// ^ function parameter generic type name (A, B, C etc.)
|
||||
// ^ call argument(like A, *B, &mut C etc)
|
||||
// ^ function parameter marker type (A, Ref<B> or Mut<C>)
|
||||
// ^ function parameter actual type (A, &B or &mut C)
|
||||
// ^ argument let statement
|
||||
// ^ number of parameters
|
||||
// ^ function parameter generic type name (A, B, C etc.)
|
||||
// ^ call argument(like A, *B, &mut C etc)
|
||||
// ^ function parameter marker type (A, Ref<B> or Mut<C>)
|
||||
// ^ function parameter actual type (A, &B or &mut C)
|
||||
// ^ argument let statement
|
||||
|
||||
impl<
|
||||
FN: Fn($($param),*) -> RET + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), RET, ()> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
|
||||
> RegisterNativeFunction<($($mark,)*), $n, RET, ()> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
@ -163,9 +167,9 @@ macro_rules! def_register {
|
||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), RET, NativeCallContext<'static>> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
|
||||
> RegisterNativeFunction<($($mark,)*), $n, RET, NativeCallContext<'static>> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
@ -188,11 +192,11 @@ macro_rules! def_register {
|
||||
FN: Fn($($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<()>> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
|
||||
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<()>> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<RhaiResultOf<RET>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
@ -211,11 +215,11 @@ macro_rules! def_register {
|
||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<NativeCallContext<'static>>> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
|
||||
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<NativeCallContext<'static>>> for FN {
|
||||
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { type_name::<RhaiResultOf<RET>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
// The arguments are assumed to be of the correct number and types!
|
||||
@ -232,19 +236,20 @@ macro_rules! def_register {
|
||||
|
||||
//def_register!(imp_pop $($par => $mark => $param),*);
|
||||
};
|
||||
($p0:ident $(, $p:ident)*) => {
|
||||
def_register!(imp Pure : $p0 => $p0 => $p0 => $p0 => let $p0 => by_value $(, $p => $p => $p => $p => let $p => by_value)*);
|
||||
def_register!(imp Method : $p0 => &mut $p0 => Mut<$p0> => &mut $p0 => let mut $p0 => by_ref $(, $p => $p => $p => $p => let $p => by_value)*);
|
||||
($p0:ident:$n0:expr $(, $p:ident: $n:expr)*) => {
|
||||
def_register!(imp Pure : $n0 ; $p0 => $p0 => $p0 => $p0 => let $p0 => by_value $(, $p => $p => $p => $p => let $p => by_value)*);
|
||||
def_register!(imp Method : $n0 ; $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)
|
||||
// ^ number of arguments
|
||||
// ^ first parameter passed through
|
||||
// ^ others passed by value (by_value)
|
||||
|
||||
// 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)*);
|
||||
|
||||
def_register!($($p),*);
|
||||
def_register!($($p: $n),*);
|
||||
};
|
||||
}
|
||||
|
||||
def_register!(A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V);
|
||||
def_register!(A:20, B:19, C:18, D:17, E:16, F:15, G:14, H:13, J:12, K:11, L:10, M:9, N:8, P:7, Q:6, R:5, S:4, T:3, U:2, V:1);
|
||||
|
@ -1237,14 +1237,14 @@ impl Module {
|
||||
/// assert!(module.contains_fn(hash));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn set_native_fn<A, T, F, S>(
|
||||
pub fn set_native_fn<A, const N: usize, T, F, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
func: F,
|
||||
) -> u64
|
||||
where
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<A, T, RhaiResultOf<S>>,
|
||||
F: RegisterNativeFunction<A, N, T, RhaiResultOf<S>>,
|
||||
{
|
||||
self.set_fn(
|
||||
name,
|
||||
@ -1280,7 +1280,7 @@ impl Module {
|
||||
where
|
||||
A: Variant + Clone,
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<(Mut<A>,), T, RhaiResultOf<S>> + SendSync + 'static,
|
||||
F: RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S>> + SendSync + 'static,
|
||||
{
|
||||
self.set_fn(
|
||||
crate::engine::make_getter(name.as_ref()).as_str(),
|
||||
@ -1321,7 +1321,7 @@ impl Module {
|
||||
where
|
||||
A: Variant + Clone,
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S>> + SendSync + 'static,
|
||||
F: RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S>> + SendSync + 'static,
|
||||
{
|
||||
self.set_fn(
|
||||
crate::engine::make_setter(name.as_ref()).as_str(),
|
||||
@ -1366,8 +1366,8 @@ impl Module {
|
||||
pub fn set_getter_setter_fn<A: Variant + Clone, T: Variant + Clone, S1, S2>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
getter: impl RegisterNativeFunction<(Mut<A>,), T, RhaiResultOf<S1>> + SendSync + 'static,
|
||||
setter: impl RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S2>> + SendSync + 'static,
|
||||
getter: impl RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S1>> + SendSync + 'static,
|
||||
setter: impl RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S2>> + SendSync + 'static,
|
||||
) -> (u64, u64) {
|
||||
(
|
||||
self.set_getter_fn(name.as_ref(), getter),
|
||||
@ -1409,7 +1409,7 @@ impl Module {
|
||||
A: Variant + Clone,
|
||||
B: Variant + Clone,
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S>> + SendSync + 'static,
|
||||
F: RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S>> + SendSync + 'static,
|
||||
{
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
|
||||
@ -1470,7 +1470,7 @@ impl Module {
|
||||
A: Variant + Clone,
|
||||
B: Variant + Clone,
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S>> + SendSync + 'static,
|
||||
F: RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S>> + SendSync + 'static,
|
||||
{
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
|
||||
@ -1542,8 +1542,10 @@ impl Module {
|
||||
S2,
|
||||
>(
|
||||
&mut self,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S1>> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S2>> + SendSync + 'static,
|
||||
get_fn: impl RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S1>> + SendSync + 'static,
|
||||
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S2>>
|
||||
+ SendSync
|
||||
+ 'static,
|
||||
) -> (u64, u64) {
|
||||
(
|
||||
self.set_indexer_get_fn(get_fn),
|
||||
|
Loading…
Reference in New Issue
Block a user