Change call_fn_raw to call_fn_with_options.

This commit is contained in:
Stephen Chung
2022-11-21 23:42:29 +08:00
parent 3feff3618a
commit d151c87687
18 changed files with 387 additions and 233 deletions

View File

@@ -66,6 +66,13 @@ impl<T: Variant + Clone> FuncArgs for Vec<T> {
}
}
impl<T: Variant + Clone, const N: usize> FuncArgs for [T; N] {
#[inline]
fn parse<ARGS: Extend<Dynamic>>(self, args: &mut ARGS) {
args.extend(IntoIterator::into_iter(self).map(Dynamic::from));
}
}
/// Macro to implement [`FuncArgs`] for tuples of standard types (each can be converted into a [`Dynamic`]).
macro_rules! impl_args {
($($p:ident),*) => {

View File

@@ -7,10 +7,10 @@ use crate::plugin::PluginFunction;
use crate::tokenizer::{is_valid_function_name, Token, TokenizeState};
use crate::types::dynamic::Variant;
use crate::{
calc_fn_hash, Dynamic, Engine, EvalContext, FuncArgs, Position, RhaiResult, RhaiResultOf,
StaticVec, VarDefInfo, ERR,
calc_fn_hash, reify, Dynamic, Engine, EvalContext, FuncArgs, Position, RhaiResult,
RhaiResultOf, StaticVec, VarDefInfo, ERR,
};
use std::any::type_name;
use std::any::{type_name, TypeId};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -305,6 +305,11 @@ impl<'a> NativeCallContext<'a> {
let result = self._call_fn_raw(fn_name, &mut args, false, false, false)?;
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
}
let typ = self.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| {
@@ -330,6 +335,11 @@ impl<'a> NativeCallContext<'a> {
let result = self._call_fn_raw(fn_name, &mut args, true, false, false)?;
// Bail out early if the return type needs no cast
if TypeId::of::<T>() == TypeId::of::<Dynamic>() {
return Ok(reify!(result => T));
}
let typ = self.engine().map_type_name(result.type_name());
result.try_cast().ok_or_else(|| {