Use FnArgsVec for params.

This commit is contained in:
Stephen Chung 2022-12-01 17:45:33 +08:00
parent 0c4935febb
commit ed423740c9
2 changed files with 32 additions and 32 deletions

View File

@ -139,8 +139,8 @@ macro_rules! def_register {
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), RET, ()> for FN { > RegisterNativeFunction<($($mark,)*), RET, ()> for FN {
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() } #[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$param>()),*].into_boxed_slice() } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[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| {
@ -164,8 +164,8 @@ macro_rules! def_register {
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), RET, NativeCallContext<'static>> for FN { > RegisterNativeFunction<($($mark,)*), RET, NativeCallContext<'static>> for FN {
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() } #[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$param>()),*].into_boxed_slice() } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[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| {
@ -189,8 +189,8 @@ macro_rules! def_register {
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<()>> for FN { > RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<()>> for FN {
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() } #[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$param>()),*].into_boxed_slice() } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() } #[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 { std::any::type_name::<RhaiResultOf<RET>>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {
@ -212,8 +212,8 @@ macro_rules! def_register {
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<NativeCallContext<'static>>> for FN { > RegisterNativeFunction<($($mark,)*), RET, RhaiResultOf<NativeCallContext<'static>>> for FN {
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() } #[inline(always)] fn param_types() -> Box<[TypeId]> { Box::new([$(TypeId::of::<$par>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$param>()),*].into_boxed_slice() } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { Box::new([$(std::any::type_name::<$param>()),*]) }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() } #[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 { std::any::type_name::<RhaiResultOf<RET>>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {

View File

@ -9,8 +9,8 @@ use crate::func::{
}; };
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection}; use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
use crate::{ use crate::{
calc_fn_hash, calc_fn_hash_full, Dynamic, Identifier, ImmutableString, NativeCallContext, calc_fn_hash, calc_fn_hash_full, Dynamic, FnArgsVec, Identifier, ImmutableString,
RhaiResultOf, Shared, SharedModule, SmartString, StaticVec, NativeCallContext, RhaiResultOf, Shared, SharedModule, SmartString, StaticVec,
}; };
use bitflags::bitflags; use bitflags::bitflags;
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -76,10 +76,10 @@ pub struct FuncInfoMetadata {
/// Number of parameters. /// Number of parameters.
pub num_params: usize, pub num_params: usize,
/// Parameter types (if applicable). /// Parameter types (if applicable).
pub param_types: StaticVec<TypeId>, pub param_types: FnArgsVec<TypeId>,
/// Parameter names and types (if available). /// Parameter names and types (if available).
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
pub params_info: StaticVec<Identifier>, pub params_info: FnArgsVec<Identifier>,
/// Return type name. /// Return type name.
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
pub return_type: Identifier, pub return_type: Identifier,
@ -115,7 +115,7 @@ impl FuncInfo {
} }
} }
} else { } else {
let params: StaticVec<_> = self let params: FnArgsVec<_> = self
.metadata .metadata
.params_info .params_info
.iter() .iter()
@ -723,7 +723,7 @@ impl Module {
namespace: FnNamespace::Internal, namespace: FnNamespace::Internal,
access: fn_def.access, access: fn_def.access,
num_params, num_params,
param_types: StaticVec::new_const(), param_types: FnArgsVec::new_const(),
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
params_info, params_info,
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -884,7 +884,7 @@ impl Module {
hash_fn: u64, hash_fn: u64,
arg_names: impl IntoIterator<Item = S>, arg_names: impl IntoIterator<Item = S>,
) -> &mut Self { ) -> &mut Self {
let mut param_names: StaticVec<_> = let mut param_names: FnArgsVec<_> =
arg_names.into_iter().map(|s| s.as_ref().into()).collect(); arg_names.into_iter().map(|s| s.as_ref().into()).collect();
if let Some(f) = self.functions.as_mut().and_then(|m| m.get_mut(&hash_fn)) { if let Some(f) = self.functions.as_mut().and_then(|m| m.get_mut(&hash_fn)) {
@ -1005,12 +1005,11 @@ impl Module {
let _arg_names = arg_names; let _arg_names = arg_names;
let is_method = func.is_method(); let is_method = func.is_method();
let mut param_types: StaticVec<_> = arg_types let mut param_types: FnArgsVec<_> = arg_types
.as_ref() .as_ref()
.iter() .iter()
.copied()
.enumerate() .enumerate()
.map(|(i, type_id)| Self::map_type(!is_method || i > 0, type_id)) .map(|(i, &type_id)| Self::map_type(!is_method || i > 0, type_id))
.collect(); .collect();
param_types.shrink_to_fit(); param_types.shrink_to_fit();
@ -1024,8 +1023,8 @@ impl Module {
.into_iter() .into_iter()
.flatten() .flatten()
.map(|&s| s.into()) .map(|&s| s.into())
.collect::<StaticVec<_>>(); .collect::<FnArgsVec<_>>();
let return_type = if names.len() > arg_types.as_ref().len() { let return_type = if names.len() > param_types.len() {
names.pop().unwrap() names.pop().unwrap()
} else { } else {
crate::SmartString::new_const() crate::SmartString::new_const()
@ -1111,17 +1110,18 @@ impl Module {
access: FnAccess, access: FnAccess,
arg_names: Option<&[&str]>, arg_names: Option<&[&str]>,
arg_types: impl AsRef<[TypeId]>, arg_types: impl AsRef<[TypeId]>,
comments: impl AsRef<[S]>, comments: impl IntoIterator<Item = S>,
func: CallableFunction, func: CallableFunction,
) -> u64 { ) -> u64 {
let hash = self.set_fn(name, namespace, access, arg_names, arg_types, func); let hash = self.set_fn(name, namespace, access, arg_names, arg_types, func);
let comments = comments.as_ref(); self.functions
.as_mut()
if !comments.is_empty() { .unwrap()
let f = self.functions.as_mut().unwrap().get_mut(&hash).unwrap(); .get_mut(&hash)
f.metadata.comments = comments.iter().map(|s| s.as_ref().into()).collect(); .unwrap()
} .metadata
.comments = comments.into_iter().map(|s| s.as_ref().into()).collect();
hash hash
} }
@ -1251,7 +1251,7 @@ impl Module {
FnNamespace::Internal, FnNamespace::Internal,
FnAccess::Public, FnAccess::Public,
None, None,
&F::param_types(), F::param_types(),
func.into_callable_function(), func.into_callable_function(),
) )
} }
@ -1287,7 +1287,7 @@ impl Module {
FnNamespace::Global, FnNamespace::Global,
FnAccess::Public, FnAccess::Public,
None, None,
&F::param_types(), F::param_types(),
func.into_callable_function(), func.into_callable_function(),
) )
} }
@ -1328,7 +1328,7 @@ impl Module {
FnNamespace::Global, FnNamespace::Global,
FnAccess::Public, FnAccess::Public,
None, None,
&F::param_types(), F::param_types(),
func.into_callable_function(), func.into_callable_function(),
) )
} }
@ -1431,7 +1431,7 @@ impl Module {
FnNamespace::Global, FnNamespace::Global,
FnAccess::Public, FnAccess::Public,
None, None,
&F::param_types(), F::param_types(),
func.into_callable_function(), func.into_callable_function(),
) )
} }
@ -1492,7 +1492,7 @@ impl Module {
FnNamespace::Global, FnNamespace::Global,
FnAccess::Public, FnAccess::Public,
None, None,
&F::param_types(), F::param_types(),
func.into_callable_function(), func.into_callable_function(),
) )
} }