Hard code number of parameters.

This commit is contained in:
Stephen Chung 2022-12-03 09:14:16 +08:00
parent feb764a203
commit f41bdb7769

View File

@ -1,10 +1,14 @@
//! Module which defines the function registration mechanism.
#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(unused_mut)]
#![allow(unused_variables)]
use super::call::FnCallArgs;
use super::callable_function::CallableFunction;
use super::native::{SendSync, Shared};
use crate::engine::{FN_IDX_SET, FN_SET};
use crate::types::dynamic::{DynamicWriteLock, Variant};
use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
#[cfg(feature = "no_std")]
@ -69,18 +73,21 @@ 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, const N: usize, RET, RESULT> {
pub trait RegisterNativeFunction<ARGS, const NUM: 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() -> [TypeId; N];
fn param_types() -> [TypeId; NUM];
/// Get the number of parameters for this function.
#[must_use]
fn num_params() -> usize;
/// _(metadata)_ Get the type names of this function's parameters.
/// Exported under the `metadata` feature only.
#[cfg(feature = "metadata")]
#[must_use]
fn param_names() -> [&'static str; N];
fn param_names() -> [&'static str; NUM];
/// _(metadata)_ Get the type ID of this function's return value.
/// Exported under the `metadata` feature only.
#[cfg(feature = "metadata")]
@ -99,20 +106,19 @@ pub trait RegisterNativeFunction<ARGS, const N: usize, RET, RESULT> {
const EXPECT_ARGS: &str = "arguments";
macro_rules! check_constant {
($abi:ident, $ctx:ident, $args:ident) => {
($abi:ident, $n:expr, $ctx:ident, $args:ident) => {
#[cfg(any(not(feature = "no_object"), not(feature = "no_index")))]
if stringify!($abi) == "Method" && !$args.is_empty() {
let deny = match $args.len() {
#[cfg(not(feature = "no_index"))]
3 if $ctx.fn_name() == crate::engine::FN_IDX_SET && $args[0].is_read_only() => true,
#[cfg(not(feature = "no_object"))]
2 if $ctx.fn_name().starts_with(crate::engine::FN_SET)
&& $args[0].is_read_only() =>
{
true
}
_ => false,
};
if stringify!($abi) == "Method" {
let mut deny = false;
#[cfg(not(feature = "no_index"))]
if $n == 3 && !deny {
deny = $ctx.fn_name() == FN_IDX_SET && $args[0].is_read_only();
}
#[cfg(not(feature = "no_object"))]
if $n == 2 && !deny {
deny = $ctx.fn_name().starts_with(FN_SET) && $args[0].is_read_only();
}
if deny {
return Err(crate::ERR::ErrorNonPureMethodCallOnConstant(
@ -129,14 +135,14 @@ macro_rules! def_register {
() => {
def_register!(imp Pure : 0;);
};
(imp $abi:ident : $n:expr ; $($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 => $clone:expr),*) => {
// ^ function ABI type
// ^ 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
// ^ parameter access function (by_value or by_ref)
impl<
FN: Fn($($param),*) -> RET + SendSync + 'static,
@ -144,15 +150,16 @@ macro_rules! def_register {
RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, ()> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n }
#[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| {
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
check_constant!($abi, _ctx, args);
check_constant!($abi, $n, ctx, args);
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().expect(EXPECT_ARGS)); )*
let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )*
// Call the function with each argument value
let r = self($($arg),*);
@ -169,15 +176,16 @@ macro_rules! def_register {
RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, NativeCallContext<'static>> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n }
#[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| {
// The arguments are assumed to be of the correct number and types!
check_constant!($abi, ctx, args);
check_constant!($abi, $n, ctx, args);
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().expect(EXPECT_ARGS)); )*
let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )*
// Call the function with each argument value
let r = self(ctx, $($arg),*);
@ -194,16 +202,17 @@ macro_rules! def_register {
RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<()>> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n }
#[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 { type_name::<RhaiResultOf<RET>>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
CallableFunction::$abi(Shared::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| {
CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types!
check_constant!($abi, _ctx, args);
check_constant!($abi, $n, ctx, args);
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().expect(EXPECT_ARGS)); )*
let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )*
// Call the function with each argument value
self($($arg),*).map(Dynamic::from)
@ -217,16 +226,17 @@ macro_rules! def_register {
RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<NativeCallContext<'static>>> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n }
#[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 { 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!
check_constant!($abi, ctx, args);
check_constant!($abi, $n, ctx, args);
let mut _drain = args.iter_mut();
$($let $par = ($clone)(_drain.next().expect(EXPECT_ARGS)); )*
let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )*
// Call the function with each argument value
self(ctx, $($arg),*).map(Dynamic::from)
@ -237,12 +247,11 @@ macro_rules! def_register {
//def_register!(imp_pop $($par => $mark => $param),*);
};
($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)*);
def_register!(imp Pure : $n0 ; $p0 => $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => $p => by_value)*);
def_register!(imp Method : $n0 ; $p0 => &mut $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => $p => by_value)*);
// ^ CallableFunction constructor
// ^ number of arguments
// ^ 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