diff --git a/CHANGELOG.md b/CHANGELOG.md index 40c5ae31..d2dafe5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Rhai Release Notes ================== +Version 0.20.3 +============== + + Version 0.20.2 ============== diff --git a/src/dynamic.rs b/src/dynamic.rs index 005252f5..a9eea0f0 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -711,6 +711,8 @@ impl fmt::Debug for Dynamic { } } +use AccessMode::*; + impl Clone for Dynamic { /// Clone the [`Dynamic`] value. /// @@ -719,33 +721,25 @@ impl Clone for Dynamic { /// The cloned copy is marked read-write even if the original is read-only. fn clone(&self) -> Self { match self.0 { - Union::Unit(value, tag, _) => Self(Union::Unit(value, tag, AccessMode::ReadWrite)), - Union::Bool(value, tag, _) => Self(Union::Bool(value, tag, AccessMode::ReadWrite)), - Union::Str(ref value, tag, _) => { - Self(Union::Str(value.clone(), tag, AccessMode::ReadWrite)) - } - Union::Char(value, tag, _) => Self(Union::Char(value, tag, AccessMode::ReadWrite)), - Union::Int(value, tag, _) => Self(Union::Int(value, tag, AccessMode::ReadWrite)), + Union::Unit(value, tag, _) => Self(Union::Unit(value, tag, ReadWrite)), + Union::Bool(value, tag, _) => Self(Union::Bool(value, tag, ReadWrite)), + Union::Str(ref value, tag, _) => Self(Union::Str(value.clone(), tag, ReadWrite)), + Union::Char(value, tag, _) => Self(Union::Char(value, tag, ReadWrite)), + Union::Int(value, tag, _) => Self(Union::Int(value, tag, ReadWrite)), #[cfg(not(feature = "no_float"))] - Union::Float(value, tag, _) => Self(Union::Float(value, tag, AccessMode::ReadWrite)), + Union::Float(value, tag, _) => Self(Union::Float(value, tag, ReadWrite)), #[cfg(feature = "decimal")] Union::Decimal(ref value, tag, _) => { - Self(Union::Decimal(value.clone(), tag, AccessMode::ReadWrite)) + Self(Union::Decimal(value.clone(), tag, ReadWrite)) } #[cfg(not(feature = "no_index"))] - Union::Array(ref value, tag, _) => { - Self(Union::Array(value.clone(), tag, AccessMode::ReadWrite)) - } + Union::Array(ref value, tag, _) => Self(Union::Array(value.clone(), tag, ReadWrite)), #[cfg(not(feature = "no_object"))] - Union::Map(ref value, tag, _) => { - Self(Union::Map(value.clone(), tag, AccessMode::ReadWrite)) - } - Union::FnPtr(ref value, tag, _) => { - Self(Union::FnPtr(value.clone(), tag, AccessMode::ReadWrite)) - } + Union::Map(ref value, tag, _) => Self(Union::Map(value.clone(), tag, ReadWrite)), + Union::FnPtr(ref value, tag, _) => Self(Union::FnPtr(value.clone(), tag, ReadWrite)), #[cfg(not(feature = "no_std"))] Union::TimeStamp(ref value, tag, _) => { - Self(Union::TimeStamp(value.clone(), tag, AccessMode::ReadWrite)) + Self(Union::TimeStamp(value.clone(), tag, ReadWrite)) } Union::Variant(ref value, tag, _) => { @@ -755,9 +749,7 @@ impl Clone for Dynamic { } #[cfg(not(feature = "no_closure"))] - Union::Shared(ref cell, tag, _) => { - Self(Union::Shared(cell.clone(), tag, AccessMode::ReadWrite)) - } + Union::Shared(ref cell, tag, _) => Self(Union::Shared(cell.clone(), tag, ReadWrite)), } } } @@ -771,21 +763,21 @@ impl Default for Dynamic { impl Dynamic { /// A [`Dynamic`] containing a `()`. - pub const UNIT: Dynamic = Self(Union::Unit((), DEFAULT_TAG, AccessMode::ReadWrite)); + pub const UNIT: Dynamic = Self(Union::Unit((), DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing a `true`. - pub const TRUE: Dynamic = Self(Union::Bool(true, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const TRUE: Dynamic = Self(Union::Bool(true, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing a [`false`]. - pub const FALSE: Dynamic = Self(Union::Bool(false, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const FALSE: Dynamic = Self(Union::Bool(false, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing the integer zero. - pub const ZERO: Dynamic = Self(Union::Int(0, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const ZERO: Dynamic = Self(Union::Int(0, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing the integer one. - pub const ONE: Dynamic = Self(Union::Int(1, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const ONE: Dynamic = Self(Union::Int(1, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing the integer two. - pub const TWO: Dynamic = Self(Union::Int(2, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const TWO: Dynamic = Self(Union::Int(2, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing the integer ten. - pub const TEN: Dynamic = Self(Union::Int(10, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const TEN: Dynamic = Self(Union::Int(10, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing the integer negative one. - pub const NEGATIVE_ONE: Dynamic = Self(Union::Int(-1, DEFAULT_TAG, AccessMode::ReadWrite)); + pub const NEGATIVE_ONE: Dynamic = Self(Union::Int(-1, DEFAULT_TAG, ReadWrite)); /// A [`Dynamic`] containing `0.0`. /// /// Not available under `no_float`. @@ -793,7 +785,7 @@ impl Dynamic { pub const FLOAT_ZERO: Dynamic = Self(Union::Float( FloatWrapper::const_new(0.0), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )); /// A [`Dynamic`] containing `1.0`. /// @@ -802,7 +794,7 @@ impl Dynamic { pub const FLOAT_ONE: Dynamic = Self(Union::Float( FloatWrapper::const_new(1.0), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )); /// A [`Dynamic`] containing `2.0`. /// @@ -811,7 +803,7 @@ impl Dynamic { pub const FLOAT_TWO: Dynamic = Self(Union::Float( FloatWrapper::const_new(2.0), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )); /// A [`Dynamic`] containing `10.0`. /// @@ -820,7 +812,7 @@ impl Dynamic { pub const FLOAT_TEN: Dynamic = Self(Union::Float( FloatWrapper::const_new(10.0), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )); /// A [`Dynamic`] containing the `-1.0`. /// @@ -829,7 +821,7 @@ impl Dynamic { pub const FLOAT_NEGATIVE_ONE: Dynamic = Self(Union::Float( FloatWrapper::const_new(-1.0), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )); /// Get the [`AccessMode`] for this [`Dynamic`]. @@ -898,7 +890,7 @@ impl Dynamic { pub fn is_read_only(&self) -> bool { #[cfg(not(feature = "no_closure"))] match self.0 { - Union::Shared(_, _, AccessMode::ReadOnly) => return true, + Union::Shared(_, _, ReadOnly) => return true, Union::Shared(ref cell, _, _) => { #[cfg(not(feature = "sync"))] let value = cell.borrow(); @@ -906,16 +898,16 @@ impl Dynamic { let value = cell.read().unwrap(); return match value.access_mode() { - AccessMode::ReadWrite => false, - AccessMode::ReadOnly => true, + ReadWrite => false, + ReadOnly => true, }; } _ => (), } match self.access_mode() { - AccessMode::ReadWrite => false, - AccessMode::ReadOnly => true, + ReadWrite => false, + ReadOnly => true, } } /// Can this [`Dynamic`] be hashed? @@ -1066,7 +1058,7 @@ impl Dynamic { Self(Union::Variant( Box::new(Box::new(value)), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )) } /// Turn the [`Dynamic`] value into a shared [`Dynamic`] value backed by an @@ -1788,37 +1780,33 @@ impl Dynamic { impl From<()> for Dynamic { #[inline(always)] fn from(value: ()) -> Self { - Self(Union::Unit(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Unit(value, DEFAULT_TAG, ReadWrite)) } } impl From for Dynamic { #[inline(always)] fn from(value: bool) -> Self { - Self(Union::Bool(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Bool(value, DEFAULT_TAG, ReadWrite)) } } impl From for Dynamic { #[inline(always)] fn from(value: INT) -> Self { - Self(Union::Int(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Int(value, DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_float"))] impl From for Dynamic { #[inline(always)] fn from(value: FLOAT) -> Self { - Self(Union::Float( - value.into(), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::Float(value.into(), DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_float"))] impl From> for Dynamic { #[inline(always)] fn from(value: FloatWrapper) -> Self { - Self(Union::Float(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Float(value, DEFAULT_TAG, ReadWrite)) } } #[cfg(feature = "decimal")] @@ -1828,20 +1816,20 @@ impl From for Dynamic { Self(Union::Decimal( Box::new(value.into()), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )) } } impl From for Dynamic { #[inline(always)] fn from(value: char) -> Self { - Self(Union::Char(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Char(value, DEFAULT_TAG, ReadWrite)) } } impl> From for Dynamic { #[inline(always)] fn from(value: S) -> Self { - Self(Union::Str(value.into(), DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::Str(value.into(), DEFAULT_TAG, ReadWrite)) } } impl From<&ImmutableString> for Dynamic { @@ -1862,11 +1850,7 @@ impl Dynamic { /// Create a [`Dynamic`] from an [`Array`]. #[inline(always)] pub(crate) fn from_array(array: Array) -> Self { - Self(Union::Array( - Box::new(array), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::Array(Box::new(array), DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_index"))] @@ -1876,7 +1860,7 @@ impl From> for Dynamic { Self(Union::Array( Box::new(value.into_iter().map(Dynamic::from).collect()), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )) } } @@ -1887,7 +1871,7 @@ impl From<&[T]> for Dynamic { Self(Union::Array( Box::new(value.iter().cloned().map(Dynamic::from).collect()), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )) } } @@ -1898,7 +1882,7 @@ impl std::iter::FromIterator for Dynamic { Self(Union::Array( Box::new(iter.into_iter().map(Dynamic::from).collect()), DEFAULT_TAG, - AccessMode::ReadWrite, + ReadWrite, )) } } @@ -1907,11 +1891,7 @@ impl Dynamic { /// Create a [`Dynamic`] from a [`Map`]. #[inline(always)] pub(crate) fn from_map(map: Map) -> Self { - Self(Union::Map( - Box::new(map), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::Map(Box::new(map), DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_object"))] @@ -1929,7 +1909,7 @@ impl, T: Variant + Clone> From, T: Variant + Clone> From for Dynamic { #[inline(always)] fn from(value: FnPtr) -> Self { - Self(Union::FnPtr( - Box::new(value), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::FnPtr(Box::new(value), DEFAULT_TAG, ReadWrite)) } } impl From> for Dynamic { #[inline(always)] fn from(value: Box) -> Self { - Self(Union::FnPtr(value, DEFAULT_TAG, AccessMode::ReadWrite)) + Self(Union::FnPtr(value, DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_std"))] impl From for Dynamic { #[inline(always)] fn from(value: Instant) -> Self { - Self(Union::TimeStamp( - Box::new(value), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::TimeStamp(Box::new(value), DEFAULT_TAG, ReadWrite)) } } #[cfg(not(feature = "no_closure"))] impl From>> for Dynamic { #[inline(always)] fn from(value: crate::Shared>) -> Self { - Self(Union::Shared( - value.into(), - DEFAULT_TAG, - AccessMode::ReadWrite, - )) + Self(Union::Shared(value.into(), DEFAULT_TAG, ReadWrite)) } } diff --git a/src/engine.rs b/src/engine.rs index 63f9ed23..7e73572c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -263,7 +263,7 @@ enum ChainArgument { Property(Position), /// Arguments to a dot method call. #[cfg(not(feature = "no_object"))] - MethodCallArgs(StaticVec, StaticVec), + MethodCallArgs(StaticVec, Position), /// Index value. #[cfg(not(feature = "no_index"))] IndexValue(Dynamic, Position), @@ -294,7 +294,7 @@ impl ChainArgument { /// Panics if not `ChainArgument::MethodCallArgs`. #[inline(always)] #[cfg(not(feature = "no_object"))] - pub fn as_fn_call_args(self) -> (StaticVec, StaticVec) { + pub fn as_fn_call_args(self) -> (StaticVec, Position) { match self { Self::Property(_) => { panic!("expecting ChainArgument::MethodCallArgs") @@ -303,16 +303,16 @@ impl ChainArgument { Self::IndexValue(_, _) => { panic!("expecting ChainArgument::MethodCallArgs") } - Self::MethodCallArgs(values, positions) => (values, positions), + Self::MethodCallArgs(values, pos) => (values, pos), } } } #[cfg(not(feature = "no_object"))] -impl From<(StaticVec, StaticVec)> for ChainArgument { +impl From<(StaticVec, Position)> for ChainArgument { #[inline(always)] - fn from((values, positions): (StaticVec, StaticVec)) -> Self { - Self::MethodCallArgs(values, positions) + fn from((values, pos): (StaticVec, Position)) -> Self { + Self::MethodCallArgs(values, pos) } } @@ -787,7 +787,7 @@ pub struct Engine { /// A module resolution service. #[cfg(not(feature = "no_module"))] - pub(crate) module_resolver: Box, + pub(crate) module_resolver: Option>, /// A map mapping type names to pretty-print names. pub(crate) type_names: BTreeMap>, @@ -890,10 +890,10 @@ impl Engine { #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] - module_resolver: Box::new(crate::module::resolvers::FileModuleResolver::new()), + module_resolver: Some(Box::new(crate::module::resolvers::FileModuleResolver::new())), #[cfg(not(feature = "no_module"))] #[cfg(any(feature = "no_std", target_arch = "wasm32",))] - module_resolver: Box::new(crate::module::resolvers::DummyModuleResolver::new()), + module_resolver: None, type_names: Default::default(), empty_string: Default::default(), @@ -950,7 +950,7 @@ impl Engine { global_sub_modules: Default::default(), #[cfg(not(feature = "no_module"))] - module_resolver: Box::new(crate::module::resolvers::DummyModuleResolver::new()), + module_resolver: None, type_names: Default::default(), empty_string: Default::default(), @@ -1629,24 +1629,25 @@ impl Engine { match expr { #[cfg(not(feature = "no_object"))] Expr::FnCall(x, _) if _parent_chain_type == ChainType::Dot && !x.is_qualified() => { - let mut arg_positions: StaticVec<_> = Default::default(); - - let mut arg_values = x + let arg_values = x .args .iter() - .inspect(|arg_expr| arg_positions.push(arg_expr.position())) .map(|arg_expr| { self.eval_expr(scope, mods, state, lib, this_ptr, arg_expr, level) .map(Dynamic::flatten) }) + .chain(x.literal_args.iter().map(|(v, _)| Ok(v.clone()))) .collect::, _>>()?; - x.literal_args + let pos = x + .args .iter() - .inspect(|(_, pos)| arg_positions.push(*pos)) - .for_each(|(v, _)| arg_values.push(v.clone())); + .map(|arg_expr| arg_expr.position()) + .chain(x.literal_args.iter().map(|(_, pos)| *pos)) + .next() + .unwrap_or_default(); - idx_values.push((arg_values, arg_positions).into()); + idx_values.push((arg_values, pos).into()); } #[cfg(not(feature = "no_object"))] Expr::FnCall(_, _) if _parent_chain_type == ChainType::Dot => { @@ -1674,24 +1675,25 @@ impl Engine { Expr::FnCall(x, _) if _parent_chain_type == ChainType::Dot && !x.is_qualified() => { - let mut arg_positions: StaticVec<_> = Default::default(); - - let mut arg_values = x + let arg_values = x .args .iter() - .inspect(|arg_expr| arg_positions.push(arg_expr.position())) .map(|arg_expr| { self.eval_expr(scope, mods, state, lib, this_ptr, arg_expr, level) .map(Dynamic::flatten) }) + .chain(x.literal_args.iter().map(|(v, _)| Ok(v.clone()))) .collect::, _>>()?; - x.literal_args + let pos = x + .args .iter() - .inspect(|(_, pos)| arg_positions.push(*pos)) - .for_each(|(v, _)| arg_values.push(v.clone())); + .map(|arg_expr| arg_expr.position()) + .chain(x.literal_args.iter().map(|(_, pos)| *pos)) + .next() + .unwrap_or_default(); - (arg_values, arg_positions).into() + (arg_values, pos).into() } #[cfg(not(feature = "no_object"))] Expr::FnCall(_, _) if _parent_chain_type == ChainType::Dot => { @@ -2037,11 +2039,7 @@ impl Engine { Expr::Unit(_) => Ok(Dynamic::UNIT), Expr::Custom(custom, _) => { - let expressions = custom - .keywords - .iter() - .map(Into::into) - .collect::>(); + let expressions: StaticVec<_> = custom.keywords.iter().map(Into::into).collect(); let key_token = custom.tokens.first().expect( "never fails because a custom syntax stream must contain at least one token", ); @@ -2757,20 +2755,26 @@ impl Engine { use crate::ModuleResolver; let source = state.source.as_ref().map(|s| s.as_str()); - let expr_pos = expr.position(); + let path_pos = expr.position(); let module = state .resolver .as_ref() - .and_then(|r| match r.resolve(self, source, &path, expr_pos) { - Ok(m) => return Some(Ok(m)), - Err(err) => match *err { - EvalAltResult::ErrorModuleNotFound(_, _) => None, - _ => return Some(Err(err)), - }, + .and_then(|r| match r.resolve(self, source, &path, path_pos) { + Err(err) + if matches!(*err, EvalAltResult::ErrorModuleNotFound(_, _)) => + { + None + } + result => Some(result), + }) + .or_else(|| { + self.module_resolver + .as_ref() + .map(|r| r.resolve(self, source, &path, path_pos)) }) .unwrap_or_else(|| { - self.module_resolver.resolve(self, source, &path, expr_pos) + EvalAltResult::ErrorModuleNotFound(path.to_string(), path_pos).into() })?; export.as_ref().map(|x| x.name.clone()).map(|name| { diff --git a/src/engine_api.rs b/src/engine_api.rs index 6a847647..b9d41ee1 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -1101,38 +1101,34 @@ impl Engine { }); } - let mut resolver = StaticModuleResolver::new(); let mut ast = self.compile_scripts_with_scope(scope, &[script])?; - let mut imports = Default::default(); - collect_imports(&ast, &mut resolver, &mut imports); + if let Some(ref module_resolver) = self.module_resolver { + let mut resolver = StaticModuleResolver::new(); + let mut imports = Default::default(); - if !imports.is_empty() { - while let Some(path) = imports.iter().next() { - let path = path.clone(); + collect_imports(&ast, &mut resolver, &mut imports); - match self - .module_resolver - .resolve_ast(self, None, &path, Position::NONE) - { - Some(Ok(module_ast)) => { - collect_imports(&module_ast, &mut resolver, &mut imports) + if !imports.is_empty() { + while let Some(path) = imports.iter().next() { + let path = path.clone(); + + match module_resolver.resolve_ast(self, None, &path, Position::NONE) { + Some(Ok(module_ast)) => { + collect_imports(&module_ast, &mut resolver, &mut imports) + } + Some(err) => return err, + None => (), } - Some(err) => return err, - None => (), + + let module = module_resolver.resolve(self, None, &path, Position::NONE)?; + let module = shared_take_or_clone(module); + + imports.remove(&path); + resolver.insert(path, module); } - - let module = shared_take_or_clone(self.module_resolver.resolve( - self, - None, - &path, - Position::NONE, - )?); - - imports.remove(&path); - resolver.insert(path, module); + ast.set_resolver(resolver); } - ast.set_resolver(resolver); } Ok(ast) diff --git a/src/engine_settings.rs b/src/engine_settings.rs index a995c71b..abce7e33 100644 --- a/src/engine_settings.rs +++ b/src/engine_settings.rs @@ -188,7 +188,7 @@ impl Engine { &mut self, resolver: impl crate::ModuleResolver + 'static, ) -> &mut Self { - self.module_resolver = Box::new(resolver); + self.module_resolver = Some(Box::new(resolver)); self } /// Disable a particular keyword or operator in the language. diff --git a/src/fn_call.rs b/src/fn_call.rs index 666add57..88e42578 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -515,13 +515,11 @@ impl Engine { ); // Merge in encapsulated environment, if any - let lib_merged; + let lib_merged: StaticVec<_>; let (unified_lib, unified) = if let Some(ref env_lib) = fn_def.lib { state.push_fn_resolution_cache(); - lib_merged = once(env_lib.as_ref()) - .chain(lib.iter().cloned()) - .collect::>(); + lib_merged = once(env_lib.as_ref()).chain(lib.iter().cloned()).collect(); (lib_merged.as_ref(), true) } else { (lib, false) @@ -895,20 +893,16 @@ impl Engine { fn_name: &str, mut hash: FnCallHashes, target: &mut crate::engine::Target, - (call_args, call_arg_positions): &mut (StaticVec, StaticVec), + (call_args, call_arg_pos): &mut (StaticVec, Position), pos: Position, level: usize, ) -> Result<(Dynamic, bool), Box> { let is_ref = target.is_ref(); - // Get a reference to the mutation target Dynamic - let obj = target.as_mut(); - let mut fn_name = fn_name; - let (result, updated) = match fn_name { - KEYWORD_FN_PTR_CALL if obj.is::() => { + KEYWORD_FN_PTR_CALL if target.is::() => { // FnPtr call - let fn_ptr = obj + let fn_ptr = target .read_lock::() .expect("never fails because `obj` is `FnPtr`"); // Redirect function name @@ -917,11 +911,8 @@ impl Engine { // Recalculate hashes let new_hash = FnCallHashes::from_script(calc_fn_hash(fn_name, args_len)); // Arguments are passed as-is, adding the curried arguments - let mut curry = fn_ptr.curry().iter().cloned().collect::>(); - let mut args = curry - .iter_mut() - .chain(call_args.iter_mut()) - .collect::>(); + let mut curry: StaticVec<_> = fn_ptr.curry().iter().cloned().collect(); + let mut args: StaticVec<_> = curry.iter_mut().chain(call_args.iter_mut()).collect(); // Map it to name(args) in function-call style self.exec_fn_call( @@ -932,20 +923,19 @@ impl Engine { if call_args.len() > 0 { if !call_args[0].is::() { return Err(self.make_type_mismatch_err::( - self.map_type_name(obj.type_name()), - call_arg_positions[0], + self.map_type_name(target.type_name()), + *call_arg_pos, )); } } else { return Err(self.make_type_mismatch_err::( - self.map_type_name(obj.type_name()), + self.map_type_name(target.type_name()), pos, )); } // FnPtr call on object let fn_ptr = call_args.remove(0).cast::(); - call_arg_positions.remove(0); // Redirect function name let fn_name = fn_ptr.fn_name(); let args_len = call_args.len() + fn_ptr.curry().len(); @@ -955,11 +945,11 @@ impl Engine { calc_fn_hash(fn_name, args_len + 1), ); // Replace the first argument with the object pointer, adding the curried arguments - let mut curry = fn_ptr.curry().iter().cloned().collect::>(); - let mut args = once(obj) + let mut curry: StaticVec<_> = fn_ptr.curry().iter().cloned().collect(); + let mut args: StaticVec<_> = once(target.as_mut()) .chain(curry.iter_mut()) .chain(call_args.iter_mut()) - .collect::>(); + .collect(); // Map it to name(args) in function-call style self.exec_fn_call( @@ -967,14 +957,14 @@ impl Engine { ) } KEYWORD_FN_PTR_CURRY => { - if !obj.is::() { + if !target.is::() { return Err(self.make_type_mismatch_err::( - self.map_type_name(obj.type_name()), + self.map_type_name(target.type_name()), pos, )); } - let fn_ptr = obj + let fn_ptr = target .read_lock::() .expect("never fails because `obj` is `FnPtr`"); @@ -1005,26 +995,21 @@ impl Engine { } _ => { + let mut fn_name = fn_name; let _redirected; // Check if it is a map method call in OOP style #[cfg(not(feature = "no_object"))] - if let Some(map) = obj.read_lock::() { + if let Some(map) = target.read_lock::() { if let Some(val) = map.get(fn_name) { if let Some(fn_ptr) = val.read_lock::() { // Remap the function name _redirected = fn_ptr.get_fn_name().clone(); fn_name = &_redirected; // Add curried arguments - fn_ptr - .curry() - .iter() - .cloned() - .enumerate() - .for_each(|(i, v)| { - call_args.insert(i, v); - call_arg_positions.insert(i, Position::NONE); - }); + if fn_ptr.is_curried() { + call_args.insert_many(0, fn_ptr.curry().iter().cloned()); + } // Recalculate the hash based on the new function name and new arguments hash = FnCallHashes::from_script_and_native( calc_fn_hash(fn_name, call_args.len()), @@ -1035,9 +1020,8 @@ impl Engine { }; // Attached object pointer in front of the arguments - let mut args = once(obj) - .chain(call_args.iter_mut()) - .collect::>(); + let mut args: StaticVec<_> = + once(target.as_mut()).chain(call_args.iter_mut()).collect(); self.exec_fn_call( mods, state, lib, fn_name, hash, &mut args, is_ref, true, pos, None, level, diff --git a/src/fn_func.rs b/src/fn_func.rs index 3c51911a..c465dab6 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -4,7 +4,7 @@ #![allow(non_snake_case)] use crate::dynamic::Variant; -use crate::{Engine, EvalAltResult, ParseError, Scope, AST}; +use crate::{Engine, EvalAltResult, ParseError, Scope, SmartString, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -98,7 +98,7 @@ macro_rules! def_anonymous_fn { #[inline(always)] fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output { - let fn_name = entry_point.to_string(); + let fn_name: SmartString = entry_point.into(); Box::new(move |$($par),*| self.call_fn(&mut Scope::new(), &ast, &fn_name, ($($par,)*))) } diff --git a/src/fn_native.rs b/src/fn_native.rs index 0b22fe9b..964488d0 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -314,6 +314,11 @@ impl FnPtr { pub fn is_curried(&self) -> bool { !self.1.is_empty() } + /// Get the number of curried arguments. + #[inline(always)] + pub fn num_curried(&self) -> usize { + self.1.len() + } /// Does the function pointer refer to an anonymous function? /// /// Not available under `no_function`. @@ -339,7 +344,7 @@ impl FnPtr { this_ptr: Option<&mut Dynamic>, mut arg_values: impl AsMut<[Dynamic]>, ) -> RhaiResult { - let mut args_data; + let mut args_data: StaticVec<_>; let arg_values = if self.curry().is_empty() { arg_values.as_mut() @@ -349,7 +354,7 @@ impl FnPtr { .iter() .cloned() .chain(arg_values.as_mut().iter_mut().map(mem::take)) - .collect::>(); + .collect(); args_data.as_mut() }; diff --git a/src/optimize.rs b/src/optimize.rs index 52c4926e..90fae768 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -147,7 +147,7 @@ fn call_fn_with_constant_arguments( state.lib, fn_name, calc_fn_hash(fn_name, arg_values.len()), - arg_values.iter_mut().collect::>().as_mut(), + &mut arg_values.iter_mut().collect::>(), false, false, Position::NONE, diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 13cbcb19..599a7540 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -745,7 +745,7 @@ mod array_functions { len as usize }; - let mut drained = array.drain(..start).collect::(); + let mut drained: Array = array.drain(..start).collect(); drained.extend(array.drain(len..)); drained diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 97700e86..a88ddf84 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -110,7 +110,7 @@ mod string_functions { pub fn index_of_char_starting_from(string: &str, character: char, start: INT) -> INT { let start = if start < 0 { if let Some(n) = start.checked_abs() { - let chars = string.chars().collect::>(); + let chars: Vec<_> = string.chars().collect(); let num_chars = chars.len(); if n as usize > num_chars { 0 diff --git a/src/parser.rs b/src/parser.rs index d087f518..6ea22399 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1864,7 +1864,7 @@ fn parse_custom_syntax( state.stack.push((empty, AccessMode::ReadWrite)); } - let parse_func = &syntax.parse; + let parse_func = syntax.parse.as_ref(); segments.push(key.into()); tokens.push(key.into());