From 314ec5e4d21adf9f8c56cae496bf42221597bd43 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 11 May 2020 10:29:33 +0800 Subject: [PATCH 01/36] Remove checks for number of arguments. --- src/fn_register.rs | 13 +------------ src/module.rs | 20 ++++++++++---------- src/packages/utils.rs | 33 --------------------------------- 3 files changed, 11 insertions(+), 55 deletions(-) diff --git a/src/fn_register.rs b/src/fn_register.rs index c1549185..f58b023d 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -131,12 +131,6 @@ pub fn by_value(data: &mut Dynamic) -> T { mem::take(data).cast::() } -/// This macro counts the number of arguments via recursion. -macro_rules! count_args { - () => { 0_usize }; - ( $head:ident $($tail:ident)* ) => { 1_usize + count_args!($($tail)*) }; -} - /// This macro creates a closure wrapping a registered function. macro_rules! make_func { ($fn_name:ident : $fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { @@ -147,12 +141,7 @@ macro_rules! make_func { // ^ dereferencing function move |args: &mut FnCallArgs, pos: Position| { - // Check for length at the beginning to avoid per-element bound checks. - const NUM_ARGS: usize = count_args!($($par)*); - - if args.len() != NUM_ARGS { - return Err(Box::new(EvalAltResult::ErrorFunctionArgsMismatch($fn_name.clone(), NUM_ARGS, args.len(), pos))); - } + // The arguments are assumed to be of the correct number and types! #[allow(unused_variables, unused_mut)] let mut drain = args.iter_mut(); diff --git a/src/module.rs b/src/module.rs index 0519a44b..0cf82564 100644 --- a/src/module.rs +++ b/src/module.rs @@ -300,7 +300,7 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs, pos| { func() - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![]; @@ -328,7 +328,7 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(mem::take(args[0]).cast::()) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; @@ -356,7 +356,7 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(args[0].downcast_mut::().unwrap()) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; @@ -389,7 +389,7 @@ impl Module { let b = mem::take(args[1]).cast::(); func(a, b) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; @@ -426,7 +426,7 @@ impl Module { let a = args[0].downcast_mut::().unwrap(); func(a, b) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; @@ -466,7 +466,7 @@ impl Module { let c = mem::take(args[2]).cast::(); func(a, b, c) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; @@ -507,7 +507,7 @@ impl Module { let a = args[0].downcast_mut::().unwrap(); func(a, b, c) - .map(|v| v.into()) + .map(Into::::into) .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; @@ -639,7 +639,7 @@ impl Module { // Index all variables for (var_name, value) in module.variables.iter() { // Qualifiers + variable name - let hash = calc_fn_hash(qualifiers.iter().map(|v| *v), var_name, empty()); + let hash = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); variables.push((hash, value.clone())); } // Index all Rust functions @@ -653,7 +653,7 @@ impl Module { // 1) Calculate a hash in a similar manner to script-defined functions, // i.e. qualifiers + function name + dummy parameter types (one for each parameter). let hash_fn_def = calc_fn_hash( - qualifiers.iter().map(|v| *v), + qualifiers.iter().map(|&v| v), fn_name, repeat(EMPTY_TYPE_ID()).take(params.len()), ); @@ -674,7 +674,7 @@ impl Module { } // Qualifiers + function name + placeholders (one for each parameter) let hash = calc_fn_hash( - qualifiers.iter().map(|v| *v), + qualifiers.iter().map(|&v| v), &fn_def.name, repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); diff --git a/src/packages/utils.rs b/src/packages/utils.rs index fbb8c31d..7a6e2231 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -63,25 +63,6 @@ macro_rules! def_package { }; } -/// Check whether the correct number of arguments is passed to the function. -fn check_num_args( - name: &str, - num_args: usize, - args: &mut FnCallArgs, - pos: Position, -) -> Result<(), Box> { - if args.len() != num_args { - Err(Box::new(EvalAltResult::ErrorFunctionArgsMismatch( - name.to_string(), - num_args, - args.len(), - pos, - ))) - } else { - Ok(()) - } -} - /// Add a function with no parameters to the package. /// /// `map_result` is a function that maps the return type of the function to `Result`. @@ -121,8 +102,6 @@ pub fn reg_none( let hash = calc_fn_hash(empty(), fn_name, ([] as [TypeId; 0]).iter().cloned()); let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { - check_num_args(fn_name, 0, args, pos)?; - let r = func(); map_result(r, pos) }); @@ -171,8 +150,6 @@ pub fn reg_unary( let hash = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { - check_num_args(fn_name, 1, args, pos)?; - let mut drain = args.iter_mut(); let x = mem::take(*drain.next().unwrap()).cast::(); @@ -231,8 +208,6 @@ pub fn reg_unary_mut( let hash = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { - check_num_args(fn_name, 1, args, pos)?; - let mut drain = args.iter_mut(); let x: &mut T = drain.next().unwrap().downcast_mut().unwrap(); @@ -288,8 +263,6 @@ pub fn reg_binary( ); let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { - check_num_args(fn_name, 2, args, pos)?; - let mut drain = args.iter_mut(); let x = mem::take(*drain.next().unwrap()).cast::(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -353,8 +326,6 @@ pub fn reg_binary_mut( ); let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { - check_num_args(fn_name, 2, args, pos)?; - let mut drain = args.iter_mut(); let x: &mut A = drain.next().unwrap().downcast_mut().unwrap(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -394,8 +365,6 @@ pub fn reg_trinary(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -436,8 +405,6 @@ pub fn reg_trinary_mut(); From 4a8710a4a94720cd66b0c5976887373d748cdeff Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 11 May 2020 13:36:50 +0800 Subject: [PATCH 02/36] Create NativeCallable trait. --- src/engine.rs | 40 ++++++--------------------------- src/fn_native.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ src/fn_register.rs | 37 ++++++++++++++---------------- src/lib.rs | 2 ++ src/module.rs | 27 ++++++++++++---------- src/optimize.rs | 6 ++--- src/packages/mod.rs | 8 +++---- src/packages/utils.rs | 16 ++++++------- src/parser.rs | 7 ++++++ 9 files changed, 115 insertions(+), 80 deletions(-) create mode 100644 src/fn_native.rs diff --git a/src/engine.rs b/src/engine.rs index 8428de09..926e2e8f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,11 +3,12 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; +use crate::fn_native::{FnCallArgs, NativeFunction, SharedNativeFunction}; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, }; -use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; +use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -46,19 +47,6 @@ pub type Array = Vec; #[cfg(not(feature = "no_object"))] pub type Map = HashMap; -pub type FnCallArgs<'a> = [&'a mut Dynamic]; - -#[cfg(feature = "sync")] -pub type FnAny = - dyn Fn(&mut FnCallArgs, Position) -> Result> + Send + Sync; -#[cfg(not(feature = "sync"))] -pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result>; - -#[cfg(feature = "sync")] -pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; -#[cfg(not(feature = "sync"))] -pub type IteratorFn = dyn Fn(Dynamic) -> Box>; - #[cfg(debug_assertions)] pub const MAX_CALL_STACK_DEPTH: usize = 28; @@ -168,20 +156,6 @@ impl<'a> State<'a> { } } -/// An external native Rust function. -#[cfg(not(feature = "sync"))] -pub type NativeFunction = Rc>; -/// An external native Rust function. -#[cfg(feature = "sync")] -pub type NativeFunction = Arc>; - -/// A sharable script-defined function. -#[cfg(feature = "sync")] -pub type ScriptedFunction = Arc; -/// A sharable script-defined function. -#[cfg(not(feature = "sync"))] -pub type ScriptedFunction = Rc; - /// A type that holds a library (`HashMap`) of script-defined functions. /// /// Since script-defined functions have `Dynamic` parameters, functions with the same name @@ -190,7 +164,7 @@ pub type ScriptedFunction = Rc; /// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash` /// with dummy parameter types `EMPTY_TYPE_ID()` repeated the correct number of times. #[derive(Debug, Clone, Default)] -pub struct FunctionsLib(HashMap); +pub struct FunctionsLib(HashMap); impl FunctionsLib { /// Create a new `FunctionsLib` from a collection of `FnDef`. @@ -261,8 +235,8 @@ impl FunctionsLib { } } -impl From> for FunctionsLib { - fn from(values: Vec<(u64, ScriptedFunction)>) -> Self { +impl From> for FunctionsLib { + fn from(values: Vec<(u64, SharedFnDef)>) -> Self { FunctionsLib(values.into_iter().collect()) } } @@ -596,7 +570,7 @@ impl Engine { .or_else(|| self.packages.get_function(hash_fn_spec)) { // Run external function - let result = func(args, pos)?; + let result = func.call(args, pos)?; // See if the function match print/debug (which requires special processing) return Ok(match fn_name { @@ -1476,7 +1450,7 @@ impl Engine { let hash = *hash_fn_def ^ hash_fn_args; match module.get_qualified_fn(name, hash, *pos) { - Ok(func) => func(args.as_mut(), *pos), + Ok(func) => func.call(args.as_mut(), *pos), Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), Err(err) => Err(err), } diff --git a/src/fn_native.rs b/src/fn_native.rs new file mode 100644 index 00000000..dfabc1d3 --- /dev/null +++ b/src/fn_native.rs @@ -0,0 +1,52 @@ +use crate::any::Dynamic; +use crate::result::EvalAltResult; +use crate::token::Position; + +use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; + +pub type FnCallArgs<'a> = [&'a mut Dynamic]; + +#[cfg(feature = "sync")] +pub type FnAny = + dyn Fn(&mut FnCallArgs, Position) -> Result> + Send + Sync; +#[cfg(not(feature = "sync"))] +pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result>; + +#[cfg(feature = "sync")] +pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; +#[cfg(not(feature = "sync"))] +pub type IteratorFn = dyn Fn(Dynamic) -> Box>; + +/// A trait implemented by all native Rust functions that are callable by Rhai. +pub trait NativeCallable { + /// Call a native Rust function. + fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; +} + +/// A type encapsulating a native Rust function callable by Rhai. +pub struct NativeFunction(Box); + +impl NativeCallable for NativeFunction { + fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result> { + (self.0)(args, pos) + } +} + +impl From> for NativeFunction { + fn from(func: Box) -> Self { + Self::new(func) + } +} +impl NativeFunction { + /// Create a new `NativeFunction`. + pub fn new(func: Box) -> Self { + Self(func) + } +} + +/// An external native Rust function. +#[cfg(not(feature = "sync"))] +pub type SharedNativeFunction = Rc>; +/// An external native Rust function. +#[cfg(feature = "sync")] +pub type SharedNativeFunction = Arc>; diff --git a/src/fn_register.rs b/src/fn_register.rs index f58b023d..a4e7d214 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -3,7 +3,8 @@ #![allow(non_snake_case)] use crate::any::{Dynamic, Variant}; -use crate::engine::{Engine, FnCallArgs}; +use crate::engine::Engine; +use crate::fn_native::{FnCallArgs, NativeFunction}; use crate::result::EvalAltResult; use crate::token::Position; use crate::utils::calc_fn_spec; @@ -117,14 +118,14 @@ pub struct Mut(T); //pub struct Ref(T); /// Dereference into &mut. -#[inline] +#[inline(always)] pub fn by_ref(data: &mut Dynamic) -> &mut T { // Directly cast the &mut Dynamic into &mut T to access the underlying data. data.downcast_mut::().unwrap() } /// Dereference into value. -#[inline] +#[inline(always)] pub fn by_value(data: &mut Dynamic) -> T { // We consume the argument and then replace it with () - the argument is not supposed to be used again. // This way, we avoid having to clone the argument again, because it is already a clone when passed here. @@ -133,14 +134,13 @@ pub fn by_value(data: &mut Dynamic) -> T { /// This macro creates a closure wrapping a registered function. macro_rules! make_func { - ($fn_name:ident : $fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { -// ^ function name -// ^ function pointer -// ^ result mapping function -// ^ function parameter generic type name (A, B, C etc.) -// ^ dereferencing function + ($fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { +// ^ function pointer +// ^ result mapping function +// ^ function parameter generic type name (A, B, C etc.) +// ^ dereferencing function - move |args: &mut FnCallArgs, pos: Position| { + NativeFunction::new(Box::new(move |args: &mut FnCallArgs, pos: Position| { // The arguments are assumed to be of the correct number and types! #[allow(unused_variables, unused_mut)] @@ -156,12 +156,12 @@ macro_rules! make_func { // Map the result $map(r, pos) - }; + })); }; } /// To Dynamic mapping function. -#[inline] +#[inline(always)] pub fn map_dynamic( data: T, _pos: Position, @@ -170,13 +170,13 @@ pub fn map_dynamic( } /// To Dynamic mapping function. -#[inline] +#[inline(always)] pub fn map_identity(data: Dynamic, _pos: Position) -> Result> { Ok(data) } /// To `Result>` mapping function. -#[inline] +#[inline(always)] pub fn map_result( data: Result>, pos: Position, @@ -207,8 +207,7 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - let fn_name = name.to_string(); - let func = make_func!(fn_name : f : map_dynamic ; $($par => $clone),*); + let func = make_func!(f : map_dynamic ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } @@ -225,8 +224,7 @@ macro_rules! def_register { > RegisterDynamicFn for Engine { fn register_dynamic_fn(&mut self, name: &str, f: FN) { - let fn_name = name.to_string(); - let func = make_func!(fn_name : f : map_identity ; $($par => $clone),*); + let func = make_func!(f : map_identity ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } @@ -244,8 +242,7 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - let fn_name = name.to_string(); - let func = make_func!(fn_name : f : map_result ; $($par => $clone),*); + let func = make_func!(f : map_result ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } diff --git a/src/lib.rs b/src/lib.rs index 1bdf8fd7..a912bdab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,7 @@ mod engine; mod error; mod fn_call; mod fn_func; +mod fn_native; mod fn_register; mod module; mod optimize; @@ -90,6 +91,7 @@ pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; pub use fn_call::FuncArgs; +pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use parser::{AST, INT}; pub use result::EvalAltResult; diff --git a/src/module.rs b/src/module.rs index 0cf82564..284d662b 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,8 +3,9 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, NativeFunction, ScriptedFunction}; -use crate::parser::{FnAccess, FnDef, AST}; +use crate::engine::{Engine, FunctionsLib}; +use crate::fn_native::{FnAny, FnCallArgs, NativeCallable, NativeFunction, SharedNativeFunction}; +use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; @@ -57,10 +58,10 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, NativeFunction)>, + functions: HashMap, SharedNativeFunction)>, /// Flattened collection of all external Rust functions, including those in sub-modules. - all_functions: HashMap, + all_functions: HashMap, /// Script-defined functions. fn_lib: FunctionsLib, @@ -269,12 +270,14 @@ impl Module { ) -> u64 { let hash = calc_fn_hash(empty(), &fn_name, params.iter().cloned()); + let f = Box::new(NativeFunction::from(func)) as Box; + #[cfg(not(feature = "sync"))] - self.functions - .insert(hash, (fn_name, access, params, Rc::new(func))); + let func = Rc::new(f); #[cfg(feature = "sync")] - self.functions - .insert(hash, (fn_name, access, params, Arc::new(func))); + let func = Arc::new(f); + + self.functions.insert(hash, (fn_name, access, params, func)); hash } @@ -528,7 +531,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash: u64) -> Option<&Box> { + pub fn get_fn(&self, hash: u64) -> Option<&Box> { self.functions.get(&hash).map(|(_, _, _, v)| v.as_ref()) } @@ -541,7 +544,7 @@ impl Module { name: &str, hash: u64, pos: Position, - ) -> Result<&Box, Box> { + ) -> Result<&Box, Box> { self.all_functions .get(&hash) .map(|f| f.as_ref()) @@ -626,8 +629,8 @@ impl Module { module: &'a mut Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, NativeFunction)>, - fn_lib: &mut Vec<(u64, ScriptedFunction)>, + functions: &mut Vec<(u64, SharedNativeFunction)>, + fn_lib: &mut Vec<(u64, SharedFnDef)>, ) { for (name, m) in module.modules.iter_mut() { // Index all the sub-modules first. diff --git a/src/optimize.rs b/src/optimize.rs index ca18d1ce..060b9c3c 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -1,9 +1,9 @@ use crate::any::Dynamic; use crate::calc_fn_hash; use crate::engine::{ - Engine, FnAny, FnCallArgs, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, - KEYWORD_TYPE_OF, + Engine, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; +use crate::fn_native::FnCallArgs; use crate::packages::{PackageStore, PackagesCollection}; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; @@ -123,7 +123,7 @@ fn call_fn( base_package .get_function(hash) .or_else(|| packages.get_function(hash)) - .map(|func| func(args, pos)) + .map(|func| func.call(args, pos)) .transpose() } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 0d935e40..f9b50d90 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! This module contains all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::engine::{FnAny, IteratorFn}; +use crate::fn_native::{IteratorFn, NativeCallable}; use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; @@ -48,7 +48,7 @@ pub trait Package { /// Type to store all functions in the package. pub struct PackageStore { /// All functions, keyed by a hash created from the function name and parameter types. - pub functions: HashMap>, + pub functions: HashMap>, /// All iterator functions, keyed by the type producing the iterator. pub type_iterators: HashMap>, @@ -64,7 +64,7 @@ impl PackageStore { self.functions.contains_key(&hash) } /// Get specified function via its hash key. - pub fn get_function(&self, hash: u64) -> Option<&Box> { + pub fn get_function(&self, hash: u64) -> Option<&Box> { self.functions.get(&hash) } /// Does the specified TypeId iterator exist in the `PackageStore`? @@ -113,7 +113,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_function(hash)) } /// Get specified function via its hash key. - pub fn get_function(&self, hash: u64) -> Option<&Box> { + pub fn get_function(&self, hash: u64) -> Option<&Box> { self.packages .iter() .map(|p| p.get_function(hash)) diff --git a/src/packages/utils.rs b/src/packages/utils.rs index 7a6e2231..d4783811 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -2,7 +2,7 @@ use super::PackageStore; use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::engine::FnCallArgs; +use crate::fn_native::{FnCallArgs, NativeFunction}; use crate::result::EvalAltResult; use crate::token::Position; @@ -106,7 +106,7 @@ pub fn reg_none( map_result(r, pos) }); - lib.functions.insert(hash, f); + lib.functions.insert(hash, Box::new(NativeFunction::new(f))); } /// Add a function with one parameter to the package. @@ -157,7 +157,7 @@ pub fn reg_unary( map_result(r, pos) }); - lib.functions.insert(hash, f); + lib.functions.insert(hash, Box::new(NativeFunction::new(f))); } /// Add a function with one mutable reference parameter to the package. @@ -215,7 +215,7 @@ pub fn reg_unary_mut( map_result(r, pos) }); - lib.functions.insert(hash, f); + lib.functions.insert(hash, Box::new(NativeFunction::new(f))); } /// Add a function with two parameters to the package. @@ -271,7 +271,7 @@ pub fn reg_binary( map_result(r, pos) }); - lib.functions.insert(hash, f); + lib.functions.insert(hash, Box::new(NativeFunction::new(f))); } /// Add a function with two parameters (the first one being a mutable reference) to the package. @@ -334,7 +334,7 @@ pub fn reg_binary_mut( map_result(r, pos) }); - lib.functions.insert(hash, f); + lib.functions.insert(hash, Box::new(NativeFunction::new(f))); } /// Add a function with three parameters to the package. @@ -374,7 +374,7 @@ pub fn reg_trinary; +/// A sharable script-defined function. +#[cfg(not(feature = "sync"))] +pub type SharedFnDef = Rc; + /// `return`/`throw` statement. #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] pub enum ReturnType { From 414f3d3c23a77f80565d3c49ef86360e208951d8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 11 May 2020 18:55:58 +0800 Subject: [PATCH 03/36] Fix bug with calling a pure function method-call style. --- src/engine.rs | 175 +++++++++++++++++++++++++----------- src/fn_native.rs | 27 ++++-- src/fn_register.rs | 40 +++++---- src/module.rs | 51 ++++++----- src/packages/array_basic.rs | 7 +- src/packages/iter_basic.rs | 4 +- src/packages/utils.rs | 23 +++-- tests/method_call.rs | 9 ++ 8 files changed, 221 insertions(+), 115 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 926e2e8f..4cf71e9a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunction, SharedNativeFunction}; +use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI, SharedNativeFunction}; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, @@ -544,13 +544,14 @@ impl Engine { scope: Option<&mut Scope>, state: &State, fn_name: &str, + is_protected: bool, hash_fn_spec: u64, hash_fn_def: u64, args: &mut FnCallArgs, def_val: Option<&Dynamic>, pos: Position, level: usize, - ) -> Result> { + ) -> Result<(Dynamic, bool), Box> { // Check for stack overflow if level > self.max_call_stack_depth { return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); @@ -559,7 +560,9 @@ impl Engine { // First search in script-defined functions (can override built-in) if hash_fn_def > 0 { if let Some(fn_def) = state.get_function(hash_fn_def) { - return self.call_script_fn(scope, state, fn_def, args, pos, level); + return self + .call_script_fn(scope, state, fn_def, args, pos, level) + .map(|v| (v, false)); } } @@ -569,32 +572,61 @@ impl Engine { .get_function(hash_fn_spec) .or_else(|| self.packages.get_function(hash_fn_spec)) { + let mut backup: Dynamic = ().into(); + let mut restore = false; + + let updated = match func.abi() { + // Calling pure function in method-call + NativeFunctionABI::Pure if is_protected && args.len() > 0 => { + backup = args[0].clone(); + restore = true; + false + } + NativeFunctionABI::Pure => false, + NativeFunctionABI::Method => true, + }; + // Run external function - let result = func.call(args, pos)?; + let result = match func.call(args, pos) { + Ok(r) => { + // Restore the backup value for the first argument since it has been consumed! + if restore { + *args[0] = backup; + } + r + } + Err(err) => return Err(err), + }; // See if the function match print/debug (which requires special processing) return Ok(match fn_name { - KEYWORD_PRINT => (self.print)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - KEYWORD_DEBUG => (self.debug)(result.as_str().map_err(|type_name| { - Box::new(EvalAltResult::ErrorMismatchOutputType( - type_name.into(), - pos, - )) - })?) - .into(), - _ => result, + KEYWORD_PRINT => ( + (self.print)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + KEYWORD_DEBUG => ( + (self.debug)(result.as_str().map_err(|type_name| { + Box::new(EvalAltResult::ErrorMismatchOutputType( + type_name.into(), + pos, + )) + })?) + .into(), + false, + ), + _ => (result, updated), }); } // Return default value (if any) if let Some(val) = def_val { - return Ok(val.clone()); + return Ok((val.clone(), false)); } // Getter function not found? @@ -730,12 +762,13 @@ impl Engine { &self, state: &State, fn_name: &str, + is_protected: bool, hash_fn_def: u64, args: &mut FnCallArgs, def_val: Option<&Dynamic>, pos: Position, level: usize, - ) -> Result> { + ) -> Result<(Dynamic, bool), Box> { // Qualifiers (none) + function name + argument `TypeId`'s. let hash_fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); @@ -744,7 +777,10 @@ impl Engine { KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, hash_fn_spec, hash_fn_def) => { - Ok(self.map_type_name(args[0].type_name()).to_string().into()) + Ok(( + self.map_type_name(args[0].type_name()).to_string().into(), + false, + )) } // eval - reaching this point it must be a method-style call @@ -757,11 +793,12 @@ impl Engine { ))) } - // Normal method call + // Normal function call _ => self.call_fn_raw( None, state, fn_name, + is_protected, hash_fn_spec, hash_fn_def, args, @@ -820,10 +857,10 @@ impl Engine { mut new_val: Option, ) -> Result<(Dynamic, bool), Box> { // Get a reference to the mutation target Dynamic - let obj = match target { - Target::Ref(r) => r, - Target::Value(ref mut r) => r.as_mut(), - Target::StringChar(ref mut x) => &mut x.2, + let (obj, is_protected) = match target { + Target::Ref(r) => (r, true), + Target::Value(ref mut r) => (r.as_mut(), false), + Target::StringChar(ref mut x) => (&mut x.2, false), }; // Pop the last index value @@ -835,8 +872,15 @@ impl Engine { Expr::Dot(x) | Expr::Index(x) => { let is_index = matches!(rhs, Expr::Index(_)); - let indexed_val = - self.get_indexed_mut(state, obj, idx_val, x.0.position(), op_pos, false)?; + let indexed_val = self.get_indexed_mut( + state, + obj, + is_protected, + idx_val, + x.0.position(), + op_pos, + false, + )?; self.eval_dot_index_chain_helper( state, indexed_val, @@ -850,14 +894,29 @@ impl Engine { } // xxx[rhs] = new_val _ if new_val.is_some() => { - let mut indexed_val = - self.get_indexed_mut(state, obj, idx_val, rhs.position(), op_pos, true)?; + let mut indexed_val = self.get_indexed_mut( + state, + obj, + is_protected, + idx_val, + rhs.position(), + op_pos, + true, + )?; indexed_val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // xxx[rhs] _ => self - .get_indexed_mut(state, obj, idx_val, rhs.position(), op_pos, false) + .get_indexed_mut( + state, + obj, + is_protected, + idx_val, + rhs.position(), + op_pos, + false, + ) .map(|v| (v.clone_into_dynamic(), false)), } } else { @@ -865,8 +924,9 @@ impl Engine { // xxx.fn_name(arg_expr_list) Expr::FnCall(x) if x.1.is_none() => { let ((name, pos), _, hash, _, def_val) = x.as_ref(); + let def_val = def_val.as_ref(); - let mut args: StaticVec<_> = once(obj) + let mut arg_values: StaticVec<_> = once(obj) .chain( idx_val .downcast_mut::>() @@ -874,10 +934,9 @@ impl Engine { .iter_mut(), ) .collect(); - // A function call is assumed to have side effects, so the value is changed - // TODO - Remove assumption of side effects by checking whether the first parameter is &mut - self.exec_fn_call(state, name, *hash, args.as_mut(), def_val.as_ref(), *pos, 0) - .map(|v| (v, true)) + let args = arg_values.as_mut(); + + self.exec_fn_call(state, name, is_protected, *hash, args, def_val, *pos, 0) } // xxx.module::fn_name(...) - syntax error Expr::FnCall(_) => unreachable!(), @@ -886,7 +945,7 @@ impl Engine { Expr::Property(x) if obj.is::() && new_val.is_some() => { let index = x.0.clone().into(); let mut indexed_val = - self.get_indexed_mut(state, obj, index, x.1, op_pos, true)?; + self.get_indexed_mut(state, obj, is_protected, index, x.1, op_pos, true)?; indexed_val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } @@ -895,22 +954,22 @@ impl Engine { Expr::Property(x) if obj.is::() => { let index = x.0.clone().into(); let indexed_val = - self.get_indexed_mut(state, obj, index, x.1, op_pos, false)?; + self.get_indexed_mut(state, obj, is_protected, index, x.1, op_pos, false)?; Ok((indexed_val.clone_into_dynamic(), false)) } // xxx.id = ??? a Expr::Property(x) if new_val.is_some() => { let fn_name = make_setter(&x.0); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, &fn_name, 0, &mut args, None, x.1, 0) - .map(|v| (v, true)) + self.exec_fn_call(state, &fn_name, is_protected, 0, &mut args, None, x.1, 0) + .map(|(v, _)| (v, true)) } // xxx.id Expr::Property(x) => { let fn_name = make_getter(&x.0); let mut args = [obj]; - self.exec_fn_call(state, &fn_name, 0, &mut args, None, x.1, 0) - .map(|v| (v, false)) + self.exec_fn_call(state, &fn_name, is_protected, 0, &mut args, None, x.1, 0) + .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] // {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs @@ -919,7 +978,7 @@ impl Engine { let indexed_val = if let Expr::Property(p) = &x.0 { let index = p.0.clone().into(); - self.get_indexed_mut(state, obj, index, x.2, op_pos, false)? + self.get_indexed_mut(state, obj, is_protected, index, x.2, op_pos, false)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -941,18 +1000,21 @@ impl Engine { // xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs Expr::Index(x) | Expr::Dot(x) => { let is_index = matches!(rhs, Expr::Index(_)); - let mut args = [obj, &mut Default::default()]; + let mut arg_values = [obj, &mut Default::default()]; - let indexed_val = &mut (if let Expr::Property(p) = &x.0 { + let (mut indexed_val, updated) = if let Expr::Property(p) = &x.0 { let fn_name = make_getter(&p.0); - self.exec_fn_call(state, &fn_name, 0, &mut args[..1], None, x.2, 0)? + let args = &mut arg_values[..1]; + self.exec_fn_call(state, &fn_name, is_protected, 0, args, None, x.2, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( "".to_string(), rhs.position(), ))); - }); + }; + let indexed_val = &mut indexed_val; + let (result, may_be_changed) = self.eval_dot_index_chain_helper( state, indexed_val.into(), @@ -965,12 +1027,13 @@ impl Engine { )?; // Feed the value back via a setter just in case it has been updated - if may_be_changed { + if updated || may_be_changed { if let Expr::Property(p) = &x.0 { let fn_name = make_setter(&p.0); // Re-use args because the first &mut parameter will not be consumed - args[1] = indexed_val; - self.exec_fn_call(state, &fn_name, 0, &mut args, None, x.2, 0) + arg_values[1] = indexed_val; + let args = &mut arg_values; + self.exec_fn_call(state, &fn_name, is_protected, 0, args, None, x.2, 0) .or_else(|err| match *err { // If there is no setter, no need to feed it back because the property is read-only EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), @@ -1099,6 +1162,7 @@ impl Engine { &self, state: &State, val: &'a mut Dynamic, + is_protected: bool, mut idx: Dynamic, idx_pos: Position, op_pos: Position, @@ -1177,8 +1241,8 @@ impl Engine { _ => { let args = &mut [val, &mut idx]; - self.exec_fn_call(state, FUNC_INDEXER, 0, args, None, op_pos, 0) - .map(|v| v.into()) + self.exec_fn_call(state, FUNC_INDEXER, is_protected, 0, args, None, op_pos, 0) + .map(|(v, _)| v.into()) .map_err(|_| { Box::new(EvalAltResult::ErrorIndexingType( // Error - cannot be indexed @@ -1223,8 +1287,9 @@ impl Engine { if self .call_fn_raw( - None, state, op, fn_spec, fn_def, args, def_value, pos, level, + None, state, op, false, fn_spec, fn_def, args, def_value, pos, level, )? + .0 .as_bool() .unwrap_or(false) { @@ -1398,12 +1463,14 @@ impl Engine { self.exec_fn_call( state, name, + false, *hash_fn_def, args.as_mut(), def_val.as_ref(), *pos, level, ) + .map(|(v, _)| v) } } diff --git a/src/fn_native.rs b/src/fn_native.rs index dfabc1d3..4bb7fde1 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -17,30 +17,45 @@ pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Sen #[cfg(not(feature = "sync"))] pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +/// A type representing the type of ABI of a native Rust function. +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +pub enum NativeFunctionABI { + /// A pure function where all arguments are passed by value. + Pure, + /// An object method where the first argument is the object passed by mutable reference. + /// All other arguments are passed by value. + Method, +} + /// A trait implemented by all native Rust functions that are callable by Rhai. pub trait NativeCallable { + /// Get the ABI type of a native Rust function. + fn abi(&self) -> NativeFunctionABI; /// Call a native Rust function. fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; } /// A type encapsulating a native Rust function callable by Rhai. -pub struct NativeFunction(Box); +pub struct NativeFunction(Box, NativeFunctionABI); impl NativeCallable for NativeFunction { + fn abi(&self) -> NativeFunctionABI { + self.1 + } fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result> { (self.0)(args, pos) } } -impl From> for NativeFunction { - fn from(func: Box) -> Self { - Self::new(func) +impl From<(Box, NativeFunctionABI)> for NativeFunction { + fn from(func: (Box, NativeFunctionABI)) -> Self { + Self::new(func.0, func.1) } } impl NativeFunction { /// Create a new `NativeFunction`. - pub fn new(func: Box) -> Self { - Self(func) + pub fn new(func: Box, abi: NativeFunctionABI) -> Self { + Self(func, abi) } } diff --git a/src/fn_register.rs b/src/fn_register.rs index a4e7d214..28c15c7f 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,7 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{FnCallArgs, NativeFunction}; +use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI::*}; use crate::result::EvalAltResult; use crate::token::Position; use crate::utils::calc_fn_spec; @@ -134,11 +134,12 @@ pub fn by_value(data: &mut Dynamic) -> T { /// This macro creates a closure wrapping a registered function. macro_rules! make_func { - ($fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { + ($fn:ident : $abi:expr ; $map:expr ; $($par:ident => $convert:expr),*) => { // ^ function pointer -// ^ result mapping function -// ^ function parameter generic type name (A, B, C etc.) -// ^ dereferencing function +// ^ function ABI type +// ^ result mapping function +// ^ function parameter generic type name (A, B, C etc.) +// ^ dereferencing function NativeFunction::new(Box::new(move |args: &mut FnCallArgs, pos: Position| { // The arguments are assumed to be of the correct number and types! @@ -156,7 +157,7 @@ macro_rules! make_func { // Map the result $map(r, pos) - })); + }), $abi); }; } @@ -187,13 +188,14 @@ pub fn map_result( macro_rules! def_register { () => { - def_register!(imp); + def_register!(imp Pure;); }; - (imp $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { - // ^ function parameter generic type name (A, B, C etc.) - // ^ function parameter marker type (T, Ref or Mut) - // ^ function parameter actual type (T, &T or &mut T) - // ^ dereferencing function + (imp $abi:expr ; $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { + // ^ function ABI type + // ^ function parameter generic type name (A, B, C etc.) + // ^ function parameter marker type (T, Ref or Mut) + // ^ function parameter actual type (T, &T or &mut T) + // ^ dereferencing function impl< $($par: Variant + Clone,)* @@ -207,7 +209,7 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : map_dynamic ; $($par => $clone),*); + let func = make_func!(f : $abi ; map_dynamic ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } @@ -224,7 +226,7 @@ macro_rules! def_register { > RegisterDynamicFn for Engine { fn register_dynamic_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : map_identity ; $($par => $clone),*); + let func = make_func!(f : $abi ; map_identity ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } @@ -242,7 +244,7 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : map_result ; $($par => $clone),*); + let func = make_func!(f : $abi ; map_result ; $($par => $clone),*); let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } @@ -251,10 +253,10 @@ macro_rules! def_register { //def_register!(imp_pop $($par => $mark => $param),*); }; ($p0:ident $(, $p:ident)*) => { - def_register!(imp $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); - def_register!(imp $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); - // handle the first parameter ^ first parameter passed through - // ^ others passed by value (by_value) + def_register!(imp Pure ; $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); + def_register!(imp Method ; $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + // handle the first parameter ^ 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 diff --git a/src/module.rs b/src/module.rs index 284d662b..2d7dc424 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,7 +4,10 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{FnAny, FnCallArgs, NativeCallable, NativeFunction, SharedNativeFunction}; +use crate::fn_native::{ + FnAny, FnCallArgs, NativeCallable, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, + SharedNativeFunction, +}; use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; @@ -39,6 +42,9 @@ pub trait ModuleResolver { ) -> Result>; } +/// Default function access mode. +const DEF_ACCESS: FnAccess = FnAccess::Public; + /// Return type of module-level Rust function. type FuncReturn = Result>; @@ -263,21 +269,22 @@ impl Module { /// If there is an existing Rust function of the same hash, it is replaced. pub fn set_fn( &mut self, - fn_name: String, + name: String, + abi: NativeFunctionABI, access: FnAccess, params: Vec, func: Box, ) -> u64 { - let hash = calc_fn_hash(empty(), &fn_name, params.iter().cloned()); + let hash = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = Box::new(NativeFunction::from(func)) as Box; + let f = Box::new(NativeFunction::from((func, abi))) as Box; #[cfg(not(feature = "sync"))] let func = Rc::new(f); #[cfg(feature = "sync")] let func = Arc::new(f); - self.functions.insert(hash, (fn_name, access, params, func)); + self.functions.insert(hash, (name, access, params, func)); hash } @@ -297,7 +304,7 @@ impl Module { /// ``` pub fn set_fn_0, T: Into>( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -307,7 +314,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -325,7 +332,7 @@ impl Module { /// ``` pub fn set_fn_1, A: Variant + Clone, T: Into>( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -335,7 +342,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -353,7 +360,7 @@ impl Module { /// ``` pub fn set_fn_1_mut, A: Variant + Clone, T: Into>( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -363,7 +370,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -383,7 +390,7 @@ impl Module { /// ``` pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Into>( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -396,7 +403,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -420,7 +427,7 @@ impl Module { T: Into, >( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -433,7 +440,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -459,7 +466,7 @@ impl Module { T: Into, >( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -473,7 +480,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -500,7 +507,7 @@ impl Module { T: Into, >( &mut self, - fn_name: K, + name: K, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -514,7 +521,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) + self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) } /// Get a Rust function. @@ -646,7 +653,7 @@ impl Module { variables.push((hash, value.clone())); } // Index all Rust functions - for (fn_name, access, params, func) in module.functions.values() { + for (name, access, params, func) in module.functions.values() { match access { // Private functions are not exported FnAccess::Private => continue, @@ -657,7 +664,7 @@ impl Module { // i.e. qualifiers + function name + dummy parameter types (one for each parameter). let hash_fn_def = calc_fn_hash( qualifiers.iter().map(|&v| v), - fn_name, + name, repeat(EMPTY_TYPE_ID()).take(params.len()), ); // 2) Calculate a second hash with no qualifiers, empty function name, and @@ -673,7 +680,7 @@ impl Module { match fn_def.access { // Private functions are not exported FnAccess::Private => continue, - FnAccess::Public => (), + DEF_ACCESS => (), } // Qualifiers + function name + placeholders (one for each parameter) let hash = calc_fn_hash( diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index dfb80422..c7938e74 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -118,9 +118,8 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { // Register array iterator lib.type_iterators.insert( TypeId::of::(), - Box::new(|a: Dynamic| { - Box::new(a.cast::().into_iter()) - as Box> - }), + Box::new(|arr| Box::new( + arr.cast::().into_iter()) as Box> + ), ); }); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 031ac095..d50ad72a 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -18,7 +18,7 @@ where { lib.type_iterators.insert( TypeId::of::>(), - Box::new(|source: Dynamic| { + Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) as Box> }), @@ -58,7 +58,7 @@ where { lib.type_iterators.insert( TypeId::of::>(), - Box::new(|source: Dynamic| { + Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) as Box> }), diff --git a/src/packages/utils.rs b/src/packages/utils.rs index d4783811..3c082a6a 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -2,7 +2,7 @@ use super::PackageStore; use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::fn_native::{FnCallArgs, NativeFunction}; +use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI::*}; use crate::result::EvalAltResult; use crate::token::Position; @@ -106,7 +106,8 @@ pub fn reg_none( map_result(r, pos) }); - lib.functions.insert(hash, Box::new(NativeFunction::new(f))); + lib.functions + .insert(hash, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with one parameter to the package. @@ -157,7 +158,8 @@ pub fn reg_unary( map_result(r, pos) }); - lib.functions.insert(hash, Box::new(NativeFunction::new(f))); + lib.functions + .insert(hash, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with one mutable reference parameter to the package. @@ -215,7 +217,8 @@ pub fn reg_unary_mut( map_result(r, pos) }); - lib.functions.insert(hash, Box::new(NativeFunction::new(f))); + lib.functions + .insert(hash, Box::new(NativeFunction::new(f, Method))); } /// Add a function with two parameters to the package. @@ -271,7 +274,8 @@ pub fn reg_binary( map_result(r, pos) }); - lib.functions.insert(hash, Box::new(NativeFunction::new(f))); + lib.functions + .insert(hash, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with two parameters (the first one being a mutable reference) to the package. @@ -334,7 +338,8 @@ pub fn reg_binary_mut( map_result(r, pos) }); - lib.functions.insert(hash, Box::new(NativeFunction::new(f))); + lib.functions + .insert(hash, Box::new(NativeFunction::new(f, Method))); } /// Add a function with three parameters to the package. @@ -374,7 +379,8 @@ pub fn reg_trinary Result<(), Box> { Ok(()) } + +#[test] +fn test_method_call_style() -> Result<(), Box> { + let mut engine = Engine::new(); + + assert_eq!(engine.eval::("let x = -123; x.abs(); x")?, -123); + + Ok(()) +} From 33c9be7efce8725d66b5bf374b79933b1b1f9ccc Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 11 May 2020 23:48:50 +0800 Subject: [PATCH 04/36] Reformat. --- src/api.rs | 4 +- src/engine.rs | 365 ++++++++++++++++-------------------------- src/fn_func.rs | 4 +- src/module.rs | 40 ++--- src/optimize.rs | 2 +- src/packages/utils.rs | 42 ++--- src/parser.rs | 7 +- 7 files changed, 190 insertions(+), 274 deletions(-) diff --git a/src/api.rs b/src/api.rs index 1918b487..096ba00d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -858,7 +858,7 @@ impl Engine { return result.try_cast::().ok_or_else(|| { Box::new(EvalAltResult::ErrorMismatchOutputType( - return_type.to_string(), + return_type.into(), Position::none(), )) }); @@ -1000,7 +1000,7 @@ impl Engine { let fn_def = fn_lib .get_function_by_signature(name, args.len(), true) - .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos)))?; + .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?; let state = State::new(fn_lib); diff --git a/src/engine.rs b/src/engine.rs index 4cf71e9a..d0889af0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI, SharedNativeFunction}; +use crate::fn_native::{FnCallArgs, NativeFunctionABI}; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, @@ -194,14 +194,14 @@ impl FunctionsLib { /// Does a certain function exist in the `FunctionsLib`? /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - pub fn has_function(&self, hash: u64) -> bool { - self.contains_key(&hash) + pub fn has_function(&self, hash_fn_def: u64) -> bool { + self.contains_key(&hash_fn_def) } /// Get a function definition from the `FunctionsLib`. /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - pub fn get_function(&self, hash: u64) -> Option<&FnDef> { - self.get(&hash).map(|fn_def| fn_def.as_ref()) + pub fn get_function(&self, hash_fn_def: u64) -> Option<&FnDef> { + self.get(&hash_fn_def).map(|fn_def| fn_def.as_ref()) } /// Get a function definition from the `FunctionsLib`. pub fn get_function_by_signature( @@ -211,8 +211,8 @@ impl FunctionsLib { public_only: bool, ) -> Option<&FnDef> { // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); - let fn_def = self.get_function(hash); + let hash_fn_def = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + let fn_def = self.get_function(hash_fn_def); match fn_def.as_ref().map(|f| f.access) { None => None, @@ -419,9 +419,7 @@ fn search_scope<'a>( ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { #[cfg(not(feature = "no_module"))] { - if let Some((modules, hash)) = modules { - let (id, root_pos) = modules.get_ref(0); - + if let Some((modules, hash_var)) = modules { let module = if let Some(index) = modules.index() { scope .get_mut(scope.len() - index.get()) @@ -429,16 +427,15 @@ fn search_scope<'a>( .downcast_mut::() .unwrap() } else { + let (id, root_pos) = modules.get_ref(0); + scope.find_module(id).ok_or_else(|| { - Box::new(EvalAltResult::ErrorModuleNotFound( - id.to_string(), - *root_pos, - )) + Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *root_pos)) })? }; return Ok(( - module.get_qualified_var_mut(name, hash, pos)?, + module.get_qualified_var_mut(name, hash_var, pos)?, // Module variables are constant ScopeEntryType::Constant, )); @@ -544,10 +541,9 @@ impl Engine { scope: Option<&mut Scope>, state: &State, fn_name: &str, - is_protected: bool, - hash_fn_spec: u64, - hash_fn_def: u64, + hashes: (u64, u64), args: &mut FnCallArgs, + is_ref: bool, def_val: Option<&Dynamic>, pos: Position, level: usize, @@ -558,8 +554,8 @@ impl Engine { } // First search in script-defined functions (can override built-in) - if hash_fn_def > 0 { - if let Some(fn_def) = state.get_function(hash_fn_def) { + if hashes.1 > 0 { + if let Some(fn_def) = state.get_function(hashes.1) { return self .call_script_fn(scope, state, fn_def, args, pos, level) .map(|v| (v, false)); @@ -569,21 +565,21 @@ impl Engine { // Search built-in's and external functions if let Some(func) = self .base_package - .get_function(hash_fn_spec) - .or_else(|| self.packages.get_function(hash_fn_spec)) + .get_function(hashes.0) + .or_else(|| self.packages.get_function(hashes.0)) { - let mut backup: Dynamic = ().into(); - let mut restore = false; + let mut backup: Dynamic = Default::default(); - let updated = match func.abi() { + let (updated, restore) = match func.abi() { // Calling pure function in method-call - NativeFunctionABI::Pure if is_protected && args.len() > 0 => { + NativeFunctionABI::Pure if is_ref && args.len() > 0 => { + // Backup the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) backup = args[0].clone(); - restore = true; - false + (false, true) } - NativeFunctionABI::Pure => false, - NativeFunctionABI::Method => true, + NativeFunctionABI::Pure => (false, false), + NativeFunctionABI::Method => (true, false), }; // Run external function @@ -742,13 +738,13 @@ impl Engine { } // Has a system function an override? - fn has_override(&self, state: &State, hash_fn_spec: u64, hash_fn_def: u64) -> bool { + fn has_override(&self, state: &State, hashes: (u64, u64)) -> bool { // First check registered functions - self.base_package.contains_function(hash_fn_spec) + self.base_package.contains_function(hashes.0) // Then check packages - || self.packages.contains_function(hash_fn_spec) + || self.packages.contains_function(hashes.0) // Then check script-defined functions - || state.has_function(hash_fn_def) + || state.has_function(hashes.1) } // Perform an actual function call, taking care of special functions @@ -762,31 +758,26 @@ impl Engine { &self, state: &State, fn_name: &str, - is_protected: bool, hash_fn_def: u64, args: &mut FnCallArgs, + is_ref: bool, def_val: Option<&Dynamic>, pos: Position, level: usize, ) -> Result<(Dynamic, bool), Box> { // Qualifiers (none) + function name + argument `TypeId`'s. - let hash_fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hashes = (hash_fn, hash_fn_def); match fn_name { // type_of - KEYWORD_TYPE_OF - if args.len() == 1 && !self.has_override(state, hash_fn_spec, hash_fn_def) => - { - Ok(( - self.map_type_name(args[0].type_name()).to_string().into(), - false, - )) - } + KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, hashes) => Ok(( + self.map_type_name(args[0].type_name()).to_string().into(), + false, + )), // eval - reaching this point it must be a method-style call - KEYWORD_EVAL - if args.len() == 1 && !self.has_override(state, hash_fn_spec, hash_fn_def) => - { + KEYWORD_EVAL if args.len() == 1 && !self.has_override(state, hashes) => { Err(Box::new(EvalAltResult::ErrorRuntime( "'eval' should not be called in method style. Try eval(...);".into(), pos, @@ -795,16 +786,7 @@ impl Engine { // Normal function call _ => self.call_fn_raw( - None, - state, - fn_name, - is_protected, - hash_fn_spec, - hash_fn_def, - args, - def_val, - pos, - level, + None, state, fn_name, hashes, args, is_ref, def_val, pos, level, ), } } @@ -857,7 +839,7 @@ impl Engine { mut new_val: Option, ) -> Result<(Dynamic, bool), Box> { // Get a reference to the mutation target Dynamic - let (obj, is_protected) = match target { + let (obj, is_ref) = match target { Target::Ref(r) => (r, true), Target::Value(ref mut r) => (r.as_mut(), false), Target::StringChar(ref mut x) => (&mut x.2, false), @@ -870,60 +852,34 @@ impl Engine { match rhs { // xxx[idx].dot_rhs... | xxx[idx][dot_rhs]... Expr::Dot(x) | Expr::Index(x) => { - let is_index = matches!(rhs, Expr::Index(_)); + let is_idx = matches!(rhs, Expr::Index(_)); + let pos = x.0.position(); + let val = + self.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, false)?; - let indexed_val = self.get_indexed_mut( - state, - obj, - is_protected, - idx_val, - x.0.position(), - op_pos, - false, - )?; self.eval_dot_index_chain_helper( - state, - indexed_val, - &x.1, - idx_values, - is_index, - x.2, - level, - new_val, + state, val, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx[rhs] = new_val _ if new_val.is_some() => { - let mut indexed_val = self.get_indexed_mut( - state, - obj, - is_protected, - idx_val, - rhs.position(), - op_pos, - true, - )?; - indexed_val.set_value(new_val.unwrap(), rhs.position())?; + let pos = rhs.position(); + let mut val = + self.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, true)?; + + val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // xxx[rhs] _ => self - .get_indexed_mut( - state, - obj, - is_protected, - idx_val, - rhs.position(), - op_pos, - false, - ) + .get_indexed_mut(state, obj, is_ref, idx_val, rhs.position(), op_pos, false) .map(|v| (v.clone_into_dynamic(), false)), } } else { match rhs { // xxx.fn_name(arg_expr_list) Expr::FnCall(x) if x.1.is_none() => { - let ((name, pos), _, hash, _, def_val) = x.as_ref(); + let ((name, pos), _, hash_fn_def, _, def_val) = x.as_ref(); let def_val = def_val.as_ref(); let mut arg_values: StaticVec<_> = once(obj) @@ -936,7 +892,7 @@ impl Engine { .collect(); let args = arg_values.as_mut(); - self.exec_fn_call(state, name, is_protected, *hash, args, def_val, *pos, 0) + self.exec_fn_call(state, name, *hash_fn_def, args, is_ref, def_val, *pos, 0) } // xxx.module::fn_name(...) - syntax error Expr::FnCall(_) => unreachable!(), @@ -944,83 +900,78 @@ impl Engine { #[cfg(not(feature = "no_object"))] Expr::Property(x) if obj.is::() && new_val.is_some() => { let index = x.0.clone().into(); - let mut indexed_val = - self.get_indexed_mut(state, obj, is_protected, index, x.1, op_pos, true)?; - indexed_val.set_value(new_val.unwrap(), rhs.position())?; + let mut val = + self.get_indexed_mut(state, obj, is_ref, index, x.1, op_pos, true)?; + + val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // {xxx:map}.id #[cfg(not(feature = "no_object"))] Expr::Property(x) if obj.is::() => { let index = x.0.clone().into(); - let indexed_val = - self.get_indexed_mut(state, obj, is_protected, index, x.1, op_pos, false)?; - Ok((indexed_val.clone_into_dynamic(), false)) + let val = + self.get_indexed_mut(state, obj, is_ref, index, x.1, op_pos, false)?; + + Ok((val.clone_into_dynamic(), false)) } - // xxx.id = ??? a + // xxx.id = ??? Expr::Property(x) if new_val.is_some() => { let fn_name = make_setter(&x.0); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, &fn_name, is_protected, 0, &mut args, None, x.1, 0) + self.exec_fn_call(state, &fn_name, 0, &mut args, is_ref, None, x.1, 0) .map(|(v, _)| (v, true)) } // xxx.id Expr::Property(x) => { let fn_name = make_getter(&x.0); let mut args = [obj]; - self.exec_fn_call(state, &fn_name, is_protected, 0, &mut args, None, x.1, 0) + self.exec_fn_call(state, &fn_name, 0, &mut args, is_ref, None, x.1, 0) .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] // {xxx:map}.idx_lhs[idx_expr] | {xxx:map}.dot_lhs.rhs Expr::Index(x) | Expr::Dot(x) if obj.is::() => { - let is_index = matches!(rhs, Expr::Index(_)); + let is_idx = matches!(rhs, Expr::Index(_)); - let indexed_val = if let Expr::Property(p) = &x.0 { + let val = if let Expr::Property(p) = &x.0 { let index = p.0.clone().into(); - self.get_indexed_mut(state, obj, is_protected, index, x.2, op_pos, false)? + self.get_indexed_mut(state, obj, is_ref, index, x.2, op_pos, false)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), + "".into(), rhs.position(), ))); }; + self.eval_dot_index_chain_helper( - state, - indexed_val, - &x.1, - idx_values, - is_index, - x.2, - level, - new_val, + state, val, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs Expr::Index(x) | Expr::Dot(x) => { - let is_index = matches!(rhs, Expr::Index(_)); - let mut arg_values = [obj, &mut Default::default()]; + let is_idx = matches!(rhs, Expr::Index(_)); + let args = &mut [obj, &mut Default::default()]; - let (mut indexed_val, updated) = if let Expr::Property(p) = &x.0 { + let (mut val, updated) = if let Expr::Property(p) = &x.0 { let fn_name = make_getter(&p.0); - let args = &mut arg_values[..1]; - self.exec_fn_call(state, &fn_name, is_protected, 0, args, None, x.2, 0)? + self.exec_fn_call(state, &fn_name, 0, &mut args[..1], is_ref, None, x.2, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), + "".into(), rhs.position(), ))); }; - let indexed_val = &mut indexed_val; + let val = &mut val; let (result, may_be_changed) = self.eval_dot_index_chain_helper( state, - indexed_val.into(), + val.into(), &x.1, idx_values, - is_index, + is_idx, x.2, level, new_val, @@ -1031,9 +982,8 @@ impl Engine { if let Expr::Property(p) = &x.0 { let fn_name = make_setter(&p.0); // Re-use args because the first &mut parameter will not be consumed - arg_values[1] = indexed_val; - let args = &mut arg_values; - self.exec_fn_call(state, &fn_name, is_protected, 0, args, None, x.2, 0) + args[1] = val; + self.exec_fn_call(state, &fn_name, 0, args, is_ref, None, x.2, 0) .or_else(|err| match *err { // If there is no setter, no need to feed it back because the property is read-only EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), @@ -1046,7 +996,7 @@ impl Engine { } // Syntax error _ => Err(Box::new(EvalAltResult::ErrorDotExpr( - "".to_string(), + "".into(), rhs.position(), ))), } @@ -1072,9 +1022,9 @@ impl Engine { match dot_lhs { // id.??? or id[???] Expr::Variable(x) => { - let ((name, pos), modules, hash, index) = x.as_ref(); + let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); + let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); let (target, typ) = search_scope(scope, &name, mod_and_hash, index, *pos)?; // Constants cannot be modified @@ -1162,14 +1112,12 @@ impl Engine { &self, state: &State, val: &'a mut Dynamic, - is_protected: bool, + is_ref: bool, mut idx: Dynamic, idx_pos: Position, op_pos: Position, create: bool, ) -> Result, Box> { - let type_name = self.map_type_name(val.type_name()); - match val { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(arr)) => { @@ -1212,43 +1160,31 @@ impl Engine { #[cfg(not(feature = "no_index"))] Dynamic(Union::Str(s)) => { // val_string[idx] + let chars_len = s.chars().count(); let index = idx .as_int() .map_err(|_| EvalAltResult::ErrorNumericIndexExpr(idx_pos))?; if index >= 0 { - let ch = s.chars().nth(index as usize).ok_or_else(|| { - Box::new(EvalAltResult::ErrorStringBounds( - s.chars().count(), - index, - idx_pos, - )) + let offset = index as usize; + let ch = s.chars().nth(offset).ok_or_else(|| { + Box::new(EvalAltResult::ErrorStringBounds(chars_len, index, idx_pos)) })?; - - Ok(Target::StringChar(Box::new(( - val, - index as usize, - ch.into(), - )))) + Ok(Target::StringChar(Box::new((val, offset, ch.into())))) } else { Err(Box::new(EvalAltResult::ErrorStringBounds( - s.chars().count(), - index, - idx_pos, + chars_len, index, idx_pos, ))) } } _ => { + let type_name = self.map_type_name(val.type_name()); let args = &mut [val, &mut idx]; - self.exec_fn_call(state, FUNC_INDEXER, is_protected, 0, args, None, op_pos, 0) + self.exec_fn_call(state, FUNC_INDEXER, 0, args, is_ref, None, op_pos, 0) .map(|(v, _)| v.into()) .map_err(|_| { - Box::new(EvalAltResult::ErrorIndexingType( - // Error - cannot be indexed - type_name.to_string(), - op_pos, - )) + Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos)) }) } } @@ -1263,36 +1199,29 @@ impl Engine { rhs: &Expr, level: usize, ) -> Result> { - let lhs_value = self.eval_expr(scope, state, lhs, level)?; + let mut lhs_value = self.eval_expr(scope, state, lhs, level)?; let rhs_value = self.eval_expr(scope, state, rhs, level)?; match rhs_value { #[cfg(not(feature = "no_index"))] - Dynamic(Union::Array(rhs_value)) => { + Dynamic(Union::Array(mut rhs_value)) => { let op = "=="; let def_value = false.into(); - let fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash_fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); // Call the `==` operator to compare each value - for value in rhs_value.iter() { - // WARNING - Always clone the values here because they'll be consumed by the function call. - // Do not pass the `&mut` straight through because the `==` implementation - // very likely takes parameters passed by value! - let args = &mut [&mut lhs_value.clone(), &mut value.clone()]; + for value in rhs_value.iter_mut() { + let args = &mut [&mut lhs_value, value]; let def_value = Some(&def_value); let pos = rhs.position(); // Qualifiers (none) + function name + argument `TypeId`'s. - let fn_spec = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); + let hashes = (hash_fn, hash_fn_def); - if self - .call_fn_raw( - None, state, op, false, fn_spec, fn_def, args, def_value, pos, level, - )? - .0 - .as_bool() - .unwrap_or(false) - { + let (r, _) = self + .call_fn_raw(None, state, op, hashes, args, true, def_value, pos, level)?; + if r.as_bool().unwrap_or(false) { return Ok(true.into()); } } @@ -1331,9 +1260,9 @@ impl Engine { Expr::StringConstant(x) => Ok(x.0.to_string().into()), Expr::CharConstant(x) => Ok(x.0.into()), Expr::Variable(x) => { - let ((name, pos), modules, hash, index) = x.as_ref(); + let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); + let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); let (val, _) = search_scope(scope, name, mod_and_hash, index, *pos)?; Ok(val.clone()) } @@ -1350,17 +1279,16 @@ impl Engine { match &x.0 { // name = rhs Expr::Variable(x) => { - let ((name, pos), modules, hash, index) = x.as_ref(); + let ((name, pos), modules, hash_var, index) = x.as_ref(); let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); - let (value_ptr, typ) = - search_scope(scope, name, mod_and_hash, index, *pos)?; + let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); + let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; match typ { ScopeEntryType::Constant => Err(Box::new( EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), )), ScopeEntryType::Normal => { - *value_ptr = rhs_val; + *lhs_ptr = rhs_val; Ok(Default::default()) } // End variable cannot be a module @@ -1429,6 +1357,7 @@ impl Engine { // Normal function call Expr::FnCall(x) if x.1.is_none() => { let ((name, pos), _, hash_fn_def, args_expr, def_val) = x.as_ref(); + let def_val = def_val.as_ref(); let mut arg_values = args_expr .iter() @@ -1437,41 +1366,35 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - let hash_fn_spec = - calc_fn_hash(empty(), KEYWORD_EVAL, once(TypeId::of::())); + if name == KEYWORD_EVAL && args.len() == 1 && args.get_ref(0).is::() { + let hash_fn = calc_fn_hash(empty(), name, once(TypeId::of::())); - if name == KEYWORD_EVAL - && args.len() == 1 - && !self.has_override(state, hash_fn_spec, *hash_fn_def) - { - // eval - only in function call style - let prev_len = scope.len(); + if !self.has_override(state, (hash_fn, *hash_fn_def)) { + // eval - only in function call style + let prev_len = scope.len(); - // Evaluate the text string as a script - let result = - self.eval_script_expr(scope, state, args.pop(), args_expr[0].position()); + // Evaluate the text string as a script + let result = self.eval_script_expr( + scope, + state, + args.pop(), + args_expr[0].position(), + ); - if scope.len() != prev_len { - // IMPORTANT! If the eval defines new variables in the current scope, - // all variable offsets from this point on will be mis-aligned. - state.always_search = true; + if scope.len() != prev_len { + // IMPORTANT! If the eval defines new variables in the current scope, + // all variable offsets from this point on will be mis-aligned. + state.always_search = true; + } + + return result; } - - result - } else { - // Normal function call - except for eval (handled above) - self.exec_fn_call( - state, - name, - false, - *hash_fn_def, - args.as_mut(), - def_val.as_ref(), - *pos, - level, - ) - .map(|(v, _)| v) } + + // Normal function call - except for eval (handled above) + let args = args.as_mut(); + self.exec_fn_call(state, name, *hash_fn_def, args, false, def_val, *pos, level) + .map(|(v, _)| v) } // Module-qualified function call @@ -1514,9 +1437,9 @@ impl Engine { // the actual list of parameter `TypeId`'.s let hash_fn_args = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); // 3) The final hash is the XOR of the two hashes. - let hash = *hash_fn_def ^ hash_fn_args; + let hash_fn_native = *hash_fn_def ^ hash_fn_args; - match module.get_qualified_fn(name, hash, *pos) { + match module.get_qualified_fn(name, hash_fn_native, *pos) { Ok(func) => func.call(args.as_mut(), *pos), Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), Err(err) => Err(err), @@ -1669,8 +1592,8 @@ impl Engine { scope.push(name, ()); let index = scope.len() - 1; - for a in iter_fn(iter_type) { - *scope.get_mut(index).0 = a; + for loop_var in iter_fn(iter_type) { + *scope.get_mut(index).0 = loop_var; match self.eval_stmt(scope, state, &x.2, level) { Ok(_) => (), @@ -1712,7 +1635,7 @@ impl Engine { Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Exception => { let val = self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?; Err(Box::new(EvalAltResult::ErrorRuntime( - val.take_string().unwrap_or_else(|_| "".to_string()), + val.take_string().unwrap_or_else(|_| "".into()), (x.0).1, ))) } @@ -1771,8 +1694,7 @@ impl Engine { resolver.resolve(self, Scope::new(), &path, expr.position())?; // TODO - avoid copying module name in inner block? - let mod_name = name.clone(); - scope.push_module(mod_name, module); + scope.push_module(name.clone(), module); Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorModuleNotFound( @@ -1789,8 +1711,6 @@ impl Engine { // Export statement Stmt::Export(list) => { for ((id, id_pos), rename) in list.as_ref() { - let mut found = false; - // Mark scope variables as public if let Some(index) = scope .get_index(id) @@ -1803,10 +1723,7 @@ impl Engine { .unwrap_or_else(|| id.clone()); scope.set_entry_alias(index, alias); - found = true; - } - - if !found { + } else { return Err(Box::new(EvalAltResult::ErrorVariableNotFound( id.into(), *id_pos, diff --git a/src/fn_func.rs b/src/fn_func.rs index af538306..a595064f 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -97,10 +97,10 @@ macro_rules! def_anonymous_fn { type Output = Box Result>>; fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output { - let name = entry_point.to_string(); + let fn_name = entry_point.to_string(); Box::new(move |$($par: $par),*| { - self.call_fn(&mut Scope::new(), &ast, &name, ($($par,)*)) + self.call_fn(&mut Scope::new(), &ast, &fn_name, ($($par,)*)) }) } diff --git a/src/module.rs b/src/module.rs index 2d7dc424..83da428c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -172,11 +172,11 @@ impl Module { pub(crate) fn get_qualified_var_mut( &mut self, name: &str, - hash: u64, + hash_var: u64, pos: Position, ) -> Result<&mut Dynamic, Box> { self.all_variables - .get_mut(&hash) + .get_mut(&hash_var) .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.to_string(), pos))) } @@ -260,8 +260,8 @@ impl Module { /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.contains_fn(hash)); /// ``` - pub fn contains_fn(&self, hash: u64) -> bool { - self.functions.contains_key(&hash) + pub fn contains_fn(&self, hash_fn: u64) -> bool { + self.functions.contains_key(&hash_fn) } /// Set a Rust function into the module, returning a hash key. @@ -275,7 +275,7 @@ impl Module { params: Vec, func: Box, ) -> u64 { - let hash = calc_fn_hash(empty(), &name, params.iter().cloned()); + let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); let f = Box::new(NativeFunction::from((func, abi))) as Box; @@ -284,9 +284,9 @@ impl Module { #[cfg(feature = "sync")] let func = Arc::new(f); - self.functions.insert(hash, (name, access, params, func)); + self.functions.insert(hash_fn, (name, access, params, func)); - hash + hash_fn } /// Set a Rust function taking no parameters into the module, returning a hash key. @@ -538,8 +538,8 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash: u64) -> Option<&Box> { - self.functions.get(&hash).map(|(_, _, _, v)| v.as_ref()) + pub fn get_fn(&self, hash_fn: u64) -> Option<&Box> { + self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } /// Get a modules-qualified function. @@ -549,11 +549,11 @@ impl Module { pub(crate) fn get_qualified_fn( &mut self, name: &str, - hash: u64, + hash_fn_native: u64, pos: Position, ) -> Result<&Box, Box> { self.all_functions - .get(&hash) + .get(&hash_fn_native) .map(|f| f.as_ref()) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos))) } @@ -575,8 +575,8 @@ impl Module { /// Get a modules-qualified script-defined functions. /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - pub(crate) fn get_qualified_scripted_fn(&mut self, hash: u64) -> Option<&FnDef> { - self.all_fn_lib.get_function(hash) + pub(crate) fn get_qualified_scripted_fn(&mut self, hash_fn_def: u64) -> Option<&FnDef> { + self.all_fn_lib.get_function(hash_fn_def) } /// Create a new `Module` by evaluating an `AST`. @@ -649,8 +649,8 @@ impl Module { // Index all variables for (var_name, value) in module.variables.iter() { // Qualifiers + variable name - let hash = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); - variables.push((hash, value.clone())); + let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); + variables.push((hash_var, value.clone())); } // Index all Rust functions for (name, access, params, func) in module.functions.values() { @@ -671,9 +671,9 @@ impl Module { // the actual list of parameter `TypeId`'.s let hash_fn_args = calc_fn_hash(empty(), "", params.iter().cloned()); // 3) The final hash is the XOR of the two hashes. - let hash = hash_fn_def ^ hash_fn_args; + let hash_fn_native = hash_fn_def ^ hash_fn_args; - functions.push((hash, func.clone())); + functions.push((hash_fn_native, func.clone())); } // Index all script-defined functions for fn_def in module.fn_lib.values() { @@ -683,12 +683,12 @@ impl Module { DEF_ACCESS => (), } // Qualifiers + function name + placeholders (one for each parameter) - let hash = calc_fn_hash( + let hash_fn_def = calc_fn_hash( qualifiers.iter().map(|&v| v), &fn_def.name, repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); - fn_lib.push((hash, fn_def.clone())); + fn_lib.push((hash_fn_def, fn_def.clone())); } } @@ -984,7 +984,7 @@ mod stat { self.0 .get(path) .cloned() - .ok_or_else(|| Box::new(EvalAltResult::ErrorModuleNotFound(path.to_string(), pos))) + .ok_or_else(|| Box::new(EvalAltResult::ErrorModuleNotFound(path.into(), pos))) } } } diff --git a/src/optimize.rs b/src/optimize.rs index 060b9c3c..0f83a703 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -95,7 +95,7 @@ impl<'a> State<'a> { } /// Add a new constant to the list. pub fn push_constant(&mut self, name: &str, value: Expr) { - self.constants.push((name.to_string(), value)) + self.constants.push((name.into(), value)) } /// Look up a constant from the list. pub fn find_constant(&self, name: &str) -> Option<&Expr> { diff --git a/src/packages/utils.rs b/src/packages/utils.rs index 3c082a6a..233218db 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -99,15 +99,15 @@ pub fn reg_none( + Sync + 'static, ) { - let hash = calc_fn_hash(empty(), fn_name, ([] as [TypeId; 0]).iter().cloned()); + let hash_fn_native = calc_fn_hash(empty(), fn_name, ([] as [TypeId; 0]).iter().cloned()); - let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { + let f = Box::new(move |_: &mut FnCallArgs, pos| { let r = func(); map_result(r, pos) }); lib.functions - .insert(hash, Box::new(NativeFunction::new(f, Pure))); + .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with one parameter to the package. @@ -148,9 +148,9 @@ pub fn reg_unary( ) { //println!("register {}({})", fn_name, crate::std::any::type_name::()); - let hash = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); + let hash_fn_native = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); - let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { + let f = Box::new(move |args: &mut FnCallArgs, pos| { let mut drain = args.iter_mut(); let x = mem::take(*drain.next().unwrap()).cast::(); @@ -159,7 +159,7 @@ pub fn reg_unary( }); lib.functions - .insert(hash, Box::new(NativeFunction::new(f, Pure))); + .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with one mutable reference parameter to the package. @@ -207,9 +207,9 @@ pub fn reg_unary_mut( ) { //println!("register {}(&mut {})", fn_name, crate::std::any::type_name::()); - let hash = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); + let hash_fn_native = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); - let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { + let f = Box::new(move |args: &mut FnCallArgs, pos| { let mut drain = args.iter_mut(); let x: &mut T = drain.next().unwrap().downcast_mut().unwrap(); @@ -218,7 +218,7 @@ pub fn reg_unary_mut( }); lib.functions - .insert(hash, Box::new(NativeFunction::new(f, Method))); + .insert(hash_fn_native, Box::new(NativeFunction::new(f, Method))); } /// Add a function with two parameters to the package. @@ -259,13 +259,13 @@ pub fn reg_binary( ) { //println!("register {}({}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::()); - let hash = calc_fn_hash( + let hash_fn_native = calc_fn_hash( empty(), fn_name, [TypeId::of::(), TypeId::of::()].iter().cloned(), ); - let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { + let f = Box::new(move |args: &mut FnCallArgs, pos| { let mut drain = args.iter_mut(); let x = mem::take(*drain.next().unwrap()).cast::(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -275,7 +275,7 @@ pub fn reg_binary( }); lib.functions - .insert(hash, Box::new(NativeFunction::new(f, Pure))); + .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); } /// Add a function with two parameters (the first one being a mutable reference) to the package. @@ -323,13 +323,13 @@ pub fn reg_binary_mut( ) { //println!("register {}(&mut {}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::()); - let hash = calc_fn_hash( + let hash_fn_native = calc_fn_hash( empty(), fn_name, [TypeId::of::(), TypeId::of::()].iter().cloned(), ); - let f = Box::new(move |args: &mut FnCallArgs, pos: Position| { + let f = Box::new(move |args: &mut FnCallArgs, pos| { let mut drain = args.iter_mut(); let x: &mut A = drain.next().unwrap().downcast_mut().unwrap(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -339,7 +339,7 @@ pub fn reg_binary_mut( }); lib.functions - .insert(hash, Box::new(NativeFunction::new(f, Method))); + .insert(hash_fn_native, Box::new(NativeFunction::new(f, Method))); } /// Add a function with three parameters to the package. @@ -361,7 +361,7 @@ pub fn reg_trinary(), crate::std::any::type_name::(), crate::std::any::type_name::()); - let hash = calc_fn_hash( + let hash_fn_native = calc_fn_hash( empty(), fn_name, [TypeId::of::(), TypeId::of::(), TypeId::of::()] @@ -369,7 +369,7 @@ pub fn reg_trinary(); let y = mem::take(*drain.next().unwrap()).cast::(); @@ -380,7 +380,7 @@ pub fn reg_trinary(), crate::std::any::type_name::(), crate::std::any::type_name::()); - let hash = calc_fn_hash( + let hash_fn_native = calc_fn_hash( empty(), fn_name, [TypeId::of::(), TypeId::of::(), TypeId::of::()] @@ -410,7 +410,7 @@ pub fn reg_trinary_mut(); @@ -421,5 +421,5 @@ pub fn reg_trinary_mut( match input.peek().unwrap() { // If another indexing level, right-bind it (Token::LeftBracket, _) => { - let follow_pos = eat_token(input, Token::LeftBracket); + let idx_pos = eat_token(input, Token::LeftBracket); // Recursively parse the indexing chain, right-binding each - let follow = - parse_index_chain(input, stack, idx_expr, follow_pos, allow_stmt_expr)?; + let idx = parse_index_chain(input, stack, idx_expr, idx_pos, allow_stmt_expr)?; // Indexing binds to right - Ok(Expr::Index(Box::new((lhs, follow, pos)))) + Ok(Expr::Index(Box::new((lhs, idx, pos)))) } // Otherwise terminate the indexing chain _ => Ok(Expr::Index(Box::new((lhs, idx_expr, pos)))), From 2e2896756530b92092a8db9139dca5b0d1b3d3d2 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 12 May 2020 10:20:29 +0800 Subject: [PATCH 05/36] Fix bug with wrong method call hash. --- src/parser.rs | 31 ++++++++++++++++++------------- tests/functions.rs | 25 +++++++++++++++++++++++++ tests/method_call.rs | 8 ++++---- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 tests/functions.rs diff --git a/src/parser.rs b/src/parser.rs index 484616ad..da35d62d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -767,6 +767,7 @@ fn parse_call_expr<'a>( // id(...args) (Token::RightParen, _) => { eat_token(input, Token::RightParen); + let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len()); #[cfg(not(feature = "no_module"))] let hash_fn_def = { @@ -779,19 +780,14 @@ fn parse_call_expr<'a>( // 2) Calculate a second hash with no qualifiers, empty function name, and // the actual list of parameter `TypeId`'.s // 3) The final hash is the XOR of the two hashes. - calc_fn_hash( - modules.iter().map(|(m, _)| m.as_str()), - &id, - repeat(EMPTY_TYPE_ID()).take(args.len()), - ) + calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, args_iter) } else { - calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())) + calc_fn_hash(empty(), &id, args_iter) } }; // Qualifiers (none) + function name + dummy parameter types (one for each parameter). #[cfg(feature = "no_module")] - let hash_fn_def = - calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())); + let hash_fn_def = calc_fn_hash(empty(), &id, args_iter); return Ok(Expr::FnCall(Box::new(( (id.into(), begin), @@ -1204,10 +1200,7 @@ fn parse_primary<'a>( let ((name, pos), modules, _, _) = *x; parse_call_expr(input, stack, name, modules, pos, allow_stmt_expr)? } - (Expr::Property(x), Token::LeftParen) => { - let (name, pos) = *x; - parse_call_expr(input, stack, name, None, pos, allow_stmt_expr)? - } + (Expr::Property(_), _) => unreachable!(), // module access #[cfg(not(feature = "no_module"))] (Expr::Variable(x), Token::DoubleColon) => match input.next().unwrap() { @@ -1742,8 +1735,20 @@ fn parse_binary_op<'a>( #[cfg(not(feature = "no_object"))] Token::Period => { - let rhs = args.pop().unwrap(); + let mut rhs = args.pop().unwrap(); let current_lhs = args.pop().unwrap(); + + match &mut rhs { + // current_lhs.rhs(...) - method call + Expr::FnCall(x) => { + let ((id, _), _, hash, args, _) = x.as_mut(); + // Recalculate function call hash because there is an additional argument + let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len() + 1); + *hash = calc_fn_hash(empty(), id, args_iter); + } + _ => (), + } + make_dot_expr(current_lhs, rhs, pos, false)? } diff --git a/tests/functions.rs b/tests/functions.rs new file mode 100644 index 00000000..8200962b --- /dev/null +++ b/tests/functions.rs @@ -0,0 +1,25 @@ +#![cfg(not(feature = "no_function"))] +use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT}; + +#[test] +fn test_functions() -> Result<(), Box> { + let engine = Engine::new(); + + assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); + + #[cfg(not(feature = "no_object"))] + assert_eq!( + engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, + 42 + ); + + assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); + + #[cfg(not(feature = "no_object"))] + assert_eq!( + engine.eval::("fn mul2(x) { x * 2 } let x = 21; x.mul2()")?, + 42 + ); + + Ok(()) +} diff --git a/tests/method_call.rs b/tests/method_call.rs index 59d43b3d..59857102 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -10,8 +10,8 @@ fn test_method_call() -> Result<(), Box> { } impl TestStruct { - fn update(&mut self) { - self.x += 1000; + fn update(&mut self, n: INT) { + self.x += n; } fn new() -> Self { @@ -27,12 +27,12 @@ fn test_method_call() -> Result<(), Box> { engine.register_fn("new_ts", TestStruct::new); assert_eq!( - engine.eval::("let x = new_ts(); x.update(); x")?, + engine.eval::("let x = new_ts(); x.update(1000); x")?, TestStruct { x: 1001 } ); assert_eq!( - engine.eval::("let x = new_ts(); update(x); x")?, + engine.eval::("let x = new_ts(); update(x, 1000); x")?, TestStruct { x: 1 } ); From 03c64688adee49d17f8742904322da9858c6a7cf Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 12 May 2020 16:32:22 +0800 Subject: [PATCH 06/36] Fix sync feature. --- src/any.rs | 2 +- src/api.rs | 51 +++----------------------------------- src/engine.rs | 15 +++-------- src/fn_native.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++ src/fn_register.rs | 4 +-- src/module.rs | 14 +++++++++++ tests/modules.rs | 3 ++- 7 files changed, 88 insertions(+), 63 deletions(-) diff --git a/src/any.rs b/src/any.rs index a96bcca2..2682115b 100644 --- a/src/any.rs +++ b/src/any.rs @@ -93,7 +93,7 @@ pub trait Variant: Any + Send + Sync { fn as_mut_any(&mut self) -> &mut dyn Any; /// Convert this `Variant` trait object to an `Any` trait object. - fn as_box_any(self) -> Box; + fn as_box_any(self: Box) -> Box; /// Get the name of this type. fn type_name(&self) -> &'static str; diff --git a/src/api.rs b/src/api.rs index 096ba00d..95423d8e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,6 +4,9 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{make_getter, make_setter, Engine, State, FUNC_INDEXER}; use crate::error::ParseError; use crate::fn_call::FuncArgs; +use crate::fn_native::{ + IteratorCallback, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback, +}; use crate::fn_register::RegisterFn; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parser::{parse, parse_global_expr, AST}; @@ -22,56 +25,10 @@ use crate::stdlib::{ mem, string::{String, ToString}, }; + #[cfg(not(feature = "no_std"))] use crate::stdlib::{fs::File, io::prelude::*, path::PathBuf}; -// Define callback function types -#[cfg(feature = "sync")] -pub trait ObjectGetCallback: Fn(&mut T) -> U + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl U + Send + Sync + 'static, T, U> ObjectGetCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectGetCallback: Fn(&mut T) -> U + 'static {} -#[cfg(not(feature = "sync"))] -impl U + 'static, T, U> ObjectGetCallback for F {} - -#[cfg(feature = "sync")] -pub trait ObjectSetCallback: Fn(&mut T, U) + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl ObjectSetCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectSetCallback: Fn(&mut T, U) + 'static {} -#[cfg(not(feature = "sync"))] -impl ObjectSetCallback for F {} - -#[cfg(feature = "sync")] -pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + Send + Sync + 'static {} -#[cfg(feature = "sync")] -impl U + Send + Sync + 'static, T, X, U> ObjectIndexerCallback for F {} - -#[cfg(not(feature = "sync"))] -pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} -#[cfg(not(feature = "sync"))] -impl U + 'static, T, X, U> ObjectIndexerCallback for F {} - -#[cfg(feature = "sync")] -pub trait IteratorCallback: - Fn(Dynamic) -> Box> + Send + Sync + 'static -{ -} -#[cfg(feature = "sync")] -impl Box> + Send + Sync + 'static> IteratorCallback - for F -{ -} - -#[cfg(not(feature = "sync"))] -pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} -#[cfg(not(feature = "sync"))] -impl Box> + 'static> IteratorCallback for F {} - /// Engine public API impl Engine { /// Register a custom type for use with the `Engine`. diff --git a/src/engine.rs b/src/engine.rs index d0889af0..301bbfca 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunctionABI}; +use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback}; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, @@ -293,18 +293,9 @@ pub struct Engine { pub(crate) type_names: HashMap, /// Closure for implementing the `print` command. - #[cfg(feature = "sync")] - pub(crate) print: Box, - /// Closure for implementing the `print` command. - #[cfg(not(feature = "sync"))] - pub(crate) print: Box, - + pub(crate) print: Box, /// Closure for implementing the `debug` command. - #[cfg(feature = "sync")] - pub(crate) debug: Box, - /// Closure for implementing the `debug` command. - #[cfg(not(feature = "sync"))] - pub(crate) debug: Box, + pub(crate) debug: Box, /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, diff --git a/src/fn_native.rs b/src/fn_native.rs index 4bb7fde1..bfc9311b 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -17,6 +17,58 @@ pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Sen #[cfg(not(feature = "sync"))] pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +#[cfg(feature = "sync")] +pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; +#[cfg(not(feature = "sync"))] +pub type PrintCallback = dyn Fn(&str) + 'static; + +// Define callback function types +#[cfg(feature = "sync")] +pub trait ObjectGetCallback: Fn(&mut T) -> U + Send + Sync + 'static {} +#[cfg(feature = "sync")] +impl U + Send + Sync + 'static, T, U> ObjectGetCallback for F {} + +#[cfg(not(feature = "sync"))] +pub trait ObjectGetCallback: Fn(&mut T) -> U + 'static {} +#[cfg(not(feature = "sync"))] +impl U + 'static, T, U> ObjectGetCallback for F {} + +#[cfg(feature = "sync")] +pub trait ObjectSetCallback: Fn(&mut T, U) + Send + Sync + 'static {} +#[cfg(feature = "sync")] +impl ObjectSetCallback for F {} + +#[cfg(not(feature = "sync"))] +pub trait ObjectSetCallback: Fn(&mut T, U) + 'static {} +#[cfg(not(feature = "sync"))] +impl ObjectSetCallback for F {} + +#[cfg(feature = "sync")] +pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + Send + Sync + 'static {} +#[cfg(feature = "sync")] +impl U + Send + Sync + 'static, T, X, U> ObjectIndexerCallback for F {} + +#[cfg(not(feature = "sync"))] +pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} +#[cfg(not(feature = "sync"))] +impl U + 'static, T, X, U> ObjectIndexerCallback for F {} + +#[cfg(feature = "sync")] +pub trait IteratorCallback: + Fn(Dynamic) -> Box> + Send + Sync + 'static +{ +} +#[cfg(feature = "sync")] +impl Box> + Send + Sync + 'static> IteratorCallback + for F +{ +} + +#[cfg(not(feature = "sync"))] +pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} +#[cfg(not(feature = "sync"))] +impl Box> + 'static> IteratorCallback for F {} + /// A type representing the type of ABI of a native Rust function. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub enum NativeFunctionABI { @@ -28,6 +80,7 @@ pub enum NativeFunctionABI { } /// A trait implemented by all native Rust functions that are callable by Rhai. +#[cfg(not(feature = "sync"))] pub trait NativeCallable { /// Get the ABI type of a native Rust function. fn abi(&self) -> NativeFunctionABI; @@ -35,6 +88,15 @@ pub trait NativeCallable { fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; } +/// A trait implemented by all native Rust functions that are callable by Rhai. +#[cfg(feature = "sync")] +pub trait NativeCallable: Send + Sync { + /// Get the ABI type of a native Rust function. + fn abi(&self) -> NativeFunctionABI; + /// Call a native Rust function. + fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; +} + /// A type encapsulating a native Rust function callable by Rhai. pub struct NativeFunction(Box, NativeFunctionABI); diff --git a/src/fn_register.rs b/src/fn_register.rs index 28c15c7f..55271ae1 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -119,14 +119,14 @@ pub struct Mut(T); /// Dereference into &mut. #[inline(always)] -pub fn by_ref(data: &mut Dynamic) -> &mut T { +pub fn by_ref(data: &mut Dynamic) -> &mut T { // Directly cast the &mut Dynamic into &mut T to access the underlying data. data.downcast_mut::().unwrap() } /// Dereference into value. #[inline(always)] -pub fn by_value(data: &mut Dynamic) -> T { +pub fn by_value(data: &mut Dynamic) -> T { // We consume the argument and then replace it with () - the argument is not supposed to be used again. // This way, we avoid having to clone the argument again, because it is already a clone when passed here. mem::take(data).cast::() diff --git a/src/module.rs b/src/module.rs index 83da428c..089acd3d 100644 --- a/src/module.rs +++ b/src/module.rs @@ -31,6 +31,7 @@ use crate::stdlib::{ }; /// A trait that encapsulates a module resolution service. +#[cfg(not(feature = "sync"))] pub trait ModuleResolver { /// Resolve a module based on a path string. fn resolve( @@ -42,6 +43,19 @@ pub trait ModuleResolver { ) -> Result>; } +/// A trait that encapsulates a module resolution service. +#[cfg(feature = "sync")] +pub trait ModuleResolver: Send + Sync { + /// Resolve a module based on a path string. + fn resolve( + &self, + engine: &Engine, + scope: Scope, + path: &str, + pos: Position, + ) -> Result>; +} + /// Default function access mode. const DEF_ACCESS: FnAccess = FnAccess::Public; diff --git a/tests/modules.rs b/tests/modules.rs index 52f6818e..acf3c0e6 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -83,6 +83,7 @@ fn test_module_resolver() -> Result<(), Box> { } #[test] +#[cfg(not(feature = "no_function"))] fn test_module_from_ast() -> Result<(), Box> { let mut engine = Engine::new(); @@ -100,7 +101,7 @@ fn test_module_from_ast() -> Result<(), Box> { x + 1 } fn add_len(x, y) { - x + y.len() + x + len(y) } private fn hidden() { throw "you shouldn't see me!"; From ec678797593777f9be836c0914a39839c7fec6b9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 12 May 2020 18:48:25 +0800 Subject: [PATCH 07/36] Derive more standard traits. --- src/engine.rs | 10 ++-------- src/error.rs | 2 +- src/fn_native.rs | 2 +- src/module.rs | 4 ++-- src/parser.rs | 6 +++--- src/scope.rs | 4 ++-- src/utils.rs | 8 ++++++++ 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 301bbfca..6245d1cc 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -173,11 +173,8 @@ impl FunctionsLib { vec.into_iter() .map(|fn_def| { // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash = calc_fn_hash( - empty(), - &fn_def.name, - repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), - ); + let args_iter = repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()); + let hash = calc_fn_hash(empty(), &fn_def.name, args_iter); #[cfg(feature = "sync")] { @@ -284,11 +281,9 @@ pub struct Engine { pub(crate) packages: PackagesCollection, /// A collection of all library packages loaded into the engine. pub(crate) base_package: PackageStore, - /// A module resolution service. #[cfg(not(feature = "no_module"))] pub(crate) module_resolver: Option>, - /// A hashmap mapping type names to pretty-print names. pub(crate) type_names: HashMap, @@ -299,7 +294,6 @@ pub struct Engine { /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, - /// Maximum levels of call-stack to prevent infinite recursion. /// /// Defaults to 28 for debug builds and 256 for non-debug builds. diff --git a/src/error.rs b/src/error.rs index ee3f87f7..11b7c9e2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -122,7 +122,7 @@ impl ParseErrorType { } /// Error when parsing a script. -#[derive(Debug, PartialEq, Eq, Clone, Hash)] +#[derive(Debug, Eq, PartialEq, Clone, Hash)] pub struct ParseError(pub(crate) ParseErrorType, pub(crate) Position); impl ParseError { diff --git a/src/fn_native.rs b/src/fn_native.rs index bfc9311b..12134550 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -70,7 +70,7 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box> + ' impl Box> + 'static> IteratorCallback for F {} /// A type representing the type of ABI of a native Rust function. -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] pub enum NativeFunctionABI { /// A pure function where all arguments are passed by value. Pure, diff --git a/src/module.rs b/src/module.rs index 089acd3d..f86b2dca 100644 --- a/src/module.rs +++ b/src/module.rs @@ -757,7 +757,7 @@ mod file { /// let mut engine = Engine::new(); /// engine.set_module_resolver(Some(resolver)); /// ``` - #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] + #[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone, Hash)] pub struct FileModuleResolver { path: PathBuf, extension: String, @@ -875,7 +875,7 @@ mod file { /// /// A `StaticVec` is used because most module-level access contains only one level, /// and it is wasteful to always allocate a `Vec` with one element. -#[derive(Clone, Hash, Default)] +#[derive(Clone, Eq, PartialEq, Hash, Default)] pub struct ModuleRef(StaticVec<(String, Position)>, Option); impl fmt::Debug for ModuleRef { diff --git a/src/parser.rs b/src/parser.rs index da35d62d..8264da56 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -13,7 +13,7 @@ use crate::utils::EMPTY_TYPE_ID; use crate::module::ModuleRef; #[cfg(feature = "no_module")] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Default)] pub struct ModuleRef; use crate::stdlib::{ @@ -179,7 +179,7 @@ impl Add for &AST { } /// A type representing the access mode of a scripted function. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum FnAccess { /// Private function. Private, @@ -210,7 +210,7 @@ pub type SharedFnDef = Arc; pub type SharedFnDef = Rc; /// `return`/`throw` statement. -#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] +#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] pub enum ReturnType { /// `return` statement. Return, diff --git a/src/scope.rs b/src/scope.rs index 9f47b446..17909970 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -22,7 +22,7 @@ pub enum EntryType { } /// An entry in the Scope. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Entry<'a> { /// Name of the entry. pub name: Cow<'a, str>, @@ -64,7 +64,7 @@ pub struct Entry<'a> { /// allowing for automatic _shadowing_. /// /// Currently, `Scope` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`. -#[derive(Debug, Default)] +#[derive(Debug, Clone, Default)] pub struct Scope<'a>(Vec>); impl<'a> Scope<'a> { diff --git a/src/utils.rs b/src/utils.rs index 7abe88ab..4694fce4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -64,6 +64,14 @@ pub struct StaticVec { more: Vec, } +impl PartialEq for StaticVec { + fn eq(&self, other: &Self) -> bool { + self.len == other.len && self.list == other.list && self.more == other.more + } +} + +impl Eq for StaticVec {} + impl Default for StaticVec { fn default() -> Self { Self { From d6fd5416b02cf5065c8e4143c6efac2d16190331 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 12 May 2020 23:30:55 +0800 Subject: [PATCH 08/36] Add replace function for characters --- README.md | 2 +- src/packages/string_more.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a8329aa3..00e5b940 100644 --- a/README.md +++ b/README.md @@ -1328,7 +1328,7 @@ The following standard methods (defined in the [`MoreStringPackage`](#packages) | `index_of` | character/sub-string to search for, start index _(optional)_ | returns the index that a certain character or sub-string occurs in the string, or -1 if not found | | `sub_string` | start index, length _(optional)_ | extracts a sub-string (to the end of the string if length is not specified) | | `crop` | start index, length _(optional)_ | retains only a portion of the string (to the end of the string if length is not specified) | -| `replace` | target sub-string, replacement string | replaces a sub-string with another | +| `replace` | target character/sub-string, replacement character/string | replaces a sub-string with another | | `trim` | _none_ | trims the string of whitespace at the beginning and end | ### Examples diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 2badac23..697a6eb0 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -226,6 +226,36 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str }, map, ); + reg_trinary_mut( + lib, + "replace", + |s: &mut String, find: String, sub: char| { + let new_str = s.replace(&find, &sub.to_string()); + s.clear(); + s.push_str(&new_str); + }, + map, + ); + reg_trinary_mut( + lib, + "replace", + |s: &mut String, find: char, sub: String| { + let new_str = s.replace(&find.to_string(), &sub); + s.clear(); + s.push_str(&new_str); + }, + map, + ); + reg_trinary_mut( + lib, + "replace", + |s: &mut String, find: char, sub: char| { + let new_str = s.replace(&find.to_string(), &sub.to_string()); + s.clear(); + s.push_str(&new_str); + }, + map, + ); reg_unary_mut( lib, "trim", From 996a54279cc60f2fadfcaa55a37dc48f3c047fff Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 10:19:18 +0800 Subject: [PATCH 09/36] Pre-calculate property getter/setter function names. --- src/engine.rs | 29 ++++++++++++++++------------- src/optimize.rs | 3 ++- src/parser.rs | 15 +++++++++++---- src/utils.rs | 1 + 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 6245d1cc..05a97502 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -884,9 +884,10 @@ impl Engine { // {xxx:map}.id = ??? #[cfg(not(feature = "no_object"))] Expr::Property(x) if obj.is::() && new_val.is_some() => { - let index = x.0.clone().into(); + let ((prop, _, _), pos) = x.as_ref(); + let index = prop.clone().into(); let mut val = - self.get_indexed_mut(state, obj, is_ref, index, x.1, op_pos, true)?; + self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, true)?; val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) @@ -894,24 +895,25 @@ impl Engine { // {xxx:map}.id #[cfg(not(feature = "no_object"))] Expr::Property(x) if obj.is::() => { - let index = x.0.clone().into(); + let ((prop, _, _), pos) = x.as_ref(); + let index = prop.clone().into(); let val = - self.get_indexed_mut(state, obj, is_ref, index, x.1, op_pos, false)?; + self.get_indexed_mut(state, obj, is_ref, index, *pos, op_pos, false)?; Ok((val.clone_into_dynamic(), false)) } // xxx.id = ??? Expr::Property(x) if new_val.is_some() => { - let fn_name = make_setter(&x.0); + let ((_, _, setter), pos) = x.as_ref(); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, &fn_name, 0, &mut args, is_ref, None, x.1, 0) + self.exec_fn_call(state, setter, 0, &mut args, is_ref, None, *pos, 0) .map(|(v, _)| (v, true)) } // xxx.id Expr::Property(x) => { - let fn_name = make_getter(&x.0); + let ((_, getter, _), pos) = x.as_ref(); let mut args = [obj]; - self.exec_fn_call(state, &fn_name, 0, &mut args, is_ref, None, x.1, 0) + self.exec_fn_call(state, getter, 0, &mut args, is_ref, None, *pos, 0) .map(|(v, _)| (v, false)) } #[cfg(not(feature = "no_object"))] @@ -920,7 +922,8 @@ impl Engine { let is_idx = matches!(rhs, Expr::Index(_)); let val = if let Expr::Property(p) = &x.0 { - let index = p.0.clone().into(); + let ((prop, _, _), _) = p.as_ref(); + let index = prop.clone().into(); self.get_indexed_mut(state, obj, is_ref, index, x.2, op_pos, false)? } else { // Syntax error @@ -940,8 +943,8 @@ impl Engine { let args = &mut [obj, &mut Default::default()]; let (mut val, updated) = if let Expr::Property(p) = &x.0 { - let fn_name = make_getter(&p.0); - self.exec_fn_call(state, &fn_name, 0, &mut args[..1], is_ref, None, x.2, 0)? + let ((_, getter, _), _) = p.as_ref(); + self.exec_fn_call(state, getter, 0, &mut args[..1], is_ref, None, x.2, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -965,10 +968,10 @@ impl Engine { // Feed the value back via a setter just in case it has been updated if updated || may_be_changed { if let Expr::Property(p) = &x.0 { - let fn_name = make_setter(&p.0); + let ((_, _, setter), _) = p.as_ref(); // Re-use args because the first &mut parameter will not be consumed args[1] = val; - self.exec_fn_call(state, &fn_name, 0, args, is_ref, None, x.2, 0) + self.exec_fn_call(state, setter, 0, args, is_ref, None, x.2, 0) .or_else(|err| match *err { // If there is no setter, no need to feed it back because the property is read-only EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()), diff --git a/src/optimize.rs b/src/optimize.rs index 0f83a703..9670f288 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -392,11 +392,12 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { Expr::Dot(x) => match (x.0, x.1) { // map.string (Expr::Map(m), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { + let ((prop, _, _), _) = p.as_ref(); // Map literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.into_iter().find(|((name, _), _)| name == &p.0) + m.0.into_iter().find(|((name, _), _)| name == prop) .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } diff --git a/src/parser.rs b/src/parser.rs index 8264da56..42f8a5a5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,7 +2,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; -use crate::engine::{Engine, FunctionsLib}; +use crate::engine::{make_getter, make_setter, Engine, FunctionsLib}; use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -395,7 +395,7 @@ pub enum Expr { /// Variable access - ((variable name, position), optional modules, hash, optional index) Variable(Box<((String, Position), MRef, u64, Option)>), /// Property access. - Property(Box<(String, Position)>), + Property(Box<((String, String, String), Position)>), /// { stmt } Stmt(Box<(Stmt, Position)>), /// func(expr, ... ) - ((function name, position), optional modules, hash, arguments, optional default value) @@ -641,7 +641,9 @@ impl Expr { match self { Self::Variable(x) if x.1.is_none() => { let (name, pos) = x.0; - Self::Property(Box::new((name.clone(), pos))) + let getter = make_getter(&name); + let setter = make_setter(&name); + Self::Property(Box::new(((name.clone(), getter, setter), pos))) } _ => self, } @@ -1431,8 +1433,13 @@ fn make_dot_expr( } // lhs.id (lhs, Expr::Variable(x)) if x.1.is_none() => { + let (name, pos) = x.0; let lhs = if is_index { lhs.into_property() } else { lhs }; - let rhs = Expr::Property(Box::new(x.0)); + + let getter = make_getter(&name); + let setter = make_setter(&name); + let rhs = Expr::Property(Box::new(((name, getter, setter), pos))); + Expr::Dot(Box::new((lhs, rhs, op_pos))) } (lhs, Expr::Property(x)) => { diff --git a/src/utils.rs b/src/utils.rs index 4694fce4..381ab1a0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -17,6 +17,7 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; +#[inline(always)] pub fn EMPTY_TYPE_ID() -> TypeId { TypeId::of::<()>() } From c37a2cc88664bc016cd093278452cdd70109ff5c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 11:56:48 +0800 Subject: [PATCH 10/36] Check scripts for calculation errors. --- scripts/fibonacci.rhai | 10 +++++++--- scripts/primes.rhai | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/scripts/fibonacci.rhai b/scripts/fibonacci.rhai index a9a54c3a..730dadae 100644 --- a/scripts/fibonacci.rhai +++ b/scripts/fibonacci.rhai @@ -3,8 +3,6 @@ const target = 30; -let now = timestamp(); - fn fib(n) { if n < 2 { n @@ -15,8 +13,14 @@ fn fib(n) { print("Ready... Go!"); +let now = timestamp(); + let result = fib(target); +print("Finished. Run time = " + now.elapsed() + " seconds."); + print("Fibonacci number #" + target + " = " + result); -print("Finished. Run time = " + now.elapsed() + " seconds."); +if result != 832_040 { + print("The answer is WRONG! Should be 832,040!"); +} \ No newline at end of file diff --git a/scripts/primes.rhai b/scripts/primes.rhai index 668fa250..2b67ec50 100644 --- a/scripts/primes.rhai +++ b/scripts/primes.rhai @@ -27,3 +27,7 @@ for p in range(2, MAX_NUMBER_TO_CHECK) { print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK); print("Run time = " + now.elapsed() + " seconds."); + +if total_primes_found != 9_592 { + print("The answer is WRONG! Should be 9,592!"); +} \ No newline at end of file From 8e8816cb0c01bee4cabf0919b3f3cc3f9e1fa439 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 11:57:07 +0800 Subject: [PATCH 11/36] Add compile_scripts_with_scope. --- src/api.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++----- src/engine.rs | 2 +- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/api.rs b/src/api.rs index 95423d8e..acb353d9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -344,6 +344,7 @@ impl Engine { } /// Compile a string into an `AST` using own scope, which can be used later for evaluation. + /// /// The scope is useful for passing constants into the script for optimization /// when using `OptimizationLevel::Full`. /// @@ -381,18 +382,71 @@ impl Engine { /// # } /// ``` pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result> { - self.compile_with_scope_and_optimization_level(scope, script, self.optimization_level) + self.compile_scripts_with_scope(scope, &[script]) } - /// Compile a string into an `AST` using own scope at a specific optimization level. + /// When passed a list of strings, first join the strings into one large script, + /// and then compile them into an `AST` using own scope, which can be used later for evaluation. + /// + /// The scope is useful for passing constants into the script for optimization + /// when using `OptimizationLevel::Full`. + /// + /// ## Note + /// + /// All strings are simply parsed one after another with nothing inserted in between, not even + /// a newline or space. + /// + /// # Example + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # #[cfg(not(feature = "no_optimize"))] + /// # { + /// use rhai::{Engine, Scope, OptimizationLevel}; + /// + /// let mut engine = Engine::new(); + /// + /// // Set optimization level to 'Full' so the Engine can fold constants + /// // into function calls and operators. + /// engine.set_optimization_level(OptimizationLevel::Full); + /// + /// // Create initialized scope + /// let mut scope = Scope::new(); + /// scope.push_constant("x", 42_i64); // 'x' is a constant + /// + /// // Compile a script made up of script segments to an AST and store it for later evaluation. + /// // Notice that `Full` optimization is on, so constants are folded + /// // into function calls and operators. + /// let ast = engine.compile_scripts_with_scope(&mut scope, &[ + /// "if x > 40", // all 'x' are replaced with 42 + /// "{ x } el" + /// "se { 0 }" // segments do not need to be valid scripts! + /// ])?; + /// + /// // Normally this would have failed because no scope is passed into the 'eval_ast' + /// // call and so the variable 'x' does not exist. Here, it passes because the script + /// // has been optimized and all references to 'x' are already gone. + /// assert_eq!(engine.eval_ast::(&ast)?, 42); + /// # } + /// # Ok(()) + /// # } + /// ``` + pub fn compile_scripts_with_scope( + &self, + scope: &Scope, + scripts: &[&str], + ) -> Result> { + self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level) + } + + /// Join a list of strings and compile into an `AST` using own scope at a specific optimization level. pub(crate) fn compile_with_scope_and_optimization_level( &self, scope: &Scope, - script: &str, + scripts: &[&str], optimization_level: OptimizationLevel, ) -> Result> { - let scripts = [script]; - let stream = lex(&scripts); + let stream = lex(scripts); parse(&mut stream.peekable(), self, scope, optimization_level) } @@ -446,6 +500,7 @@ impl Engine { } /// Compile a script file into an `AST` using own scope, which can be used later for evaluation. + /// /// The scope is useful for passing constants into the script for optimization /// when using `OptimizationLevel::Full`. /// @@ -697,8 +752,11 @@ impl Engine { script: &str, ) -> Result> { // Since the AST will be thrown away afterwards, don't bother to optimize it - let ast = - self.compile_with_scope_and_optimization_level(scope, script, OptimizationLevel::None)?; + let ast = self.compile_with_scope_and_optimization_level( + scope, + &[script], + OptimizationLevel::None, + )?; self.eval_ast_with_scope(scope, &ast) } diff --git a/src/engine.rs b/src/engine.rs index 05a97502..e92d538b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -792,7 +792,7 @@ impl Engine { // No optimizations because we only run it once let mut ast = self.compile_with_scope_and_optimization_level( &Scope::new(), - script, + &[script], OptimizationLevel::None, )?; From d613764c03a825cd9939385b579ea7bf70bfd7ac Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 13:49:01 +0800 Subject: [PATCH 12/36] Test for private functions. --- tests/call_fn.rs | 38 +++++++++++++++++++++++++++++++++++--- tests/functions.rs | 8 ++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/tests/call_fn.rs b/tests/call_fn.rs index f3d2a689..e3c61765 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -41,13 +41,13 @@ fn test_call_fn() -> Result<(), Box> { ", )?; - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; assert_eq!(r, 165); - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; assert_eq!(r, 5166); - let r: i64 = engine.call_fn(&mut scope, &ast, "hello", ())?; + let r: INT = engine.call_fn(&mut scope, &ast, "hello", ())?; assert_eq!(r, 42); assert_eq!( @@ -60,6 +60,27 @@ fn test_call_fn() -> Result<(), Box> { Ok(()) } +#[test] +fn test_call_fn_private() -> Result<(), Box> { + let engine = Engine::new(); + let mut scope = Scope::new(); + + let ast = engine.compile("fn add(x, n) { x + n }")?; + + let r: INT = engine.call_fn(&mut scope, &ast, "add", (40 as INT, 2 as INT))?; + assert_eq!(r, 42); + + let ast = engine.compile("private fn add(x, n) { x + n }")?; + + assert!(matches!( + *engine.call_fn::<_, INT>(&mut scope, &ast, "add", (40 as INT, 2 as INT)) + .expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "add" + )); + + Ok(()) +} + #[test] fn test_anonymous_fn() -> Result<(), Box> { let calc_func = Func::<(INT, INT, INT), INT>::create_from_script( @@ -70,5 +91,16 @@ fn test_anonymous_fn() -> Result<(), Box> { assert_eq!(calc_func(42, 123, 9)?, 1485); + let calc_func = Func::<(INT, INT, INT), INT>::create_from_script( + Engine::new(), + "private fn calc(x, y, z) { (x + y) * z }", + "calc", + )?; + + assert!(matches!( + *calc_func(42, 123, 9).expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "calc" + )); + Ok(()) } diff --git a/tests/functions.rs b/tests/functions.rs index 8200962b..422394c1 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -5,19 +5,19 @@ use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT}; fn test_functions() -> Result<(), Box> { let engine = Engine::new(); - assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); + assert_eq!(engine.eval::("fn add(x, n) { x + n } add(40, 2)")?, 42); #[cfg(not(feature = "no_object"))] assert_eq!( - engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, + engine.eval::("fn add(x, n) { x + n } let x = 40; x.add(2)")?, 42 ); - assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); + assert_eq!(engine.eval::("fn mul2(x) { x * 2 } mul2(21)")?, 42); #[cfg(not(feature = "no_object"))] assert_eq!( - engine.eval::("fn mul2(x) { x * 2 } let x = 21; x.mul2()")?, + engine.eval::("fn mul2(x) { x * 2 } let x = 21; x.mul2()")?, 42 ); From 30e5e2f034c641709d226d034dae8d5e6093c9cb Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 19:21:42 +0800 Subject: [PATCH 13/36] Use modules to implement packages. --- src/api.rs | 7 +- src/engine.rs | 27 +-- src/fn_native.rs | 7 + src/fn_register.rs | 38 ++-- src/lib.rs | 3 +- src/module.rs | 269 ++++++++++++---------- src/optimize.rs | 11 +- src/packages/arithmetic.rs | 152 ++++++------- src/packages/array_basic.rs | 72 +++--- src/packages/iter_basic.rs | 44 ++-- src/packages/logic.rs | 66 +++--- src/packages/map_basic.rs | 46 ++-- src/packages/math_basic.rs | 106 +++++---- src/packages/mod.rs | 113 +++++----- src/packages/string_basic.rs | 64 +++--- src/packages/string_more.rs | 158 ++++++------- src/packages/time_basic.rs | 25 +-- src/packages/utils.rs | 425 ----------------------------------- 18 files changed, 610 insertions(+), 1023 deletions(-) delete mode 100644 src/packages/utils.rs diff --git a/src/api.rs b/src/api.rs index acb353d9..d69933e9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -125,9 +125,8 @@ impl Engine { /// Register an iterator adapter for a type with the `Engine`. /// This is an advanced feature. pub fn register_iterator(&mut self, f: F) { - self.base_package - .type_iterators - .insert(TypeId::of::(), Box::new(f)); + self.global_module + .set_iterator(TypeId::of::(), Box::new(f)); } /// Register a getter function for a member of a registered type with the `Engine`. @@ -419,7 +418,7 @@ impl Engine { /// // into function calls and operators. /// let ast = engine.compile_scripts_with_scope(&mut scope, &[ /// "if x > 40", // all 'x' are replaced with 42 - /// "{ x } el" + /// "{ x } el", /// "se { 0 }" // segments do not need to be valid scripts! /// ])?; /// diff --git a/src/engine.rs b/src/engine.rs index e92d538b..2acb7fad 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -4,10 +4,9 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback}; +use crate::module::Module; use crate::optimize::OptimizationLevel; -use crate::packages::{ - CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, -}; +use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -15,7 +14,7 @@ use crate::token::Position; use crate::utils::{StaticVec, EMPTY_TYPE_ID}; #[cfg(not(feature = "no_module"))] -use crate::module::{resolvers, Module, ModuleRef, ModuleResolver}; +use crate::module::{resolvers, ModuleRef, ModuleResolver}; #[cfg(feature = "no_module")] use crate::parser::ModuleRef; @@ -277,13 +276,15 @@ impl DerefMut for FunctionsLib { /// /// Currently, `Engine` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`. pub struct Engine { - /// A collection of all library packages loaded into the engine. + /// A module containing all functions directly loaded into the Engine. + pub(crate) global_module: Module, + /// A collection of all library packages loaded into the Engine. pub(crate) packages: PackagesCollection, - /// A collection of all library packages loaded into the engine. - pub(crate) base_package: PackageStore, + /// A module resolution service. #[cfg(not(feature = "no_module"))] pub(crate) module_resolver: Option>, + /// A hashmap mapping type names to pretty-print names. pub(crate) type_names: HashMap, @@ -305,7 +306,7 @@ impl Default for Engine { // Create the new scripting Engine let mut engine = Self { packages: Default::default(), - base_package: Default::default(), + global_module: Default::default(), #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] @@ -450,7 +451,7 @@ impl Engine { pub fn new_raw() -> Self { Self { packages: Default::default(), - base_package: Default::default(), + global_module: Default::default(), #[cfg(not(feature = "no_module"))] module_resolver: None, @@ -549,8 +550,8 @@ impl Engine { // Search built-in's and external functions if let Some(func) = self - .base_package - .get_function(hashes.0) + .global_module + .get_fn(hashes.0) .or_else(|| self.packages.get_function(hashes.0)) { let mut backup: Dynamic = Default::default(); @@ -725,7 +726,7 @@ impl Engine { // Has a system function an override? fn has_override(&self, state: &State, hashes: (u64, u64)) -> bool { // First check registered functions - self.base_package.contains_function(hashes.0) + self.global_module.contains_fn(hashes.0) // Then check packages || self.packages.contains_function(hashes.0) // Then check script-defined functions @@ -1571,7 +1572,7 @@ impl Engine { let tid = iter_type.type_id(); if let Some(iter_fn) = self - .base_package + .global_module .get_iterator(tid) .or_else(|| self.packages.get_iterator(tid)) { diff --git a/src/fn_native.rs b/src/fn_native.rs index 12134550..b56d2644 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -127,3 +127,10 @@ pub type SharedNativeFunction = Rc>; /// An external native Rust function. #[cfg(feature = "sync")] pub type SharedNativeFunction = Arc>; + +/// A type iterator function. +#[cfg(not(feature = "sync"))] +pub type SharedIteratorFunction = Rc>; +/// An external native Rust function. +#[cfg(feature = "sync")] +pub type SharedIteratorFunction = Arc>; diff --git a/src/fn_register.rs b/src/fn_register.rs index 55271ae1..80e09cd5 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,10 +4,10 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI::*}; +use crate::fn_native::{FnCallArgs, NativeFunctionABI::*}; +use crate::parser::FnAccess; use crate::result::EvalAltResult; use crate::token::Position; -use crate::utils::calc_fn_spec; use crate::stdlib::{any::TypeId, boxed::Box, iter::empty, mem, string::ToString}; @@ -134,14 +134,13 @@ pub fn by_value(data: &mut Dynamic) -> T { /// This macro creates a closure wrapping a registered function. macro_rules! make_func { - ($fn:ident : $abi:expr ; $map:expr ; $($par:ident => $convert:expr),*) => { + ($fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { // ^ function pointer -// ^ function ABI type -// ^ result mapping function -// ^ function parameter generic type name (A, B, C etc.) -// ^ dereferencing function +// ^ result mapping function +// ^ function parameter generic type name (A, B, C etc.) +// ^ dereferencing function - NativeFunction::new(Box::new(move |args: &mut FnCallArgs, pos: Position| { + Box::new(move |args: &mut FnCallArgs, pos: Position| { // The arguments are assumed to be of the correct number and types! #[allow(unused_variables, unused_mut)] @@ -157,7 +156,7 @@ macro_rules! make_func { // Map the result $map(r, pos) - }), $abi); + }) }; } @@ -209,9 +208,10 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : $abi ; map_dynamic ; $($par => $clone),*); - let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.base_package.functions.insert(hash, Box::new(func)); + self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + &[$(TypeId::of::<$par>()),*], + make_func!(f : map_dynamic ; $($par => $clone),*) + ); } } @@ -226,9 +226,10 @@ macro_rules! def_register { > RegisterDynamicFn for Engine { fn register_dynamic_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : $abi ; map_identity ; $($par => $clone),*); - let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.base_package.functions.insert(hash, Box::new(func)); + self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + &[$(TypeId::of::<$par>()),*], + make_func!(f : map_identity ; $($par => $clone),*) + ); } } @@ -244,9 +245,10 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - let func = make_func!(f : $abi ; map_result ; $($par => $clone),*); - let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.base_package.functions.insert(hash, Box::new(func)); + self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + &[$(TypeId::of::<$par>()),*], + make_func!(f : map_result ; $($par => $clone),*) + ); } } diff --git a/src/lib.rs b/src/lib.rs index a912bdab..f82bc6d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,7 @@ pub use error::{ParseError, ParseErrorType}; pub use fn_call::FuncArgs; pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; +pub use module::Module; pub use parser::{AST, INT}; pub use result::EvalAltResult; pub use scope::Scope; @@ -112,7 +113,7 @@ pub use engine::Map; pub use parser::FLOAT; #[cfg(not(feature = "no_module"))] -pub use module::{Module, ModuleResolver}; +pub use module::ModuleResolver; #[cfg(not(feature = "no_module"))] pub mod module_resolvers { diff --git a/src/module.rs b/src/module.rs index f86b2dca..d1bcabb1 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,12 +1,11 @@ //! Module defining external-loaded modules for Rhai. -#![cfg(not(feature = "no_module"))] use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, NativeCallable, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, - SharedNativeFunction, + FnAny, FnCallArgs, IteratorFn, NativeCallable, NativeFunction, NativeFunctionABI, + NativeFunctionABI::*, SharedIteratorFunction, SharedNativeFunction, }; use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; @@ -30,43 +29,17 @@ use crate::stdlib::{ vec::Vec, }; -/// A trait that encapsulates a module resolution service. -#[cfg(not(feature = "sync"))] -pub trait ModuleResolver { - /// Resolve a module based on a path string. - fn resolve( - &self, - engine: &Engine, - scope: Scope, - path: &str, - pos: Position, - ) -> Result>; -} - -/// A trait that encapsulates a module resolution service. -#[cfg(feature = "sync")] -pub trait ModuleResolver: Send + Sync { - /// Resolve a module based on a path string. - fn resolve( - &self, - engine: &Engine, - scope: Scope, - path: &str, - pos: Position, - ) -> Result>; -} - /// Default function access mode. const DEF_ACCESS: FnAccess = FnAccess::Public; /// Return type of module-level Rust function. -type FuncReturn = Result>; +pub type FuncReturn = Result>; /// An imported module, which may contain variables, sub-modules, /// external Rust functions, and script-defined functions. /// /// Not available under the `no_module` feature. -#[derive(Default, Clone)] +#[derive(Clone, Default)] pub struct Module { /// Sub-modules. modules: HashMap, @@ -88,6 +61,9 @@ pub struct Module { /// Flattened collection of all script-defined functions, including those in sub-modules. all_fn_lib: FunctionsLib, + + /// Iterator functions, keyed by the type producing the iterator. + type_iterators: HashMap, } impl fmt::Debug for Module { @@ -176,8 +152,8 @@ impl Module { /// module.set_var("answer", 42_i64); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); /// ``` - pub fn set_var, T: Into>(&mut self, name: K, value: T) { - self.variables.insert(name.into(), value.into()); + pub fn set_var, T: Variant + Clone>(&mut self, name: K, value: T) { + self.variables.insert(name.into(), Dynamic::from(value)); } /// Get a mutable reference to a modules-qualified variable. @@ -286,7 +262,7 @@ impl Module { name: String, abi: NativeFunctionABI, access: FnAccess, - params: Vec, + params: &[TypeId], func: Box, ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); @@ -298,7 +274,8 @@ impl Module { #[cfg(feature = "sync")] let func = Arc::new(f); - self.functions.insert(hash_fn, (name, access, params, func)); + self.functions + .insert(hash_fn, (name, access, params.to_vec(), func)); hash_fn } @@ -316,7 +293,7 @@ impl Module { /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_0, T: Into>( + pub fn set_fn_0, T: Variant + Clone>( &mut self, name: K, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, @@ -324,11 +301,11 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs, pos| { func() - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![]; - self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = []; + self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -344,7 +321,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_1, A: Variant + Clone, T: Into>( + pub fn set_fn_1, A: Variant + Clone, T: Variant + Clone>( &mut self, name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, @@ -352,11 +329,11 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(mem::take(args[0]).cast::()) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::()]; + self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -372,7 +349,7 @@ impl Module { /// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_1_mut, A: Variant + Clone, T: Into>( + pub fn set_fn_1_mut, A: Variant + Clone, T: Variant + Clone>( &mut self, name: K, #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, @@ -380,11 +357,11 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(args[0].downcast_mut::().unwrap()) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::()]; + self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -402,7 +379,7 @@ impl Module { /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Into>( + pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( &mut self, name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn + 'static, @@ -413,11 +390,11 @@ impl Module { let b = mem::take(args[1]).cast::(); func(a, b) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -438,7 +415,7 @@ impl Module { K: Into, A: Variant + Clone, B: Variant + Clone, - T: Into, + T: Variant + Clone, >( &mut self, name: K, @@ -450,11 +427,11 @@ impl Module { let a = args[0].downcast_mut::().unwrap(); func(a, b) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -477,7 +454,7 @@ impl Module { A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, - T: Into, + T: Variant + Clone, >( &mut self, name: K, @@ -490,11 +467,11 @@ impl Module { let c = mem::take(args[2]).cast::(); func(a, b, c) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -518,7 +495,7 @@ impl Module { A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, - T: Into, + T: Variant + Clone, >( &mut self, name: K, @@ -531,11 +508,11 @@ impl Module { let a = args[0].downcast_mut::().unwrap(); func(a, b, c) - .map(Into::::into) + .map(Dynamic::from) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, arg_types, Box::new(f)) + let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) } /// Get a Rust function. @@ -609,6 +586,7 @@ impl Module { /// # Ok(()) /// # } /// ``` + #[cfg(not(feature = "no_module"))] pub fn eval_ast_as_new(mut scope: Scope, ast: &AST, engine: &Engine) -> FuncReturn { // Run the script engine.eval_ast_with_scope_raw(&mut scope, &ast)?; @@ -722,9 +700,114 @@ impl Module { self.all_functions = functions.into_iter().collect(); self.all_fn_lib = fn_lib.into(); } + + /// Does a type iterator exist in the module? + pub fn contains_iterator(&self, id: TypeId) -> bool { + self.type_iterators.contains_key(&id) + } + + /// Set a type iterator into the module. + pub fn set_iterator(&mut self, typ: TypeId, func: Box) { + #[cfg(not(feature = "sync"))] + self.type_iterators.insert(typ, Rc::new(func)); + #[cfg(feature = "sync")] + self.type_iterators.insert(typ, Arc::new(func)); + } + + /// Get the specified type iterator. + pub fn get_iterator(&self, id: TypeId) -> Option<&SharedIteratorFunction> { + self.type_iterators.get(&id) + } +} + +/// A chain of module names to qualify a variable or function call. +/// A `u64` hash key is kept for quick search purposes. +/// +/// A `StaticVec` is used because most module-level access contains only one level, +/// and it is wasteful to always allocate a `Vec` with one element. +#[derive(Clone, Eq, PartialEq, Hash, Default)] +pub struct ModuleRef(StaticVec<(String, Position)>, Option); + +impl fmt::Debug for ModuleRef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f)?; + + if let Some(index) = self.1 { + write!(f, " -> {}", index) + } else { + Ok(()) + } + } +} + +impl Deref for ModuleRef { + type Target = StaticVec<(String, Position)>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ModuleRef { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl fmt::Display for ModuleRef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for (m, _) in self.0.iter() { + write!(f, "{}{}", m, Token::DoubleColon.syntax())?; + } + Ok(()) + } +} + +impl From> for ModuleRef { + fn from(modules: StaticVec<(String, Position)>) -> Self { + Self(modules, None) + } +} + +impl ModuleRef { + pub(crate) fn index(&self) -> Option { + self.1 + } + pub(crate) fn set_index(&mut self, index: Option) { + self.1 = index + } +} + +/// A trait that encapsulates a module resolution service. +#[cfg(not(feature = "no_module"))] +#[cfg(not(feature = "sync"))] +pub trait ModuleResolver { + /// Resolve a module based on a path string. + fn resolve( + &self, + engine: &Engine, + scope: Scope, + path: &str, + pos: Position, + ) -> Result>; +} + +/// A trait that encapsulates a module resolution service. +#[cfg(not(feature = "no_module"))] +#[cfg(feature = "sync")] +pub trait ModuleResolver: Send + Sync { + /// Resolve a module based on a path string. + fn resolve( + &self, + engine: &Engine, + scope: Scope, + path: &str, + pos: Position, + ) -> Result>; } /// Re-export module resolvers. +#[cfg(not(feature = "no_module"))] pub mod resolvers { #[cfg(not(feature = "no_std"))] pub use super::file::FileModuleResolver; @@ -732,6 +815,7 @@ pub mod resolvers { } /// Script file-based module resolver. +#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] mod file { use super::*; @@ -870,65 +954,8 @@ mod file { } } -/// A chain of module names to qualify a variable or function call. -/// A `u64` hash key is kept for quick search purposes. -/// -/// A `StaticVec` is used because most module-level access contains only one level, -/// and it is wasteful to always allocate a `Vec` with one element. -#[derive(Clone, Eq, PartialEq, Hash, Default)] -pub struct ModuleRef(StaticVec<(String, Position)>, Option); - -impl fmt::Debug for ModuleRef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.0, f)?; - - if let Some(index) = self.1 { - write!(f, " -> {}", index) - } else { - Ok(()) - } - } -} - -impl Deref for ModuleRef { - type Target = StaticVec<(String, Position)>; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for ModuleRef { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl fmt::Display for ModuleRef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for (m, _) in self.0.iter() { - write!(f, "{}{}", m, Token::DoubleColon.syntax())?; - } - Ok(()) - } -} - -impl From> for ModuleRef { - fn from(modules: StaticVec<(String, Position)>) -> Self { - Self(modules, None) - } -} - -impl ModuleRef { - pub(crate) fn index(&self) -> Option { - self.1 - } - pub(crate) fn set_index(&mut self, index: Option) { - self.1 = index - } -} - /// Static module resolver. +#[cfg(not(feature = "no_module"))] mod stat { use super::*; diff --git a/src/optimize.rs b/src/optimize.rs index 9670f288..babb58c0 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -4,7 +4,8 @@ use crate::engine::{ Engine, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; use crate::fn_native::FnCallArgs; -use crate::packages::{PackageStore, PackagesCollection}; +use crate::module::Module; +use crate::packages::PackagesCollection; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; @@ -112,7 +113,7 @@ impl<'a> State<'a> { /// Call a registered function fn call_fn( packages: &PackagesCollection, - base_package: &PackageStore, + global_module: &Module, fn_name: &str, args: &mut FnCallArgs, pos: Position, @@ -120,8 +121,8 @@ fn call_fn( // Search built-in's and external functions let hash = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); - base_package - .get_function(hash) + global_module + .get_fn(hash) .or_else(|| packages.get_function(hash)) .map(|func| func.call(args, pos)) .transpose() @@ -556,7 +557,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { "" }; - call_fn(&state.engine.packages, &state.engine.base_package, name, &mut call_args, *pos).ok() + call_fn(&state.engine.packages, &state.engine.global_module, name, &mut call_args, *pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index c788fbb2..e4a26844 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -1,7 +1,5 @@ -use super::{reg_binary, reg_unary}; - use crate::def_package; -use crate::fn_register::{map_dynamic as map, map_result as result}; +use crate::module::FuncReturn; use crate::parser::INT; use crate::result::EvalAltResult; use crate::token::Position; @@ -22,7 +20,7 @@ use crate::stdlib::{ }; // Checked add -fn add(x: T, y: T) -> Result> { +fn add(x: T, y: T) -> FuncReturn { x.checked_add(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Addition overflow: {} + {}", x, y), @@ -31,7 +29,7 @@ fn add(x: T, y: T) -> Result> { }) } // Checked subtract -fn sub(x: T, y: T) -> Result> { +fn sub(x: T, y: T) -> FuncReturn { x.checked_sub(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Subtraction underflow: {} - {}", x, y), @@ -40,7 +38,7 @@ fn sub(x: T, y: T) -> Result> { }) } // Checked multiply -fn mul(x: T, y: T) -> Result> { +fn mul(x: T, y: T) -> FuncReturn { x.checked_mul(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Multiplication overflow: {} * {}", x, y), @@ -49,7 +47,7 @@ fn mul(x: T, y: T) -> Result> { }) } // Checked divide -fn div(x: T, y: T) -> Result> +fn div(x: T, y: T) -> FuncReturn where T: Display + CheckedDiv + PartialEq + Zero, { @@ -69,7 +67,7 @@ where }) } // Checked negative - e.g. -(i32::MIN) will overflow i32::MAX -fn neg(x: T) -> Result> { +fn neg(x: T) -> FuncReturn { x.checked_neg().ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Negation overflow: -{}", x), @@ -78,7 +76,7 @@ fn neg(x: T) -> Result> { }) } // Checked absolute -fn abs(x: T) -> Result> { +fn abs(x: T) -> FuncReturn { // FIX - We don't use Signed::abs() here because, contrary to documentation, it panics // when the number is ::MIN instead of returning ::MIN itself. if x >= ::zero() { @@ -93,49 +91,49 @@ fn abs(x: T) -> Result(x: T, y: T) -> ::Output { - x + y +fn add_u(x: T, y: T) -> FuncReturn<::Output> { + Ok(x + y) } // Unchecked subtract - may panic on underflow -fn sub_u(x: T, y: T) -> ::Output { - x - y +fn sub_u(x: T, y: T) -> FuncReturn<::Output> { + Ok(x - y) } // Unchecked multiply - may panic on overflow -fn mul_u(x: T, y: T) -> ::Output { - x * y +fn mul_u(x: T, y: T) -> FuncReturn<::Output> { + Ok(x * y) } // Unchecked divide - may panic when dividing by zero -fn div_u(x: T, y: T) -> ::Output { - x / y +fn div_u(x: T, y: T) -> FuncReturn<::Output> { + Ok(x / y) } // Unchecked negative - may panic on overflow -fn neg_u(x: T) -> ::Output { - -x +fn neg_u(x: T) -> FuncReturn<::Output> { + Ok(-x) } // Unchecked absolute - may panic on overflow -fn abs_u(x: T) -> ::Output +fn abs_u(x: T) -> FuncReturn<::Output> where T: Neg + PartialOrd + Default + Into<::Output>, { // Numbers should default to zero if x < Default::default() { - -x + Ok(-x) } else { - x.into() + Ok(x.into()) } } // Bit operators -fn binary_and(x: T, y: T) -> ::Output { - x & y +fn binary_and(x: T, y: T) -> FuncReturn<::Output> { + Ok(x & y) } -fn binary_or(x: T, y: T) -> ::Output { - x | y +fn binary_or(x: T, y: T) -> FuncReturn<::Output> { + Ok(x | y) } -fn binary_xor(x: T, y: T) -> ::Output { - x ^ y +fn binary_xor(x: T, y: T) -> FuncReturn<::Output> { + Ok(x ^ y) } // Checked left-shift -fn shl(x: T, y: INT) -> Result> { +fn shl(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -152,7 +150,7 @@ fn shl(x: T, y: INT) -> Result> { }) } // Checked right-shift -fn shr(x: T, y: INT) -> Result> { +fn shr(x: T, y: INT) -> FuncReturn { // Cannot shift by a negative number of bits if y < 0 { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -169,15 +167,15 @@ fn shr(x: T, y: INT) -> Result> { }) } // Unchecked left-shift - may panic if shifting by a negative number of bits -fn shl_u>(x: T, y: T) -> >::Output { - x.shl(y) +fn shl_u>(x: T, y: T) -> FuncReturn<>::Output> { + Ok(x.shl(y)) } // Unchecked right-shift - may panic if shifting by a negative number of bits -fn shr_u>(x: T, y: T) -> >::Output { - x.shr(y) +fn shr_u>(x: T, y: T) -> FuncReturn<>::Output> { + Ok(x.shr(y)) } // Checked modulo -fn modulo(x: T, y: T) -> Result> { +fn modulo(x: T, y: T) -> FuncReturn { x.checked_rem(&y).ok_or_else(|| { Box::new(EvalAltResult::ErrorArithmetic( format!("Modulo division by zero or overflow: {} % {}", x, y), @@ -186,11 +184,11 @@ fn modulo(x: T, y: T) -> Result> }) } // Unchecked modulo - may panic if dividing by zero -fn modulo_u(x: T, y: T) -> ::Output { - x % y +fn modulo_u(x: T, y: T) -> FuncReturn<::Output> { + Ok(x % y) } // Checked power -fn pow_i_i(x: INT, y: INT) -> Result> { +fn pow_i_i(x: INT, y: INT) -> FuncReturn { #[cfg(not(feature = "only_i32"))] { if y > (u32::MAX as INT) { @@ -231,17 +229,17 @@ fn pow_i_i(x: INT, y: INT) -> Result> { } } // Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX) -fn pow_i_i_u(x: INT, y: INT) -> INT { - x.pow(y as u32) +fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { + Ok(x.pow(y as u32)) } // Floating-point power - always well-defined #[cfg(not(feature = "no_float"))] -fn pow_f_f(x: FLOAT, y: FLOAT) -> FLOAT { - x.powf(y) +fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { + Ok(x.powf(y)) } // Checked power #[cfg(not(feature = "no_float"))] -fn pow_f_i(x: FLOAT, y: INT) -> Result> { +fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { return Err(Box::new(EvalAltResult::ErrorArithmetic( @@ -255,39 +253,37 @@ fn pow_f_i(x: FLOAT, y: INT) -> Result> { // Unchecked power - may be incorrect if the power index is too high (> i32::MAX) #[cfg(feature = "unchecked")] #[cfg(not(feature = "no_float"))] -fn pow_f_i_u(x: FLOAT, y: INT) -> FLOAT { - x.powi(y as i32) +fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { + Ok(x.powi(y as i32)) } -macro_rules! reg_unary_x { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_unary($lib, $op, $func::<$par>, result);)* }; +macro_rules! reg_unary { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_1($op, $func::<$par>); )* + }; } -macro_rules! reg_unary { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_unary($lib, $op, $func::<$par>, map);)* }; -} -macro_rules! reg_op_x { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_binary($lib, $op, $func::<$par>, result);)* }; -} -macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_binary($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_op { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_2($op, $func::<$par>); )* + }; } def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Checked basic arithmetic #[cfg(not(feature = "unchecked"))] { - reg_op_x!(lib, "+", add, INT); - reg_op_x!(lib, "-", sub, INT); - reg_op_x!(lib, "*", mul, INT); - reg_op_x!(lib, "/", div, INT); + reg_op!(lib, "+", add, INT); + reg_op!(lib, "-", sub, INT); + reg_op!(lib, "*", mul, INT); + reg_op!(lib, "/", div, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op_x!(lib, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op_x!(lib, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op_x!(lib, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op_x!(lib, "/", div, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "/", div, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); } } @@ -334,16 +330,16 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Checked bit shifts #[cfg(not(feature = "unchecked"))] { - reg_op_x!(lib, "<<", shl, INT); - reg_op_x!(lib, ">>", shr, INT); - reg_op_x!(lib, "%", modulo, INT); + reg_op!(lib, "<<", shl, INT); + reg_op!(lib, ">>", shr, INT); + reg_op!(lib, "%", modulo, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_op_x!(lib, "<<", shl, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op_x!(lib, ">>", shr, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); - reg_op_x!(lib, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); + reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, i64, u32, u64, i128, u128); } } @@ -366,39 +362,39 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { // Checked power #[cfg(not(feature = "unchecked"))] { - reg_binary(lib, "~", pow_i_i, result); + lib.set_fn_2("~", pow_i_i); #[cfg(not(feature = "no_float"))] - reg_binary(lib, "~", pow_f_i, result); + lib.set_fn_2("~", pow_f_i); } // Unchecked power #[cfg(feature = "unchecked")] { - reg_binary(lib, "~", pow_i_i_u, map); + lib.set_fn_2("~", pow_i_i_u); #[cfg(not(feature = "no_float"))] - reg_binary(lib, "~", pow_f_i_u, map); + lib.set_fn_2("~", pow_f_i_u); } // Floating-point modulo and power #[cfg(not(feature = "no_float"))] { reg_op!(lib, "%", modulo_u, f32, f64); - reg_binary(lib, "~", pow_f_f, map); + lib.set_fn_2("~", pow_f_f); } // Checked unary #[cfg(not(feature = "unchecked"))] { - reg_unary_x!(lib, "-", neg, INT); - reg_unary_x!(lib, "abs", abs, INT); + reg_unary!(lib, "-", neg, INT); + reg_unary!(lib, "abs", abs, INT); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_unary_x!(lib, "-", neg, i8, i16, i32, i64, i128); - reg_unary_x!(lib, "abs", abs, i8, i16, i32, i64, i128); + reg_unary!(lib, "-", neg, i8, i16, i32, i64, i128); + reg_unary!(lib, "abs", abs, i8, i16, i32, i64, i128); } } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index c7938e74..6ff90f39 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -1,20 +1,19 @@ #![cfg(not(feature = "no_index"))] -use super::{reg_binary, reg_binary_mut, reg_trinary_mut, reg_unary_mut}; - use crate::any::{Dynamic, Variant}; use crate::def_package; use crate::engine::Array; -use crate::fn_register::{map_dynamic as map, map_identity as pass}; +use crate::module::FuncReturn; use crate::parser::INT; use crate::stdlib::{any::TypeId, boxed::Box, string::String}; // Register array utility functions -fn push(list: &mut Array, item: T) { +fn push(list: &mut Array, item: T) -> FuncReturn<()> { list.push(Dynamic::from(item)); + Ok(()) } -fn ins(list: &mut Array, position: INT, item: T) { +fn ins(list: &mut Array, position: INT, item: T) -> FuncReturn<()> { if position <= 0 { list.insert(0, Dynamic::from(item)); } else if (position as usize) >= list.len() - 1 { @@ -22,20 +21,26 @@ fn ins(list: &mut Array, position: INT, item: T) { } else { list.insert(position as usize, Dynamic::from(item)); } + Ok(()) } -fn pad(list: &mut Array, len: INT, item: T) { +fn pad(list: &mut Array, len: INT, item: T) -> FuncReturn<()> { if len >= 0 { while list.len() < len as usize { push(list, item.clone()); } } + Ok(()) } -macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_binary_mut($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_op { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_2_mut($op, $func::<$par>); )* + }; } -macro_rules! reg_tri { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_trinary_mut($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_tri { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_3_mut($op, $func::<$par>); )* + }; } #[cfg(not(feature = "no_index"))] @@ -44,15 +49,16 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { reg_tri!(lib, "pad", pad, INT, bool, char, String, Array, ()); reg_tri!(lib, "insert", ins, INT, bool, char, String, Array, ()); - reg_binary_mut(lib, "append", |x: &mut Array, y: Array| x.extend(y), map); - reg_binary( - lib, + lib.set_fn_2_mut("append", |x: &mut Array, y: Array| { + x.extend(y); + Ok(()) + }); + lib.set_fn_2( "+", |mut x: Array, y: Array| { x.extend(y); - x + Ok(x) }, - map, ); #[cfg(not(feature = "only_i32"))] @@ -70,40 +76,36 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { reg_tri!(lib, "insert", ins, f32, f64); } - reg_unary_mut( - lib, + lib.set_fn_1_mut( "pop", - |list: &mut Array| list.pop().unwrap_or_else(|| ().into()), - pass, + |list: &mut Array| Ok(list.pop().unwrap_or_else(|| ().into())), ); - reg_unary_mut( - lib, + lib.set_fn_1_mut( "shift", |list: &mut Array| { - if list.is_empty() { + Ok(if list.is_empty() { ().into() } else { list.remove(0) - } + }) }, - pass, ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "remove", |list: &mut Array, len: INT| { - if len < 0 || (len as usize) >= list.len() { + Ok(if len < 0 || (len as usize) >= list.len() { ().into() } else { list.remove(len as usize) - } + }) }, - pass, ); - reg_unary_mut(lib, "len", |list: &mut Array| list.len() as INT, map); - reg_unary_mut(lib, "clear", |list: &mut Array| list.clear(), map); - reg_binary_mut( - lib, + lib.set_fn_1_mut("len", |list: &mut Array| Ok(list.len() as INT)); + lib.set_fn_1_mut("clear", |list: &mut Array| { + list.clear(); + Ok(()) + }); + lib.set_fn_2_mut( "truncate", |list: &mut Array, len: INT| { if len >= 0 { @@ -111,12 +113,12 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { } else { list.clear(); } + Ok(()) }, - map, ); // Register array iterator - lib.type_iterators.insert( + lib.set_iterator( TypeId::of::(), Box::new(|arr| Box::new( arr.cast::().into_iter()) as Box> diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index d50ad72a..daf57c44 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -1,8 +1,6 @@ -use super::{reg_binary, reg_trinary, PackageStore}; - use crate::any::{Dynamic, Variant}; use crate::def_package; -use crate::fn_register::map_dynamic as map; +use crate::module::{FuncReturn, Module}; use crate::parser::INT; use crate::stdlib::{ @@ -12,11 +10,11 @@ use crate::stdlib::{ }; // Register range function -fn reg_range(lib: &mut PackageStore) +fn reg_range(lib: &mut Module) where Range: Iterator, { - lib.type_iterators.insert( + lib.set_iterator( TypeId::of::>(), Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) @@ -25,6 +23,10 @@ where ); } +fn get_range(from: T, to: T) -> FuncReturn> { + Ok(from..to) +} + // Register range function with step #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)] struct StepRange(T, T, T) @@ -50,13 +52,13 @@ where } } -fn reg_step(lib: &mut PackageStore) +fn reg_step(lib: &mut Module) where for<'a> &'a T: Add<&'a T, Output = T>, T: Variant + Clone + PartialOrd, StepRange: Iterator, { - lib.type_iterators.insert( + lib.set_iterator( TypeId::of::>(), Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) @@ -65,22 +67,26 @@ where ); } -def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { - fn get_range(from: T, to: T) -> Range { - from..to - } +fn get_step_range(from: T, to: T, step: T) -> FuncReturn> +where + for<'a> &'a T: Add<&'a T, Output = T>, + T: Variant + Clone + PartialOrd, +{ + Ok(StepRange::(from, to, step)) +} +def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { reg_range::(lib); - reg_binary(lib, "range", get_range::, map); + lib.set_fn_2("range", get_range::); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { macro_rules! reg_range { - ($self:expr, $x:expr, $( $y:ty ),*) => ( + ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( - reg_range::<$y>($self); - reg_binary($self, $x, get_range::<$y>, map); + reg_range::<$y>($lib); + $lib.set_fn_2($x, get_range::<$y>); )* ) } @@ -89,16 +95,16 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { } reg_step::(lib); - reg_trinary(lib, "range", StepRange::, map); + lib.set_fn_3("range", get_step_range::); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { macro_rules! reg_step { - ($self:expr, $x:expr, $( $y:ty ),*) => ( + ($lib:expr, $x:expr, $( $y:ty ),*) => ( $( - reg_step::<$y>($self); - reg_trinary($self, $x, StepRange::<$y>, map); + reg_step::<$y>($lib); + $lib.set_fn_3($x, get_step_range::<$y>); )* ) } diff --git a/src/packages/logic.rs b/src/packages/logic.rs index f993044a..ef771688 100644 --- a/src/packages/logic.rs +++ b/src/packages/logic.rs @@ -1,44 +1,44 @@ -use super::{reg_binary, reg_binary_mut, reg_unary}; - use crate::def_package; -use crate::fn_register::map_dynamic as map; +use crate::module::FuncReturn; use crate::parser::INT; use crate::stdlib::string::String; // Comparison operators -pub fn lt(x: T, y: T) -> bool { - x < y +pub fn lt(x: T, y: T) -> FuncReturn { + Ok(x < y) } -pub fn lte(x: T, y: T) -> bool { - x <= y +pub fn lte(x: T, y: T) -> FuncReturn { + Ok(x <= y) } -pub fn gt(x: T, y: T) -> bool { - x > y +pub fn gt(x: T, y: T) -> FuncReturn { + Ok(x > y) } -pub fn gte(x: T, y: T) -> bool { - x >= y +pub fn gte(x: T, y: T) -> FuncReturn { + Ok(x >= y) } -pub fn eq(x: T, y: T) -> bool { - x == y +pub fn eq(x: T, y: T) -> FuncReturn { + Ok(x == y) } -pub fn ne(x: T, y: T) -> bool { - x != y +pub fn ne(x: T, y: T) -> FuncReturn { + Ok(x != y) } // Logic operators -fn and(x: bool, y: bool) -> bool { - x && y +fn and(x: bool, y: bool) -> FuncReturn { + Ok(x && y) } -fn or(x: bool, y: bool) -> bool { - x || y +fn or(x: bool, y: bool) -> FuncReturn { + Ok(x || y) } -fn not(x: bool) -> bool { - !x +fn not(x: bool) -> FuncReturn { + Ok(!x) } -macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_binary($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_op { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_2($op, $func::<$par>); )* + }; } def_package!(crate:LogicPackage:"Logical operators.", lib, { @@ -50,14 +50,12 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { reg_op!(lib, "!=", ne, INT, char, bool, ()); // Special versions for strings - at least avoid copying the first string - // use super::utils::reg_test; - // reg_test(lib, "<", |x: &mut String, y: String| *x < y, |v| v, map); - reg_binary_mut(lib, "<", |x: &mut String, y: String| *x < y, map); - reg_binary_mut(lib, "<=", |x: &mut String, y: String| *x <= y, map); - reg_binary_mut(lib, ">", |x: &mut String, y: String| *x > y, map); - reg_binary_mut(lib, ">=", |x: &mut String, y: String| *x >= y, map); - reg_binary_mut(lib, "==", |x: &mut String, y: String| *x == y, map); - reg_binary_mut(lib, "!=", |x: &mut String, y: String| *x != y, map); + lib.set_fn_2_mut("<", |x: &mut String, y: String| Ok(*x < y)); + lib.set_fn_2_mut("<=", |x: &mut String, y: String| Ok(*x <= y)); + lib.set_fn_2_mut(">", |x: &mut String, y: String| Ok(*x > y)); + lib.set_fn_2_mut(">=", |x: &mut String, y: String| Ok(*x >= y)); + lib.set_fn_2_mut("==", |x: &mut String, y: String| Ok(*x == y)); + lib.set_fn_2_mut("!=", |x: &mut String, y: String| Ok(*x != y)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] @@ -85,7 +83,7 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, { //reg_op!(lib, "||", or, bool); //reg_op!(lib, "&&", and, bool); - reg_binary(lib, "|", or, map); - reg_binary(lib, "&", and, map); - reg_unary(lib, "!", not, map); + lib.set_fn_2("|", or); + lib.set_fn_2("&", and); + lib.set_fn_1("!", not); }); diff --git a/src/packages/map_basic.rs b/src/packages/map_basic.rs index 451897bf..c9f4e894 100644 --- a/src/packages/map_basic.rs +++ b/src/packages/map_basic.rs @@ -1,11 +1,9 @@ #![cfg(not(feature = "no_object"))] -use super::{reg_binary, reg_binary_mut, reg_unary_mut}; - use crate::any::Dynamic; use crate::def_package; use crate::engine::Map; -use crate::fn_register::map_dynamic as map; +use crate::module::FuncReturn; use crate::parser::INT; use crate::stdlib::{ @@ -13,55 +11,51 @@ use crate::stdlib::{ vec::Vec, }; -fn map_get_keys(map: &mut Map) -> Vec { - map.iter().map(|(k, _)| k.to_string().into()).collect() +fn map_get_keys(map: &mut Map) -> FuncReturn> { + Ok(map.iter().map(|(k, _)| k.to_string().into()).collect()) } -fn map_get_values(map: &mut Map) -> Vec { - map.iter().map(|(_, v)| v.clone()).collect() +fn map_get_values(map: &mut Map) -> FuncReturn> { + Ok(map.iter().map(|(_, v)| v.clone()).collect()) } #[cfg(not(feature = "no_object"))] def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, { - reg_binary_mut( - lib, + lib.set_fn_2_mut( "has", - |map: &mut Map, prop: String| map.contains_key(&prop), - map, + |map: &mut Map, prop: String| Ok(map.contains_key(&prop)), ); - reg_unary_mut(lib, "len", |map: &mut Map| map.len() as INT, map); - reg_unary_mut(lib, "clear", |map: &mut Map| map.clear(), map); - reg_binary_mut( - lib, + lib.set_fn_1_mut("len", |map: &mut Map| Ok(map.len() as INT)); + lib.set_fn_1_mut("clear", |map: &mut Map| { + map.clear(); + Ok(()) + }); + lib.set_fn_2_mut( "remove", - |x: &mut Map, name: String| x.remove(&name).unwrap_or_else(|| ().into()), - map, + |x: &mut Map, name: String| Ok(x.remove(&name).unwrap_or_else(|| ().into())), ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "mixin", |map1: &mut Map, map2: Map| { map2.into_iter().for_each(|(key, value)| { map1.insert(key, value); }); + Ok(()) }, - map, ); - reg_binary( - lib, + lib.set_fn_2( "+", |mut map1: Map, map2: Map| { map2.into_iter().for_each(|(key, value)| { map1.insert(key, value); }); - map1 + Ok(map1) }, - map, ); // Register map access functions #[cfg(not(feature = "no_index"))] - reg_unary_mut(lib, "keys", map_get_keys, map); + lib.set_fn_1_mut("keys", map_get_keys); #[cfg(not(feature = "no_index"))] - reg_unary_mut(lib, "values", map_get_values, map); + lib.set_fn_1_mut("values", map_get_values); }); diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index a39f477c..0698befb 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -1,7 +1,5 @@ -use super::{reg_binary, reg_unary}; - use crate::def_package; -use crate::fn_register::{map_dynamic as map, map_result as result}; +use crate::module::FuncReturn; use crate::parser::INT; use crate::result::EvalAltResult; use crate::token::Position; @@ -20,78 +18,77 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { #[cfg(not(feature = "no_float"))] { // Advanced math functions - reg_unary(lib, "sin", |x: FLOAT| x.to_radians().sin(), map); - reg_unary(lib, "cos", |x: FLOAT| x.to_radians().cos(), map); - reg_unary(lib, "tan", |x: FLOAT| x.to_radians().tan(), map); - reg_unary(lib, "sinh", |x: FLOAT| x.to_radians().sinh(), map); - reg_unary(lib, "cosh", |x: FLOAT| x.to_radians().cosh(), map); - reg_unary(lib, "tanh", |x: FLOAT| x.to_radians().tanh(), map); - reg_unary(lib, "asin", |x: FLOAT| x.asin().to_degrees(), map); - reg_unary(lib, "acos", |x: FLOAT| x.acos().to_degrees(), map); - reg_unary(lib, "atan", |x: FLOAT| x.atan().to_degrees(), map); - reg_unary(lib, "asinh", |x: FLOAT| x.asinh().to_degrees(), map); - reg_unary(lib, "acosh", |x: FLOAT| x.acosh().to_degrees(), map); - reg_unary(lib, "atanh", |x: FLOAT| x.atanh().to_degrees(), map); - reg_unary(lib, "sqrt", |x: FLOAT| x.sqrt(), map); - reg_unary(lib, "exp", |x: FLOAT| x.exp(), map); - reg_unary(lib, "ln", |x: FLOAT| x.ln(), map); - reg_binary(lib, "log", |x: FLOAT, base: FLOAT| x.log(base), map); - reg_unary(lib, "log10", |x: FLOAT| x.log10(), map); - reg_unary(lib, "floor", |x: FLOAT| x.floor(), map); - reg_unary(lib, "ceiling", |x: FLOAT| x.ceil(), map); - reg_unary(lib, "round", |x: FLOAT| x.ceil(), map); - reg_unary(lib, "int", |x: FLOAT| x.trunc(), map); - reg_unary(lib, "fraction", |x: FLOAT| x.fract(), map); - reg_unary(lib, "is_nan", |x: FLOAT| x.is_nan(), map); - reg_unary(lib, "is_finite", |x: FLOAT| x.is_finite(), map); - reg_unary(lib, "is_infinite", |x: FLOAT| x.is_infinite(), map); + lib.set_fn_1("sin", |x: FLOAT| Ok(x.to_radians().sin())); + lib.set_fn_1("cos", |x: FLOAT| Ok(x.to_radians().cos())); + lib.set_fn_1("tan", |x: FLOAT| Ok(x.to_radians().tan())); + lib.set_fn_1("sinh", |x: FLOAT| Ok(x.to_radians().sinh())); + lib.set_fn_1("cosh", |x: FLOAT| Ok(x.to_radians().cosh())); + lib.set_fn_1("tanh", |x: FLOAT| Ok(x.to_radians().tanh())); + lib.set_fn_1("asin", |x: FLOAT| Ok(x.asin().to_degrees())); + lib.set_fn_1("acos", |x: FLOAT| Ok(x.acos().to_degrees())); + lib.set_fn_1("atan", |x: FLOAT| Ok(x.atan().to_degrees())); + lib.set_fn_1("asinh", |x: FLOAT| Ok(x.asinh().to_degrees())); + lib.set_fn_1("acosh", |x: FLOAT| Ok(x.acosh().to_degrees())); + lib.set_fn_1("atanh", |x: FLOAT| Ok(x.atanh().to_degrees())); + lib.set_fn_1("sqrt", |x: FLOAT| Ok(x.sqrt())); + lib.set_fn_1("exp", |x: FLOAT| Ok(x.exp())); + lib.set_fn_1("ln", |x: FLOAT| Ok(x.ln())); + lib.set_fn_2("log", |x: FLOAT, base: FLOAT| Ok(x.log(base))); + lib.set_fn_1("log10", |x: FLOAT| Ok(x.log10())); + lib.set_fn_1("floor", |x: FLOAT| Ok(x.floor())); + lib.set_fn_1("ceiling", |x: FLOAT| Ok(x.ceil())); + lib.set_fn_1("round", |x: FLOAT| Ok(x.ceil())); + lib.set_fn_1("int", |x: FLOAT| Ok(x.trunc())); + lib.set_fn_1("fraction", |x: FLOAT| Ok(x.fract())); + lib.set_fn_1("is_nan", |x: FLOAT| Ok(x.is_nan())); + lib.set_fn_1("is_finite", |x: FLOAT| Ok(x.is_finite())); + lib.set_fn_1("is_infinite", |x: FLOAT| Ok(x.is_infinite())); // Register conversion functions - reg_unary(lib, "to_float", |x: INT| x as FLOAT, map); - reg_unary(lib, "to_float", |x: f32| x as FLOAT, map); + lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_unary(lib, "to_float", |x: i8| x as FLOAT, map); - reg_unary(lib, "to_float", |x: u8| x as FLOAT, map); - reg_unary(lib, "to_float", |x: i16| x as FLOAT, map); - reg_unary(lib, "to_float", |x: u16| x as FLOAT, map); - reg_unary(lib, "to_float", |x: i32| x as FLOAT, map); - reg_unary(lib, "to_float", |x: u32| x as FLOAT, map); - reg_unary(lib, "to_float", |x: i64| x as FLOAT, map); - reg_unary(lib, "to_float", |x: u64| x as FLOAT, map); - reg_unary(lib, "to_float", |x: i128| x as FLOAT, map); - reg_unary(lib, "to_float", |x: u128| x as FLOAT, map); + lib.set_fn_1("to_float", |x: i8| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: u8| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: i16| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: u16| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: i32| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: u32| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: i64| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: u64| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: i128| Ok(x as FLOAT)); + lib.set_fn_1("to_float", |x: u128| Ok(x as FLOAT)); } } - reg_unary(lib, "to_int", |ch: char| ch as INT, map); + lib.set_fn_1("to_int", |ch: char| Ok(ch as INT)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] { - reg_unary(lib, "to_int", |x: i8| x as INT, map); - reg_unary(lib, "to_int", |x: u8| x as INT, map); - reg_unary(lib, "to_int", |x: i16| x as INT, map); - reg_unary(lib, "to_int", |x: u16| x as INT, map); + lib.set_fn_1("to_int", |x: i8| Ok(x as INT)); + lib.set_fn_1("to_int", |x: u8| Ok(x as INT)); + lib.set_fn_1("to_int", |x: i16| Ok(x as INT)); + lib.set_fn_1("to_int", |x: u16| Ok(x as INT)); } #[cfg(not(feature = "only_i32"))] { - reg_unary(lib, "to_int", |x: i32| x as INT, map); - reg_unary(lib, "to_int", |x: u64| x as INT, map); + lib.set_fn_1("to_int", |x: i32| Ok(x as INT)); + lib.set_fn_1("to_int", |x: u64| Ok(x as INT)); #[cfg(feature = "only_i64")] - reg_unary(lib, "to_int", |x: u32| x as INT, map); + lib.set_fn_1("to_int", |x: u32| Ok(x as INT)); } #[cfg(not(feature = "no_float"))] { #[cfg(not(feature = "unchecked"))] { - reg_unary( - lib, + lib.set_fn_1( "to_int", |x: f32| { if x > (MAX_INT as f32) { @@ -103,10 +100,8 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { Ok(x.trunc() as INT) }, - result, ); - reg_unary( - lib, + lib.set_fn_1( "to_int", |x: FLOAT| { if x > (MAX_INT as FLOAT) { @@ -118,14 +113,13 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { Ok(x.trunc() as INT) }, - result, ); } #[cfg(feature = "unchecked")] { - reg_unary(lib, "to_int", |x: f32| x as INT, map); - reg_unary(lib, "to_int", |x: f64| x as INT, map); + lib.set_fn_1("to_int", |x: f32| Ok(x as INT)); + lib.set_fn_1("to_int", |x: f64| Ok(x as INT)); } } }); diff --git a/src/packages/mod.rs b/src/packages/mod.rs index f9b50d90..a8599417 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,7 @@ //! This module contains all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{IteratorFn, NativeCallable}; +use crate::fn_native::{NativeCallable, SharedIteratorFunction}; +use crate::module::Module; use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; @@ -15,7 +16,6 @@ mod pkg_std; mod string_basic; mod string_more; mod time_basic; -mod utils; pub use arithmetic::ArithmeticPackage; #[cfg(not(feature = "no_index"))] @@ -32,67 +32,22 @@ pub use string_more::MoreStringPackage; #[cfg(not(feature = "no_std"))] pub use time_basic::BasicTimePackage; -pub use utils::*; - -const NUM_NATIVE_FUNCTIONS: usize = 512; - /// Trait that all packages must implement. pub trait Package { /// Register all the functions in a package into a store. - fn init(lib: &mut PackageStore); + fn init(lib: &mut Module); /// Retrieve the generic package library from this package. fn get(&self) -> PackageLibrary; } -/// Type to store all functions in the package. -pub struct PackageStore { - /// All functions, keyed by a hash created from the function name and parameter types. - pub functions: HashMap>, - - /// All iterator functions, keyed by the type producing the iterator. - pub type_iterators: HashMap>, -} - -impl PackageStore { - /// Create a new `PackageStore`. - pub fn new() -> Self { - Default::default() - } - /// Does the specified function hash key exist in the `PackageStore`? - pub fn contains_function(&self, hash: u64) -> bool { - self.functions.contains_key(&hash) - } - /// Get specified function via its hash key. - pub fn get_function(&self, hash: u64) -> Option<&Box> { - self.functions.get(&hash) - } - /// Does the specified TypeId iterator exist in the `PackageStore`? - pub fn contains_iterator(&self, id: TypeId) -> bool { - self.type_iterators.contains_key(&id) - } - /// Get the specified TypeId iterator. - pub fn get_iterator(&self, id: TypeId) -> Option<&Box> { - self.type_iterators.get(&id) - } -} - -impl Default for PackageStore { - fn default() -> Self { - Self { - functions: HashMap::with_capacity(NUM_NATIVE_FUNCTIONS), - type_iterators: HashMap::with_capacity(4), - } - } -} - -/// Type which `Rc`-wraps a `PackageStore` to facilitate sharing library instances. +/// Type which `Rc`-wraps a `Module` to facilitate sharing library instances. #[cfg(not(feature = "sync"))] -pub type PackageLibrary = Rc; +pub type PackageLibrary = Rc; -/// Type which `Arc`-wraps a `PackageStore` to facilitate sharing library instances. +/// Type which `Arc`-wraps a `Module` to facilitate sharing library instances. #[cfg(feature = "sync")] -pub type PackageLibrary = Arc; +pub type PackageLibrary = Arc; /// Type containing a collection of `PackageLibrary` instances. /// All function and type iterator keys in the loaded packages are indexed for fast access. @@ -110,13 +65,13 @@ impl PackagesCollection { } /// Does the specified function hash key exist in the `PackagesCollection`? pub fn contains_function(&self, hash: u64) -> bool { - self.packages.iter().any(|p| p.contains_function(hash)) + self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. pub fn get_function(&self, hash: u64) -> Option<&Box> { self.packages .iter() - .map(|p| p.get_function(hash)) + .map(|p| p.get_fn(hash)) .find(|f| f.is_some()) .flatten() } @@ -125,7 +80,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iterator(id)) } /// Get the specified TypeId iterator. - pub fn get_iterator(&self, id: TypeId) -> Option<&Box> { + pub fn get_iterator(&self, id: TypeId) -> Option<&SharedIteratorFunction> { self.packages .iter() .map(|p| p.get_iterator(id)) @@ -133,3 +88,51 @@ impl PackagesCollection { .flatten() } } + +/// This macro makes it easy to define a _package_ (which is basically a shared module) +/// and register functions into it. +/// +/// Functions can be added to the package using the standard module methods such as +/// `set_fn_2`, `set_fn_3_mut`, `set_fn_0` etc. +/// +/// # Examples +/// +/// ``` +/// use rhai::{Dynamic, EvalAltResult}; +/// use rhai::def_package; +/// +/// fn add(x: i64, y: i64) -> Result> { Ok(x + y) } +/// +/// def_package!(rhai:MyPackage:"My super-duper package", lib, +/// { +/// // Load a binary function with all value parameters. +/// lib.set_fn_2("my_add", add); +/// }); +/// ``` +/// +/// The above defines a package named 'MyPackage' with a single function named 'my_add'. +#[macro_export] +macro_rules! def_package { + ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => { + #[doc=$comment] + pub struct $package($root::packages::PackageLibrary); + + impl $root::packages::Package for $package { + fn get(&self) -> $root::packages::PackageLibrary { + self.0.clone() + } + + fn init($lib: &mut $root::Module) { + $block + } + } + + impl $package { + pub fn new() -> Self { + let mut module = $root::Module::new(); + ::init(&mut module); + Self(module.into()) + } + } + }; +} diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index e58890d1..3b1689cf 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -1,8 +1,6 @@ -use super::{reg_binary, reg_binary_mut, reg_none, reg_unary, reg_unary_mut}; - use crate::def_package; use crate::engine::{FUNC_TO_STRING, KEYWORD_DEBUG, KEYWORD_PRINT}; -use crate::fn_register::map_dynamic as map; +use crate::module::FuncReturn; use crate::parser::INT; #[cfg(not(feature = "no_index"))] @@ -18,31 +16,33 @@ use crate::stdlib::{ }; // Register print and debug -fn to_debug(x: &mut T) -> String { - format!("{:?}", x) +fn to_debug(x: &mut T) -> FuncReturn { + Ok(format!("{:?}", x)) } -fn to_string(x: &mut T) -> String { - format!("{}", x) +fn to_string(x: &mut T) -> FuncReturn { + Ok(format!("{}", x)) } #[cfg(not(feature = "no_object"))] -fn format_map(x: &mut Map) -> String { - format!("#{:?}", x) +fn format_map(x: &mut Map) -> FuncReturn { + Ok(format!("#{:?}", x)) } -macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_unary_mut($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_op { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_1_mut($op, $func::<$par>); )* + }; } def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { reg_op!(lib, KEYWORD_PRINT, to_string, INT, bool, char); reg_op!(lib, FUNC_TO_STRING, to_string, INT, bool, char); - reg_none(lib, KEYWORD_PRINT, || "".to_string(), map); - reg_unary(lib, KEYWORD_PRINT, |_: ()| "".to_string(), map); - reg_unary(lib, FUNC_TO_STRING, |_: ()| "".to_string(), map); + lib.set_fn_0(KEYWORD_PRINT, || Ok("".to_string())); + lib.set_fn_1(KEYWORD_PRINT, |_: ()| Ok("".to_string())); + lib.set_fn_1(FUNC_TO_STRING, |_: ()| Ok("".to_string())); - reg_unary_mut(lib, KEYWORD_PRINT, |s: &mut String| s.clone(), map); - reg_unary_mut(lib, FUNC_TO_STRING, |s: &mut String| s.clone(), map); + lib.set_fn_1_mut(KEYWORD_PRINT, |s: &mut String| Ok(s.clone())); + lib.set_fn_1_mut(FUNC_TO_STRING, |s: &mut String| Ok(s.clone())); reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, String); @@ -73,34 +73,34 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin #[cfg(not(feature = "no_object"))] { - reg_unary_mut(lib, KEYWORD_PRINT, format_map, map); - reg_unary_mut(lib, FUNC_TO_STRING, format_map, map); - reg_unary_mut(lib, KEYWORD_DEBUG, format_map, map); + lib.set_fn_1_mut(KEYWORD_PRINT, format_map); + lib.set_fn_1_mut(FUNC_TO_STRING, format_map); + lib.set_fn_1_mut(KEYWORD_DEBUG, format_map); } - reg_binary( - lib, + lib.set_fn_2( "+", |mut s: String, ch: char| { s.push(ch); - s + Ok(s) }, - map, ); - reg_binary( - lib, + lib.set_fn_2( "+", |mut s: String, s2: String| { s.push_str(&s2); - s + Ok(s) }, - map, ); - reg_binary_mut(lib, "append", |s: &mut String, ch: char| s.push(ch), map); - reg_binary_mut( - lib, + lib.set_fn_2_mut("append", |s: &mut String, ch: char| { + s.push(ch); + Ok(()) + }); + lib.set_fn_2_mut( "append", - |s: &mut String, s2: String| s.push_str(&s2), - map, + |s: &mut String, s2: String| { + s.push_str(&s2); + Ok(()) + } ); }); diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 697a6eb0..eb6208f1 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,7 +1,5 @@ -use super::{reg_binary, reg_binary_mut, reg_trinary_mut, reg_unary_mut}; - use crate::def_package; -use crate::fn_register::map_dynamic as map; +use crate::module::FuncReturn; use crate::parser::INT; #[cfg(not(feature = "no_index"))] @@ -14,19 +12,19 @@ use crate::stdlib::{ vec::Vec, }; -fn prepend(x: T, y: String) -> String { - format!("{}{}", x, y) +fn prepend(x: T, y: String) -> FuncReturn { + Ok(format!("{}{}", x, y)) } -fn append(x: String, y: T) -> String { - format!("{}{}", x, y) +fn append(x: String, y: T) -> FuncReturn { + Ok(format!("{}{}", x, y)) } -fn sub_string(s: &mut String, start: INT, len: INT) -> String { +fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn { let offset = if s.is_empty() || len <= 0 { - return "".to_string(); + return Ok("".to_string()); } else if start < 0 { 0 } else if (start as usize) >= s.chars().count() { - return "".to_string(); + return Ok("".to_string()); } else { start as usize }; @@ -39,17 +37,17 @@ fn sub_string(s: &mut String, start: INT, len: INT) -> String { len as usize }; - chars[offset..][..len].into_iter().collect() + Ok(chars[offset..][..len].into_iter().collect()) } -fn crop_string(s: &mut String, start: INT, len: INT) { +fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> { let offset = if s.is_empty() || len <= 0 { s.clear(); - return; + return Ok(()); } else if start < 0 { 0 } else if (start as usize) >= s.chars().count() { s.clear(); - return; + return Ok(()); } else { start as usize }; @@ -67,18 +65,22 @@ fn crop_string(s: &mut String, start: INT, len: INT) { chars[offset..][..len] .into_iter() .for_each(|&ch| s.push(ch)); + + Ok(()) } -macro_rules! reg_op { ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { - $(reg_binary($lib, $op, $func::<$par>, map);)* }; +macro_rules! reg_op { + ($lib:expr, $op:expr, $func:ident, $($par:ty),*) => { + $( $lib.set_fn_2($op, $func::<$par>); )* + }; } def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, { reg_op!(lib, "+", append, INT, bool, char); - reg_binary_mut(lib, "+", |x: &mut String, _: ()| x.clone(), map); + lib.set_fn_2_mut( "+", |x: &mut String, _: ()| Ok(x.clone())); reg_op!(lib, "+", prepend, INT, bool, char); - reg_binary(lib, "+", |_: (), y: String| y, map); + lib.set_fn_2("+", |_: (), y: String| Ok(y)); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] @@ -95,105 +97,95 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str #[cfg(not(feature = "no_index"))] { - reg_binary(lib, "+", |x: String, y: Array| format!("{}{:?}", x, y), map); - reg_binary(lib, "+", |x: Array, y: String| format!("{:?}{}", x, y), map); + lib.set_fn_2("+", |x: String, y: Array| Ok(format!("{}{:?}", x, y))); + lib.set_fn_2("+", |x: Array, y: String| Ok(format!("{:?}{}", x, y))); } - reg_unary_mut(lib, "len", |s: &mut String| s.chars().count() as INT, map); - reg_binary_mut( - lib, + lib.set_fn_1_mut("len", |s: &mut String| Ok(s.chars().count() as INT)); + lib.set_fn_2_mut( "contains", - |s: &mut String, ch: char| s.contains(ch), - map, + |s: &mut String, ch: char| Ok(s.contains(ch)), ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "contains", - |s: &mut String, find: String| s.contains(&find), - map, + |s: &mut String, find: String| Ok(s.contains(&find)), ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "index_of", |s: &mut String, ch: char, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { - return -1 as INT; + return Ok(-1 as INT); } else { s.chars().take(start as usize).collect::().len() }; - s[start..] + Ok(s[start..] .find(ch) .map(|index| s[0..start + index].chars().count() as INT) - .unwrap_or(-1 as INT) + .unwrap_or(-1 as INT)) }, - map, ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "index_of", |s: &mut String, ch: char| { - s.find(ch) + Ok(s.find(ch) .map(|index| s[0..index].chars().count() as INT) - .unwrap_or(-1 as INT) + .unwrap_or(-1 as INT)) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "index_of", |s: &mut String, find: String, start: INT| { let start = if start < 0 { 0 } else if (start as usize) >= s.chars().count() { - return -1 as INT; + return Ok(-1 as INT); } else { s.chars().take(start as usize).collect::().len() }; - s[start..] + Ok(s[start..] .find(&find) .map(|index| s[0..start + index].chars().count() as INT) - .unwrap_or(-1 as INT) + .unwrap_or(-1 as INT)) }, - map, ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "index_of", |s: &mut String, find: String| { - s.find(&find) + Ok(s.find(&find) .map(|index| s[0..index].chars().count() as INT) - .unwrap_or(-1 as INT) + .unwrap_or(-1 as INT)) }, - map, ); - reg_unary_mut(lib, "clear", |s: &mut String| s.clear(), map); - reg_binary_mut(lib, "append", |s: &mut String, ch: char| s.push(ch), map); - reg_binary_mut( - lib, + lib.set_fn_1_mut("clear", |s: &mut String| { + s.clear(); + Ok(()) + }); + lib.set_fn_2_mut( "append", |s: &mut String, ch: char| { + s.push(ch); + Ok(()) + }); + lib.set_fn_2_mut( "append", - |s: &mut String, add: String| s.push_str(&add), - map, + |s: &mut String, add: String| { + s.push_str(&add); + Ok(()) + } ); - reg_trinary_mut(lib, "sub_string", sub_string, map); - reg_binary_mut( - lib, + lib.set_fn_3_mut( "sub_string", sub_string); + lib.set_fn_2_mut( "sub_string", |s: &mut String, start: INT| sub_string(s, start, s.len() as INT), - map, ); - reg_trinary_mut(lib, "crop", crop_string, map); - reg_binary_mut( - lib, + lib.set_fn_3_mut( "crop", crop_string); + lib.set_fn_2_mut( "crop", |s: &mut String, start: INT| crop_string(s, start, s.len() as INT), - map, ); - reg_binary_mut( - lib, + lib.set_fn_2_mut( "truncate", |s: &mut String, len: INT| { if len >= 0 { @@ -203,61 +195,55 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str } else { s.clear(); } + Ok(()) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "pad", |s: &mut String, len: INT, ch: char| { for _ in 0..s.chars().count() - len as usize { s.push(ch); } + Ok(()) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "replace", |s: &mut String, find: String, sub: String| { let new_str = s.replace(&find, &sub); s.clear(); s.push_str(&new_str); + Ok(()) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "replace", |s: &mut String, find: String, sub: char| { let new_str = s.replace(&find, &sub.to_string()); s.clear(); s.push_str(&new_str); + Ok(()) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "replace", |s: &mut String, find: char, sub: String| { let new_str = s.replace(&find.to_string(), &sub); s.clear(); s.push_str(&new_str); + Ok(()) }, - map, ); - reg_trinary_mut( - lib, + lib.set_fn_3_mut( "replace", |s: &mut String, find: char, sub: char| { let new_str = s.replace(&find.to_string(), &sub.to_string()); s.clear(); s.push_str(&new_str); + Ok(()) }, - map, ); - reg_unary_mut( - lib, + lib.set_fn_1_mut( "trim", |s: &mut String| { let trimmed = s.trim(); @@ -265,7 +251,7 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str if trimmed.len() < s.len() { *s = trimmed.to_string(); } + Ok(()) }, - map, ); }); diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index 173c3e22..7d80344c 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -1,9 +1,8 @@ use super::logic::{eq, gt, gte, lt, lte, ne}; use super::math_basic::MAX_INT; -use super::{reg_binary, reg_none, reg_unary}; use crate::def_package; -use crate::fn_register::{map_dynamic as map, map_result as result}; +use crate::module::FuncReturn; use crate::parser::INT; use crate::result::EvalAltResult; use crate::token::Position; @@ -14,10 +13,9 @@ use crate::stdlib::time::Instant; #[cfg(not(feature = "no_std"))] def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { // Register date/time functions - reg_none(lib, "timestamp", || Instant::now(), map); + lib.set_fn_0("timestamp", || Ok(Instant::now())); - reg_binary( - lib, + lib.set_fn_2( "-", |ts1: Instant, ts2: Instant| { if ts2 > ts1 { @@ -63,18 +61,16 @@ def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { } } }, - result, ); - reg_binary(lib, "<", lt::, map); - reg_binary(lib, "<=", lte::, map); - reg_binary(lib, ">", gt::, map); - reg_binary(lib, ">=", gte::, map); - reg_binary(lib, "==", eq::, map); - reg_binary(lib, "!=", ne::, map); + lib.set_fn_2("<", lt::); + lib.set_fn_2("<=", lte::); + lib.set_fn_2(">", gt::); + lib.set_fn_2(">=", gte::); + lib.set_fn_2("==", eq::); + lib.set_fn_2("!=", ne::); - reg_unary( - lib, + lib.set_fn_1( "elapsed", |timestamp: Instant| { #[cfg(not(feature = "no_float"))] @@ -96,6 +92,5 @@ def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, { return Ok(seconds as INT); } }, - result, ); }); diff --git a/src/packages/utils.rs b/src/packages/utils.rs deleted file mode 100644 index 233218db..00000000 --- a/src/packages/utils.rs +++ /dev/null @@ -1,425 +0,0 @@ -use super::PackageStore; - -use crate::any::{Dynamic, Variant}; -use crate::calc_fn_hash; -use crate::fn_native::{FnCallArgs, NativeFunction, NativeFunctionABI::*}; -use crate::result::EvalAltResult; -use crate::token::Position; - -use crate::stdlib::{ - any::TypeId, - boxed::Box, - iter::empty, - mem, - string::{String, ToString}, -}; - -/// This macro makes it easy to define a _package_ and register functions into it. -/// -/// Functions can be added to the package using a number of helper functions under the `packages` module, -/// such as `reg_unary`, `reg_binary_mut`, `reg_trinary_mut` etc. -/// -/// # Examples -/// -/// ``` -/// use rhai::Dynamic; -/// use rhai::def_package; -/// use rhai::packages::reg_binary; -/// -/// fn add(x: i64, y: i64) -> i64 { x + y } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_binary(lib, "my_add", add, |v, _| Ok(v.into())); -/// // ^^^^^^^^^^^^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single function named 'my_add'. -#[macro_export] -macro_rules! def_package { - ($root:ident : $package:ident : $comment:expr , $lib:ident , $block:stmt) => { - #[doc=$comment] - pub struct $package($root::packages::PackageLibrary); - - impl $root::packages::Package for $package { - fn get(&self) -> $root::packages::PackageLibrary { - self.0.clone() - } - - fn init($lib: &mut $root::packages::PackageStore) { - $block - } - } - - impl $package { - pub fn new() -> Self { - let mut pkg = $root::packages::PackageStore::new(); - ::init(&mut pkg); - Self(pkg.into()) - } - } - }; -} - -/// Add a function with no parameters to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -/// -/// # Examples -/// -/// ``` -/// use rhai::Dynamic; -/// use rhai::def_package; -/// use rhai::packages::reg_none; -/// -/// fn get_answer() -> i64 { 42 } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_none(lib, "my_answer", get_answer, |v, _| Ok(v.into())); -/// // ^^^^^^^^^^^^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single function named 'my_add_1'. -pub fn reg_none( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn() -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn() -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - let hash_fn_native = calc_fn_hash(empty(), fn_name, ([] as [TypeId; 0]).iter().cloned()); - - let f = Box::new(move |_: &mut FnCallArgs, pos| { - let r = func(); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); -} - -/// Add a function with one parameter to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -/// -/// # Examples -/// -/// ``` -/// use rhai::Dynamic; -/// use rhai::def_package; -/// use rhai::packages::reg_unary; -/// -/// fn add_1(x: i64) -> i64 { x + 1 } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_unary(lib, "my_add_1", add_1, |v, _| Ok(v.into())); -/// // ^^^^^^^^^^^^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single function named 'my_add_1'. -pub fn reg_unary( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(T) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(T) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}({})", fn_name, crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x = mem::take(*drain.next().unwrap()).cast::(); - - let r = func(x); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); -} - -/// Add a function with one mutable reference parameter to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -/// -/// # Examples -/// -/// ``` -/// use rhai::{Dynamic, EvalAltResult}; -/// use rhai::def_package; -/// use rhai::packages::reg_unary_mut; -/// -/// fn inc(x: &mut i64) -> Result> { -/// if *x == 0 { -/// return Err("boo! zero cannot be incremented!".into()) -/// } -/// *x += 1; -/// Ok(().into()) -/// } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_unary_mut(lib, "try_inc", inc, |r, _| r); -/// // ^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single fallible function named 'try_inc' -/// which takes a first argument of `&mut`, return a `Result>`. -pub fn reg_unary_mut( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(&mut T) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut T) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}(&mut {})", fn_name, crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash(empty(), fn_name, [TypeId::of::()].iter().cloned()); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x: &mut T = drain.next().unwrap().downcast_mut().unwrap(); - - let r = func(x); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Method))); -} - -/// Add a function with two parameters to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -/// -/// # Examples -/// -/// ``` -/// use rhai::Dynamic; -/// use rhai::def_package; -/// use rhai::packages::reg_binary; -/// -/// fn add(x: i64, y: i64) -> i64 { x + y } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_binary(lib, "my_add", add, |v, _| Ok(v.into())); -/// // ^^^^^^^^^^^^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single function named 'my_add'. -pub fn reg_binary( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(A, B) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}({}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash( - empty(), - fn_name, - [TypeId::of::(), TypeId::of::()].iter().cloned(), - ); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x = mem::take(*drain.next().unwrap()).cast::(); - let y = mem::take(*drain.next().unwrap()).cast::(); - - let r = func(x, y); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); -} - -/// Add a function with two parameters (the first one being a mutable reference) to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -/// -/// # Examples -/// -/// ``` -/// use rhai::{Dynamic, EvalAltResult}; -/// use rhai::def_package; -/// use rhai::packages::reg_binary_mut; -/// -/// fn add(x: &mut i64, y: i64) -> Result> { -/// if y == 0 { -/// return Err("boo! cannot add zero!".into()) -/// } -/// *x += y; -/// Ok(().into()) -/// } -/// -/// def_package!(rhai:MyPackage:"My super-duper package", lib, -/// { -/// reg_binary_mut(lib, "try_add", add, |r, _| r); -/// // ^^^^^^^^ -/// // map into Result> -/// }); -/// ``` -/// -/// The above defines a package named 'MyPackage' with a single fallible function named 'try_add' -/// which takes a first argument of `&mut`, return a `Result>`. -pub fn reg_binary_mut( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}(&mut {}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash( - empty(), - fn_name, - [TypeId::of::(), TypeId::of::()].iter().cloned(), - ); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x: &mut A = drain.next().unwrap().downcast_mut().unwrap(); - let y = mem::take(*drain.next().unwrap()).cast::(); - - let r = func(x, y); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Method))); -} - -/// Add a function with three parameters to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -pub fn reg_trinary( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(A, B, C) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}({}, {}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::(), crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash( - empty(), - fn_name, - [TypeId::of::(), TypeId::of::(), TypeId::of::()] - .iter() - .cloned(), - ); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x = mem::take(*drain.next().unwrap()).cast::(); - let y = mem::take(*drain.next().unwrap()).cast::(); - let z = mem::take(*drain.next().unwrap()).cast::(); - - let r = func(x, y, z); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Pure))); -} - -/// Add a function with three parameters (the first one is a mutable reference) to the package. -/// -/// `map_result` is a function that maps the return type of the function to `Result`. -pub fn reg_trinary_mut( - lib: &mut PackageStore, - fn_name: &'static str, - - #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> R + 'static, - #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> R + Send + Sync + 'static, - - #[cfg(not(feature = "sync"))] map_result: impl Fn(R, Position) -> Result> - + 'static, - #[cfg(feature = "sync")] map_result: impl Fn(R, Position) -> Result> - + Send - + Sync - + 'static, -) { - //println!("register {}(&mut {}, {}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::(), crate::std::any::type_name::()); - - let hash_fn_native = calc_fn_hash( - empty(), - fn_name, - [TypeId::of::(), TypeId::of::(), TypeId::of::()] - .iter() - .cloned(), - ); - - let f = Box::new(move |args: &mut FnCallArgs, pos| { - let mut drain = args.iter_mut(); - let x: &mut A = drain.next().unwrap().downcast_mut().unwrap(); - let y = mem::take(*drain.next().unwrap()).cast::(); - let z = mem::take(*drain.next().unwrap()).cast::(); - - let r = func(x, y, z); - map_result(r, pos) - }); - - lib.functions - .insert(hash_fn_native, Box::new(NativeFunction::new(f, Method))); -} From 5a02548ebc5aca6434a763ff83df4c12a55c573a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 19:39:34 +0800 Subject: [PATCH 14/36] Allocate packages with higher functions capacity. --- examples/hello.rs | 1 - examples/repl.rs | 7 ++----- examples/rhai_runner.rs | 2 +- src/module.rs | 18 ++++++++++++++++++ src/packages/array_basic.rs | 4 ++-- src/packages/mod.rs | 2 +- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/examples/hello.rs b/examples/hello.rs index edec8cb8..af5e8b9c 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,5 +1,4 @@ use rhai::{packages::*, Engine, EvalAltResult, INT}; -use std::rc::Rc; fn main() -> Result<(), Box> { let mut engine = Engine::new_raw(); diff --git a/examples/repl.rs b/examples/repl.rs index a8763e28..f980a98e 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -1,12 +1,9 @@ -use rhai::{Dynamic, Engine, EvalAltResult, Scope, AST, INT}; +use rhai::{Dynamic, Engine, EvalAltResult, Scope, AST}; #[cfg(not(feature = "no_optimize"))] use rhai::OptimizationLevel; -use std::{ - io::{stdin, stdout, Write}, - iter, -}; +use std::io::{stdin, stdout, Write}; fn print_error(input: &str, err: EvalAltResult) { let lines: Vec<_> = input.trim().split('\n').collect(); diff --git a/examples/rhai_runner.rs b/examples/rhai_runner.rs index 2bc273e2..c5c5128d 100644 --- a/examples/rhai_runner.rs +++ b/examples/rhai_runner.rs @@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult}; #[cfg(not(feature = "no_optimize"))] use rhai::OptimizationLevel; -use std::{env, fs::File, io::Read, iter, process::exit}; +use std::{env, fs::File, io::Read, process::exit}; fn eprint_error(input: &str, err: EvalAltResult) { fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) { diff --git a/src/module.rs b/src/module.rs index d1bcabb1..5bc11d74 100644 --- a/src/module.rs +++ b/src/module.rs @@ -94,6 +94,24 @@ impl Module { Default::default() } + /// Create a new module with a specified capacity for native Rust functions. + /// + /// # Examples + /// + /// ``` + /// use rhai::Module; + /// + /// let mut module = Module::new(); + /// module.set_var("answer", 42_i64); + /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); + /// ``` + pub fn new_with_capacity(capacity: usize) -> Self { + Self { + functions: HashMap::with_capacity(capacity), + ..Default::default() + } + } + /// Does a variable exist in the module? /// /// # Examples diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 6ff90f39..5aed286e 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -17,7 +17,7 @@ fn ins(list: &mut Array, position: INT, item: T) -> FuncRetu if position <= 0 { list.insert(0, Dynamic::from(item)); } else if (position as usize) >= list.len() - 1 { - push(list, item); + push(list, item)?; } else { list.insert(position as usize, Dynamic::from(item)); } @@ -26,7 +26,7 @@ fn ins(list: &mut Array, position: INT, item: T) -> FuncRetu fn pad(list: &mut Array, len: INT, item: T) -> FuncReturn<()> { if len >= 0 { while list.len() < len as usize { - push(list, item.clone()); + push(list, item.clone())?; } } Ok(()) diff --git a/src/packages/mod.rs b/src/packages/mod.rs index a8599417..ceebf4bf 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -129,7 +129,7 @@ macro_rules! def_package { impl $package { pub fn new() -> Self { - let mut module = $root::Module::new(); + let mut module = $root::Module::new_with_capacity(512); ::init(&mut module); Self(module.into()) } From 9d8d074940ec4e2e63e0a6de50b7a3c20aae1765 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 20:22:05 +0800 Subject: [PATCH 15/36] Consistent naming of methods. --- src/api.rs | 3 +-- src/engine.rs | 8 ++++---- src/fn_func.rs | 1 - src/module.rs | 20 +++----------------- src/optimize.rs | 2 +- src/packages/array_basic.rs | 2 +- src/packages/iter_basic.rs | 4 ++-- src/packages/mod.rs | 12 ++++++------ 8 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/api.rs b/src/api.rs index d69933e9..0af6954f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -125,8 +125,7 @@ impl Engine { /// Register an iterator adapter for a type with the `Engine`. /// This is an advanced feature. pub fn register_iterator(&mut self, f: F) { - self.global_module - .set_iterator(TypeId::of::(), Box::new(f)); + self.global_module.set_iter(TypeId::of::(), Box::new(f)); } /// Register a getter function for a member of a registered type with the `Engine`. diff --git a/src/engine.rs b/src/engine.rs index 2acb7fad..eda78ae0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -552,7 +552,7 @@ impl Engine { if let Some(func) = self .global_module .get_fn(hashes.0) - .or_else(|| self.packages.get_function(hashes.0)) + .or_else(|| self.packages.get_fn(hashes.0)) { let mut backup: Dynamic = Default::default(); @@ -728,7 +728,7 @@ impl Engine { // First check registered functions self.global_module.contains_fn(hashes.0) // Then check packages - || self.packages.contains_function(hashes.0) + || self.packages.contains_fn(hashes.0) // Then check script-defined functions || state.has_function(hashes.1) } @@ -1573,8 +1573,8 @@ impl Engine { if let Some(iter_fn) = self .global_module - .get_iterator(tid) - .or_else(|| self.packages.get_iterator(tid)) + .get_iter(tid) + .or_else(|| self.packages.get_iter(tid)) { // Add the loop variable let name = x.0.clone(); diff --git a/src/fn_func.rs b/src/fn_func.rs index a595064f..e619ebdf 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -92,7 +92,6 @@ macro_rules! def_anonymous_fn { { #[cfg(feature = "sync")] type Output = Box Result> + Send + Sync>; - #[cfg(not(feature = "sync"))] type Output = Box Result>>; diff --git a/src/module.rs b/src/module.rs index 5bc11d74..698004c7 100644 --- a/src/module.rs +++ b/src/module.rs @@ -567,20 +567,6 @@ impl Module { .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos))) } - /// Get the script-defined functions. - /// - /// # Examples - /// - /// ``` - /// use rhai::Module; - /// - /// let mut module = Module::new(); - /// assert_eq!(module.get_fn_lib().len(), 0); - /// ``` - pub fn get_fn_lib(&self) -> &FunctionsLib { - &self.fn_lib - } - /// Get a modules-qualified script-defined functions. /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. @@ -720,12 +706,12 @@ impl Module { } /// Does a type iterator exist in the module? - pub fn contains_iterator(&self, id: TypeId) -> bool { + pub fn contains_iter(&self, id: TypeId) -> bool { self.type_iterators.contains_key(&id) } /// Set a type iterator into the module. - pub fn set_iterator(&mut self, typ: TypeId, func: Box) { + pub fn set_iter(&mut self, typ: TypeId, func: Box) { #[cfg(not(feature = "sync"))] self.type_iterators.insert(typ, Rc::new(func)); #[cfg(feature = "sync")] @@ -733,7 +719,7 @@ impl Module { } /// Get the specified type iterator. - pub fn get_iterator(&self, id: TypeId) -> Option<&SharedIteratorFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { self.type_iterators.get(&id) } } diff --git a/src/optimize.rs b/src/optimize.rs index babb58c0..e03ea76e 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -123,7 +123,7 @@ fn call_fn( global_module .get_fn(hash) - .or_else(|| packages.get_function(hash)) + .or_else(|| packages.get_fn(hash)) .map(|func| func.call(args, pos)) .transpose() } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 5aed286e..bdfd03aa 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -118,7 +118,7 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { ); // Register array iterator - lib.set_iterator( + lib.set_iter( TypeId::of::(), Box::new(|arr| Box::new( arr.cast::().into_iter()) as Box> diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index daf57c44..553873e7 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -14,7 +14,7 @@ fn reg_range(lib: &mut Module) where Range: Iterator, { - lib.set_iterator( + lib.set_iter( TypeId::of::>(), Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) @@ -58,7 +58,7 @@ where T: Variant + Clone + PartialOrd, StepRange: Iterator, { - lib.set_iterator( + lib.set_iter( TypeId::of::>(), Box::new(|source| { Box::new(source.cast::>().map(|x| x.into_dynamic())) diff --git a/src/packages/mod.rs b/src/packages/mod.rs index ceebf4bf..5c2a6dd5 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -64,11 +64,11 @@ impl PackagesCollection { self.packages.insert(0, package); } /// Does the specified function hash key exist in the `PackagesCollection`? - pub fn contains_function(&self, hash: u64) -> bool { + pub fn contains_fn(&self, hash: u64) -> bool { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_function(&self, hash: u64) -> Option<&Box> { + pub fn get_fn(&self, hash: u64) -> Option<&Box> { self.packages .iter() .map(|p| p.get_fn(hash)) @@ -76,14 +76,14 @@ impl PackagesCollection { .flatten() } /// Does the specified TypeId iterator exist in the `PackagesCollection`? - pub fn contains_iterator(&self, id: TypeId) -> bool { - self.packages.iter().any(|p| p.contains_iterator(id)) + pub fn contains_iter(&self, id: TypeId) -> bool { + self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iterator(&self, id: TypeId) -> Option<&SharedIteratorFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { self.packages .iter() - .map(|p| p.get_iterator(id)) + .map(|p| p.get_iter(id)) .find(|f| f.is_some()) .flatten() } From cabceb74982cf4ed332bd70698cc65797d8f82d7 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 21:58:38 +0800 Subject: [PATCH 16/36] Better handling of errors during function calls. --- src/api.rs | 3 +- src/engine.rs | 45 ++++++++++++++++++++++++------ src/fn_native.rs | 14 ++++------ src/fn_register.rs | 16 ++++------- src/module.rs | 56 ++++++++++++++------------------------ src/optimize.rs | 3 +- src/packages/math_basic.rs | 1 - src/parser.rs | 2 +- src/result.rs | 25 +++++++++++++---- tests/functions.rs | 2 +- tests/method_call.rs | 2 +- tests/stack.rs | 3 +- 12 files changed, 97 insertions(+), 75 deletions(-) diff --git a/src/api.rs b/src/api.rs index 0af6954f..24b28474 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1017,7 +1017,8 @@ impl Engine { let state = State::new(fn_lib); - let result = self.call_script_fn(Some(scope), &state, fn_def, args.as_mut(), pos, 0)?; + let result = + self.call_script_fn(Some(scope), &state, name, fn_def, args.as_mut(), pos, 0)?; let return_type = self.map_type_name(result.type_name()); diff --git a/src/engine.rs b/src/engine.rs index eda78ae0..d63787d4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -543,7 +543,7 @@ impl Engine { if hashes.1 > 0 { if let Some(fn_def) = state.get_function(hashes.1) { return self - .call_script_fn(scope, state, fn_def, args, pos, level) + .call_script_fn(scope, state, fn_name, fn_def, args, pos, level) .map(|v| (v, false)); } } @@ -569,7 +569,7 @@ impl Engine { }; // Run external function - let result = match func.call(args, pos) { + let result = match func.call(args) { Ok(r) => { // Restore the backup value for the first argument since it has been consumed! if restore { @@ -577,7 +577,9 @@ impl Engine { } r } - Err(err) => return Err(err), + Err(err) => { + return Err(err.new_position(pos)); + } }; // See if the function match print/debug (which requires special processing) @@ -658,6 +660,7 @@ impl Engine { &self, scope: Option<&mut Scope>, state: &State, + fn_name: &str, fn_def: &FnDef, args: &mut FnCallArgs, pos: Position, @@ -687,7 +690,18 @@ impl Engine { .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), - _ => Err(EvalAltResult::set_position(err, pos)), + EvalAltResult::ErrorInFunctionCall(name, err, _) => { + Err(Box::new(EvalAltResult::ErrorInFunctionCall( + format!("{} > {}", fn_name, name), + err, + pos, + ))) + } + _ => Err(Box::new(EvalAltResult::ErrorInFunctionCall( + fn_name.to_string(), + err, + pos, + ))), }); scope.rewind(scope_len); @@ -717,7 +731,18 @@ impl Engine { .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), - _ => Err(EvalAltResult::set_position(err, pos)), + EvalAltResult::ErrorInFunctionCall(name, err, _) => { + Err(Box::new(EvalAltResult::ErrorInFunctionCall( + format!("{} > {}", fn_name, name), + err, + pos, + ))) + } + _ => Err(Box::new(EvalAltResult::ErrorInFunctionCall( + fn_name.to_string(), + err, + pos, + ))), }); } } @@ -809,7 +834,7 @@ impl Engine { // Evaluate the AST self.eval_ast_with_scope_raw(scope, &ast) - .map_err(|err| EvalAltResult::set_position(err, pos)) + .map_err(|err| err.new_position(pos)) } /// Chain-evaluate a dot/index chain. @@ -1415,7 +1440,7 @@ impl Engine { // First search in script-defined functions (can override built-in) if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { - self.call_script_fn(None, state, fn_def, args.as_mut(), *pos, level) + self.call_script_fn(None, state, name, fn_def, args.as_mut(), *pos, level) } else { // Then search in Rust functions @@ -1428,8 +1453,10 @@ impl Engine { // 3) The final hash is the XOR of the two hashes. let hash_fn_native = *hash_fn_def ^ hash_fn_args; - match module.get_qualified_fn(name, hash_fn_native, *pos) { - Ok(func) => func.call(args.as_mut(), *pos), + match module.get_qualified_fn(name, hash_fn_native) { + Ok(func) => func + .call(args.as_mut()) + .map_err(|err| err.new_position(*pos)), Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), Err(err) => Err(err), } diff --git a/src/fn_native.rs b/src/fn_native.rs index b56d2644..a8daac5f 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,16 +1,14 @@ use crate::any::Dynamic; use crate::result::EvalAltResult; -use crate::token::Position; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; pub type FnCallArgs<'a> = [&'a mut Dynamic]; #[cfg(feature = "sync")] -pub type FnAny = - dyn Fn(&mut FnCallArgs, Position) -> Result> + Send + Sync; +pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> + Send + Sync; #[cfg(not(feature = "sync"))] -pub type FnAny = dyn Fn(&mut FnCallArgs, Position) -> Result>; +pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result>; #[cfg(feature = "sync")] pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; @@ -85,7 +83,7 @@ pub trait NativeCallable { /// Get the ABI type of a native Rust function. fn abi(&self) -> NativeFunctionABI; /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; + fn call(&self, args: &mut FnCallArgs) -> Result>; } /// A trait implemented by all native Rust functions that are callable by Rhai. @@ -94,7 +92,7 @@ pub trait NativeCallable: Send + Sync { /// Get the ABI type of a native Rust function. fn abi(&self) -> NativeFunctionABI; /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result>; + fn call(&self, args: &mut FnCallArgs) -> Result>; } /// A type encapsulating a native Rust function callable by Rhai. @@ -104,8 +102,8 @@ impl NativeCallable for NativeFunction { fn abi(&self) -> NativeFunctionABI { self.1 } - fn call(&self, args: &mut FnCallArgs, pos: Position) -> Result> { - (self.0)(args, pos) + fn call(&self, args: &mut FnCallArgs) -> Result> { + (self.0)(args) } } diff --git a/src/fn_register.rs b/src/fn_register.rs index 80e09cd5..8b91ea42 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -7,9 +7,8 @@ use crate::engine::Engine; use crate::fn_native::{FnCallArgs, NativeFunctionABI::*}; use crate::parser::FnAccess; use crate::result::EvalAltResult; -use crate::token::Position; -use crate::stdlib::{any::TypeId, boxed::Box, iter::empty, mem, string::ToString}; +use crate::stdlib::{any::TypeId, boxed::Box, mem, string::ToString}; /// A trait to register custom functions with the `Engine`. pub trait RegisterFn { @@ -140,7 +139,7 @@ macro_rules! make_func { // ^ function parameter generic type name (A, B, C etc.) // ^ dereferencing function - Box::new(move |args: &mut FnCallArgs, pos: Position| { + Box::new(move |args: &mut FnCallArgs| { // The arguments are assumed to be of the correct number and types! #[allow(unused_variables, unused_mut)] @@ -155,23 +154,20 @@ macro_rules! make_func { let r = $fn($($par),*); // Map the result - $map(r, pos) + $map(r) }) }; } /// To Dynamic mapping function. #[inline(always)] -pub fn map_dynamic( - data: T, - _pos: Position, -) -> Result> { +pub fn map_dynamic(data: T) -> Result> { Ok(data.into_dynamic()) } /// To Dynamic mapping function. #[inline(always)] -pub fn map_identity(data: Dynamic, _pos: Position) -> Result> { +pub fn map_identity(data: Dynamic) -> Result> { Ok(data) } @@ -179,10 +175,8 @@ pub fn map_identity(data: Dynamic, _pos: Position) -> Result( data: Result>, - pos: Position, ) -> Result> { data.map(|v| v.into_dynamic()) - .map_err(|err| EvalAltResult::set_position(err, pos)) } macro_rules! def_register { diff --git a/src/module.rs b/src/module.rs index 698004c7..6e56a128 100644 --- a/src/module.rs +++ b/src/module.rs @@ -317,11 +317,7 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |_: &mut FnCallArgs, pos| { - func() - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) - }; + let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let arg_types = []; self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } @@ -345,11 +341,8 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { - func(mem::take(args[0]).cast::()) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) - }; + let f = + move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); let arg_types = [TypeId::of::()]; self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) } @@ -373,10 +366,8 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(&mut A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { - func(args[0].downcast_mut::().unwrap()) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) + let f = move |args: &mut FnCallArgs| { + func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let arg_types = [TypeId::of::()]; self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) @@ -403,13 +394,11 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { + let f = move |args: &mut FnCallArgs| { let a = mem::take(args[0]).cast::(); let b = mem::take(args[1]).cast::(); - func(a, b) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) + func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) @@ -440,13 +429,11 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { + let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::(); let a = args[0].downcast_mut::().unwrap(); - func(a, b) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) + func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) @@ -479,14 +466,12 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { + let f = move |args: &mut FnCallArgs| { let a = mem::take(args[0]).cast::(); let b = mem::take(args[1]).cast::(); let c = mem::take(args[2]).cast::(); - func(a, b, c) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) + func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) @@ -520,14 +505,12 @@ impl Module { #[cfg(not(feature = "sync"))] func: impl Fn(&mut A, B, C) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(&mut A, B, C) -> FuncReturn + Send + Sync + 'static, ) -> u64 { - let f = move |args: &mut FnCallArgs, pos| { + let f = move |args: &mut FnCallArgs| { let b = mem::take(args[1]).cast::(); let c = mem::take(args[2]).cast::(); let a = args[0].downcast_mut::().unwrap(); - func(a, b, c) - .map(Dynamic::from) - .map_err(|err| EvalAltResult::set_position(err, pos)) + func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) @@ -559,12 +542,16 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - pos: Position, ) -> Result<&Box, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) - .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos))) + .ok_or_else(|| { + Box::new(EvalAltResult::ErrorFunctionNotFound( + name.to_string(), + Position::none(), + )) + }) } /// Get a modules-qualified script-defined functions. @@ -950,10 +937,9 @@ mod file { // Compile it let ast = engine .compile_file(file_path) - .map_err(|err| EvalAltResult::set_position(err, pos))?; + .map_err(|err| err.new_position(pos))?; - Module::eval_ast_as_new(scope, &ast, engine) - .map_err(|err| EvalAltResult::set_position(err, pos)) + Module::eval_ast_as_new(scope, &ast, engine).map_err(|err| err.new_position(pos)) } } } diff --git a/src/optimize.rs b/src/optimize.rs index e03ea76e..df385b74 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -124,8 +124,9 @@ fn call_fn( global_module .get_fn(hash) .or_else(|| packages.get_fn(hash)) - .map(|func| func.call(args, pos)) + .map(|func| func.call(args)) .transpose() + .map_err(|err| err.new_position(pos)) } /// Optimize a statement. diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 0698befb..9c7313ed 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -1,5 +1,4 @@ use crate::def_package; -use crate::module::FuncReturn; use crate::parser::INT; use crate::result::EvalAltResult; use crate::token::Position; diff --git a/src/parser.rs b/src/parser.rs index 42f8a5a5..0fefc84c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -516,7 +516,7 @@ impl Expr { } } - /// Get the `Position` of the expression. + /// Override the `Position` of the expression. pub(crate) fn set_position(mut self, new_pos: Position) -> Self { match &mut self { #[cfg(not(feature = "no_float"))] diff --git a/src/result.rs b/src/result.rs index 5e38f8d9..c33c810b 100644 --- a/src/result.rs +++ b/src/result.rs @@ -33,6 +33,9 @@ pub enum EvalAltResult { /// Call to an unknown function. Wrapped value is the name of the function. ErrorFunctionNotFound(String, Position), + /// An error has occurred inside a called function. + /// Wrapped values re the name of the function and the interior error. + ErrorInFunctionCall(String, Box, Position), /// Function call has incorrect number of arguments. /// Wrapped values are the name of the function, the number of parameters required /// and the actual number of arguments passed. @@ -97,6 +100,7 @@ impl EvalAltResult { Self::ErrorReadingScriptFile(_, _, _) => "Cannot read from script file", Self::ErrorParsing(p) => p.desc(), + Self::ErrorInFunctionCall(_, _, _) => "Error in called function", Self::ErrorFunctionNotFound(_, _) => "Function not found", Self::ErrorFunctionArgsMismatch(_, _, _, _) => { "Function call with wrong number of arguments" @@ -160,6 +164,10 @@ impl fmt::Display for EvalAltResult { Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p), + Self::ErrorInFunctionCall(s, err, pos) => { + write!(f, "Error in call to function '{}' ({}): {}", s, pos, err) + } + Self::ErrorFunctionNotFound(s, pos) | Self::ErrorVariableNotFound(s, pos) | Self::ErrorModuleNotFound(s, pos) => write!(f, "{}: '{}' ({})", desc, s, pos), @@ -262,6 +270,7 @@ impl> From for Box { } impl EvalAltResult { + /// Get the `Position` of this error. pub fn position(&self) -> Position { match self { #[cfg(not(feature = "no_std"))] @@ -270,6 +279,7 @@ impl EvalAltResult { Self::ErrorParsing(err) => err.position(), Self::ErrorFunctionNotFound(_, pos) + | Self::ErrorInFunctionCall(_, _, pos) | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) @@ -296,16 +306,16 @@ impl EvalAltResult { } } - /// Consume the current `EvalAltResult` and return a new one - /// with the specified `Position`. - pub(crate) fn set_position(mut err: Box, new_position: Position) -> Box { - match err.as_mut() { + /// Override the `Position` of this error. + pub fn set_position(&mut self, new_position: Position) { + match self { #[cfg(not(feature = "no_std"))] Self::ErrorReadingScriptFile(_, pos, _) => *pos = new_position, Self::ErrorParsing(err) => err.1 = new_position, Self::ErrorFunctionNotFound(_, pos) + | Self::ErrorInFunctionCall(_, _, pos) | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) @@ -330,7 +340,12 @@ impl EvalAltResult { | Self::ErrorLoopBreak(_, pos) | Self::Return(_, pos) => *pos = new_position, } + } - err + /// Consume the current `EvalAltResult` and return a new one + /// with the specified `Position`. + pub(crate) fn new_position(mut self: Box, new_position: Position) -> Box { + self.set_position(new_position); + self } } diff --git a/tests/functions.rs b/tests/functions.rs index 422394c1..26d9b387 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -1,5 +1,5 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Engine, EvalAltResult, Func, ParseErrorType, Scope, INT}; +use rhai::{Engine, EvalAltResult, INT}; #[test] fn test_functions() -> Result<(), Box> { diff --git a/tests/method_call.rs b/tests/method_call.rs index 59857102..850cf247 100644 --- a/tests/method_call.rs +++ b/tests/method_call.rs @@ -41,7 +41,7 @@ fn test_method_call() -> Result<(), Box> { #[test] fn test_method_call_style() -> Result<(), Box> { - let mut engine = Engine::new(); + let engine = Engine::new(); assert_eq!(engine.eval::("let x = -123; x.abs(); x")?, -123); diff --git a/tests/stack.rs b/tests/stack.rs index 503bcbde..1eeb4250 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -23,7 +23,8 @@ fn test_stack_overflow() -> Result<(), Box> { ) { Ok(_) => panic!("should be stack overflow"), Err(err) => match *err { - EvalAltResult::ErrorStackOverflow(_) => (), + EvalAltResult::ErrorInFunctionCall(name, _, _) + if name.starts_with("foo > foo > foo") => {} _ => panic!("should be stack overflow"), }, } From e6d6a709f00a820d92660e60d20fa6ca3d2d3995 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 13 May 2020 22:49:12 +0800 Subject: [PATCH 17/36] Expand section on modules and packages. --- README.md | 84 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 00e5b940..04e5ebd1 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,9 @@ Rhai's current features set: * Fairly efficient (1 million iterations in 0.75 sec on my 5 year old laptop) * Low compile-time overhead (~0.6 sec debug/~3 sec release for script runner app) * [`no-std`](#optional-features) support -* Support for [function overloading](#function-overloading) -* Support for [operator overloading](#operator-overloading) -* Support for loading external [modules] +* [Function overloading](#function-overloading) +* [Operator overloading](#operator-overloading) +* [Modules] * Compiled script is [optimized](#script-optimization) for repeat evaluations * Support for [minimal builds](#minimal-builds) by excluding unneeded language [features](#optional-features) * Very few additional dependencies (right now only [`num-traits`](https://crates.io/crates/num-traits/) @@ -126,7 +126,8 @@ Disable script-defined functions (`no_function`) only when the feature is not ne [`Engine::new_raw`](#raw-engine) creates a _raw_ engine which does not register _any_ utility functions. This makes the scripting language quite useless as even basic arithmetic operators are not supported. -Selectively include the necessary operators by loading specific [packages](#packages) while minimizing the code footprint. +Selectively include the necessary functionalities by loading specific [packages](#packages) to minimize the footprint. +Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. Related ------- @@ -371,7 +372,7 @@ Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, n ### Packages Rhai functional features are provided in different _packages_ that can be loaded via a call to `load_package`. -Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be imported in order for +Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for packages to be used. ```rust @@ -382,7 +383,7 @@ use rhai::packages::CorePackage; // the 'core' package contains b let mut engine = Engine::new_raw(); // create a 'raw' Engine let package = CorePackage::new(); // create a package - can be shared among multiple `Engine` instances -engine.load_package(package.get()); // load the package manually +engine.load_package(package.get()); // load the package manually. 'get' returns a reference to the shared package ``` The follow packages are available: @@ -401,6 +402,20 @@ The follow packages are available: | `CorePackage` | Basic essentials | | | | `StandardPackage` | Standard library | | | +Packages typically contain Rust functions that are callable within a Rhai script. +All functions registered in a package is loaded under the _global namespace_ (i.e. they're available without module qualifiers). +Once a package is created (e.g. via `new`), it can be _shared_ (via `get`) among multiple instances of [`Engine`], +even across threads (if the [`sync`] feature is turned on). +Therefore, a package only has to be created _once_. + +Packages are actually implemented as [modules], so they share a lot of behavior and characteristics. +The main difference is that a package loads under the _global_ namespace, while a module loads under its own +namespace alias specified in an `import` statement (see also [modules]). +A package is _static_ (i.e. pre-loaded into an [`Engine`]), while a module is _dynamic_ (i.e. loaded with +the `import` statement). + +Custom packages can also be created. See the macro [`def_package!`](https://docs.rs/rhai/0.13.0/rhai/macro.def_package.html). + Evaluate expressions only ------------------------- @@ -762,11 +777,17 @@ println!("result: {}", result); // prints 42 let result: f64 = engine.eval("1.0 + 0.0"); // '+' operator for two floats not overloaded println!("result: {}", result); // prints 1.0 + +fn mixed_add(a: i64, b: f64) -> f64 { (a as f64) + b } + +engine.register_fn("+", mixed_add); // register '+' operator for an integer and a float + +let result: i64 = engine.eval("1 + 1.0"); // prints 2.0 (normally an error) ``` -Use operator overloading for custom types (described below) only. Be very careful when overloading built-in operators because -script writers expect standard operators to behave in a consistent and predictable manner, and will be annoyed if a calculation -for '+' turns into a subtraction, for example. +Use operator overloading for custom types (described below) only. +Be very careful when overloading built-in operators because script writers expect standard operators to behave in a +consistent and predictable manner, and will be annoyed if a calculation for '`+`' turns into a subtraction, for example. Operator overloading also impacts script optimization when using [`OptimizationLevel::Full`]. See the [relevant section](#script-optimization) for more details. @@ -2048,21 +2069,21 @@ for entry in logbook.read().unwrap().iter() { } ``` -Using external modules ----------------------- +Modules +------- -[module]: #using-external-modules -[modules]: #using-external-modules +[module]: #modules +[modules]: #modules -Rhai allows organizing code (functions and variables) into _modules_. +Rhai allows organizing code (functions, both Rust-based or script-based, and variables) into _modules_. Modules can be disabled via the [`no_module`] feature. -### Exporting variables and functions +### Exporting variables and functions from modules -A module is a single script (or pre-compiled `AST`) containing global variables and functions. +A _module_ is a single script (or pre-compiled `AST`) containing global variables and functions. The `export` statement, which can only be at global level, exposes selected variables as members of a module. -Variables not exported are private and invisible to the outside. -All functions are automatically exported, unless it is prefixed with `private`. +Variables not exported are _private_ and invisible to the outside. +On the other hand, all functions are automatically exported, _unless_ it is explicitly opt-out with the `private` prefix. Functions declared `private` are invisible to the outside. Everything exported from a module is **constant** (**read-only**). @@ -2070,11 +2091,11 @@ Everything exported from a module is **constant** (**read-only**). ```rust // This is a module script. -fn inc(x) { x + 1 } // public function +fn inc(x) { x + 1 } // script-defined function - default public private fn foo() {} // private function - invisible to outside -let private = 123; // variable not exported - invisible to outside +let private = 123; // variable not exported - default invisible to outside let x = 42; // this will be exported below export x; // the variable 'x' is exported under its own name @@ -2085,19 +2106,25 @@ export x as answer; // the variable 'x' is exported under the alias 'ans ### Importing modules -A module can be _imported_ via the `import` statement, and its members accessed via '`::`' similar to C++. +A module can be _imported_ via the `import` statement, and its members are accessed via '`::`' similar to C++. ```rust import "crypto" as crypto; // import the script file 'crypto.rhai' as a module crypto::encrypt(secret); // use functions defined under the module via '::' +crypto::hash::sha256(key); // sub-modules are also supported + print(crypto::status); // module variables are constants -crypto::hash::sha256(key); // sub-modules are also supported +crypto::status = "off"; // <- runtime error - cannot modify a constant ``` `import` statements are _scoped_, meaning that they are only accessible inside the scope that they're imported. +They can appear anywhere a normal statement can be, but in the vast majority of cases `import` statements are +group at the beginning of a script. It is not advised to deviate from this common practice unless there is +a _Very Good Reason™_. Especially, do not place an `import` statement within a loop; doing so will repeatedly +re-load the same module during every iteration of the loop! ```rust let mod = "crypto"; @@ -2110,9 +2137,15 @@ if secured { // new block scope crypto::encrypt(others); // <- this causes a run-time error because the 'crypto' module // is no longer available! + +for x in range(0, 1000) { + import "crypto" as c; // <- importing a module inside a loop is a Very Bad Idea™ + + c.encrypt(something); +} ``` -### Creating custom modules from Rust +### Creating custom modules with Rust To load a custom module (written in Rust) into an [`Engine`], first create a `Module` type, add variables/functions into it, then finally push it into a custom [`Scope`]. This has the equivalent effect of putting an `import` statement @@ -2141,8 +2174,9 @@ engine.eval_expression_with_scope::(&scope, "question::inc(question::answer ### Creating a module from an `AST` -It is easy to convert a pre-compiled `AST` into a module, just use `Module::eval_ast_as_new`. -Don't forget the `export` statement, otherwise there will be nothing inside the module! +It is easy to convert a pre-compiled `AST` into a module: just use `Module::eval_ast_as_new`. +Don't forget the `export` statement, otherwise there will be no variables exposed by the module +other than non-`private` functions (unless that's intentional). ```rust use rhai::{Engine, Module}; From 5c61827c7ce58b4f28f6f335829fb4278abc35d1 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 14 May 2020 11:21:56 +0800 Subject: [PATCH 18/36] Force-cast local variable names when pushing into scope. --- README.md | 1 + src/any.rs | 52 +++++++++++++++++++------------------- src/engine.rs | 69 +++++++++++++++++++++++++++++++++++++++------------ src/utils.rs | 4 +-- 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 04e5ebd1..0b359294 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Rhai's current features set: * Freely pass variables/constants into a script via an external [`Scope`] * Fairly efficient (1 million iterations in 0.75 sec on my 5 year old laptop) * Low compile-time overhead (~0.6 sec debug/~3 sec release for script runner app) +* Relatively little `unsafe` code (yes there are some for performance reasons) * [`no-std`](#optional-features) support * [Function overloading](#function-overloading) * [Operator overloading](#operator-overloading) diff --git a/src/any.rs b/src/any.rs index 2682115b..7ce26170 100644 --- a/src/any.rs +++ b/src/any.rs @@ -299,7 +299,7 @@ impl Default for Dynamic { } /// Cast a type into another type. -fn try_cast(a: A) -> Option { +fn unsafe_try_cast(a: A) -> Option { if TypeId::of::() == a.type_id() { // SAFETY: Just checked we have the right type. We explicitly forget the // value immediately after moving out, removing any chance of a destructor @@ -315,7 +315,7 @@ fn try_cast(a: A) -> Option { } /// Cast a Boxed type into another type. -fn cast_box(item: Box) -> Result, Box> { +fn unsafe_cast_box(item: Box) -> Result, Box> { // Only allow casting to the exact same type if TypeId::of::() == TypeId::of::() { // SAFETY: just checked whether we are pointing to the correct type @@ -383,17 +383,17 @@ impl Dynamic { let mut var = Box::new(value); - var = match cast_box::<_, Dynamic>(var) { + var = match unsafe_cast_box::<_, Dynamic>(var) { Ok(d) => return *d, Err(var) => var, }; - var = match cast_box::<_, String>(var) { + var = match unsafe_cast_box::<_, String>(var) { Ok(s) => return Self(Union::Str(s)), Err(var) => var, }; #[cfg(not(feature = "no_index"))] { - var = match cast_box::<_, Array>(var) { + var = match unsafe_cast_box::<_, Array>(var) { Ok(array) => return Self(Union::Array(array)), Err(var) => var, }; @@ -401,7 +401,7 @@ impl Dynamic { #[cfg(not(feature = "no_object"))] { - var = match cast_box::<_, Map>(var) { + var = match unsafe_cast_box::<_, Map>(var) { Ok(map) => return Self(Union::Map(map)), Err(var) => var, } @@ -426,23 +426,23 @@ impl Dynamic { /// ``` pub fn try_cast(self) -> Option { if TypeId::of::() == TypeId::of::() { - return cast_box::<_, T>(Box::new(self)).ok().map(|v| *v); + return unsafe_cast_box::<_, T>(Box::new(self)).ok().map(|v| *v); } match self.0 { - Union::Unit(value) => try_cast(value), - Union::Bool(value) => try_cast(value), - Union::Str(value) => cast_box::<_, T>(value).ok().map(|v| *v), - Union::Char(value) => try_cast(value), - Union::Int(value) => try_cast(value), + Union::Unit(value) => unsafe_try_cast(value), + Union::Bool(value) => unsafe_try_cast(value), + Union::Str(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), + Union::Char(value) => unsafe_try_cast(value), + Union::Int(value) => unsafe_try_cast(value), #[cfg(not(feature = "no_float"))] - Union::Float(value) => try_cast(value), + Union::Float(value) => unsafe_try_cast(value), #[cfg(not(feature = "no_index"))] - Union::Array(value) => cast_box::<_, T>(value).ok().map(|v| *v), + Union::Array(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), #[cfg(not(feature = "no_object"))] - Union::Map(value) => cast_box::<_, T>(value).ok().map(|v| *v), + Union::Map(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), #[cfg(not(feature = "no_module"))] - Union::Module(value) => cast_box::<_, T>(value).ok().map(|v| *v), + Union::Module(value) => unsafe_cast_box::<_, T>(value).ok().map(|v| *v), Union::Variant(value) => (*value).as_box_any().downcast().map(|x| *x).ok(), } } @@ -467,23 +467,23 @@ impl Dynamic { //self.try_cast::().unwrap() if TypeId::of::() == TypeId::of::() { - return *cast_box::<_, T>(Box::new(self)).unwrap(); + return *unsafe_cast_box::<_, T>(Box::new(self)).unwrap(); } match self.0 { - Union::Unit(value) => try_cast(value).unwrap(), - Union::Bool(value) => try_cast(value).unwrap(), - Union::Str(value) => *cast_box::<_, T>(value).unwrap(), - Union::Char(value) => try_cast(value).unwrap(), - Union::Int(value) => try_cast(value).unwrap(), + Union::Unit(value) => unsafe_try_cast(value).unwrap(), + Union::Bool(value) => unsafe_try_cast(value).unwrap(), + Union::Str(value) => *unsafe_cast_box::<_, T>(value).unwrap(), + Union::Char(value) => unsafe_try_cast(value).unwrap(), + Union::Int(value) => unsafe_try_cast(value).unwrap(), #[cfg(not(feature = "no_float"))] - Union::Float(value) => try_cast(value).unwrap(), + Union::Float(value) => unsafe_try_cast(value).unwrap(), #[cfg(not(feature = "no_index"))] - Union::Array(value) => *cast_box::<_, T>(value).unwrap(), + Union::Array(value) => *unsafe_cast_box::<_, T>(value).unwrap(), #[cfg(not(feature = "no_object"))] - Union::Map(value) => *cast_box::<_, T>(value).unwrap(), + Union::Map(value) => *unsafe_cast_box::<_, T>(value).unwrap(), #[cfg(not(feature = "no_module"))] - Union::Module(value) => *cast_box::<_, T>(value).unwrap(), + Union::Module(value) => *unsafe_cast_box::<_, T>(value).unwrap(), Union::Variant(value) => (*value).as_box_any().downcast().map(|x| *x).unwrap(), } } diff --git a/src/engine.rs b/src/engine.rs index d63787d4..5f730730 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -21,6 +21,7 @@ use crate::parser::ModuleRef; use crate::stdlib::{ any::TypeId, + borrow::Cow, boxed::Box, collections::HashMap, format, @@ -135,6 +136,10 @@ pub struct State<'a> { /// In some situation, e.g. after running an `eval` statement, subsequent offsets may become mis-aligned. /// When that happens, this flag is turned on to force a scope lookup by name. pub always_search: bool, + + /// Level of the current scope. The global (root) level is zero, a new block (or function call) + /// is one level higher, and so on. + pub scope_level: usize, } impl<'a> State<'a> { @@ -143,6 +148,7 @@ impl<'a> State<'a> { Self { always_search: false, fn_lib, + scope_level: 0, } } /// Does a certain script-defined function exist in the `State`? @@ -259,6 +265,24 @@ impl DerefMut for FunctionsLib { } } +/// A dangerous function that blindly casts a `&str` from one lifetime to a `Cow` of +/// another lifetime. This is mainly used to let us push a block-local variable into the +/// current `Scope` without cloning the variable name. Doing this is safe because all local +/// variables in the `Scope` are cleared out before existing the block. +/// +/// Force-casting a local variable lifetime to the current `Scope`'s larger lifetime saves +/// on allocations and string cloning, thus avoids us having to maintain a chain of `Scope`'s. +fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { + // If not at global level, we can force-cast + if state.scope_level > 0 { + // WARNING - force-cast the variable name into the scope's lifetime to avoid cloning it + // this is safe because all local variables are cleared at the end of the block + unsafe { mem::transmute::<_, &'s str>(name) }.into() + } else { + name.to_string().into() + } +} + /// Rhai main scripting engine. /// /// ``` @@ -656,9 +680,9 @@ impl Engine { /// Function call arguments may be _consumed_ when the function requires them to be passed by value. /// All function arguments not in the first position are always passed by value and thus consumed. /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! - pub(crate) fn call_script_fn<'a>( + pub(crate) fn call_script_fn<'s>( &self, - scope: Option<&mut Scope>, + scope: Option<&mut Scope<'s>>, state: &State, fn_name: &str, fn_def: &FnDef, @@ -672,7 +696,9 @@ impl Engine { let scope_len = scope.len(); let mut state = State::new(state.fn_lib); - // Put arguments into scope as variables - variable name is copied + state.scope_level += 1; + + // Put arguments into scope as variables scope.extend( fn_def .params @@ -681,7 +707,10 @@ impl Engine { // Actually consume the arguments instead of cloning them args.into_iter().map(|v| mem::take(*v)), ) - .map(|(name, value)| (name.clone(), ScopeEntryType::Normal, value)), + .map(|(name, value)| { + let var_name = unsafe_cast_var_name(name.as_str(), &state); + (var_name, ScopeEntryType::Normal, value) + }), ); // Evaluate the function at one higher level of call depth @@ -704,6 +733,8 @@ impl Engine { ))), }); + // Remove all local variables + // No need to reset `state.scope_level` because it is thrown away scope.rewind(scope_len); return result; @@ -712,6 +743,7 @@ impl Engine { _ => { let mut scope = Scope::new(); let mut state = State::new(state.fn_lib); + state.scope_level += 1; // Put arguments into scope as variables scope.extend( @@ -726,6 +758,7 @@ impl Engine { ); // Evaluate the function at one higher level of call depth + // No need to reset `state.scope_level` because it is thrown away return self .eval_stmt(&mut scope, &mut state, &fn_def.body, level + 1) .or_else(|err| match *err { @@ -1510,9 +1543,9 @@ impl Engine { } /// Evaluate a statement - pub(crate) fn eval_stmt( + pub(crate) fn eval_stmt<'s>( &self, - scope: &mut Scope, + scope: &mut Scope<'s>, state: &mut State, stmt: &Stmt, level: usize, @@ -1536,12 +1569,14 @@ impl Engine { // Block scope Stmt::Block(x) => { let prev_len = scope.len(); + state.scope_level += 1; let result = x.0.iter().try_fold(Default::default(), |_, stmt| { self.eval_stmt(scope, state, stmt, level) }); scope.rewind(prev_len); + state.scope_level -= 1; // The impact of an eval statement goes away at the end of a block // because any new variables introduced will go out of scope @@ -1604,9 +1639,10 @@ impl Engine { .or_else(|| self.packages.get_iter(tid)) { // Add the loop variable - let name = x.0.clone(); - scope.push(name, ()); + let var_name = unsafe_cast_var_name(&x.0, &state); + scope.push(var_name, ()); let index = scope.len() - 1; + state.scope_level += 1; for loop_var in iter_fn(iter_type) { *scope.get_mut(index).0 = loop_var; @@ -1622,6 +1658,7 @@ impl Engine { } scope.rewind(scope.len() - 1); + state.scope_level -= 1; Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorFor(x.1.position()))) @@ -1667,15 +1704,15 @@ impl Engine { Stmt::Let(x) if x.1.is_some() => { let ((var_name, _), expr) = x.as_ref(); let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; - // TODO - avoid copying variable name in inner block? - scope.push_dynamic_value(var_name.clone(), ScopeEntryType::Normal, val, false); + let var_name = unsafe_cast_var_name(var_name, &state); + scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); Ok(Default::default()) } Stmt::Let(x) => { let ((var_name, _), _) = x.as_ref(); - // TODO - avoid copying variable name in inner block? - scope.push(var_name.clone(), ()); + let var_name = unsafe_cast_var_name(var_name, &state); + scope.push(var_name, ()); Ok(Default::default()) } @@ -1683,8 +1720,8 @@ impl Engine { Stmt::Const(x) if x.1.is_constant() => { let ((var_name, _), expr) = x.as_ref(); let val = self.eval_expr(scope, state, &expr, level)?; - // TODO - avoid copying variable name in inner block? - scope.push_dynamic_value(var_name.clone(), ScopeEntryType::Constant, val, true); + let var_name = unsafe_cast_var_name(var_name, &state); + scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); Ok(Default::default()) } @@ -1709,8 +1746,8 @@ impl Engine { let module = resolver.resolve(self, Scope::new(), &path, expr.position())?; - // TODO - avoid copying module name in inner block? - scope.push_module(name.clone(), module); + let mod_name = unsafe_cast_var_name(name, &state); + scope.push_module(mod_name, module); Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorModuleNotFound( diff --git a/src/utils.rs b/src/utils.rs index 381ab1a0..4e53878d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,4 @@ //! Module containing various utility types and functions. -// -// TODO - remove unsafe code use crate::stdlib::{ any::TypeId, @@ -55,6 +53,8 @@ pub fn calc_fn_spec<'a>( /// # Safety /// /// This type uses some unsafe code (mainly to zero out unused array slots) for efficiency. +// +// TODO - remove unsafe code #[derive(Clone, Hash)] pub struct StaticVec { /// Total number of values held. From 5d5ceb4049abf4fb050f845b8021a00b9ea151ac Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 14 May 2020 18:27:22 +0800 Subject: [PATCH 19/36] Consolidate all unsafe code under one single file. --- src/any.rs | 36 +++------------------------ src/engine.rs | 24 +++++------------- src/lib.rs | 1 + src/unsafe.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.rs | 26 +++++++++++--------- 5 files changed, 92 insertions(+), 63 deletions(-) create mode 100644 src/unsafe.rs diff --git a/src/any.rs b/src/any.rs index 7ce26170..483eee84 100644 --- a/src/any.rs +++ b/src/any.rs @@ -1,10 +1,11 @@ //! Helper module which defines the `Any` trait to to allow dynamic value handling. +use crate::parser::INT; +use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; + #[cfg(not(feature = "no_module"))] use crate::module::Module; -use crate::parser::INT; - #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; @@ -298,37 +299,6 @@ impl Default for Dynamic { } } -/// Cast a type into another type. -fn unsafe_try_cast(a: A) -> Option { - if TypeId::of::() == a.type_id() { - // SAFETY: Just checked we have the right type. We explicitly forget the - // value immediately after moving out, removing any chance of a destructor - // running or value otherwise being used again. - unsafe { - let ret: B = ptr::read(&a as *const _ as *const B); - mem::forget(a); - Some(ret) - } - } else { - None - } -} - -/// Cast a Boxed type into another type. -fn unsafe_cast_box(item: Box) -> Result, Box> { - // Only allow casting to the exact same type - if TypeId::of::() == TypeId::of::() { - // SAFETY: just checked whether we are pointing to the correct type - unsafe { - let raw: *mut dyn Any = Box::into_raw(item as Box); - Ok(Box::from_raw(raw as *mut T)) - } - } else { - // Return the consumed item for chaining. - Err(item) - } -} - impl Dynamic { /// Create a `Dynamic` from any type. A `Dynamic` value is simply returned as is. /// diff --git a/src/engine.rs b/src/engine.rs index 5f730730..7ef00d45 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -8,6 +8,7 @@ use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; +use crate::r#unsafe::unsafe_cast_var_name; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -127,6 +128,11 @@ impl> From for Target<'_> { } /// A type that holds all the current states of the Engine. +/// +/// # Safety +/// +/// This type uses some unsafe code, mainly for avoiding cloning of local variable names via +/// direct lifetime casting. #[derive(Debug, Clone, Copy)] pub struct State<'a> { /// Global script-defined functions. @@ -265,24 +271,6 @@ impl DerefMut for FunctionsLib { } } -/// A dangerous function that blindly casts a `&str` from one lifetime to a `Cow` of -/// another lifetime. This is mainly used to let us push a block-local variable into the -/// current `Scope` without cloning the variable name. Doing this is safe because all local -/// variables in the `Scope` are cleared out before existing the block. -/// -/// Force-casting a local variable lifetime to the current `Scope`'s larger lifetime saves -/// on allocations and string cloning, thus avoids us having to maintain a chain of `Scope`'s. -fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { - // If not at global level, we can force-cast - if state.scope_level > 0 { - // WARNING - force-cast the variable name into the scope's lifetime to avoid cloning it - // this is safe because all local variables are cleared at the end of the block - unsafe { mem::transmute::<_, &'s str>(name) }.into() - } else { - name.to_string().into() - } -} - /// Rhai main scripting engine. /// /// ``` diff --git a/src/lib.rs b/src/lib.rs index f82bc6d3..f5f1ad87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,6 +85,7 @@ mod result; mod scope; mod stdlib; mod token; +mod r#unsafe; mod utils; pub use any::Dynamic; diff --git a/src/unsafe.rs b/src/unsafe.rs new file mode 100644 index 00000000..47d5b40c --- /dev/null +++ b/src/unsafe.rs @@ -0,0 +1,68 @@ +//! A module containing all unsafe code. + +use crate::any::Variant; +use crate::engine::State; +use crate::utils::StaticVec; + +use crate::stdlib::{ + any::{Any, TypeId}, + borrow::Cow, + boxed::Box, + mem, ptr, + string::ToString, + vec::Vec, +}; + +/// Cast a type into another type. +pub fn unsafe_try_cast(a: A) -> Option { + if TypeId::of::() == a.type_id() { + // SAFETY: Just checked we have the right type. We explicitly forget the + // value immediately after moving out, removing any chance of a destructor + // running or value otherwise being used again. + unsafe { + let ret: B = ptr::read(&a as *const _ as *const B); + mem::forget(a); + Some(ret) + } + } else { + None + } +} + +/// Cast a Boxed type into another type. +pub fn unsafe_cast_box(item: Box) -> Result, Box> { + // Only allow casting to the exact same type + if TypeId::of::() == TypeId::of::() { + // SAFETY: just checked whether we are pointing to the correct type + unsafe { + let raw: *mut dyn Any = Box::into_raw(item as Box); + Ok(Box::from_raw(raw as *mut T)) + } + } else { + // Return the consumed item for chaining. + Err(item) + } +} + +/// A dangerous function that blindly casts a `&str` from one lifetime to a `Cow` of +/// another lifetime. This is mainly used to let us push a block-local variable into the +/// current `Scope` without cloning the variable name. Doing this is safe because all local +/// variables in the `Scope` are cleared out before existing the block. +/// +/// Force-casting a local variable lifetime to the current `Scope`'s larger lifetime saves +/// on allocations and string cloning, thus avoids us having to maintain a chain of `Scope`'s. +pub fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { + // If not at global level, we can force-cast + if state.scope_level > 0 { + // WARNING - force-cast the variable name into the scope's lifetime to avoid cloning it + // this is safe because all local variables are cleared at the end of the block + unsafe { mem::transmute::<_, &'s str>(name) }.into() + } else { + name.to_string().into() + } +} + +/// Provide a type instance that is memory-zeroed. +pub fn unsafe_zeroed() -> T { + unsafe { mem::MaybeUninit::zeroed().assume_init() } +} diff --git a/src/utils.rs b/src/utils.rs index 4e53878d..f61d24ea 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,7 @@ //! Module containing various utility types and functions. +use crate::r#unsafe::unsafe_zeroed; + use crate::stdlib::{ any::TypeId, fmt, @@ -73,16 +75,6 @@ impl PartialEq for StaticVec { impl Eq for StaticVec {} -impl Default for StaticVec { - fn default() -> Self { - Self { - len: 0, - list: unsafe { mem::MaybeUninit::zeroed().assume_init() }, - more: Vec::new(), - } - } -} - impl FromIterator for StaticVec { fn from_iter>(iter: X) -> Self { let mut vec = StaticVec::new(); @@ -95,6 +87,16 @@ impl FromIterator for StaticVec { } } +impl Default for StaticVec { + fn default() -> Self { + Self { + len: 0, + list: unsafe_zeroed(), + more: Vec::new(), + } + } +} + impl StaticVec { /// Create a new `StaticVec`. pub fn new() -> Self { @@ -105,7 +107,7 @@ impl StaticVec { if self.len == self.list.len() { // Move the fixed list to the Vec for x in 0..self.list.len() { - let def_val: T = unsafe { mem::MaybeUninit::zeroed().assume_init() }; + let def_val: T = unsafe_zeroed(); self.more .push(mem::replace(self.list.get_mut(x).unwrap(), def_val)); } @@ -126,7 +128,7 @@ impl StaticVec { let result = if self.len <= 0 { panic!("nothing to pop!") } else if self.len <= self.list.len() { - let def_val: T = unsafe { mem::MaybeUninit::zeroed().assume_init() }; + let def_val: T = unsafe_zeroed(); mem::replace(self.list.get_mut(self.len - 1).unwrap(), def_val) } else { let r = self.more.pop().unwrap(); From 55c97eb64927953a2ef80dcab6c3facc4895c4d9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 15 May 2020 11:43:32 +0800 Subject: [PATCH 20/36] Add progress tracking and operations limit. --- README.md | 150 +++++++++++++++++++++++++++----- src/api.rs | 110 ++++++++++++++++++++---- src/engine.rs | 204 +++++++++++++++++++++++++++++++++----------- src/fn_native.rs | 5 ++ src/result.rs | 14 ++- tests/operations.rs | 93 ++++++++++++++++++++ tests/stack.rs | 1 + 7 files changed, 489 insertions(+), 88 deletions(-) create mode 100644 tests/operations.rs diff --git a/README.md b/README.md index 0b359294..c602fa92 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,15 @@ Rhai's current features set: * Fairly efficient (1 million iterations in 0.75 sec on my 5 year old laptop) * Low compile-time overhead (~0.6 sec debug/~3 sec release for script runner app) * Relatively little `unsafe` code (yes there are some for performance reasons) +* Sand-boxed (the scripting [`Engine`] can be declared immutable which cannot mutate the containing environment + unless explicitly allowed via `RefCell` etc.) +* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations)) +* Able to set limits on script resource usage (e.g. see [tracking progress](#tracking-progress)) * [`no-std`](#optional-features) support * [Function overloading](#function-overloading) * [Operator overloading](#operator-overloading) * [Modules] -* Compiled script is [optimized](#script-optimization) for repeat evaluations +* Compiled script is [optimized](#script-optimization) for repeated evaluations * Support for [minimal builds](#minimal-builds) by excluding unneeded language [features](#optional-features) * Very few additional dependencies (right now only [`num-traits`](https://crates.io/crates/num-traits/) to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are @@ -63,19 +67,19 @@ Beware that in order to use pre-releases (e.g. alpha and beta), the exact versio Optional features ----------------- -| Feature | Description | -| ------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| `unchecked` | Exclude arithmetic checking (such as overflows and division by zero). Beware that a bad script may panic the entire system! | -| `no_function` | Disable script-defined functions if not needed. | -| `no_index` | Disable [arrays] and indexing features if not needed. | -| `no_object` | Disable support for custom types and objects. | -| `no_float` | Disable floating-point numbers and math if not needed. | -| `no_optimize` | Disable the script optimizer. | -| `no_module` | Disable modules. | -| `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | -| `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | -| `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | -| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, [`Engine`], [`Scope`] and `AST` are all `Send + Sync`. | +| Feature | Description | +| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `unchecked` | Exclude arithmetic checking (such as over-flows and division by zero), stack depth limit and operations count limit. Beware that a bad script may panic the entire system! | +| `no_function` | Disable script-defined functions if not needed. | +| `no_index` | Disable [arrays] and indexing features if not needed. | +| `no_object` | Disable support for custom types and objects. | +| `no_float` | Disable floating-point numbers and math if not needed. | +| `no_optimize` | Disable the script optimizer. | +| `no_module` | Disable modules. | +| `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | +| `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | +| `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | +| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, [`Engine`], [`Scope`] and `AST` are all `Send + Sync`. | By default, Rhai includes all the standard functionalities in a small, tight package. Most features are here to opt-**out** of certain functionalities that are not needed. @@ -269,7 +273,7 @@ let ast = engine.compile_file("hello_world.rhai".into())?; ### Calling Rhai functions from Rust -Rhai also allows working _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust via `call_fn`. +Rhai also allows working _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust via `Engine::call_fn`. Functions declared with `private` are hidden and cannot be called from Rust (see also [modules]). ```rust @@ -372,7 +376,7 @@ Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, n ### Packages -Rhai functional features are provided in different _packages_ that can be loaded via a call to `load_package`. +Rhai functional features are provided in different _packages_ that can be loaded via a call to `Engine::load_package`. Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for packages to be used. @@ -759,7 +763,7 @@ Because they [_short-circuit_](#boolean-operators), `&&` and `||` are handled sp overriding them has no effect at all. Operator functions cannot be defined as a script function (because operators syntax are not valid function names). -However, operator functions _can_ be registered to the [`Engine`] via `register_fn`, `register_result_fn` etc. +However, operator functions _can_ be registered to the [`Engine`] via the methods `Engine::register_fn`, `Engine::register_result_fn` etc. When a custom operator function is registered with the same name as an operator, it _overloads_ (or overrides) the built-in version. ```rust @@ -2043,7 +2047,7 @@ debug("world!"); // prints "world!" to stdout using debug formatting ### Overriding `print` and `debug` with callback functions When embedding Rhai into an application, it is usually necessary to trap `print` and `debug` output -(for logging into a tracking log, for example). +(for logging into a tracking log, for example) with the `Engine::on_print` and `Engine::on_debug` methods: ```rust // Any function or closure that takes an '&str' argument can be used to override @@ -2237,7 +2241,7 @@ Built-in module resolvers are grouped under the `rhai::module_resolvers` module | `FileModuleResolver` | The default module resolution service, not available under the [`no_std`] feature. Loads a script file (based off the current directory) with `.rhai` extension.
The base directory can be changed via the `FileModuleResolver::new_with_path()` constructor function.
`FileModuleResolver::create_module()` loads a script file and returns a module. | | `StaticModuleResolver` | Loads modules that are statically added. This can be used when the [`no_std`] feature is turned on. | -An [`Engine`]'s module resolver is set via a call to `set_module_resolver`: +An [`Engine`]'s module resolver is set via a call to `Engine::set_module_resolver`: ```rust // Use the 'StaticModuleResolver' @@ -2248,6 +2252,112 @@ engine.set_module_resolver(Some(resolver)); engine.set_module_resolver(None); ``` +Ruggedization - protect against DoS attacks +------------------------------------------ + +For scripting systems open to user-land scripts, it is always best to limit the amount of resources used by a script +so that it does not crash the system by consuming all resources. + +The most important resources to watch out for are: + +* **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. +* **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. +* **Time**: A malignant script may run indefinitely, thereby blocking the calling system waiting for a result. +* **Stack**: A malignant script may consume attempt an infinite recursive call that exhausts the call stack. +* **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, and/or bad + floating-point representations, in order to crash the system. +* **Files**: A malignant script may continuously `import` an external module within an infinite loop, + thereby putting heavy load on the file-system (or even the network if the file is not local). +* **Data**: A malignant script may attempt to read from and/or write to data that it does not own. If this happens, + it is a severe security breach and may put the entire system at risk. + +### Maximum number of operations + +Rhai by default does not limit how much time or CPU a script consumes. +This can be changed via the `Engine::set_max_operations` method, with zero being unlimited (the default). + +```rust +let mut engine = Engine::new(); + +engine.set_max_operations(500); // allow only up to 500 operations for this script + +engine.set_max_operations(0); // allow unlimited operations +``` + +The concept of one single _operation_ in Rhai is volatile - it roughly equals one expression node, +one statement, one iteration of a loop, or one function call etc. with sub-expressions and statement +blocks executed inside these contexts accumulated on top. + +One _operation_ can take an unspecified amount of time and CPU cycles, depending on the particular operation +involved. For example, loading a constant consumes very few CPU cycles, while calling an external Rust function, +though also counted as only one operation, may consume much more resources. + +The _operation count_ is intended to be a very course-grained measurement of the amount of CPU that a script +is consuming, and allows the system to impose a hard upper limit. + +A script exceeding the maximum operations count will terminate with an error result. +This check can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). + +### Tracking progress + +To track script evaluation progress and to force-terminate a script prematurely (for any reason), +provide a closure to the `Engine::on_progress` method: + +```rust +let mut engine = Engine::new(); + +engine.on_progress(|count| { // 'count' is the number of operations performed + if count % 1000 == 0 { + println!("{}", count); // print out a progress log every 1,000 operations + } + true // return 'true' to continue the script + // returning 'false' will terminate the script +}); +``` + +The closure passed to `Engine::on_progress` will be called once every operation. +Return `false` to terminate the script immediately. + +### Maximum stack depth + +Rhai by default limits function calls to a maximum depth of 256 levels (28 levels in debug build). +This limit may be changed via the `Engine::set_max_call_levels` method. +The limit can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). + +```rust +let mut engine = Engine::new(); + +engine.set_max_call_levels(10); // allow only up to 10 levels of function calls + +engine.set_max_call_levels(0); // allow no function calls at all (max depth = zero) +``` + +A script exceeding the maximum call stack depth will terminate with an error result. + +### Checked arithmetic + +All arithmetic calculations in Rhai are _checked_, meaning that the script terminates with an error whenever +it detects a numeric over-flow/under-flow condition or an invalid floating-point operation, instead of +crashing the entire system. This checking can be turned off via the [`unchecked`] feature for higher performance +(but higher risks as well). + +### Access to external data + +Rhai is _sand-boxed_ so a script can never read from outside its own environment. +Furthermore, an [`Engine`] created non-`mut` cannot mutate any state outside of itself; +so it is highly recommended that [`Engine`]'s are created immutable as much as possible. + +```rust +let mut engine = Engine::new(); // create mutable 'Engine' + +engine.register_get("add", add); // configure 'engine' + +let engine = engine; // shadow the variable so that 'engine' is now immutable +``` + + Script optimization =================== @@ -2358,7 +2468,7 @@ There are actually three levels of optimizations: `None`, `Simple` and `Full`. * `Full` is _much_ more aggressive, _including_ running functions on constant arguments to determine their result. One benefit to this is that many more optimization opportunities arise, especially with regards to comparison operators. -An [`Engine`]'s optimization level is set via a call to `set_optimization_level`: +An [`Engine`]'s optimization level is set via a call to `Engine::set_optimization_level`: ```rust // Turn on aggressive optimizations diff --git a/src/api.rs b/src/api.rs index 24b28474..78ceaf72 100644 --- a/src/api.rs +++ b/src/api.rs @@ -865,7 +865,7 @@ impl Engine { scope: &mut Scope, ast: &AST, ) -> Result> { - let result = self.eval_ast_with_scope_raw(scope, ast)?; + let (result, _) = self.eval_ast_with_scope_raw(scope, ast)?; let return_type = self.map_type_name(result.type_name()); @@ -881,7 +881,7 @@ impl Engine { &self, scope: &mut Scope, ast: &AST, - ) -> Result> { + ) -> Result<(Dynamic, u64), Box> { let mut state = State::new(ast.fn_lib()); ast.statements() @@ -893,6 +893,7 @@ impl Engine { EvalAltResult::Return(out, _) => Ok(out), _ => Err(err), }) + .map(|v| (v, state.operations)) } /// Evaluate a file, but throw away the result and only return error (if any). @@ -1015,10 +1016,11 @@ impl Engine { .get_function_by_signature(name, args.len(), true) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?; - let state = State::new(fn_lib); + let mut state = State::new(fn_lib); + let args = args.as_mut(); - let result = - self.call_script_fn(Some(scope), &state, name, fn_def, args.as_mut(), pos, 0)?; + let (result, _) = + self.call_script_fn(Some(scope), &mut state, name, fn_def, args, pos, 0, 0)?; let return_type = self.map_type_name(result.type_name()); @@ -1058,6 +1060,84 @@ impl Engine { optimize_into_ast(self, scope, stmt, fn_lib, optimization_level) } + /// Register a callback for script evaluation progress. + /// + /// # Example + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # use std::sync::RwLock; + /// # use std::sync::Arc; + /// use rhai::Engine; + /// + /// let result = Arc::new(RwLock::new(0_u64)); + /// let logger = result.clone(); + /// + /// let mut engine = Engine::new(); + /// + /// engine.on_progress(move |ops| { + /// if ops > 10000 { + /// false + /// } else if ops % 800 == 0 { + /// *logger.write().unwrap() = ops; + /// true + /// } else { + /// true + /// } + /// }); + /// + /// engine.consume("for x in range(0, 50000) {}") + /// .expect_err("should error"); + /// + /// assert_eq!(*result.read().unwrap(), 9600); + /// + /// # Ok(()) + /// # } + /// ``` + #[cfg(feature = "sync")] + pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + Send + Sync + 'static) { + self.progress = Some(Box::new(callback)); + } + + /// Register a callback for script evaluation progress. + /// + /// # Example + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # use std::cell::Cell; + /// # use std::rc::Rc; + /// use rhai::Engine; + /// + /// let result = Rc::new(Cell::new(0_u64)); + /// let logger = result.clone(); + /// + /// let mut engine = Engine::new(); + /// + /// engine.on_progress(move |ops| { + /// if ops > 10000 { + /// false + /// } else if ops % 800 == 0 { + /// logger.set(ops); + /// true + /// } else { + /// true + /// } + /// }); + /// + /// engine.consume("for x in range(0, 50000) {}") + /// .expect_err("should error"); + /// + /// assert_eq!(result.get(), 9600); + /// + /// # Ok(()) + /// # } + /// ``` + #[cfg(not(feature = "sync"))] + pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + 'static) { + self.progress = Some(Box::new(callback)); + } + /// Override default action of `print` (print to stdout using `println!`) /// /// # Example @@ -1092,21 +1172,21 @@ impl Engine { /// /// ``` /// # fn main() -> Result<(), Box> { - /// # use std::sync::RwLock; - /// # use std::sync::Arc; + /// # use std::cell::RefCell; + /// # use std::rc::Rc; /// use rhai::Engine; /// - /// let result = Arc::new(RwLock::new(String::from(""))); + /// let result = Rc::new(RefCell::new(String::from(""))); /// /// let mut engine = Engine::new(); /// /// // Override action of 'print' function /// let logger = result.clone(); - /// engine.on_print(move |s| logger.write().unwrap().push_str(s)); + /// engine.on_print(move |s| logger.borrow_mut().push_str(s)); /// /// engine.consume("print(40 + 2);")?; /// - /// assert_eq!(*result.read().unwrap(), "42"); + /// assert_eq!(*result.borrow(), "42"); /// # Ok(()) /// # } /// ``` @@ -1149,21 +1229,21 @@ impl Engine { /// /// ``` /// # fn main() -> Result<(), Box> { - /// # use std::sync::RwLock; - /// # use std::sync::Arc; + /// # use std::cell::RefCell; + /// # use std::rc::Rc; /// use rhai::Engine; /// - /// let result = Arc::new(RwLock::new(String::from(""))); + /// let result = Rc::new(RefCell::new(String::from(""))); /// /// let mut engine = Engine::new(); /// /// // Override action of 'print' function /// let logger = result.clone(); - /// engine.on_debug(move |s| logger.write().unwrap().push_str(s)); + /// engine.on_debug(move |s| logger.borrow_mut().push_str(s)); /// /// engine.consume(r#"debug("hello");"#)?; /// - /// assert_eq!(*result.read().unwrap(), r#""hello""#); + /// assert_eq!(*result.borrow(), r#""hello""#); /// # Ok(()) /// # } /// ``` diff --git a/src/engine.rs b/src/engine.rs index 7ef00d45..6f159487 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback}; +use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback, ProgressCallback}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -28,7 +28,7 @@ use crate::stdlib::{ format, iter::{empty, once, repeat}, mem, - num::NonZeroUsize, + num::{NonZeroU64, NonZeroUsize}, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, @@ -48,12 +48,17 @@ pub type Array = Vec; #[cfg(not(feature = "no_object"))] pub type Map = HashMap; +#[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] pub const MAX_CALL_STACK_DEPTH: usize = 28; +#[cfg(not(feature = "unchecked"))] #[cfg(not(debug_assertions))] pub const MAX_CALL_STACK_DEPTH: usize = 256; +#[cfg(feature = "unchecked")] +pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX; + pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; pub const KEYWORD_TYPE_OF: &str = "type_of"; @@ -146,6 +151,9 @@ pub struct State<'a> { /// Level of the current scope. The global (root) level is zero, a new block (or function call) /// is one level higher, and so on. pub scope_level: usize, + + /// Number of operations performed. + pub operations: u64, } impl<'a> State<'a> { @@ -155,6 +163,7 @@ impl<'a> State<'a> { always_search: false, fn_lib, scope_level: 0, + operations: 0, } } /// Does a certain script-defined function exist in the `State`? @@ -304,6 +313,8 @@ pub struct Engine { pub(crate) print: Box, /// Closure for implementing the `debug` command. pub(crate) debug: Box, + /// Closure for progress reporting. + pub(crate) progress: Option>, /// Optimize the AST after compilation. pub(crate) optimization_level: OptimizationLevel, @@ -311,6 +322,8 @@ pub struct Engine { /// /// Defaults to 28 for debug builds and 256 for non-debug builds. pub(crate) max_call_stack_depth: usize, + /// Maximum number of operations to run. + pub(crate) max_operations: Option, } impl Default for Engine { @@ -333,6 +346,9 @@ impl Default for Engine { print: Box::new(default_print), debug: Box::new(default_print), + // progress callback + progress: None, + // optimization level #[cfg(feature = "no_optimize")] optimization_level: OptimizationLevel::None, @@ -346,6 +362,7 @@ impl Default for Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_operations: None, }; #[cfg(feature = "no_stdlib")] @@ -471,6 +488,7 @@ impl Engine { type_names: Default::default(), print: Box::new(|_| {}), debug: Box::new(|_| {}), + progress: None, #[cfg(feature = "no_optimize")] optimization_level: OptimizationLevel::None, @@ -484,6 +502,7 @@ impl Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_operations: None, } } @@ -515,10 +534,17 @@ impl Engine { /// Set the maximum levels of function calls allowed for a script in order to avoid /// infinite recursion and stack overflows. + #[cfg(not(feature = "unchecked"))] pub fn set_max_call_levels(&mut self, levels: usize) { self.max_call_stack_depth = levels } + /// Set the maximum number of operations allowed for a script to run to avoid + /// consuming too much resources (0 for unlimited). + #[cfg(not(feature = "unchecked"))] + pub fn set_max_operations(&mut self, operations: u64) { + self.max_operations = NonZeroU64::new(operations); + } /// Set the module resolution service used by the `Engine`. /// /// Not available under the `no_module` feature. @@ -537,7 +563,7 @@ impl Engine { pub(crate) fn call_fn_raw( &self, scope: Option<&mut Scope>, - state: &State, + state: &mut State, fn_name: &str, hashes: (u64, u64), args: &mut FnCallArgs, @@ -546,6 +572,8 @@ impl Engine { pos: Position, level: usize, ) -> Result<(Dynamic, bool), Box> { + self.inc_operations(state, pos)?; + // Check for stack overflow if level > self.max_call_stack_depth { return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos))); @@ -554,9 +582,12 @@ impl Engine { // First search in script-defined functions (can override built-in) if hashes.1 > 0 { if let Some(fn_def) = state.get_function(hashes.1) { - return self - .call_script_fn(scope, state, fn_name, fn_def, args, pos, level) - .map(|v| (v, false)); + let ops = state.operations; + let (result, operations) = + self.call_script_fn(scope, &state, fn_name, fn_def, args, pos, level, ops)?; + state.operations = operations; + self.inc_operations(state, pos)?; + return Ok((result, false)); } } @@ -677,14 +708,17 @@ impl Engine { args: &mut FnCallArgs, pos: Position, level: usize, - ) -> Result> { + operations: u64, + ) -> Result<(Dynamic, u64), Box> { match scope { // Extern scope passed in which is not empty Some(scope) if scope.len() > 0 => { let scope_len = scope.len(); - let mut state = State::new(state.fn_lib); + let mut local_state = State::new(state.fn_lib); - state.scope_level += 1; + local_state.operations = operations; + self.inc_operations(&mut local_state, pos)?; + local_state.scope_level += 1; // Put arguments into scope as variables scope.extend( @@ -696,14 +730,14 @@ impl Engine { args.into_iter().map(|v| mem::take(*v)), ) .map(|(name, value)| { - let var_name = unsafe_cast_var_name(name.as_str(), &state); + let var_name = unsafe_cast_var_name(name.as_str(), &local_state); (var_name, ScopeEntryType::Normal, value) }), ); // Evaluate the function at one higher level of call depth let result = self - .eval_stmt(scope, &mut state, &fn_def.body, level + 1) + .eval_stmt(scope, &mut local_state, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -725,13 +759,16 @@ impl Engine { // No need to reset `state.scope_level` because it is thrown away scope.rewind(scope_len); - return result; + return result.map(|v| (v, local_state.operations)); } // No new scope - create internal scope _ => { let mut scope = Scope::new(); - let mut state = State::new(state.fn_lib); - state.scope_level += 1; + let mut local_state = State::new(state.fn_lib); + + local_state.operations = operations; + self.inc_operations(&mut local_state, pos)?; + local_state.scope_level += 1; // Put arguments into scope as variables scope.extend( @@ -748,7 +785,7 @@ impl Engine { // Evaluate the function at one higher level of call depth // No need to reset `state.scope_level` because it is thrown away return self - .eval_stmt(&mut scope, &mut state, &fn_def.body, level + 1) + .eval_stmt(&mut scope, &mut local_state, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -764,7 +801,8 @@ impl Engine { err, pos, ))), - }); + }) + .map(|v| (v, local_state.operations)); } } } @@ -788,7 +826,7 @@ impl Engine { /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! fn exec_fn_call( &self, - state: &State, + state: &mut State, fn_name: &str, hash_fn_def: u64, args: &mut FnCallArgs, @@ -827,7 +865,7 @@ impl Engine { fn eval_script_expr( &self, scope: &mut Scope, - state: &State, + state: &mut State, script: &Dynamic, pos: Position, ) -> Result> { @@ -854,14 +892,20 @@ impl Engine { let ast = AST::new(statements, state.fn_lib.clone()); // Evaluate the AST - self.eval_ast_with_scope_raw(scope, &ast) - .map_err(|err| err.new_position(pos)) + let (result, operations) = self + .eval_ast_with_scope_raw(scope, &ast) + .map_err(|err| err.new_position(pos))?; + + state.operations += operations; + self.inc_operations(state, pos)?; + + return Ok(result); } /// Chain-evaluate a dot/index chain. fn eval_dot_index_chain_helper( &self, - state: &State, + state: &mut State, mut target: Target, rhs: &Expr, idx_values: &mut StaticVec, @@ -1145,7 +1189,7 @@ impl Engine { /// Get the value at the indexed position of a base type fn get_indexed_mut<'a>( &self, - state: &State, + state: &mut State, val: &'a mut Dynamic, is_ref: bool, mut idx: Dynamic, @@ -1153,6 +1197,8 @@ impl Engine { op_pos: Position, create: bool, ) -> Result, Box> { + self.inc_operations(state, op_pos)?; + match val { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(arr)) => { @@ -1234,6 +1280,8 @@ impl Engine { rhs: &Expr, level: usize, ) -> Result> { + self.inc_operations(state, rhs.position())?; + let mut lhs_value = self.eval_expr(scope, state, lhs, level)?; let rhs_value = self.eval_expr(scope, state, rhs, level)?; @@ -1288,6 +1336,8 @@ impl Engine { expr: &Expr, level: usize, ) -> Result> { + self.inc_operations(state, expr.position())?; + match expr { Expr::IntegerConstant(x) => Ok(x.0.into()), #[cfg(not(feature = "no_float"))] @@ -1461,9 +1511,16 @@ impl Engine { // First search in script-defined functions (can override built-in) if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { - self.call_script_fn(None, state, name, fn_def, args.as_mut(), *pos, level) + let args = args.as_mut(); + let ops = state.operations; + let (result, operations) = + self.call_script_fn(None, state, name, fn_def, args, *pos, level, ops)?; + state.operations = operations; + self.inc_operations(state, *pos)?; + Ok(result) } else { // Then search in Rust functions + self.inc_operations(state, *pos)?; // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -1538,6 +1595,8 @@ impl Engine { stmt: &Stmt, level: usize, ) -> Result> { + self.inc_operations(state, stmt.position())?; + match stmt { // No-op Stmt::Noop(_) => Ok(Default::default()), @@ -1546,11 +1605,10 @@ impl Engine { Stmt::Expr(expr) => { let result = self.eval_expr(scope, state, expr, level)?; - Ok(if let Expr::Assignment(_) = *expr.as_ref() { + Ok(match expr.as_ref() { // If it is an assignment, erase the result at the root - Default::default() - } else { - result + Expr::Assignment(_) => Default::default(), + _ => result, }) } @@ -1574,25 +1632,32 @@ impl Engine { } // If-else statement - Stmt::IfThenElse(x) => self - .eval_expr(scope, state, &x.0, level)? - .as_bool() - .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(x.0.position()))) - .and_then(|guard_val| { - if guard_val { - self.eval_stmt(scope, state, &x.1, level) - } else if let Some(stmt) = &x.2 { - self.eval_stmt(scope, state, stmt, level) - } else { - Ok(Default::default()) - } - }), + Stmt::IfThenElse(x) => { + let (expr, if_block, else_block) = x.as_ref(); + + self.eval_expr(scope, state, expr, level)? + .as_bool() + .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(expr.position()))) + .and_then(|guard_val| { + if guard_val { + self.eval_stmt(scope, state, if_block, level) + } else if let Some(stmt) = else_block { + self.eval_stmt(scope, state, stmt, level) + } else { + Ok(Default::default()) + } + }) + } // While loop Stmt::While(x) => loop { - match self.eval_expr(scope, state, &x.0, level)?.as_bool() { - Ok(true) => match self.eval_stmt(scope, state, &x.1, level) { - Ok(_) => (), + let (expr, body) = x.as_ref(); + + match self.eval_expr(scope, state, expr, level)?.as_bool() { + Ok(true) => match self.eval_stmt(scope, state, body, level) { + Ok(_) => { + self.inc_operations(state, body.position())?; + } Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()), @@ -1600,14 +1665,18 @@ impl Engine { }, }, Ok(false) => return Ok(Default::default()), - Err(_) => return Err(Box::new(EvalAltResult::ErrorLogicGuard(x.0.position()))), + Err(_) => { + return Err(Box::new(EvalAltResult::ErrorLogicGuard(expr.position()))) + } } }, // Loop statement Stmt::Loop(body) => loop { match self.eval_stmt(scope, state, body, level) { - Ok(_) => (), + Ok(_) => { + self.inc_operations(state, body.position())?; + } Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()), @@ -1618,7 +1687,8 @@ impl Engine { // For loop Stmt::For(x) => { - let iter_type = self.eval_expr(scope, state, &x.1, level)?; + let (name, expr, stmt) = x.as_ref(); + let iter_type = self.eval_expr(scope, state, expr, level)?; let tid = iter_type.type_id(); if let Some(iter_fn) = self @@ -1627,15 +1697,16 @@ impl Engine { .or_else(|| self.packages.get_iter(tid)) { // Add the loop variable - let var_name = unsafe_cast_var_name(&x.0, &state); + let var_name = unsafe_cast_var_name(name, &state); scope.push(var_name, ()); let index = scope.len() - 1; state.scope_level += 1; for loop_var in iter_fn(iter_type) { *scope.get_mut(index).0 = loop_var; + self.inc_operations(state, stmt.position())?; - match self.eval_stmt(scope, state, &x.2, level) { + match self.eval_stmt(scope, state, stmt, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1690,26 +1761,29 @@ impl Engine { // Let statement Stmt::Let(x) if x.1.is_some() => { - let ((var_name, _), expr) = x.as_ref(); + let ((var_name, pos), expr) = x.as_ref(); let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; let var_name = unsafe_cast_var_name(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); + self.inc_operations(state, *pos)?; Ok(Default::default()) } Stmt::Let(x) => { - let ((var_name, _), _) = x.as_ref(); + let ((var_name, pos), _) = x.as_ref(); let var_name = unsafe_cast_var_name(var_name, &state); scope.push(var_name, ()); + self.inc_operations(state, *pos)?; Ok(Default::default()) } // Const statement Stmt::Const(x) if x.1.is_constant() => { - let ((var_name, _), expr) = x.as_ref(); + let ((var_name, pos), expr) = x.as_ref(); let val = self.eval_expr(scope, state, &expr, level)?; let var_name = unsafe_cast_var_name(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); + self.inc_operations(state, *pos)?; Ok(Default::default()) } @@ -1718,7 +1792,7 @@ impl Engine { // Import statement Stmt::Import(x) => { - let (expr, (name, _)) = x.as_ref(); + let (expr, (name, pos)) = x.as_ref(); #[cfg(feature = "no_module")] unreachable!(); @@ -1736,6 +1810,7 @@ impl Engine { let mod_name = unsafe_cast_var_name(name, &state); scope.push_module(mod_name, module); + self.inc_operations(state, *pos)?; Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorModuleNotFound( @@ -1776,6 +1851,31 @@ impl Engine { } } + /// Check if the number of operations stay within limit. + fn inc_operations(&self, state: &mut State, pos: Position) -> Result<(), Box> { + state.operations += 1; + + #[cfg(not(feature = "unchecked"))] + { + // Guard against too many operations + if let Some(max) = self.max_operations { + if state.operations > max.get() { + return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos))); + } + } + } + + // Report progress - only in steps + if let Some(progress) = self.progress.as_ref() { + if !progress(state.operations) { + // Terminate script if progress returns false + return Err(Box::new(EvalAltResult::ErrorTerminated(pos))); + } + } + + Ok(()) + } + /// Map a type_name into a pretty-print name pub(crate) fn map_type_name<'a>(&'a self, name: &'a str) -> &'a str { self.type_names diff --git a/src/fn_native.rs b/src/fn_native.rs index a8daac5f..335f9919 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -20,6 +20,11 @@ pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; #[cfg(not(feature = "sync"))] pub type PrintCallback = dyn Fn(&str) + 'static; +#[cfg(feature = "sync")] +pub type ProgressCallback = dyn Fn(u64) -> bool + Send + Sync + 'static; +#[cfg(not(feature = "sync"))] +pub type ProgressCallback = dyn Fn(u64) -> bool + 'static; + // Define callback function types #[cfg(feature = "sync")] pub trait ObjectGetCallback: Fn(&mut T) -> U + Send + Sync + 'static {} diff --git a/src/result.rs b/src/result.rs index c33c810b..455a67ce 100644 --- a/src/result.rs +++ b/src/result.rs @@ -79,8 +79,12 @@ pub enum EvalAltResult { ErrorDotExpr(String, Position), /// Arithmetic error encountered. Wrapped value is the error message. ErrorArithmetic(String, Position), + /// Number of operations over maximum limit. + ErrorTooManyOperations(Position), /// Call stack over maximum limit. ErrorStackOverflow(Position), + /// The script is prematurely terminated. + ErrorTerminated(Position), /// Run-time error encountered. Wrapped value is the error message. ErrorRuntime(String, Position), @@ -137,7 +141,9 @@ impl EvalAltResult { Self::ErrorInExpr(_) => "Malformed 'in' expression", Self::ErrorDotExpr(_, _) => "Malformed dot expression", Self::ErrorArithmetic(_, _) => "Arithmetic error", + Self::ErrorTooManyOperations(_) => "Too many operations", Self::ErrorStackOverflow(_) => "Stack overflow", + Self::ErrorTerminated(_) => "Script terminated.", Self::ErrorRuntime(_, _) => "Runtime error", Self::ErrorLoopBreak(true, _) => "Break statement not inside a loop", Self::ErrorLoopBreak(false, _) => "Continue statement not inside a loop", @@ -183,7 +189,9 @@ impl fmt::Display for EvalAltResult { | Self::ErrorAssignmentToUnknownLHS(pos) | Self::ErrorInExpr(pos) | Self::ErrorDotExpr(_, pos) - | Self::ErrorStackOverflow(pos) => write!(f, "{} ({})", desc, pos), + | Self::ErrorTooManyOperations(pos) + | Self::ErrorStackOverflow(pos) + | Self::ErrorTerminated(pos) => write!(f, "{} ({})", desc, pos), Self::ErrorRuntime(s, pos) => { write!(f, "{} ({})", if s.is_empty() { desc } else { s }, pos) @@ -299,7 +307,9 @@ impl EvalAltResult { | Self::ErrorInExpr(pos) | Self::ErrorDotExpr(_, pos) | Self::ErrorArithmetic(_, pos) + | Self::ErrorTooManyOperations(pos) | Self::ErrorStackOverflow(pos) + | Self::ErrorTerminated(pos) | Self::ErrorRuntime(_, pos) | Self::ErrorLoopBreak(_, pos) | Self::Return(_, pos) => *pos, @@ -335,7 +345,9 @@ impl EvalAltResult { | Self::ErrorInExpr(pos) | Self::ErrorDotExpr(_, pos) | Self::ErrorArithmetic(_, pos) + | Self::ErrorTooManyOperations(pos) | Self::ErrorStackOverflow(pos) + | Self::ErrorTerminated(pos) | Self::ErrorRuntime(_, pos) | Self::ErrorLoopBreak(_, pos) | Self::Return(_, pos) => *pos = new_position, diff --git a/tests/operations.rs b/tests/operations.rs new file mode 100644 index 00000000..2637ef3b --- /dev/null +++ b/tests/operations.rs @@ -0,0 +1,93 @@ +#![cfg(not(feature = "unchecked"))] +use rhai::{Engine, EvalAltResult}; + +#[test] +fn test_max_operations() -> Result<(), Box> { + let mut engine = Engine::new(); + engine.set_max_operations(500); + + engine.on_progress(|count| { + if count % 100 == 0 { + println!("{}", count); + } + true + }); + + engine.eval::<()>("let x = 0; while x < 20 { x += 1; }")?; + + assert!(matches!( + *engine + .eval::<()>("for x in range(0, 500) {}") + .expect_err("should error"), + EvalAltResult::ErrorTooManyOperations(_) + )); + + engine.set_max_operations(0); + + engine.eval::<()>("for x in range(0, 10000) {}")?; + + Ok(()) +} + +#[test] +fn test_max_operations_functions() -> Result<(), Box> { + let mut engine = Engine::new(); + engine.set_max_operations(500); + + engine.on_progress(|count| { + if count % 100 == 0 { + println!("{}", count); + } + true + }); + + engine.eval::<()>( + r#" + fn inc(x) { x + 1 } + let x = 0; + while x < 20 { x = inc(x); } + "#, + )?; + + assert!(matches!( + *engine + .eval::<()>( + r#" + fn inc(x) { x + 1 } + let x = 0; + while x < 1000 { x = inc(x); } + "# + ) + .expect_err("should error"), + EvalAltResult::ErrorTooManyOperations(_) + )); + + Ok(()) +} + +#[test] +fn test_max_operations_eval() -> Result<(), Box> { + let mut engine = Engine::new(); + engine.set_max_operations(500); + + engine.on_progress(|count| { + if count % 100 == 0 { + println!("{}", count); + } + true + }); + + assert!(matches!( + *engine + .eval::<()>( + r#" + let script = "for x in range(0, 500) {}"; + eval(script); + "# + ) + .expect_err("should error"), + EvalAltResult::ErrorTooManyOperations(_) + )); + + Ok(()) +} diff --git a/tests/stack.rs b/tests/stack.rs index 1eeb4250..5ebb550b 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -15,6 +15,7 @@ fn test_stack_overflow() -> Result<(), Box> { 325 ); + #[cfg(not(feature = "unchecked"))] match engine.eval::<()>( r" fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } } From be97047e512c212bad513557d249bd967d5d26fa Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 15 May 2020 21:40:54 +0800 Subject: [PATCH 21/36] Limit modules loading. --- README.md | 20 ++++++++-- src/any.rs | 8 ++-- src/api.rs | 6 +-- src/engine.rs | 95 ++++++++++++++++++++++++++------------------- src/fn_func.rs | 2 +- src/fn_native.rs | 4 +- src/fn_register.rs | 6 +-- src/lib.rs | 1 + src/module.rs | 8 ++-- src/optimize.rs | 1 - src/packages/mod.rs | 4 +- src/result.rs | 6 +++ src/scope.rs | 2 +- src/unsafe.rs | 2 - tests/modules.rs | 51 +++++++++++++++++++++++- 15 files changed, 147 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index c602fa92..7edc5667 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ Rhai's current features set: * Relatively little `unsafe` code (yes there are some for performance reasons) * Sand-boxed (the scripting [`Engine`] can be declared immutable which cannot mutate the containing environment unless explicitly allowed via `RefCell` etc.) -* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations)) -* Able to set limits on script resource usage (e.g. see [tracking progress](#tracking-progress)) +* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.) +* Able to track script evaluation [progress](#tracking-progress) and manually terminate a script run * [`no-std`](#optional-features) support * [Function overloading](#function-overloading) * [Operator overloading](#operator-overloading) @@ -2268,6 +2268,7 @@ The most important resources to watch out for are: floating-point representations, in order to crash the system. * **Files**: A malignant script may continuously `import` an external module within an infinite loop, thereby putting heavy load on the file-system (or even the network if the file is not local). + Even when modules are not created from files, they still typically consume a lot of resources to load. * **Data**: A malignant script may attempt to read from and/or write to data that it does not own. If this happens, it is a severe security breach and may put the entire system at risk. @@ -2319,6 +2320,19 @@ engine.on_progress(|count| { // 'count' is the number of operatio The closure passed to `Engine::on_progress` will be called once every operation. Return `false` to terminate the script immediately. +### Maximum number of modules + +Rhai by default does not limit how many [modules] are loaded via the `import` statement. +This can be changed via the `Engine::set_max_modules` method, with zero being unlimited (the default). + +```rust +let mut engine = Engine::new(); + +engine.set_max_modules(5); // allow loading only up to 5 modules + +engine.set_max_modules(0); // allow unlimited modules +``` + ### Maximum stack depth Rhai by default limits function calls to a maximum depth of 256 levels (28 levels in debug build). @@ -2646,7 +2660,7 @@ let x = eval("40 + 2"); // 'eval' here throws "eval is evil! I refuse to run Or override it from Rust: ```rust -fn alt_eval(script: String) -> Result<(), EvalAltResult> { +fn alt_eval(script: String) -> Result<(), Box> { Err(format!("eval is evil! I refuse to run {}", script).into()) } diff --git a/src/any.rs b/src/any.rs index 483eee84..7f209685 100644 --- a/src/any.rs +++ b/src/any.rs @@ -19,7 +19,7 @@ use crate::stdlib::{ any::{type_name, Any, TypeId}, boxed::Box, collections::HashMap, - fmt, mem, ptr, + fmt, string::String, vec::Vec, }; @@ -27,7 +27,7 @@ use crate::stdlib::{ #[cfg(not(feature = "no_std"))] use crate::stdlib::time::Instant; -/// A trait to represent any type. +/// Trait to represent any type. /// /// Currently, `Variant` is not `Send` nor `Sync`, so it can practically be any type. /// Turn on the `sync` feature to restrict it to only types that implement `Send + Sync`. @@ -81,7 +81,7 @@ impl Variant for T { } } -/// A trait to represent any type. +/// Trait to represent any type. /// /// `From<_>` is implemented for `i64` (`i32` if `only_i32`), `f64` (if not `no_float`), /// `bool`, `String`, `char`, `Vec` (into `Array`) and `HashMap` (into `Map`). @@ -142,7 +142,7 @@ impl dyn Variant { } } -/// A dynamic type containing any value. +/// Dynamic type containing any value. pub struct Dynamic(pub(crate) Union); /// Internal `Dynamic` representation. diff --git a/src/api.rs b/src/api.rs index 78ceaf72..5e144bcf 100644 --- a/src/api.rs +++ b/src/api.rs @@ -21,7 +21,6 @@ use crate::engine::Map; use crate::stdlib::{ any::{type_name, TypeId}, boxed::Box, - collections::HashMap, mem, string::{String, ToString}, }; @@ -1016,11 +1015,10 @@ impl Engine { .get_function_by_signature(name, args.len(), true) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?; - let mut state = State::new(fn_lib); + let state = State::new(fn_lib); let args = args.as_mut(); - let (result, _) = - self.call_script_fn(Some(scope), &mut state, name, fn_def, args, pos, 0, 0)?; + let (result, _) = self.call_script_fn(Some(scope), state, name, fn_def, args, pos, 0)?; let return_type = self.map_type_name(result.type_name()); diff --git a/src/engine.rs b/src/engine.rs index 6f159487..6423ca6b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -22,7 +22,6 @@ use crate::parser::ModuleRef; use crate::stdlib::{ any::TypeId, - borrow::Cow, boxed::Box, collections::HashMap, format, @@ -36,13 +35,13 @@ use crate::stdlib::{ vec::Vec, }; -/// An dynamic array of `Dynamic` values. +/// Variable-sized array of `Dynamic` values. /// /// Not available under the `no_index` feature. #[cfg(not(feature = "no_index"))] pub type Array = Vec; -/// An dynamic hash map of `Dynamic` values with `String` keys. +/// Hash map of `Dynamic` values with `String` keys. /// /// Not available under the `no_object` feature. #[cfg(not(feature = "no_object"))] @@ -154,16 +153,20 @@ pub struct State<'a> { /// Number of operations performed. pub operations: u64, + + /// Number of modules loaded. + pub modules: u64, } impl<'a> State<'a> { /// Create a new `State`. pub fn new(fn_lib: &'a FunctionsLib) -> Self { Self { - always_search: false, fn_lib, + always_search: false, scope_level: 0, operations: 0, + modules: 0, } } /// Does a certain script-defined function exist in the `State`? @@ -322,8 +325,10 @@ pub struct Engine { /// /// Defaults to 28 for debug builds and 256 for non-debug builds. pub(crate) max_call_stack_depth: usize, - /// Maximum number of operations to run. + /// Maximum number of operations allowed to run. pub(crate) max_operations: Option, + /// Maximum number of modules allowed to load. + pub(crate) max_modules: Option, } impl Default for Engine { @@ -363,6 +368,7 @@ impl Default for Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_operations: None, + max_modules: None, }; #[cfg(feature = "no_stdlib")] @@ -503,6 +509,7 @@ impl Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_operations: None, + max_modules: None, } } @@ -545,6 +552,13 @@ impl Engine { pub fn set_max_operations(&mut self, operations: u64) { self.max_operations = NonZeroU64::new(operations); } + + /// Set the maximum number of imported modules allowed for a script (0 for unlimited). + #[cfg(not(feature = "unchecked"))] + pub fn set_max_modules(&mut self, modules: u64) { + self.max_modules = NonZeroU64::new(modules); + } + /// Set the module resolution service used by the `Engine`. /// /// Not available under the `no_module` feature. @@ -582,11 +596,9 @@ impl Engine { // First search in script-defined functions (can override built-in) if hashes.1 > 0 { if let Some(fn_def) = state.get_function(hashes.1) { - let ops = state.operations; - let (result, operations) = - self.call_script_fn(scope, &state, fn_name, fn_def, args, pos, level, ops)?; - state.operations = operations; - self.inc_operations(state, pos)?; + let (result, state2) = + self.call_script_fn(scope, *state, fn_name, fn_def, args, pos, level)?; + *state = state2; return Ok((result, false)); } } @@ -701,24 +713,23 @@ impl Engine { /// **DO NOT** reuse the argument values unless for the first `&mut` argument - all others are silently replaced by `()`! pub(crate) fn call_script_fn<'s>( &self, - scope: Option<&mut Scope<'s>>, - state: &State, + scope: Option<&mut Scope>, + mut state: State<'s>, fn_name: &str, fn_def: &FnDef, args: &mut FnCallArgs, pos: Position, level: usize, - operations: u64, - ) -> Result<(Dynamic, u64), Box> { + ) -> Result<(Dynamic, State<'s>), Box> { + self.inc_operations(&mut state, pos)?; + + let orig_scope_level = state.scope_level; + state.scope_level += 1; + match scope { // Extern scope passed in which is not empty Some(scope) if scope.len() > 0 => { let scope_len = scope.len(); - let mut local_state = State::new(state.fn_lib); - - local_state.operations = operations; - self.inc_operations(&mut local_state, pos)?; - local_state.scope_level += 1; // Put arguments into scope as variables scope.extend( @@ -730,14 +741,14 @@ impl Engine { args.into_iter().map(|v| mem::take(*v)), ) .map(|(name, value)| { - let var_name = unsafe_cast_var_name(name.as_str(), &local_state); + let var_name = unsafe_cast_var_name(name.as_str(), &mut state); (var_name, ScopeEntryType::Normal, value) }), ); // Evaluate the function at one higher level of call depth let result = self - .eval_stmt(scope, &mut local_state, &fn_def.body, level + 1) + .eval_stmt(scope, &mut state, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -756,19 +767,14 @@ impl Engine { }); // Remove all local variables - // No need to reset `state.scope_level` because it is thrown away scope.rewind(scope_len); + state.scope_level = orig_scope_level; - return result.map(|v| (v, local_state.operations)); + return result.map(|v| (v, state)); } // No new scope - create internal scope _ => { let mut scope = Scope::new(); - let mut local_state = State::new(state.fn_lib); - - local_state.operations = operations; - self.inc_operations(&mut local_state, pos)?; - local_state.scope_level += 1; // Put arguments into scope as variables scope.extend( @@ -783,9 +789,8 @@ impl Engine { ); // Evaluate the function at one higher level of call depth - // No need to reset `state.scope_level` because it is thrown away - return self - .eval_stmt(&mut scope, &mut local_state, &fn_def.body, level + 1) + let result = self + .eval_stmt(&mut scope, &mut state, &fn_def.body, level + 1) .or_else(|err| match *err { // Convert return statement to return value EvalAltResult::Return(x, _) => Ok(x), @@ -801,8 +806,10 @@ impl Engine { err, pos, ))), - }) - .map(|v| (v, local_state.operations)); + }); + + state.scope_level = orig_scope_level; + return result.map(|v| (v, state)); } } } @@ -1512,11 +1519,9 @@ impl Engine { // First search in script-defined functions (can override built-in) if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { let args = args.as_mut(); - let ops = state.operations; - let (result, operations) = - self.call_script_fn(None, state, name, fn_def, args, *pos, level, ops)?; - state.operations = operations; - self.inc_operations(state, *pos)?; + let (result, state2) = + self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; + *state = state2; Ok(result) } else { // Then search in Rust functions @@ -1792,13 +1797,20 @@ impl Engine { // Import statement Stmt::Import(x) => { - let (expr, (name, pos)) = x.as_ref(); - #[cfg(feature = "no_module")] unreachable!(); #[cfg(not(feature = "no_module"))] { + let (expr, (name, pos)) = x.as_ref(); + + // Guard against too many modules + if let Some(max) = self.max_modules { + if state.modules >= max.get() { + return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); + } + } + if let Some(path) = self .eval_expr(scope, state, &expr, level)? .try_cast::() @@ -1810,7 +1822,10 @@ impl Engine { let mod_name = unsafe_cast_var_name(name, &state); scope.push_module(mod_name, module); + + state.modules += 1; self.inc_operations(state, *pos)?; + Ok(Default::default()) } else { Err(Box::new(EvalAltResult::ErrorModuleNotFound( diff --git a/src/fn_func.rs b/src/fn_func.rs index e619ebdf..f606b794 100644 --- a/src/fn_func.rs +++ b/src/fn_func.rs @@ -11,7 +11,7 @@ use crate::scope::Scope; use crate::stdlib::{boxed::Box, string::ToString}; -/// A trait to create a Rust anonymous function from a script. +/// Trait to create a Rust anonymous function from a script. pub trait Func { type Output; diff --git a/src/fn_native.rs b/src/fn_native.rs index 335f9919..7a1dd0e6 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -82,7 +82,7 @@ pub enum NativeFunctionABI { Method, } -/// A trait implemented by all native Rust functions that are callable by Rhai. +/// Trait implemented by all native Rust functions that are callable by Rhai. #[cfg(not(feature = "sync"))] pub trait NativeCallable { /// Get the ABI type of a native Rust function. @@ -91,7 +91,7 @@ pub trait NativeCallable { fn call(&self, args: &mut FnCallArgs) -> Result>; } -/// A trait implemented by all native Rust functions that are callable by Rhai. +/// Trait implemented by all native Rust functions that are callable by Rhai. #[cfg(feature = "sync")] pub trait NativeCallable: Send + Sync { /// Get the ABI type of a native Rust function. diff --git a/src/fn_register.rs b/src/fn_register.rs index 8b91ea42..42e81c9b 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -10,7 +10,7 @@ use crate::result::EvalAltResult; use crate::stdlib::{any::TypeId, boxed::Box, mem, string::ToString}; -/// A trait to register custom functions with the `Engine`. +/// Trait to register custom functions with the `Engine`. pub trait RegisterFn { /// Register a custom function with the `Engine`. /// @@ -42,7 +42,7 @@ pub trait RegisterFn { fn register_fn(&mut self, name: &str, f: FN); } -/// A trait to register custom functions that return `Dynamic` values with the `Engine`. +/// Trait to register custom functions that return `Dynamic` values with the `Engine`. pub trait RegisterDynamicFn { /// Register a custom function returning `Dynamic` values with the `Engine`. /// @@ -69,7 +69,7 @@ pub trait RegisterDynamicFn { fn register_dynamic_fn(&mut self, name: &str, f: FN); } -/// A trait to register fallible custom functions returning `Result<_, Box>` with the `Engine`. +/// Trait to register fallible custom functions returning `Result<_, Box>` with the `Engine`. pub trait RegisterResultFn { /// Register a custom fallible function with the `Engine`. /// diff --git a/src/lib.rs b/src/lib.rs index f5f1ad87..203a6dfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ pub use parser::FLOAT; #[cfg(not(feature = "no_module"))] pub use module::ModuleResolver; +/// Module containing all built-in _module resolvers_ available to Rhai. #[cfg(not(feature = "no_module"))] pub mod module_resolvers { pub use crate::module::resolvers::*; diff --git a/src/module.rs b/src/module.rs index 6e56a128..f65428a0 100644 --- a/src/module.rs +++ b/src/module.rs @@ -769,7 +769,7 @@ impl ModuleRef { } } -/// A trait that encapsulates a module resolution service. +/// Trait that encapsulates a module resolution service. #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "sync"))] pub trait ModuleResolver { @@ -783,7 +783,7 @@ pub trait ModuleResolver { ) -> Result>; } -/// A trait that encapsulates a module resolution service. +/// Trait that encapsulates a module resolution service. #[cfg(not(feature = "no_module"))] #[cfg(feature = "sync")] pub trait ModuleResolver: Send + Sync { @@ -812,7 +812,7 @@ mod file { use super::*; use crate::stdlib::path::PathBuf; - /// A module resolution service that loads module script files from the file system. + /// Module resolution service that loads module script files from the file system. /// /// The `new_with_path` and `new_with_path_and_extension` constructor functions /// allow specification of a base directory with module path used as a relative path offset @@ -949,7 +949,7 @@ mod file { mod stat { use super::*; - /// A module resolution service that serves modules added into it. + /// Module resolution service that serves modules added into it. /// /// # Examples /// diff --git a/src/optimize.rs b/src/optimize.rs index df385b74..04026059 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -13,7 +13,6 @@ use crate::token::Position; use crate::stdlib::{ boxed::Box, - collections::HashMap, iter::empty, string::{String, ToString}, vec, diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 5c2a6dd5..7c146f7f 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,4 +1,4 @@ -//! This module contains all built-in _packages_ available to Rhai, plus facilities to define custom packages. +//! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. use crate::fn_native::{NativeCallable, SharedIteratorFunction}; use crate::module::Module; @@ -89,7 +89,7 @@ impl PackagesCollection { } } -/// This macro makes it easy to define a _package_ (which is basically a shared module) +/// Macro that makes it easy to define a _package_ (which is basically a shared module) /// and register functions into it. /// /// Functions can be added to the package using the standard module methods such as diff --git a/src/result.rs b/src/result.rs index 455a67ce..4ae0d9e5 100644 --- a/src/result.rs +++ b/src/result.rs @@ -81,6 +81,8 @@ pub enum EvalAltResult { ErrorArithmetic(String, Position), /// Number of operations over maximum limit. ErrorTooManyOperations(Position), + /// Modules over maximum limit. + ErrorTooManyModules(Position), /// Call stack over maximum limit. ErrorStackOverflow(Position), /// The script is prematurely terminated. @@ -142,6 +144,7 @@ impl EvalAltResult { Self::ErrorDotExpr(_, _) => "Malformed dot expression", Self::ErrorArithmetic(_, _) => "Arithmetic error", Self::ErrorTooManyOperations(_) => "Too many operations", + Self::ErrorTooManyModules(_) => "Too many modules imported", Self::ErrorStackOverflow(_) => "Stack overflow", Self::ErrorTerminated(_) => "Script terminated.", Self::ErrorRuntime(_, _) => "Runtime error", @@ -190,6 +193,7 @@ impl fmt::Display for EvalAltResult { | Self::ErrorInExpr(pos) | Self::ErrorDotExpr(_, pos) | Self::ErrorTooManyOperations(pos) + | Self::ErrorTooManyModules(pos) | Self::ErrorStackOverflow(pos) | Self::ErrorTerminated(pos) => write!(f, "{} ({})", desc, pos), @@ -308,6 +312,7 @@ impl EvalAltResult { | Self::ErrorDotExpr(_, pos) | Self::ErrorArithmetic(_, pos) | Self::ErrorTooManyOperations(pos) + | Self::ErrorTooManyModules(pos) | Self::ErrorStackOverflow(pos) | Self::ErrorTerminated(pos) | Self::ErrorRuntime(_, pos) @@ -346,6 +351,7 @@ impl EvalAltResult { | Self::ErrorDotExpr(_, pos) | Self::ErrorArithmetic(_, pos) | Self::ErrorTooManyOperations(pos) + | Self::ErrorTooManyModules(pos) | Self::ErrorStackOverflow(pos) | Self::ErrorTerminated(pos) | Self::ErrorRuntime(_, pos) diff --git a/src/scope.rs b/src/scope.rs index 17909970..1b9c8b1d 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -36,7 +36,7 @@ pub struct Entry<'a> { pub expr: Option>, } -/// A type containing information about the current scope. +/// Type containing information about the current scope. /// Useful for keeping state between `Engine` evaluation runs. /// /// # Example diff --git a/src/unsafe.rs b/src/unsafe.rs index 47d5b40c..65ae0b45 100644 --- a/src/unsafe.rs +++ b/src/unsafe.rs @@ -2,7 +2,6 @@ use crate::any::Variant; use crate::engine::State; -use crate::utils::StaticVec; use crate::stdlib::{ any::{Any, TypeId}, @@ -10,7 +9,6 @@ use crate::stdlib::{ boxed::Box, mem, ptr, string::ToString, - vec::Vec, }; /// Cast a type into another type. diff --git a/tests/modules.rs b/tests/modules.rs index acf3c0e6..569cb4af 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -72,13 +72,60 @@ fn test_module_resolver() -> Result<(), Box> { assert_eq!( engine.eval::( r#" - import "hello" as h; - h::answer + import "hello" as h1; + import "hello" as h2; + h2::answer "# )?, 42 ); + engine.set_max_modules(5); + + assert!(matches!( + *engine + .eval::<()>( + r#" + for x in range(0, 10) { + import "hello" as h; + } + "# + ) + .expect_err("should error"), + EvalAltResult::ErrorTooManyModules(_) + )); + + assert!(matches!( + *engine + .eval::<()>( + r#" + fn foo() { + import "hello" as h; + } + + for x in range(0, 10) { + foo(); + } + "# + ) + .expect_err("should error"), + EvalAltResult::ErrorInFunctionCall(fn_name, _, _) if fn_name == "foo" + )); + + engine.set_max_modules(0); + + engine.eval::<()>( + r#" + fn foo() { + import "hello" as h; + } + + for x in range(0, 10) { + foo(); + } + "#, + )?; + Ok(()) } From 0cb781c1aa11182fca627e774c4b3d417b442b54 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 16 May 2020 11:42:56 +0800 Subject: [PATCH 22/36] Avoid allocation in Target. --- README.md | 28 +++++++------ src/engine.rs | 103 +++++++++++++++++++++++++---------------------- src/unsafe.rs | 2 + tests/get_set.rs | 6 +-- 4 files changed, 75 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 7edc5667..0339ae85 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,15 @@ to add scripting to any application. Rhai's current features set: -* Easy-to-use language similar to JS+Rust -* Easy integration with Rust [native functions](#working-with-functions) and [types](#custom-types-and-methods), +* Easy-to-use language similar to JS+Rust with dynamic typing but _no_ garbage collector +* Tight integration with native Rust [functions](#working-with-functions) and [types](#custom-types-and-methods), including [getters/setters](#getters-and-setters), [methods](#members-and-methods) and [indexers](#indexers) +* Freely pass Rust variables/constants into a script via an external [`Scope`] * Easily [call a script-defined function](#calling-rhai-functions-from-rust) from Rust -* Freely pass variables/constants into a script via an external [`Scope`] -* Fairly efficient (1 million iterations in 0.75 sec on my 5 year old laptop) -* Low compile-time overhead (~0.6 sec debug/~3 sec release for script runner app) -* Relatively little `unsafe` code (yes there are some for performance reasons) +* Low compile-time overhead (~0.6 sec debug/~3 sec release for `rhai_runner` sample app) +* Fairly efficient evaluation (1 million iterations in 0.75 sec on my 5 year old laptop) +* Relatively little `unsafe` code (yes there are some for performance reasons, and all `unsafe` code is limited to + one single source file, all with names starting with `"unsafe_"`) * Sand-boxed (the scripting [`Engine`] can be declared immutable which cannot mutate the containing environment unless explicitly allowed via `RefCell` etc.) * Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.) @@ -70,20 +71,21 @@ Optional features | Feature | Description | | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `unchecked` | Exclude arithmetic checking (such as over-flows and division by zero), stack depth limit and operations count limit. Beware that a bad script may panic the entire system! | -| `no_function` | Disable script-defined functions if not needed. | -| `no_index` | Disable [arrays] and indexing features if not needed. | -| `no_object` | Disable support for custom types and objects. | -| `no_float` | Disable floating-point numbers and math if not needed. | +| `no_function` | Disable script-defined functions. | +| `no_index` | Disable [arrays] and indexing features. | +| `no_object` | Disable support for custom types and object maps. | +| `no_float` | Disable floating-point numbers and math. | | `no_optimize` | Disable the script optimizer. | | `no_module` | Disable modules. | | `only_i32` | Set the system integer type to `i32` and disable all other integer types. `INT` is set to `i32`. | | `only_i64` | Set the system integer type to `i64` and disable all other integer types. `INT` is set to `i64`. | | `no_std` | Build for `no-std`. Notice that additional dependencies will be pulled in to replace `std` features. | -| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, [`Engine`], [`Scope`] and `AST` are all `Send + Sync`. | +| `sync` | Restrict all values types to those that are `Send + Sync`. Under this feature, all Rhai types, including [`Engine`], [`Scope`] and `AST`, are all `Send + Sync`. | By default, Rhai includes all the standard functionalities in a small, tight package. Most features are here to opt-**out** of certain functionalities that are not needed. -Excluding unneeded functionalities can result in smaller, faster builds as well as less bugs due to a more restricted language. +Excluding unneeded functionalities can result in smaller, faster builds +as well as more control over what a script can (or cannot) do. [`unchecked`]: #optional-features [`no_index`]: #optional-features @@ -967,7 +969,7 @@ Indexers -------- Custom types can also expose an _indexer_ by registering an indexer function. -A custom with an indexer function defined can use the bracket '`[]`' notation to get a property value +A custom type with an indexer function defined can use the bracket '`[]`' notation to get a property value (but not update it - indexers are read-only). ```rust diff --git a/src/engine.rs b/src/engine.rs index 6423ca6b..2c9a3a82 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -72,19 +72,36 @@ enum Target<'a> { /// The target is a mutable reference to a `Dynamic` value somewhere. Ref(&'a mut Dynamic), /// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects). - Value(Box), + Value(Dynamic), /// The target is a character inside a String. /// This is necessary because directly pointing to a char inside a String is impossible. - StringChar(Box<(&'a mut Dynamic, usize, Dynamic)>), + StringChar(&'a mut Dynamic, usize, Dynamic), } impl Target<'_> { - /// Get the value of the `Target` as a `Dynamic`. + /// Is the `Target` a reference pointing to other data? + pub fn is_ref(&self) -> bool { + match self { + Target::Ref(_) => true, + Target::Value(_) | Target::StringChar(_, _, _) => false, + } + } + + /// Get the value of the `Target` as a `Dynamic`, cloning a referenced value if necessary. pub fn clone_into_dynamic(self) -> Dynamic { match self { - Target::Ref(r) => r.clone(), - Target::Value(v) => *v, - Target::StringChar(s) => s.2, + Target::Ref(r) => r.clone(), // Referenced value is cloned + Target::Value(v) => v, // Owned value is simply taken + Target::StringChar(_, _, ch) => ch, // Character is taken + } + } + + /// Get a mutable reference from the `Target`. + pub fn as_mut(&mut self) -> &mut Dynamic { + match self { + Target::Ref(r) => *r, + Target::Value(ref mut r) => r, + Target::StringChar(_, _, ref mut r) => r, } } @@ -95,25 +112,23 @@ impl Target<'_> { Target::Value(_) => { return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(pos))) } - Target::StringChar(x) => match x.0 { - Dynamic(Union::Str(s)) => { - // Replace the character at the specified index position - let new_ch = new_val - .as_char() - .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; + Target::StringChar(Dynamic(Union::Str(s)), index, _) => { + // Replace the character at the specified index position + let new_ch = new_val + .as_char() + .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; - let mut chars: StaticVec = s.chars().collect(); - let ch = *chars.get_ref(x.1); + let mut chars: StaticVec = s.chars().collect(); + let ch = *chars.get_ref(*index); - // See if changed - if so, update the String - if ch != new_ch { - *chars.get_mut(x.1) = new_ch; - s.clear(); - chars.iter().for_each(|&ch| s.push(ch)); - } + // See if changed - if so, update the String + if ch != new_ch { + *chars.get_mut(*index) = new_ch; + s.clear(); + chars.iter().for_each(|&ch| s.push(ch)); } - _ => unreachable!(), - }, + } + _ => unreachable!(), } Ok(()) @@ -127,7 +142,7 @@ impl<'a> From<&'a mut Dynamic> for Target<'a> { } impl> From for Target<'_> { fn from(value: T) -> Self { - Self::Value(Box::new(value.into())) + Self::Value(value.into()) } } @@ -913,7 +928,7 @@ impl Engine { fn eval_dot_index_chain_helper( &self, state: &mut State, - mut target: Target, + target: &mut Target, rhs: &Expr, idx_values: &mut StaticVec, is_index: bool, @@ -921,12 +936,10 @@ impl Engine { level: usize, mut new_val: Option, ) -> Result<(Dynamic, bool), Box> { + let is_ref = target.is_ref(); + // Get a reference to the mutation target Dynamic - let (obj, is_ref) = match target { - Target::Ref(r) => (r, true), - Target::Value(ref mut r) => (r.as_mut(), false), - Target::StringChar(ref mut x) => (&mut x.2, false), - }; + let obj = target.as_mut(); // Pop the last index value let mut idx_val = idx_values.pop(); @@ -937,20 +950,20 @@ impl Engine { Expr::Dot(x) | Expr::Index(x) => { let is_idx = matches!(rhs, Expr::Index(_)); let pos = x.0.position(); - let val = - self.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, false)?; + let this_ptr = &mut self + .get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, false)?; self.eval_dot_index_chain_helper( - state, val, &x.1, idx_values, is_idx, x.2, level, new_val, + state, this_ptr, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx[rhs] = new_val _ if new_val.is_some() => { let pos = rhs.position(); - let mut val = - self.get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, true)?; + let this_ptr = &mut self + .get_indexed_mut(state, obj, is_ref, idx_val, pos, op_pos, true)?; - val.set_value(new_val.unwrap(), rhs.position())?; + this_ptr.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // xxx[rhs] @@ -1019,7 +1032,7 @@ impl Engine { Expr::Index(x) | Expr::Dot(x) if obj.is::() => { let is_idx = matches!(rhs, Expr::Index(_)); - let val = if let Expr::Property(p) = &x.0 { + let mut val = if let Expr::Property(p) = &x.0 { let ((prop, _, _), _) = p.as_ref(); let index = prop.clone().into(); self.get_indexed_mut(state, obj, is_ref, index, x.2, op_pos, false)? @@ -1032,7 +1045,7 @@ impl Engine { }; self.eval_dot_index_chain_helper( - state, val, &x.1, idx_values, is_idx, x.2, level, new_val, + state, &mut val, &x.1, idx_values, is_idx, x.2, level, new_val, ) } // xxx.idx_lhs[idx_expr] | xxx.dot_lhs.rhs @@ -1051,16 +1064,10 @@ impl Engine { ))); }; let val = &mut val; + let target = &mut val.into(); let (result, may_be_changed) = self.eval_dot_index_chain_helper( - state, - val.into(), - &x.1, - idx_values, - is_idx, - x.2, - level, - new_val, + state, target, &x.1, idx_values, is_idx, x.2, level, new_val, )?; // Feed the value back via a setter just in case it has been updated @@ -1125,7 +1132,7 @@ impl Engine { ScopeEntryType::Constant | ScopeEntryType::Normal => (), } - let this_ptr = target.into(); + let this_ptr = &mut target.into(); self.eval_dot_index_chain_helper( state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) @@ -1140,7 +1147,7 @@ impl Engine { // {expr}.??? or {expr}[???] expr => { let val = self.eval_expr(scope, state, expr, level)?; - let this_ptr = val.into(); + let this_ptr = &mut val.into(); self.eval_dot_index_chain_helper( state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) @@ -1258,7 +1265,7 @@ impl Engine { let ch = s.chars().nth(offset).ok_or_else(|| { Box::new(EvalAltResult::ErrorStringBounds(chars_len, index, idx_pos)) })?; - Ok(Target::StringChar(Box::new((val, offset, ch.into())))) + Ok(Target::StringChar(val, offset, ch.into())) } else { Err(Box::new(EvalAltResult::ErrorStringBounds( chars_len, index, idx_pos, diff --git a/src/unsafe.rs b/src/unsafe.rs index 65ae0b45..64ba8904 100644 --- a/src/unsafe.rs +++ b/src/unsafe.rs @@ -56,6 +56,8 @@ pub fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { // this is safe because all local variables are cleared at the end of the block unsafe { mem::transmute::<_, &'s str>(name) }.into() } else { + // The variable is introduced at global (top) level and may persist after the script run. + // Therefore, clone the variable name. name.to_string().into() } } diff --git a/tests/get_set.rs b/tests/get_set.rs index 57712304..94c3064a 100644 --- a/tests/get_set.rs +++ b/tests/get_set.rs @@ -43,20 +43,20 @@ fn test_get_set() -> Result<(), Box> { engine.register_fn("new_ts", TestStruct::new); #[cfg(not(feature = "no_index"))] - engine.register_indexer(|value: &mut TestStruct, index: INT| value.array[index as usize]); + engine.register_indexer(|value: &mut TestStruct, index: String| value.array[index.len()]); assert_eq!(engine.eval::("let a = new_ts(); a.x = 500; a.x")?, 500); assert_eq!(engine.eval::("let a = new_ts(); a.x.add(); a.x")?, 42); assert_eq!(engine.eval::("let a = new_ts(); a.y.add(); a.y")?, 0); #[cfg(not(feature = "no_index"))] - assert_eq!(engine.eval::("let a = new_ts(); a[3]")?, 4); + assert_eq!(engine.eval::(r#"let a = new_ts(); a["abc"]"#)?, 4); Ok(()) } #[test] -fn test_big_get_set() -> Result<(), Box> { +fn test_get_set_chain() -> Result<(), Box> { #[derive(Clone)] struct TestChild { x: INT, From a2c50879fe03598ecbc599400b13651a1b578f0b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 17 May 2020 00:24:07 +0800 Subject: [PATCH 23/36] Fix dropping issues with StaticVec and use it everywhere. --- README.md | 2 +- src/engine.rs | 34 +++-- src/fn_call.rs | 2 +- src/lib.rs | 1 - src/module.rs | 2 +- src/optimize.rs | 61 +++++---- src/packages/mod.rs | 3 +- src/packages/string_more.rs | 17 ++- src/parser.rs | 76 +++++++---- src/unsafe.rs | 10 +- src/utils.rs | 250 ++++++++++++++++++++++++++++-------- 11 files changed, 313 insertions(+), 145 deletions(-) diff --git a/README.md b/README.md index 0339ae85..be949d40 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Rhai's current features set: * Easily [call a script-defined function](#calling-rhai-functions-from-rust) from Rust * Low compile-time overhead (~0.6 sec debug/~3 sec release for `rhai_runner` sample app) * Fairly efficient evaluation (1 million iterations in 0.75 sec on my 5 year old laptop) -* Relatively little `unsafe` code (yes there are some for performance reasons, and all `unsafe` code is limited to +* Relatively little `unsafe` code (yes there are some for performance reasons, and most `unsafe` code is limited to one single source file, all with names starting with `"unsafe_"`) * Sand-boxed (the scripting [`Engine`] can be declared immutable which cannot mutate the containing environment unless explicitly allowed via `RefCell` etc.) diff --git a/src/engine.rs b/src/engine.rs index 2c9a3a82..534a506a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -8,7 +8,7 @@ use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; -use crate::r#unsafe::unsafe_cast_var_name; +use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -756,7 +756,8 @@ impl Engine { args.into_iter().map(|v| mem::take(*v)), ) .map(|(name, value)| { - let var_name = unsafe_cast_var_name(name.as_str(), &mut state); + let var_name = + unsafe_cast_var_name_to_lifetime(name.as_str(), &mut state); (var_name, ScopeEntryType::Normal, value) }), ); @@ -1172,11 +1173,10 @@ impl Engine { ) -> Result<(), Box> { match expr { Expr::FnCall(x) if x.1.is_none() => { - let mut arg_values = StaticVec::::new(); - - for arg_expr in x.3.iter() { - arg_values.push(self.eval_expr(scope, state, arg_expr, level)?); - } + let arg_values = + x.3.iter() + .map(|arg_expr| self.eval_expr(scope, state, arg_expr, level)) + .collect::, _>>()?; idx_values.push(Dynamic::from(arg_values)); } @@ -1471,14 +1471,10 @@ impl Engine { if !self.has_override(state, (hash_fn, *hash_fn_def)) { // eval - only in function call style let prev_len = scope.len(); + let pos = args_expr.get_ref(0).position(); // Evaluate the text string as a script - let result = self.eval_script_expr( - scope, - state, - args.pop(), - args_expr[0].position(), - ); + let result = self.eval_script_expr(scope, state, args.pop(), pos); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1709,7 +1705,7 @@ impl Engine { .or_else(|| self.packages.get_iter(tid)) { // Add the loop variable - let var_name = unsafe_cast_var_name(name, &state); + let var_name = unsafe_cast_var_name_to_lifetime(name, &state); scope.push(var_name, ()); let index = scope.len() - 1; state.scope_level += 1; @@ -1775,7 +1771,7 @@ impl Engine { Stmt::Let(x) if x.1.is_some() => { let ((var_name, pos), expr) = x.as_ref(); let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; - let var_name = unsafe_cast_var_name(var_name, &state); + let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); self.inc_operations(state, *pos)?; Ok(Default::default()) @@ -1783,7 +1779,7 @@ impl Engine { Stmt::Let(x) => { let ((var_name, pos), _) = x.as_ref(); - let var_name = unsafe_cast_var_name(var_name, &state); + let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push(var_name, ()); self.inc_operations(state, *pos)?; Ok(Default::default()) @@ -1793,7 +1789,7 @@ impl Engine { Stmt::Const(x) if x.1.is_constant() => { let ((var_name, pos), expr) = x.as_ref(); let val = self.eval_expr(scope, state, &expr, level)?; - let var_name = unsafe_cast_var_name(var_name, &state); + let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); self.inc_operations(state, *pos)?; Ok(Default::default()) @@ -1827,7 +1823,7 @@ impl Engine { let module = resolver.resolve(self, Scope::new(), &path, expr.position())?; - let mod_name = unsafe_cast_var_name(name, &state); + let mod_name = unsafe_cast_var_name_to_lifetime(name, &state); scope.push_module(mod_name, module); state.modules += 1; @@ -1848,7 +1844,7 @@ impl Engine { // Export statement Stmt::Export(list) => { - for ((id, id_pos), rename) in list.as_ref() { + for ((id, id_pos), rename) in list.iter() { // Mark scope variables as public if let Some(index) = scope .get_index(id) diff --git a/src/fn_call.rs b/src/fn_call.rs index e1da767b..58711ebb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -9,7 +9,7 @@ use crate::utils::StaticVec; /// Any data type that can be converted into a `Vec` can be used /// as arguments to a function call. pub trait FuncArgs { - /// Convert to a `Vec` of the function call arguments. + /// Convert to a `StaticVec` of the function call arguments. fn into_vec(self) -> StaticVec; } diff --git a/src/lib.rs b/src/lib.rs index 203a6dfb..5c7a10ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,6 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -pub use fn_call::FuncArgs; pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; diff --git a/src/module.rs b/src/module.rs index f65428a0..056d1a75 100644 --- a/src/module.rs +++ b/src/module.rs @@ -716,7 +716,7 @@ impl Module { /// /// A `StaticVec` is used because most module-level access contains only one level, /// and it is wasteful to always allocate a `Vec` with one element. -#[derive(Clone, Eq, PartialEq, Hash, Default)] +#[derive(Clone, Eq, PartialEq, Default)] pub struct ModuleRef(StaticVec<(String, Position)>, Option); impl fmt::Debug for ModuleRef { diff --git a/src/optimize.rs b/src/optimize.rs index 04026059..f118fc3a 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -10,10 +10,12 @@ use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::Position; +use crate::utils::StaticVec; use crate::stdlib::{ boxed::Box, iter::empty, + mem, string::{String, ToString}, vec, vec::Vec, @@ -140,7 +142,11 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - if preserve_result { // -> { expr, Noop } - Stmt::Block(Box::new((vec![Stmt::Expr(Box::new(expr)), x.1], pos))) + let mut statements = StaticVec::new(); + statements.push(Stmt::Expr(Box::new(expr))); + statements.push(x.1); + + Stmt::Block(Box::new((statements, pos))) } else { // -> expr Stmt::Expr(Box::new(expr)) @@ -193,7 +199,8 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - Stmt::Break(pos) => { // Only a single break statement - turn into running the guard expression once state.set_dirty(); - let mut statements = vec![Stmt::Expr(Box::new(optimize_expr(expr, state)))]; + let mut statements = StaticVec::new(); + statements.push(Stmt::Expr(Box::new(optimize_expr(expr, state)))); if preserve_result { statements.push(Stmt::Noop(pos)) } @@ -229,24 +236,24 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - // import expr as id; Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1))), // { block } - Stmt::Block(x) => { + Stmt::Block(mut x) => { let orig_len = x.0.len(); // Original number of statements in the block, for change detection let orig_constants_len = state.constants.len(); // Original number of constants in the state, for restore later let pos = x.1; // Optimize each statement in the block let mut result: Vec<_> = - x.0.into_iter() + x.0.iter_mut() .map(|stmt| match stmt { // Add constant into the state Stmt::Const(v) => { - let ((name, pos), expr) = *v; - state.push_constant(&name, expr); + let ((name, pos), expr) = v.as_mut(); + state.push_constant(name, mem::take(expr)); state.set_dirty(); - Stmt::Noop(pos) // No need to keep constants + Stmt::Noop(*pos) // No need to keep constants } // Optimize the statement - _ => optimize_stmt(stmt, state, preserve_result), + _ => optimize_stmt(mem::take(stmt), state, preserve_result), }) .collect(); @@ -324,13 +331,13 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - Stmt::Noop(pos) } // Only one let/import statement - leave it alone - [Stmt::Let(_)] | [Stmt::Import(_)] => Stmt::Block(Box::new((result, pos))), + [Stmt::Let(_)] | [Stmt::Import(_)] => Stmt::Block(Box::new((result.into(), pos))), // Only one statement - promote [_] => { state.set_dirty(); result.remove(0) } - _ => Stmt::Block(Box::new((result, pos))), + _ => Stmt::Block(Box::new((result.into(), pos))), } } // expr; @@ -392,14 +399,14 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { #[cfg(not(feature = "no_object"))] Expr::Dot(x) => match (x.0, x.1) { // map.string - (Expr::Map(m), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { + (Expr::Map(mut m), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { let ((prop, _, _), _) = p.as_ref(); // Map literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.into_iter().find(|((name, _), _)| name == prop) - .map(|(_, expr)| expr.set_position(pos)) + m.0.iter_mut().find(|((name, _), _)| name == prop) + .map(|(_, expr)| mem::take(expr).set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // lhs.rhs @@ -416,16 +423,16 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // Array literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); - a.0.remove(i.0 as usize).set_position(a.1) + a.0.get(i.0 as usize).set_position(a.1) } // map[string] - (Expr::Map(m), Expr::StringConstant(s)) if m.0.iter().all(|(_, x)| x.is_pure()) => { + (Expr::Map(mut m), Expr::StringConstant(s)) if m.0.iter().all(|(_, x)| x.is_pure()) => { // Map literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.into_iter().find(|((name, _), _)| name == &s.0) - .map(|(_, expr)| expr.set_position(pos)) + m.0.iter_mut().find(|((name, _), _)| name == &s.0) + .map(|(_, expr)| mem::take(expr).set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // string[int] @@ -439,15 +446,13 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }, // [ items .. ] #[cfg(not(feature = "no_index"))] - Expr::Array(a) => Expr::Array(Box::new((a.0 - .into_iter() - .map(|expr| optimize_expr(expr, state)) - .collect(), a.1))), + Expr::Array(mut a) => Expr::Array(Box::new((a.0 + .iter_mut().map(|expr| optimize_expr(mem::take(expr), state)) + .collect(), a.1))), // [ items .. ] #[cfg(not(feature = "no_object"))] - Expr::Map(m) => Expr::Map(Box::new((m.0 - .into_iter() - .map(|((key, pos), expr)| ((key, pos), optimize_expr(expr, state))) + Expr::Map(mut m) => Expr::Map(Box::new((m.0 + .iter_mut().map(|((key, pos), expr)| ((mem::take(key), *pos), optimize_expr(mem::take(expr), state))) .collect(), m.1))), // lhs in rhs Expr::In(x) => match (x.0, x.1) { @@ -527,7 +532,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // Do not call some special keywords Expr::FnCall(mut x) if DONT_EVAL_KEYWORDS.contains(&(x.0).0.as_ref())=> { - x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); Expr::FnCall(x) } @@ -542,7 +547,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // First search in script-defined functions (can override built-in) if state.fn_lib.iter().find(|(id, len)| *id == name && *len == args.len()).is_some() { // A script-defined function overrides the built-in function - do not make the call - x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); return Expr::FnCall(x); } @@ -574,14 +579,14 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }) ).unwrap_or_else(|| { // Optimize function call arguments - x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); Expr::FnCall(x) }) } // id(args ..) -> optimize function call arguments Expr::FnCall(mut x) => { - x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); Expr::FnCall(x) } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 7c146f7f..e56843f0 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -2,6 +2,7 @@ use crate::fn_native::{NativeCallable, SharedIteratorFunction}; use crate::module::Module; +use crate::utils::StaticVec; use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; @@ -54,7 +55,7 @@ pub type PackageLibrary = Arc; #[derive(Clone, Default)] pub(crate) struct PackagesCollection { /// Collection of `PackageLibrary` instances. - packages: Vec, + packages: StaticVec, } impl PackagesCollection { diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index eb6208f1..2c90bea5 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,6 +1,7 @@ use crate::def_package; use crate::module::FuncReturn; use crate::parser::INT; +use crate::utils::StaticVec; #[cfg(not(feature = "no_index"))] use crate::engine::Array; @@ -29,7 +30,7 @@ fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn { start as usize }; - let chars: Vec<_> = s.chars().collect(); + let chars: StaticVec<_> = s.chars().collect(); let len = if offset + (len as usize) > chars.len() { chars.len() - offset @@ -37,7 +38,7 @@ fn sub_string(s: &mut String, start: INT, len: INT) -> FuncReturn { len as usize }; - Ok(chars[offset..][..len].into_iter().collect()) + Ok(chars.iter().skip(offset).take(len).cloned().collect()) } fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> { let offset = if s.is_empty() || len <= 0 { @@ -52,7 +53,7 @@ fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> { start as usize }; - let chars: Vec<_> = s.chars().collect(); + let chars: StaticVec<_> = s.chars().collect(); let len = if offset + (len as usize) > chars.len() { chars.len() - offset @@ -62,8 +63,10 @@ fn crop_string(s: &mut String, start: INT, len: INT) -> FuncReturn<()> { s.clear(); - chars[offset..][..len] - .into_iter() + chars + .iter() + .skip(offset) + .take(len) .for_each(|&ch| s.push(ch)); Ok(()) @@ -189,9 +192,9 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str "truncate", |s: &mut String, len: INT| { if len >= 0 { - let chars: Vec<_> = s.chars().take(len as usize).collect(); + let chars: StaticVec<_> = s.chars().take(len as usize).collect(); s.clear(); - chars.into_iter().for_each(|ch| s.push(ch)); + chars.iter().for_each(|&ch| s.push(ch)); } else { s.clear(); } diff --git a/src/parser.rs b/src/parser.rs index 0fefc84c..9e608b79 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,7 +7,7 @@ use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::EMPTY_TYPE_ID; +use crate::utils::{StaticVec, EMPTY_TYPE_ID}; #[cfg(not(feature = "no_module"))] use crate::module::ModuleRef; @@ -195,7 +195,7 @@ pub struct FnDef { /// Function access mode. pub access: FnAccess, /// Names of function parameters. - pub params: Vec, + pub params: StaticVec, /// Function body. pub body: Stmt, /// Position of the function definition. @@ -294,7 +294,7 @@ pub enum Stmt { /// const id = expr Const(Box<((String, Position), Expr)>), /// { stmt; ... } - Block(Box<(Vec, Position)>), + Block(Box<(StaticVec, Position)>), /// { stmt } Expr(Box), /// continue @@ -306,7 +306,13 @@ pub enum Stmt { /// import expr as module Import(Box<(Expr, (String, Position))>), /// expr id as name, ... - Export(Box)>>), + Export(Box)>>), +} + +impl Default for Stmt { + fn default() -> Self { + Self::Noop(Default::default()) + } } impl Stmt { @@ -324,7 +330,7 @@ impl Stmt { Stmt::Loop(x) => x.position(), Stmt::For(x) => x.2.position(), Stmt::Import(x) => (x.1).1, - Stmt::Export(x) => (x.get(0).unwrap().0).1, + Stmt::Export(x) => (x.get_ref(0).0).1, } } @@ -406,7 +412,7 @@ pub enum Expr { (Cow<'static, str>, Position), MRef, u64, - Vec, + StaticVec, Option, )>, ), @@ -417,9 +423,9 @@ pub enum Expr { /// expr[expr] Index(Box<(Expr, Expr, Position)>), /// [ expr, ... ] - Array(Box<(Vec, Position)>), + Array(Box<(StaticVec, Position)>), /// #{ name:expr, ... } - Map(Box<(Vec<((String, Position), Expr)>, Position)>), + Map(Box<(StaticVec<((String, Position), Expr)>, Position)>), /// lhs in rhs In(Box<(Expr, Expr, Position)>), /// lhs && rhs @@ -434,6 +440,12 @@ pub enum Expr { Unit(Position), } +impl Default for Expr { + fn default() -> Self { + Self::Unit(Default::default()) + } +} + impl Expr { /// Get the `Dynamic` value of a constant expression. /// @@ -713,7 +725,7 @@ fn parse_call_expr<'a>( begin: Position, allow_stmt_expr: bool, ) -> Result> { - let mut args = Vec::new(); + let mut args = StaticVec::new(); match input.peek().unwrap() { // id @@ -1013,7 +1025,7 @@ fn parse_array_literal<'a>( pos: Position, allow_stmt_expr: bool, ) -> Result> { - let mut arr = Vec::new(); + let mut arr = StaticVec::new(); if !match_token(input, Token::RightBracket)? { while !input.peek().unwrap().0.is_eof() { @@ -1056,7 +1068,7 @@ fn parse_map_literal<'a>( pos: Position, allow_stmt_expr: bool, ) -> Result> { - let mut map = Vec::new(); + let mut map = StaticVec::new(); if !match_token(input, Token::RightBrace)? { while !input.peek().unwrap().0.is_eof() { @@ -1296,15 +1308,17 @@ fn parse_unary<'a>( Expr::FloatConstant(x) => Ok(Expr::FloatConstant(Box::new((-x.0, x.1)))), // Call negative function - e => { + expr => { let op = "-"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let mut args = StaticVec::new(); + args.push(expr); Ok(Expr::FnCall(Box::new(( (op.into(), pos), None, hash, - vec![e], + args, None, )))) } @@ -1318,7 +1332,8 @@ fn parse_unary<'a>( // !expr (Token::Bang, _) => { let pos = eat_token(input, Token::Bang); - let expr = vec![parse_primary(input, stack, allow_stmt_expr)?]; + let mut args = StaticVec::new(); + args.push(parse_primary(input, stack, allow_stmt_expr)?); let op = "!"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); @@ -1327,7 +1342,7 @@ fn parse_unary<'a>( (op.into(), pos), None, hash, - expr, + args, Some(false.into()), // NOT operator, when operating on invalid operand, defaults to false )))) } @@ -1412,9 +1427,13 @@ fn parse_op_assignment_stmt<'a>( let rhs = parse_expr(input, stack, allow_stmt_expr)?; // lhs op= rhs -> lhs = op(lhs, rhs) - let args = vec![lhs_copy, rhs]; + let mut args = StaticVec::new(); + args.push(lhs_copy); + args.push(rhs); + let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); let rhs_expr = Expr::FnCall(Box::new(((op.into(), pos), None, hash, args, None))); + make_assignment_stmt(stack, lhs, rhs_expr, pos) } @@ -1695,7 +1714,10 @@ fn parse_binary_op<'a>( let cmp_def = Some(false.into()); let op = op_token.syntax(); let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(2)); - let mut args = vec![current_lhs, rhs]; + + let mut args = StaticVec::new(); + args.push(current_lhs); + args.push(rhs); current_lhs = match op_token { Token::Plus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), @@ -1721,13 +1743,13 @@ fn parse_binary_op<'a>( } Token::Or => { - let rhs = args.pop().unwrap(); - let current_lhs = args.pop().unwrap(); + let rhs = args.pop(); + let current_lhs = args.pop(); Expr::Or(Box::new((current_lhs, rhs, pos))) } Token::And => { - let rhs = args.pop().unwrap(); - let current_lhs = args.pop().unwrap(); + let rhs = args.pop(); + let current_lhs = args.pop(); Expr::And(Box::new((current_lhs, rhs, pos))) } Token::Ampersand => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), @@ -1735,15 +1757,15 @@ fn parse_binary_op<'a>( Token::XOr => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::In => { - let rhs = args.pop().unwrap(); - let current_lhs = args.pop().unwrap(); + let rhs = args.pop(); + let current_lhs = args.pop(); make_in_expr(current_lhs, rhs, pos)? } #[cfg(not(feature = "no_object"))] Token::Period => { - let mut rhs = args.pop().unwrap(); - let current_lhs = args.pop().unwrap(); + let mut rhs = args.pop(); + let current_lhs = args.pop(); match &mut rhs { // current_lhs.rhs(...) - method call @@ -2025,7 +2047,7 @@ fn parse_import<'a>( fn parse_export<'a>(input: &mut Peekable>) -> Result> { eat_token(input, Token::Export); - let mut exports = Vec::new(); + let mut exports = StaticVec::new(); loop { let (id, id_pos) = match input.next().unwrap() { @@ -2098,7 +2120,7 @@ fn parse_block<'a>( } }; - let mut statements = Vec::new(); + let mut statements = StaticVec::new(); let prev_len = stack.len(); while !match_token(input, Token::RightBrace)? { diff --git a/src/unsafe.rs b/src/unsafe.rs index 64ba8904..efbf80c7 100644 --- a/src/unsafe.rs +++ b/src/unsafe.rs @@ -47,9 +47,9 @@ pub fn unsafe_cast_box(item: Box) -> Result, B /// current `Scope` without cloning the variable name. Doing this is safe because all local /// variables in the `Scope` are cleared out before existing the block. /// -/// Force-casting a local variable lifetime to the current `Scope`'s larger lifetime saves +/// Force-casting a local variable's lifetime to the current `Scope`'s larger lifetime saves /// on allocations and string cloning, thus avoids us having to maintain a chain of `Scope`'s. -pub fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { +pub fn unsafe_cast_var_name_to_lifetime<'s>(name: &str, state: &State) -> Cow<'s, str> { // If not at global level, we can force-cast if state.scope_level > 0 { // WARNING - force-cast the variable name into the scope's lifetime to avoid cloning it @@ -62,7 +62,7 @@ pub fn unsafe_cast_var_name<'s>(name: &str, state: &State) -> Cow<'s, str> { } } -/// Provide a type instance that is memory-zeroed. -pub fn unsafe_zeroed() -> T { - unsafe { mem::MaybeUninit::zeroed().assume_init() } +/// Provide a type instance that is uninitialized. +pub fn unsafe_uninit() -> T { + unsafe { mem::MaybeUninit::uninit().assume_init() } } diff --git a/src/utils.rs b/src/utils.rs index f61d24ea..e7e62eeb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,10 @@ //! Module containing various utility types and functions. +//! +//! # Safety +//! +//! The `StaticVec` type has some `unsafe` blocks. -use crate::r#unsafe::unsafe_zeroed; +use crate::r#unsafe::unsafe_uninit; use crate::stdlib::{ any::TypeId, @@ -8,6 +12,7 @@ use crate::stdlib::{ hash::{Hash, Hasher}, iter::FromIterator, mem, + ops::Drop, vec::Vec, }; @@ -47,6 +52,8 @@ pub fn calc_fn_spec<'a>( s.finish() } +const MAX_STATIC_VEC: usize = 4; + /// A type to hold a number of values in static storage for speed, and any spill-overs in a `Vec`. /// /// This is essentially a knock-off of the [`staticvec`](https://crates.io/crates/staticvec) crate. @@ -54,22 +61,57 @@ pub fn calc_fn_spec<'a>( /// /// # Safety /// -/// This type uses some unsafe code (mainly to zero out unused array slots) for efficiency. +/// This type uses some unsafe code (mainly for uninitialized/unused array slots) for efficiency. // // TODO - remove unsafe code -#[derive(Clone, Hash)] pub struct StaticVec { /// Total number of values held. len: usize, - /// Static storage. 4 slots should be enough for most cases - i.e. four levels of indirection. - list: [T; 4], + /// Static storage. 4 slots should be enough for most cases - i.e. four items of fast, no-allocation access. + list: [mem::MaybeUninit; MAX_STATIC_VEC], /// Dynamic storage. For spill-overs. more: Vec, } +impl Drop for StaticVec { + fn drop(&mut self) { + self.clear(); + } +} + +impl Default for StaticVec { + fn default() -> Self { + Self { + len: 0, + list: unsafe_uninit(), + more: Vec::new(), + } + } +} + impl PartialEq for StaticVec { fn eq(&self, other: &Self) -> bool { - self.len == other.len && self.list == other.list && self.more == other.more + self.len == other.len + //&& self.list[0..self.len] == other.list[0..self.len] + && self.more == other.more + } +} + +impl Clone for StaticVec { + fn clone(&self) -> Self { + let mut value: Self = Default::default(); + value.len = self.len; + + if self.len <= self.list.len() { + for x in 0..self.len { + let item: &T = unsafe { mem::transmute(self.list.get(x).unwrap()) }; + value.list[x] = mem::MaybeUninit::new(item.clone()); + } + } else { + value.more = self.more.clone(); + } + + value } } @@ -87,35 +129,75 @@ impl FromIterator for StaticVec { } } -impl Default for StaticVec { - fn default() -> Self { - Self { - len: 0, - list: unsafe_zeroed(), - more: Vec::new(), - } - } -} - impl StaticVec { + fn extract(value: mem::MaybeUninit) -> T { + unsafe { value.assume_init() } + } /// Create a new `StaticVec`. pub fn new() -> Self { Default::default() } + /// Empty the `StaticVec`. + pub fn clear(&mut self) { + if self.len <= self.list.len() { + for x in 0..self.len { + Self::extract(mem::replace( + self.list.get_mut(x).unwrap(), + mem::MaybeUninit::uninit(), + )); + } + } else { + self.more.clear(); + } + self.len = 0; + } /// Push a new value to the end of this `StaticVec`. pub fn push>(&mut self, value: X) { if self.len == self.list.len() { // Move the fixed list to the Vec - for x in 0..self.list.len() { - let def_val: T = unsafe_zeroed(); - self.more - .push(mem::replace(self.list.get_mut(x).unwrap(), def_val)); - } - self.more.push(value.into()); - } else if self.len > self.list.len() { + self.more.extend( + self.list + .iter_mut() + .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) + .map(Self::extract), + ); self.more.push(value.into()); + } else if self.len < self.list.len() { + mem::replace( + self.list.get_mut(self.len).unwrap(), + mem::MaybeUninit::new(value.into()), + ); } else { - self.list[self.len] = value.into(); + self.more.push(value.into()); + } + self.len += 1; + } + /// Insert a new value to this `StaticVec` at a particular position. + pub fn insert>(&mut self, index: usize, value: X) { + if index > self.len { + panic!("index OOB in StaticVec"); + } + + if self.len == self.list.len() { + // Move the fixed list to the Vec + self.more.extend( + self.list + .iter_mut() + .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) + .map(Self::extract), + ); + self.more.insert(index, value.into()); + } else if self.len < self.list.len() { + for x in (index..self.len).rev() { + let temp = mem::replace(self.list.get_mut(x).unwrap(), mem::MaybeUninit::uninit()); + mem::replace(self.list.get_mut(x + 1).unwrap(), temp); + } + mem::replace( + self.list.get_mut(index).unwrap(), + mem::MaybeUninit::new(value.into()), + ); + } else { + self.more.insert(index, value.into()); } self.len += 1; } @@ -125,18 +207,25 @@ impl StaticVec { /// /// Panics if the `StaticVec` is empty. pub fn pop(&mut self) -> T { - let result = if self.len <= 0 { - panic!("nothing to pop!") - } else if self.len <= self.list.len() { - let def_val: T = unsafe_zeroed(); - mem::replace(self.list.get_mut(self.len - 1).unwrap(), def_val) + if self.len <= 0 { + panic!("nothing to pop!"); + } + + let result = if self.len <= self.list.len() { + Self::extract(mem::replace( + self.list.get_mut(self.len - 1).unwrap(), + mem::MaybeUninit::uninit(), + )) } else { let r = self.more.pop().unwrap(); // Move back to the fixed list if self.more.len() == self.list.len() { - for x in 0..self.list.len() { - self.list[self.list.len() - 1 - x] = self.more.pop().unwrap(); + for index in (0..self.list.len()).rev() { + mem::replace( + self.list.get_mut(index).unwrap(), + mem::MaybeUninit::new(self.more.pop().unwrap()), + ); } } @@ -151,6 +240,10 @@ impl StaticVec { pub fn len(&self) -> usize { self.len } + /// Is this `StaticVec` empty? + pub fn is_empty(&self) -> bool { + self.len == 0 + } /// Get a reference to the item at a particular index. /// /// # Panics @@ -161,8 +254,10 @@ impl StaticVec { panic!("index OOB in StaticVec"); } - if self.len < self.list.len() { - self.list.get(index).unwrap() + let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; + + if self.len <= list.len() { + list.get(index).unwrap() } else { self.more.get(index).unwrap() } @@ -177,46 +272,52 @@ impl StaticVec { panic!("index OOB in StaticVec"); } - if self.len < self.list.len() { - self.list.get_mut(index).unwrap() + let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; + + if self.len <= list.len() { + list.get_mut(index).unwrap() } else { self.more.get_mut(index).unwrap() } } /// Get an iterator to entries in the `StaticVec`. pub fn iter(&self) -> impl Iterator { - if self.len > self.list.len() { - self.more.iter() + let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; + + if self.len <= list.len() { + list[..self.len].iter() } else { - self.list[..self.len].iter() + self.more.iter() } } /// Get a mutable iterator to entries in the `StaticVec`. pub fn iter_mut(&mut self) -> impl Iterator { - if self.len > self.list.len() { - self.more.iter_mut() + let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; + + if self.len <= list.len() { + list[..self.len].iter_mut() } else { - self.list[..self.len].iter_mut() + self.more.iter_mut() } } } -impl StaticVec { - /// Get the item at a particular index. +impl StaticVec { + /// Get the item at a particular index, replacing it with the default. /// /// # Panics /// /// Panics if the index is out of bounds. - pub fn get(&self, index: usize) -> T { + pub fn get(&mut self, index: usize) -> T { if index >= self.len { panic!("index OOB in StaticVec"); } - if self.len < self.list.len() { - *self.list.get(index).unwrap() + mem::take(if self.len <= self.list.len() { + unsafe { mem::transmute(self.list.get_mut(index).unwrap()) } } else { - *self.more.get(index).unwrap() - } + self.more.get_mut(index).unwrap() + }) } } @@ -230,20 +331,61 @@ impl fmt::Debug for StaticVec { impl AsRef<[T]> for StaticVec { fn as_ref(&self) -> &[T] { - if self.len > self.list.len() { - &self.more[..] + let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; + + if self.len <= list.len() { + &list[..self.len] } else { - &self.list[..self.len] + &self.more[..] } } } impl AsMut<[T]> for StaticVec { fn as_mut(&mut self) -> &mut [T] { - if self.len > self.list.len() { - &mut self.more[..] + let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; + + if self.len <= list.len() { + &mut list[..self.len] } else { - &mut self.list[..self.len] + &mut self.more[..] } } } + +impl From> for Vec { + fn from(mut value: StaticVec) -> Self { + if value.len <= value.list.len() { + value + .list + .iter_mut() + .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) + .map(StaticVec::extract) + .collect() + } else { + let mut arr = Self::new(); + arr.append(&mut value.more); + arr + } + } +} + +impl From> for StaticVec { + fn from(mut value: Vec) -> Self { + let mut arr: Self = Default::default(); + arr.len = value.len(); + + if arr.len <= arr.list.len() { + for x in (0..arr.len).rev() { + mem::replace( + arr.list.get_mut(x).unwrap(), + mem::MaybeUninit::new(value.pop().unwrap()), + ); + } + } else { + arr.more = value; + } + + arr + } +} From 8b5550eeb6b5c98e4e86867810d9c69395c63b22 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 17 May 2020 22:19:49 +0800 Subject: [PATCH 24/36] Complete StaticVec implementation. --- README.md | 5 +- src/api.rs | 8 +- src/engine.rs | 41 +++-- src/module.rs | 13 +- src/optimize.rs | 57 +++---- src/parser.rs | 10 +- src/token.rs | 3 +- src/unsafe.rs | 5 - src/utils.rs | 360 ++++++++++++++++++++++++++++++++------------ tests/modules.rs | 16 +- tests/operations.rs | 24 ++- 11 files changed, 373 insertions(+), 169 deletions(-) diff --git a/README.md b/README.md index be949d40..46bae69f 100644 --- a/README.md +++ b/README.md @@ -2288,8 +2288,9 @@ engine.set_max_operations(0); // allow unlimited operations ``` The concept of one single _operation_ in Rhai is volatile - it roughly equals one expression node, -one statement, one iteration of a loop, or one function call etc. with sub-expressions and statement -blocks executed inside these contexts accumulated on top. +loading a variable/constant, one operator call, one complete statement, one iteration of a loop, +or one function call etc. with sub-expressions and statement blocks executed inside these contexts accumulated on top. +A good rule-of-thumb is that one simple non-trivial expression consumes on average 5-10 operations. One _operation_ can take an unspecified amount of time and CPU cycles, depending on the particular operation involved. For example, loading a constant consumes very few CPU cycles, while calling an external Rust function, diff --git a/src/api.rs b/src/api.rs index 5e144bcf..26a825ab 100644 --- a/src/api.rs +++ b/src/api.rs @@ -652,7 +652,10 @@ impl Engine { let scripts = [script]; let stream = lex(&scripts); - parse_global_expr(&mut stream.peekable(), self, scope, self.optimization_level) + { + let mut peekable = stream.peekable(); + parse_global_expr(&mut peekable, self, scope, self.optimization_level) + } } /// Evaluate a script file. @@ -748,11 +751,10 @@ impl Engine { scope: &mut Scope, script: &str, ) -> Result> { - // Since the AST will be thrown away afterwards, don't bother to optimize it let ast = self.compile_with_scope_and_optimization_level( scope, &[script], - OptimizationLevel::None, + self.optimization_level, )?; self.eval_ast_with_scope(scope, &ast) } diff --git a/src/engine.rs b/src/engine.rs index 534a506a..4c91f39c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -119,11 +119,11 @@ impl Target<'_> { .map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?; let mut chars: StaticVec = s.chars().collect(); - let ch = *chars.get_ref(*index); + let ch = chars[*index]; // See if changed - if so, update the String if ch != new_ch { - *chars.get_mut(*index) = new_ch; + chars[*index] = new_ch; s.clear(); chars.iter().for_each(|&ch| s.push(ch)); } @@ -190,7 +190,7 @@ impl<'a> State<'a> { } /// Get a script-defined function definition from the `State`. pub fn get_function(&self, hash: u64) -> Option<&FnDef> { - self.fn_lib.get(&hash).map(|f| f.as_ref()) + self.fn_lib.get(&hash).map(|fn_def| fn_def.as_ref()) } } @@ -463,7 +463,7 @@ fn search_scope<'a>( .downcast_mut::() .unwrap() } else { - let (id, root_pos) = modules.get_ref(0); + let (id, root_pos) = modules.get(0); scope.find_module(id).ok_or_else(|| { Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *root_pos)) @@ -736,8 +736,6 @@ impl Engine { pos: Position, level: usize, ) -> Result<(Dynamic, State<'s>), Box> { - self.inc_operations(&mut state, pos)?; - let orig_scope_level = state.scope_level; state.scope_level += 1; @@ -1120,6 +1118,7 @@ impl Engine { let index = if state.always_search { None } else { *index }; let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); let (target, typ) = search_scope(scope, &name, mod_and_hash, index, *pos)?; + self.inc_operations(state, *pos)?; // Constants cannot be modified match typ { @@ -1171,6 +1170,8 @@ impl Engine { size: usize, level: usize, ) -> Result<(), Box> { + self.inc_operations(state, expr.position())?; + match expr { Expr::FnCall(x) if x.1.is_none() => { let arg_values = @@ -1328,13 +1329,13 @@ impl Engine { #[cfg(not(feature = "no_object"))] Dynamic(Union::Map(rhs_value)) => match lhs_value { // Only allows String or char - Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_ref()).into()), + Dynamic(Union::Str(s)) => Ok(rhs_value.contains_key(s.as_str()).into()), Dynamic(Union::Char(c)) => Ok(rhs_value.contains_key(&c.to_string()).into()), _ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))), }, Dynamic(Union::Str(rhs_value)) => match lhs_value { // Only allows String or char - Dynamic(Union::Str(s)) => Ok(rhs_value.contains(s.as_ref()).into()), + Dynamic(Union::Str(s)) => Ok(rhs_value.contains(s.as_str()).into()), Dynamic(Union::Char(c)) => Ok(rhs_value.contains(c).into()), _ => Err(Box::new(EvalAltResult::ErrorInExpr(lhs.position()))), }, @@ -1382,6 +1383,8 @@ impl Engine { let index = if state.always_search { None } else { *index }; let mod_and_hash = modules.as_ref().map(|m| (m, *hash_var)); let (lhs_ptr, typ) = search_scope(scope, name, mod_and_hash, index, *pos)?; + self.inc_operations(state, *pos)?; + match typ { ScopeEntryType::Constant => Err(Box::new( EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), @@ -1465,13 +1468,13 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - if name == KEYWORD_EVAL && args.len() == 1 && args.get_ref(0).is::() { + if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { let hash_fn = calc_fn_hash(empty(), name, once(TypeId::of::())); if !self.has_override(state, (hash_fn, *hash_fn_def)) { // eval - only in function call style let prev_len = scope.len(); - let pos = args_expr.get_ref(0).position(); + let pos = args_expr.get(0).position(); // Evaluate the text string as a script let result = self.eval_script_expr(scope, state, args.pop(), pos); @@ -1505,7 +1508,7 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); - let (id, root_pos) = modules.get_ref(0); // First module + let (id, root_pos) = modules.get(0); // First module let module = if let Some(index) = modules.index() { scope @@ -1663,9 +1666,7 @@ impl Engine { match self.eval_expr(scope, state, expr, level)?.as_bool() { Ok(true) => match self.eval_stmt(scope, state, body, level) { - Ok(_) => { - self.inc_operations(state, body.position())?; - } + Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()), @@ -1682,9 +1683,7 @@ impl Engine { // Loop statement Stmt::Loop(body) => loop { match self.eval_stmt(scope, state, body, level) { - Ok(_) => { - self.inc_operations(state, body.position())?; - } + Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), EvalAltResult::ErrorLoopBreak(true, _) => return Ok(Default::default()), @@ -1773,7 +1772,6 @@ impl Engine { let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); - self.inc_operations(state, *pos)?; Ok(Default::default()) } @@ -1781,7 +1779,6 @@ impl Engine { let ((var_name, pos), _) = x.as_ref(); let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push(var_name, ()); - self.inc_operations(state, *pos)?; Ok(Default::default()) } @@ -1791,7 +1788,6 @@ impl Engine { let val = self.eval_expr(scope, state, &expr, level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); - self.inc_operations(state, *pos)?; Ok(Default::default()) } @@ -1818,7 +1814,7 @@ impl Engine { .eval_expr(scope, state, &expr, level)? .try_cast::() { - if let Some(resolver) = self.module_resolver.as_ref() { + if let Some(resolver) = &self.module_resolver { // Use an empty scope to create a module let module = resolver.resolve(self, Scope::new(), &path, expr.position())?; @@ -1827,7 +1823,6 @@ impl Engine { scope.push_module(mod_name, module); state.modules += 1; - self.inc_operations(state, *pos)?; Ok(Default::default()) } else { @@ -1884,7 +1879,7 @@ impl Engine { } // Report progress - only in steps - if let Some(progress) = self.progress.as_ref() { + if let Some(progress) = &self.progress { if !progress(state.operations) { // Terminate script if progress returns false return Err(Box::new(EvalAltResult::ErrorTerminated(pos))); diff --git a/src/module.rs b/src/module.rs index 056d1a75..377a4357 100644 --- a/src/module.rs +++ b/src/module.rs @@ -51,7 +51,7 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, SharedNativeFunction)>, + functions: HashMap, SharedNativeFunction)>, /// Flattened collection of all external Rust functions, including those in sub-modules. all_functions: HashMap, @@ -292,8 +292,9 @@ impl Module { #[cfg(feature = "sync")] let func = Arc::new(f); - self.functions - .insert(hash_fn, (name, access, params.to_vec(), func)); + let params = params.into_iter().cloned().collect(); + + self.functions.insert(hash_fn, (name, access, params, func)); hash_fn } @@ -616,13 +617,13 @@ impl Module { pub(crate) fn index_all_sub_modules(&mut self) { // Collect a particular module. fn index_module<'a>( - module: &'a mut Module, + module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, functions: &mut Vec<(u64, SharedNativeFunction)>, fn_lib: &mut Vec<(u64, SharedFnDef)>, ) { - for (name, m) in module.modules.iter_mut() { + for (name, m) in &module.modules { // Index all the sub-modules first. qualifiers.push(name); index_module(m, qualifiers, variables, functions, fn_lib); @@ -630,7 +631,7 @@ impl Module { } // Index all variables - for (var_name, value) in module.variables.iter() { + for (var_name, value) in &module.variables { // Qualifiers + variable name let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); variables.push((hash_var, value.clone())); diff --git a/src/optimize.rs b/src/optimize.rs index f118fc3a..671415e3 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -236,24 +236,24 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - // import expr as id; Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1))), // { block } - Stmt::Block(mut x) => { + Stmt::Block(x) => { let orig_len = x.0.len(); // Original number of statements in the block, for change detection let orig_constants_len = state.constants.len(); // Original number of constants in the state, for restore later let pos = x.1; // Optimize each statement in the block let mut result: Vec<_> = - x.0.iter_mut() + x.0.into_iter() .map(|stmt| match stmt { // Add constant into the state Stmt::Const(v) => { - let ((name, pos), expr) = v.as_mut(); - state.push_constant(name, mem::take(expr)); + let ((name, pos), expr) = *v; + state.push_constant(&name, expr); state.set_dirty(); - Stmt::Noop(*pos) // No need to keep constants + Stmt::Noop(pos) // No need to keep constants } // Optimize the statement - _ => optimize_stmt(mem::take(stmt), state, preserve_result), + _ => optimize_stmt(stmt, state, preserve_result), }) .collect(); @@ -399,14 +399,14 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { #[cfg(not(feature = "no_object"))] Expr::Dot(x) => match (x.0, x.1) { // map.string - (Expr::Map(mut m), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { + (Expr::Map(m), Expr::Property(p)) if m.0.iter().all(|(_, x)| x.is_pure()) => { let ((prop, _, _), _) = p.as_ref(); // Map literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.iter_mut().find(|((name, _), _)| name == prop) - .map(|(_, expr)| mem::take(expr).set_position(pos)) + m.0.into_iter().find(|((name, _), _)| name == prop) + .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // lhs.rhs @@ -423,16 +423,16 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // Array literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); - a.0.get(i.0 as usize).set_position(a.1) + a.0.take(i.0 as usize).set_position(a.1) } // map[string] - (Expr::Map(mut m), Expr::StringConstant(s)) if m.0.iter().all(|(_, x)| x.is_pure()) => { + (Expr::Map(m), Expr::StringConstant(s)) if m.0.iter().all(|(_, x)| x.is_pure()) => { // Map literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); let pos = m.1; - m.0.iter_mut().find(|((name, _), _)| name == &s.0) - .map(|(_, expr)| mem::take(expr).set_position(pos)) + m.0.into_iter().find(|((name, _), _)| name == &s.0) + .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // string[int] @@ -446,13 +446,13 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }, // [ items .. ] #[cfg(not(feature = "no_index"))] - Expr::Array(mut a) => Expr::Array(Box::new((a.0 - .iter_mut().map(|expr| optimize_expr(mem::take(expr), state)) + Expr::Array(a) => Expr::Array(Box::new((a.0 + .into_iter().map(|expr| optimize_expr(expr, state)) .collect(), a.1))), // [ items .. ] #[cfg(not(feature = "no_object"))] - Expr::Map(mut m) => Expr::Map(Box::new((m.0 - .iter_mut().map(|((key, pos), expr)| ((mem::take(key), *pos), optimize_expr(mem::take(expr), state))) + Expr::Map(m) => Expr::Map(Box::new((m.0 + .into_iter().map(|((key, pos), expr)| ((key, pos), optimize_expr(expr, state))) .collect(), m.1))), // lhs in rhs Expr::In(x) => match (x.0, x.1) { @@ -532,7 +532,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // Do not call some special keywords Expr::FnCall(mut x) if DONT_EVAL_KEYWORDS.contains(&(x.0).0.as_ref())=> { - x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); Expr::FnCall(x) } @@ -547,12 +547,12 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // First search in script-defined functions (can override built-in) if state.fn_lib.iter().find(|(id, len)| *id == name && *len == args.len()).is_some() { // A script-defined function overrides the built-in function - do not make the call - x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); return Expr::FnCall(x); } - let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect(); - let mut call_args: Vec<_> = arg_values.iter_mut().collect(); + let mut arg_values: StaticVec<_> = args.iter().map(Expr::get_constant_value).collect(); + let mut call_args: StaticVec<_> = arg_values.iter_mut().collect(); // Save the typename of the first argument if it is `type_of()` // This is to avoid `call_args` being passed into the closure @@ -562,7 +562,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { "" }; - call_fn(&state.engine.packages, &state.engine.global_module, name, &mut call_args, *pos).ok() + call_fn(&state.engine.packages, &state.engine.global_module, name, call_args.as_mut(), *pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { @@ -579,14 +579,14 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }) ).unwrap_or_else(|| { // Optimize function call arguments - x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); Expr::FnCall(x) }) } // id(args ..) -> optimize function call arguments Expr::FnCall(mut x) => { - x.3 = x.3.iter_mut().map(|a| optimize_expr(mem::take(a), state)).collect(); + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); Expr::FnCall(x) } @@ -702,11 +702,14 @@ pub fn optimize_into_ast( const level: OptimizationLevel = OptimizationLevel::None; #[cfg(not(feature = "no_function"))] - let fn_lib: Vec<_> = functions + let fn_lib_values: StaticVec<_> = functions .iter() .map(|fn_def| (fn_def.name.as_str(), fn_def.params.len())) .collect(); + #[cfg(not(feature = "no_function"))] + let fn_lib = fn_lib_values.as_ref(); + #[cfg(feature = "no_function")] const fn_lib: &[(&str, usize)] = &[]; @@ -716,7 +719,7 @@ pub fn optimize_into_ast( let pos = fn_def.body.position(); // Optimize the function body - let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), &fn_lib, level); + let mut body = optimize(vec![fn_def.body], engine, &Scope::new(), fn_lib, level); // {} -> Noop fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { @@ -742,7 +745,7 @@ pub fn optimize_into_ast( match level { OptimizationLevel::None => statements, OptimizationLevel::Simple | OptimizationLevel::Full => { - optimize(statements, engine, &scope, &fn_lib, level) + optimize(statements, engine, &scope, fn_lib, level) } }, lib, diff --git a/src/parser.rs b/src/parser.rs index 9e608b79..29c955e2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -330,7 +330,7 @@ impl Stmt { Stmt::Loop(x) => x.position(), Stmt::For(x) => x.2.position(), Stmt::Import(x) => (x.1).1, - Stmt::Export(x) => (x.get_ref(0).0).1, + Stmt::Export(x) => (x.get(0).0).1, } } @@ -745,7 +745,7 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] let hash_fn_def = { if let Some(modules) = modules.as_mut() { - modules.set_index(stack.find_module(&modules.get_ref(0).0)); + modules.set_index(stack.find_module(&modules.get(0).0)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -786,7 +786,7 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] let hash_fn_def = { if let Some(modules) = modules.as_mut() { - modules.set_index(stack.find_module(&modules.get_ref(0).0)); + modules.set_index(stack.find_module(&modules.get(0).0)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -1251,7 +1251,7 @@ fn parse_primary<'a>( // Qualifiers + variable name *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); - modules.set_index(stack.find_module(&modules.get_ref(0).0)); + modules.set_index(stack.find_module(&modules.get(0).0)); } _ => (), } @@ -1471,7 +1471,7 @@ fn make_dot_expr( #[cfg(feature = "no_module")] unreachable!(); #[cfg(not(feature = "no_module"))] - return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get_ref(0).1)); + return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1)); } // lhs.dot_lhs.dot_rhs (lhs, Expr::Dot(x)) => { diff --git a/src/token.rs b/src/token.rs index 25d26ff7..97df2d54 100644 --- a/src/token.rs +++ b/src/token.rs @@ -2,6 +2,7 @@ use crate::error::LexError; use crate::parser::INT; +use crate::utils::StaticVec; #[cfg(not(feature = "no_float"))] use crate::parser::FLOAT; @@ -425,7 +426,7 @@ pub struct TokenIterator<'a> { /// Current position. pos: Position, /// The input character streams. - streams: Vec>>, + streams: StaticVec>>, } impl<'a> TokenIterator<'a> { diff --git a/src/unsafe.rs b/src/unsafe.rs index efbf80c7..46d91198 100644 --- a/src/unsafe.rs +++ b/src/unsafe.rs @@ -61,8 +61,3 @@ pub fn unsafe_cast_var_name_to_lifetime<'s>(name: &str, state: &State) -> Cow<'s name.to_string().into() } } - -/// Provide a type instance that is uninitialized. -pub fn unsafe_uninit() -> T { - unsafe { mem::MaybeUninit::uninit().assume_init() } -} diff --git a/src/utils.rs b/src/utils.rs index e7e62eeb..a8f73390 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,9 +2,7 @@ //! //! # Safety //! -//! The `StaticVec` type has some `unsafe` blocks. - -use crate::r#unsafe::unsafe_uninit; +//! The `StaticVec` type has some `unsafe` blocks to handle conversions between `MaybeUninit` and regular types. use crate::stdlib::{ any::TypeId, @@ -12,7 +10,8 @@ use crate::stdlib::{ hash::{Hash, Hasher}, iter::FromIterator, mem, - ops::Drop, + mem::MaybeUninit, + ops::{Drop, Index, IndexMut}, vec::Vec, }; @@ -52,13 +51,41 @@ pub fn calc_fn_spec<'a>( s.finish() } -const MAX_STATIC_VEC: usize = 4; - -/// A type to hold a number of values in static storage for speed, and any spill-overs in a `Vec`. +/// A type to hold a number of values in static storage for no-allocation, quick access. +/// If too many items are stored, it converts into using a `Vec`. /// /// This is essentially a knock-off of the [`staticvec`](https://crates.io/crates/staticvec) crate. /// This simplified implementation here is to avoid pulling in another crate. /// +/// # Implementation +/// +/// A `StaticVec` holds data in _either one_ of two storages: 1) a fixed-size array of `MAX_STATIC_VEC` +/// items, and 2) a dynamic `Vec`. At any time, either one of them (or both) must be empty, depending on the +/// total number of items. +/// +/// There is a `len` field containing the total number of items held by the `StaticVec`. +/// +/// The fixed-size array (`list`) is not initialized (i.e. initialized with `MaybeUninit::uninit()`). +/// +/// When `len <= MAX_STATIC_VEC`, all elements are stored in the fixed-size array. +/// Array slots `>= len` are `MaybeUninit::uninit()` while slots `< len` are considered actual data. +/// In this scenario, the `Vec` (`more`) is empty. +/// +/// As soon as we try to push a new item into the `StaticVec` that makes the total number exceed +/// `MAX_STATIC_VEC`, all the items in the fixed-sized array are taken out, replaced with +/// `MaybeUninit::uninit()` (via `mem::replace`) and pushed into the `Vec`. +/// Then the new item is added to the `Vec`. +/// +/// Therefore, if `len > MAX_STATIC_VEC`, then the fixed-size array (`list`) is considered +/// empty and uninitialized while all data resides in the `Vec` (`more`). +/// +/// When popping an item off of the `StaticVec`, the reverse is true. When `len = MAX_STATIC_VEC + 1`, +/// after popping the item, all the items residing in the `Vec` are moved back to the fixed-size array (`list`). +/// The `Vec` will then be empty. +/// +/// Therefore, if `len <= MAX_STATIC_VEC`, data is in the fixed-size array (`list`). +/// Otherwise, data is in the `Vec` (`more`). +/// /// # Safety /// /// This type uses some unsafe code (mainly for uninitialized/unused array slots) for efficiency. @@ -67,12 +94,16 @@ const MAX_STATIC_VEC: usize = 4; pub struct StaticVec { /// Total number of values held. len: usize, - /// Static storage. 4 slots should be enough for most cases - i.e. four items of fast, no-allocation access. - list: [mem::MaybeUninit; MAX_STATIC_VEC], + /// Fixed-size storage for fast, no-allocation access. + list: [MaybeUninit; MAX_STATIC_VEC], /// Dynamic storage. For spill-overs. more: Vec, } +/// Maximum slots of fixed-size storage for a `StaticVec`. +/// 4 slots should be enough for most cases. +const MAX_STATIC_VEC: usize = 4; + impl Drop for StaticVec { fn drop(&mut self) { self.clear(); @@ -83,7 +114,7 @@ impl Default for StaticVec { fn default() -> Self { Self { len: 0, - list: unsafe_uninit(), + list: unsafe { mem::MaybeUninit::uninit().assume_init() }, more: Vec::new(), } } @@ -91,9 +122,18 @@ impl Default for StaticVec { impl PartialEq for StaticVec { fn eq(&self, other: &Self) -> bool { - self.len == other.len - //&& self.list[0..self.len] == other.list[0..self.len] - && self.more == other.more + if self.len != other.len || self.more != other.more { + return false; + } + + if self.len > MAX_STATIC_VEC { + return true; + } + + unsafe { + mem::transmute::<_, &[T; MAX_STATIC_VEC]>(&self.list) + == mem::transmute::<_, &[T; MAX_STATIC_VEC]>(&other.list) + } } } @@ -102,10 +142,10 @@ impl Clone for StaticVec { let mut value: Self = Default::default(); value.len = self.len; - if self.len <= self.list.len() { + if self.is_fixed_storage() { for x in 0..self.len { let item: &T = unsafe { mem::transmute(self.list.get(x).unwrap()) }; - value.list[x] = mem::MaybeUninit::new(item.clone()); + value.list[x] = MaybeUninit::new(item.clone()); } } else { value.more = self.more.clone(); @@ -130,72 +170,118 @@ impl FromIterator for StaticVec { } impl StaticVec { - fn extract(value: mem::MaybeUninit) -> T { - unsafe { value.assume_init() } - } /// Create a new `StaticVec`. pub fn new() -> Self { Default::default() } /// Empty the `StaticVec`. pub fn clear(&mut self) { - if self.len <= self.list.len() { + if self.is_fixed_storage() { for x in 0..self.len { - Self::extract(mem::replace( - self.list.get_mut(x).unwrap(), - mem::MaybeUninit::uninit(), - )); + self.extract_from_list(x); } } else { self.more.clear(); } self.len = 0; } + /// Extract a `MaybeUninit` into a concrete initialized type. + fn extract(value: MaybeUninit) -> T { + unsafe { value.assume_init() } + } + /// Extract an item from the fixed-size array, replacing it with `MaybeUninit::uninit()`. + /// + /// # Panics + /// + /// Panics if fixed-size storage is not used, or if the `index` is out of bounds. + fn extract_from_list(&mut self, index: usize) -> T { + if !self.is_fixed_storage() { + panic!("not fixed storage in StaticVec"); + } + if index >= self.len { + panic!("index OOB in StaticVec"); + } + Self::extract(mem::replace( + self.list.get_mut(index).unwrap(), + MaybeUninit::uninit(), + )) + } + /// Set an item into the fixed-size array. + /// If `drop` is `true`, the original value is extracted then automatically dropped. + /// + /// # Panics + /// + /// Panics if fixed-size storage is not used, or if the `index` is out of bounds. + fn set_into_list(&mut self, index: usize, value: T, drop: bool) { + if !self.is_fixed_storage() { + panic!("not fixed storage in StaticVec"); + } + // Allow setting at most one slot to the right + if index > self.len { + panic!("index OOB in StaticVec"); + } + let temp = mem::replace(self.list.get_mut(index).unwrap(), MaybeUninit::new(value)); + if drop { + // Extract the original value - which will drop it automatically + Self::extract(temp); + } + } + /// Move item in the fixed-size array into the `Vec`. + /// + /// # Panics + /// + /// Panics if fixed-size storage is not used, or if the fixed-size storage is not full. + fn move_fixed_into_vec(&mut self, num: usize) { + if !self.is_fixed_storage() { + panic!("not fixed storage in StaticVec"); + } + if self.len != num { + panic!("fixed storage is not full in StaticVec"); + } + self.more.extend( + self.list + .iter_mut() + .take(num) + .map(|v| mem::replace(v, MaybeUninit::uninit())) + .map(Self::extract), + ); + } + /// Is data stored in fixed-size storage? + fn is_fixed_storage(&self) -> bool { + self.len <= MAX_STATIC_VEC + } /// Push a new value to the end of this `StaticVec`. pub fn push>(&mut self, value: X) { - if self.len == self.list.len() { - // Move the fixed list to the Vec - self.more.extend( - self.list - .iter_mut() - .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) - .map(Self::extract), - ); + if self.len == MAX_STATIC_VEC { + self.move_fixed_into_vec(MAX_STATIC_VEC); self.more.push(value.into()); - } else if self.len < self.list.len() { - mem::replace( - self.list.get_mut(self.len).unwrap(), - mem::MaybeUninit::new(value.into()), - ); + } else if self.is_fixed_storage() { + self.set_into_list(self.len, value.into(), false); } else { self.more.push(value.into()); } self.len += 1; } /// Insert a new value to this `StaticVec` at a particular position. + /// + /// # Panics + /// + /// Panics if `index` is out of bounds. pub fn insert>(&mut self, index: usize, value: X) { if index > self.len { panic!("index OOB in StaticVec"); } - if self.len == self.list.len() { - // Move the fixed list to the Vec - self.more.extend( - self.list - .iter_mut() - .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) - .map(Self::extract), - ); + if self.len == MAX_STATIC_VEC { + self.move_fixed_into_vec(MAX_STATIC_VEC); self.more.insert(index, value.into()); - } else if self.len < self.list.len() { + } else if self.is_fixed_storage() { + // Move all items one slot to the right for x in (index..self.len).rev() { - let temp = mem::replace(self.list.get_mut(x).unwrap(), mem::MaybeUninit::uninit()); - mem::replace(self.list.get_mut(x + 1).unwrap(), temp); + let orig_value = self.extract_from_list(x); + self.set_into_list(x + 1, orig_value, false); } - mem::replace( - self.list.get_mut(index).unwrap(), - mem::MaybeUninit::new(value.into()), - ); + self.set_into_list(index, value.into(), false); } else { self.more.insert(index, value.into()); } @@ -207,29 +293,62 @@ impl StaticVec { /// /// Panics if the `StaticVec` is empty. pub fn pop(&mut self) -> T { - if self.len <= 0 { + if self.is_empty() { panic!("nothing to pop!"); } - let result = if self.len <= self.list.len() { - Self::extract(mem::replace( - self.list.get_mut(self.len - 1).unwrap(), - mem::MaybeUninit::uninit(), - )) + let result = if self.is_fixed_storage() { + self.extract_from_list(self.len - 1) } else { - let r = self.more.pop().unwrap(); + let value = self.more.pop().unwrap(); // Move back to the fixed list - if self.more.len() == self.list.len() { - for index in (0..self.list.len()).rev() { - mem::replace( - self.list.get_mut(index).unwrap(), - mem::MaybeUninit::new(self.more.pop().unwrap()), - ); + if self.more.len() == MAX_STATIC_VEC { + for index in (0..MAX_STATIC_VEC).rev() { + let item = self.more.pop().unwrap(); + self.set_into_list(index, item, false); } } - r + value + }; + + self.len -= 1; + + result + } + /// Remove a value from this `StaticVec` at a particular position. + /// + /// # Panics + /// + /// Panics if `index` is out of bounds. + pub fn remove(&mut self, index: usize) -> T { + if index >= self.len { + panic!("index OOB in StaticVec"); + } + + let result = if self.is_fixed_storage() { + let value = self.extract_from_list(index); + + // Move all items one slot to the left + for x in index..self.len - 1 { + let orig_value = self.extract_from_list(x + 1); + self.set_into_list(x, orig_value, false); + } + + value + } else { + let value = self.more.remove(index); + + // Move back to the fixed list + if self.more.len() == MAX_STATIC_VEC { + for index in (0..MAX_STATIC_VEC).rev() { + let item = self.more.pop().unwrap(); + self.set_into_list(index, item, false); + } + } + + value }; self.len -= 1; @@ -248,15 +367,15 @@ impl StaticVec { /// /// # Panics /// - /// Panics if the index is out of bounds. - pub fn get_ref(&self, index: usize) -> &T { + /// Panics if `index` is out of bounds. + pub fn get(&self, index: usize) -> &T { if index >= self.len { panic!("index OOB in StaticVec"); } let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { list.get(index).unwrap() } else { self.more.get(index).unwrap() @@ -266,7 +385,7 @@ impl StaticVec { /// /// # Panics /// - /// Panics if the index is out of bounds. + /// Panics if `index` is out of bounds. pub fn get_mut(&mut self, index: usize) -> &mut T { if index >= self.len { panic!("index OOB in StaticVec"); @@ -274,7 +393,7 @@ impl StaticVec { let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { list.get_mut(index).unwrap() } else { self.more.get_mut(index).unwrap() @@ -284,7 +403,7 @@ impl StaticVec { pub fn iter(&self) -> impl Iterator { let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { list[..self.len].iter() } else { self.more.iter() @@ -294,7 +413,7 @@ impl StaticVec { pub fn iter_mut(&mut self) -> impl Iterator { let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { list[..self.len].iter_mut() } else { self.more.iter_mut() @@ -302,18 +421,66 @@ impl StaticVec { } } +impl StaticVec { + /// Get a mutable iterator to entries in the `StaticVec`. + pub fn into_iter(mut self) -> Box> { + if self.is_fixed_storage() { + let mut it = FixedStorageIterator { + data: unsafe { mem::MaybeUninit::uninit().assume_init() }, + index: 0, + limit: self.len, + }; + + for x in 0..self.len { + it.data[x] = mem::replace(self.list.get_mut(x).unwrap(), MaybeUninit::uninit()); + } + self.len = 0; + + Box::new(it) + } else { + Box::new(Vec::from(self).into_iter()) + } + } +} + +/// An iterator that takes control of the fixed-size storage of a `StaticVec` and returns its values. +struct FixedStorageIterator { + data: [MaybeUninit; MAX_STATIC_VEC], + index: usize, + limit: usize, +} + +impl Iterator for FixedStorageIterator { + type Item = T; + + fn next(&mut self) -> Option { + if self.index >= self.limit { + None + } else { + self.index += 1; + + let value = mem::replace( + self.data.get_mut(self.index - 1).unwrap(), + MaybeUninit::uninit(), + ); + + unsafe { Some(value.assume_init()) } + } + } +} + impl StaticVec { /// Get the item at a particular index, replacing it with the default. /// /// # Panics /// - /// Panics if the index is out of bounds. - pub fn get(&mut self, index: usize) -> T { + /// Panics if `index` is out of bounds. + pub fn take(&mut self, index: usize) -> T { if index >= self.len { panic!("index OOB in StaticVec"); } - mem::take(if self.len <= self.list.len() { + mem::take(if self.is_fixed_storage() { unsafe { mem::transmute(self.list.get_mut(index).unwrap()) } } else { self.more.get_mut(index).unwrap() @@ -333,7 +500,7 @@ impl AsRef<[T]> for StaticVec { fn as_ref(&self) -> &[T] { let list: &[T; MAX_STATIC_VEC] = unsafe { mem::transmute(&self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { &list[..self.len] } else { &self.more[..] @@ -345,7 +512,7 @@ impl AsMut<[T]> for StaticVec { fn as_mut(&mut self) -> &mut [T] { let list: &mut [T; MAX_STATIC_VEC] = unsafe { mem::transmute(&mut self.list) }; - if self.len <= list.len() { + if self.is_fixed_storage() { &mut list[..self.len] } else { &mut self.more[..] @@ -353,20 +520,30 @@ impl AsMut<[T]> for StaticVec { } } +impl Index for StaticVec { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + self.get(index) + } +} + +impl IndexMut for StaticVec { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + self.get_mut(index) + } +} + impl From> for Vec { fn from(mut value: StaticVec) -> Self { - if value.len <= value.list.len() { - value - .list - .iter_mut() - .map(|v| mem::replace(v, mem::MaybeUninit::uninit())) - .map(StaticVec::extract) - .collect() - } else { - let mut arr = Self::new(); - arr.append(&mut value.more); - arr + if value.len <= MAX_STATIC_VEC { + value.move_fixed_into_vec(value.len); } + value.len = 0; + + let mut arr = Self::new(); + arr.append(&mut value.more); + arr } } @@ -375,12 +552,9 @@ impl From> for StaticVec { let mut arr: Self = Default::default(); arr.len = value.len(); - if arr.len <= arr.list.len() { + if arr.len <= MAX_STATIC_VEC { for x in (0..arr.len).rev() { - mem::replace( - arr.list.get_mut(x).unwrap(), - mem::MaybeUninit::new(value.pop().unwrap()), - ); + arr.set_into_list(x, value.pop().unwrap(), false); } } else { arr.more = value; diff --git a/tests/modules.rs b/tests/modules.rs index 569cb4af..9c6c8ec4 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -84,28 +84,39 @@ fn test_module_resolver() -> Result<(), Box> { assert!(matches!( *engine - .eval::<()>( + .eval::( r#" + let x = 0; + for x in range(0, 10) { import "hello" as h; + x += h::answer; } + + x "# ) .expect_err("should error"), EvalAltResult::ErrorTooManyModules(_) )); + #[cfg(not(feature = "no_function"))] assert!(matches!( *engine - .eval::<()>( + .eval::( r#" + let x = 0; + fn foo() { import "hello" as h; + x += h::answer; } for x in range(0, 10) { foo(); } + + x "# ) .expect_err("should error"), @@ -114,6 +125,7 @@ fn test_module_resolver() -> Result<(), Box> { engine.set_max_modules(0); + #[cfg(not(feature = "no_function"))] engine.eval::<()>( r#" fn foo() { diff --git a/tests/operations.rs b/tests/operations.rs index 2637ef3b..c02e2381 100644 --- a/tests/operations.rs +++ b/tests/operations.rs @@ -43,20 +43,40 @@ fn test_max_operations_functions() -> Result<(), Box> { engine.eval::<()>( r#" + print("Test1"); + let x = 0; + + while x < 28 { + print(x); + x += 1; + } + "#, + )?; + + #[cfg(not(feature = "no_function"))] + engine.eval::<()>( + r#" + print("Test2"); fn inc(x) { x + 1 } let x = 0; while x < 20 { x = inc(x); } "#, )?; + #[cfg(not(feature = "no_function"))] assert!(matches!( *engine .eval::<()>( r#" + print("Test3"); fn inc(x) { x + 1 } let x = 0; - while x < 1000 { x = inc(x); } - "# + + while x < 28 { + print(x); + x = inc(x); + } + "#, ) .expect_err("should error"), EvalAltResult::ErrorTooManyOperations(_) From f4a528a88a24782b6878becce9f31a4f430f1790 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 18 May 2020 09:36:34 +0800 Subject: [PATCH 25/36] Add release notes. --- README.md | 69 +++++++++++++++++++++++++++------------------------ RELEASES.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/engine.rs | 6 ++--- 3 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 RELEASES.md diff --git a/README.md b/README.md index 46bae69f..831f3765 100644 --- a/README.md +++ b/README.md @@ -13,25 +13,25 @@ to add scripting to any application. Rhai's current features set: -* Easy-to-use language similar to JS+Rust with dynamic typing but _no_ garbage collector +* Easy-to-use language similar to JS+Rust with dynamic typing but _no_ garbage collector. * Tight integration with native Rust [functions](#working-with-functions) and [types](#custom-types-and-methods), - including [getters/setters](#getters-and-setters), [methods](#members-and-methods) and [indexers](#indexers) -* Freely pass Rust variables/constants into a script via an external [`Scope`] -* Easily [call a script-defined function](#calling-rhai-functions-from-rust) from Rust -* Low compile-time overhead (~0.6 sec debug/~3 sec release for `rhai_runner` sample app) -* Fairly efficient evaluation (1 million iterations in 0.75 sec on my 5 year old laptop) + including [getters/setters](#getters-and-setters), [methods](#members-and-methods) and [indexers](#indexers). +* Freely pass Rust variables/constants into a script via an external [`Scope`]. +* Easily [call a script-defined function](#calling-rhai-functions-from-rust) from Rust. +* Low compile-time overhead (~0.6 sec debug/~3 sec release for `rhai_runner` sample app). +* Fairly efficient evaluation (1 million iterations in 0.75 sec on my 5 year old laptop). * Relatively little `unsafe` code (yes there are some for performance reasons, and most `unsafe` code is limited to - one single source file, all with names starting with `"unsafe_"`) -* Sand-boxed (the scripting [`Engine`] can be declared immutable which cannot mutate the containing environment - unless explicitly allowed via `RefCell` etc.) -* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.) -* Able to track script evaluation [progress](#tracking-progress) and manually terminate a script run -* [`no-std`](#optional-features) support -* [Function overloading](#function-overloading) -* [Operator overloading](#operator-overloading) -* [Modules] -* Compiled script is [optimized](#script-optimization) for repeated evaluations -* Support for [minimal builds](#minimal-builds) by excluding unneeded language [features](#optional-features) + one single source file, all with names starting with `"unsafe_"`). +* Re-entrant scripting [`Engine`] can be made `Send + Sync` (via the [`sync`] feature). +* Sand-boxed - the scripting [`Engine`], if declared immutable, cannot mutate the containing environment without explicit permission. +* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). +* Track script evaluation [progress](#tracking-progress) and manually terminate a script run. +* [`no-std`](#optional-features) support. +* [Function overloading](#function-overloading). +* [Operator overloading](#operator-overloading). +* Organize code base with dynamically-loadable [Modules]. +* Compiled script is [optimized](#script-optimization) for repeated evaluations. +* Support for [minimal builds](#minimal-builds) by excluding unneeded language [features](#optional-features). * Very few additional dependencies (right now only [`num-traits`](https://crates.io/crates/num-traits/) to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are pulled in to provide for functionalities that used to be in `std`. @@ -111,7 +111,7 @@ requiring more CPU cycles to complete. Also, turning on `no_float`, and `only_i32` makes the key [`Dynamic`] data type only 8 bytes small on 32-bit targets while normally it can be up to 16 bytes (e.g. on x86/x64 CPU's) in order to hold an `i64` or `f64`. -Making [`Dynamic`] small helps performance due to more caching efficiency. +Making [`Dynamic`] small helps performance due to better cache efficiency. ### Minimal builds @@ -120,6 +120,8 @@ the correct linker flags are used in `cargo.toml`: ```toml [profile.release] +lto = "fat" # turn on Link-Time Optimizations +codegen-units = 1 # trade compile time with maximum optimization opt-level = "z" # optimize for size ``` @@ -417,7 +419,7 @@ Therefore, a package only has to be created _once_. Packages are actually implemented as [modules], so they share a lot of behavior and characteristics. The main difference is that a package loads under the _global_ namespace, while a module loads under its own -namespace alias specified in an `import` statement (see also [modules]). +namespace alias specified in an [`import`] statement (see also [modules]). A package is _static_ (i.e. pre-loaded into an [`Engine`]), while a module is _dynamic_ (i.e. loaded with the `import` statement). @@ -2113,6 +2115,8 @@ export x as answer; // the variable 'x' is exported under the alias 'ans ### Importing modules +[`import`]: #importing-modules + A module can be _imported_ via the `import` statement, and its members are accessed via '`::`' similar to C++. ```rust @@ -2258,17 +2262,17 @@ Ruggedization - protect against DoS attacks ------------------------------------------ For scripting systems open to user-land scripts, it is always best to limit the amount of resources used by a script -so that it does not crash the system by consuming all resources. +so that it does not consume more resources that it is allowed to. The most important resources to watch out for are: * **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. * **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. -* **Time**: A malignant script may run indefinitely, thereby blocking the calling system waiting for a result. -* **Stack**: A malignant script may consume attempt an infinite recursive call that exhausts the call stack. -* **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, and/or bad - floating-point representations, in order to crash the system. -* **Files**: A malignant script may continuously `import` an external module within an infinite loop, +* **Time**: A malignant script may run indefinitely, thereby blocking the calling system which is waiting for a result. +* **Stack**: A malignant script may attempt an infinite recursive call that exhausts the call stack. +* **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, divide by zero, and/or + create bad floating-point representations, in order to crash the system. +* **Files**: A malignant script may continuously [`import`] an external module within an infinite loop, thereby putting heavy load on the file-system (or even the network if the file is not local). Even when modules are not created from files, they still typically consume a lot of resources to load. * **Data**: A malignant script may attempt to read from and/or write to data that it does not own. If this happens, @@ -2288,13 +2292,14 @@ engine.set_max_operations(0); // allow unlimited operations ``` The concept of one single _operation_ in Rhai is volatile - it roughly equals one expression node, -loading a variable/constant, one operator call, one complete statement, one iteration of a loop, -or one function call etc. with sub-expressions and statement blocks executed inside these contexts accumulated on top. +loading one variable/constant, one operator call, one iteration of a loop, or one function call etc. +with sub-expressions, statements and function calls executed inside these contexts accumulated on top. A good rule-of-thumb is that one simple non-trivial expression consumes on average 5-10 operations. -One _operation_ can take an unspecified amount of time and CPU cycles, depending on the particular operation -involved. For example, loading a constant consumes very few CPU cycles, while calling an external Rust function, -though also counted as only one operation, may consume much more resources. +One _operation_ can take an unspecified amount of time and real CPU cycles, depending on the particulars. +For example, loading a constant consumes very few CPU cycles, while calling an external Rust function, +though also counted as only one operation, may consume much more computing resources. +If it helps to visualize, think of an _operation_ as roughly equals to one _instruction_ of a hypothetical CPU. The _operation count_ is intended to be a very course-grained measurement of the amount of CPU that a script is consuming, and allows the system to impose a hard upper limit. @@ -2325,7 +2330,7 @@ Return `false` to terminate the script immediately. ### Maximum number of modules -Rhai by default does not limit how many [modules] are loaded via the `import` statement. +Rhai by default does not limit how many [modules] are loaded via the [`import`] statement. This can be changed via the `Engine::set_max_modules` method, with zero being unlimited (the default). ```rust @@ -2360,7 +2365,7 @@ it detects a numeric over-flow/under-flow condition or an invalid floating-point crashing the entire system. This checking can be turned off via the [`unchecked`] feature for higher performance (but higher risks as well). -### Access to external data +### Blocking access to external data Rhai is _sand-boxed_ so a script can never read from outside its own environment. Furthermore, an [`Engine`] created non-`mut` cannot mutate any state outside of itself; diff --git a/RELEASES.md b/RELEASES.md new file mode 100644 index 00000000..cee016d7 --- /dev/null +++ b/RELEASES.md @@ -0,0 +1,66 @@ +Rhai Release Notes +================== + +Version 0.14.1 +============== + +The major features for this release is modules, script resource limits, and speed improvements +(mainly due to avoiding allocations). + +New features +------------ + +* Modules and _module resolvers_ allow loading external scripts under a module namespace. + A module can contain constant variables, Rust functions and Rhai functions. +* `export` variables and `private` functions. +* _Indexers_ for Rust types. +* Track script evaluation progress and terminate script run. +* Set limit on maximum number of operations allowed per script run. +* Set limit on maximum number of modules loaded per script run. +* A new API, `Engine::compile_scripts_with_scope`, can compile a list of script segments without needing to + first concatenate them together into one large string. +* Stepped `range` function with a custom step. + +Speed improvements +------------------ + +### `StaticVec` + +A script contains many lists - statements in a block, arguments to a function call etc. +In a typical script, most of these lists tend to be short - e.g. the vast majority of function calls contain +fewer than 4 arguments, while most statement blocks have fewer than 4-5 statements, with one or two being +the most common. Before, dynamic `Vec`'s are used to hold these short lists for very brief periods of time, +causing allocations churn. + +In this version, large amounts of allocations are avoided by converting to a `StaticVec` - +a list type based on a static array for a small number of items (currently four) - +wherever possible plus other tricks. Most real-life scripts should see material speed increases. + +### Pre-computed variable lookups + +Almost all variable lookups, as well as lookups in loaded modules, are now pre-computed. +A variable's name is almost never used to search for the variable in the current scope. + +_Getters_ and _setter_ function names are also pre-computed and cached, so no string allocations are +performed during a property get/set call. + +### Pre-computed function call hashes + +Lookup of all function calls, including Rust and Rhai ones, are now through pre-computed hashes. +The function name is no longer used to search for a function, making function call dispatches +much faster. + +### Large Boxes for expressions and statements + +The expression (`Expr`) and statement (`Stmt`) types are modified so that all of the variants contain only +one single `Box` to a large allocated structure containing _all_ the fields. This makes the `Expr` and +`Stmt` types very small (only one single pointer) and improves evaluation speed due to cache efficiency. + +Error handling +-------------- + +Previously, when an error occurs inside a function call, the error position reported is the function +call site. This makes it difficult to diagnose the actual location of the error within the function. + +A new error variant `EvalAltResult::ErrorInFunctionCall` is added in this version. +It wraps the internal error returned by the called function, including the error position within the function. diff --git a/src/engine.rs b/src/engine.rs index 4c91f39c..3febbc6f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1768,7 +1768,7 @@ impl Engine { // Let statement Stmt::Let(x) if x.1.is_some() => { - let ((var_name, pos), expr) = x.as_ref(); + let ((var_name, _), expr) = x.as_ref(); let val = self.eval_expr(scope, state, expr.as_ref().unwrap(), level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); @@ -1776,7 +1776,7 @@ impl Engine { } Stmt::Let(x) => { - let ((var_name, pos), _) = x.as_ref(); + let ((var_name, _), _) = x.as_ref(); let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push(var_name, ()); Ok(Default::default()) @@ -1784,7 +1784,7 @@ impl Engine { // Const statement Stmt::Const(x) if x.1.is_constant() => { - let ((var_name, pos), expr) = x.as_ref(); + let ((var_name, _), expr) = x.as_ref(); let val = self.eval_expr(scope, state, &expr, level)?; let var_name = unsafe_cast_var_name_to_lifetime(var_name, &state); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); From 1824dced69a9b15e6f94bd14d00b9ff157be1c0d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 18 May 2020 19:32:22 +0800 Subject: [PATCH 26/36] Limit expression/statement nesting depths. --- README.md | 82 +++++++-- RELEASES.md | 9 + src/api.rs | 37 +++- src/engine.rs | 37 +++- src/error.rs | 7 +- src/parser.rs | 479 +++++++++++++++++++++++++++++++------------------ tests/stack.rs | 60 ++++++- 7 files changed, 513 insertions(+), 198 deletions(-) diff --git a/README.md b/README.md index 831f3765..6edb1ca7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Rhai's current features set: one single source file, all with names starting with `"unsafe_"`). * Re-entrant scripting [`Engine`] can be made `Send + Sync` (via the [`sync`] feature). * Sand-boxed - the scripting [`Engine`], if declared immutable, cannot mutate the containing environment without explicit permission. -* Rugged (protection against [stack-overflow](#maximum-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). +* Rugged (protection against [stack-overflow](#maximum-call-stack-depth) and [runaway scripts](#maximum-number-of-operations) etc.). * Track script evaluation [progress](#tracking-progress) and manually terminate a script run. * [`no-std`](#optional-features) support. * [Function overloading](#function-overloading). @@ -1066,12 +1066,13 @@ fn main() -> Result<(), Box> Engine configuration options --------------------------- -| Method | Description | -| ------------------------ | ---------------------------------------------------------------------------------------- | -| `set_optimization_level` | Set the amount of script _optimizations_ performed. See [`script optimization`]. | -| `set_max_call_levels` | Set the maximum number of function call levels (default 50) to avoid infinite recursion. | - -[`script optimization`]: #script-optimization +| Method | Description | +| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| `set_optimization_level` | Set the amount of script _optimizations_ performed. See [script optimization]. | +| `set_max_expr_depths` | Set the maximum nesting levels of an expression/statement. See [maximum statement depth](#maximum-statement-depth). | +| `set_max_call_levels` | Set the maximum number of function call levels (default 50) to avoid infinite recursion. See [maximum call stack depth](#maximum-call-stack-depth). | +| `set_max_operations` | Set the maximum number of _operations_ that a script is allowed to consume. See [maximum number of operations](#maximum-number-of-operations). | +| `set_max_modules` | Set the maximum number of [modules] that a script is allowed to load. See [maximum number of modules](#maximum-number-of-modules). | ------- @@ -2267,9 +2268,12 @@ so that it does not consume more resources that it is allowed to. The most important resources to watch out for are: * **Memory**: A malignant script may continuously grow an [array] or [object map] until all memory is consumed. + It may also create a large [array] or [objecct map] literal that exhausts all memory during parsing. * **CPU**: A malignant script may run an infinite tight loop that consumes all CPU cycles. * **Time**: A malignant script may run indefinitely, thereby blocking the calling system which is waiting for a result. * **Stack**: A malignant script may attempt an infinite recursive call that exhausts the call stack. + Alternatively, it may create a degenerated deep expression with so many levels that the parser exhausts the call stack + when parsing the expression; or even deeply-nested statement blocks, if nested deep enough. * **Overflows**: A malignant script may deliberately cause numeric over-flows and/or under-flows, divide by zero, and/or create bad floating-point representations, in order to crash the system. * **Files**: A malignant script may continuously [`import`] an external module within an infinite loop, @@ -2341,10 +2345,15 @@ engine.set_max_modules(5); // allow loading only up to 5 module engine.set_max_modules(0); // allow unlimited modules ``` -### Maximum stack depth +### Maximum call stack depth -Rhai by default limits function calls to a maximum depth of 256 levels (28 levels in debug build). +Rhai by default limits function calls to a maximum depth of 128 levels (8 levels in debug build). This limit may be changed via the `Engine::set_max_call_levels` method. + +When setting this limit, care must be also taken to the evaluation depth of each _statement_ +within the function. It is entirely possible for a malignant script to embed an recursive call deep +inside a nested expression or statement block (see [maximum statement depth](#maximum-statement-depth)). + The limit can be disabled via the [`unchecked`] feature for higher performance (but higher risks as well). @@ -2358,12 +2367,57 @@ engine.set_max_call_levels(0); // allow no function calls at all (m A script exceeding the maximum call stack depth will terminate with an error result. +### Maximum statement depth + +Rhai by default limits statements and expressions nesting to a maximum depth of 128 +(which should be plenty) when they are at _global_ level, but only a depth of 32 +when they are within function bodies. For debug builds, these limits are set further +downwards to 32 and 16 respectively. + +That is because it is possible to overflow the [`Engine`]'s stack when it tries to +recursively parse an extremely deeply-nested code stream. + +```rust +// The following, if long enough, can easily cause stack overflow during parsing. +let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(...)+1))))))))))); +``` + +This limit may be changed via the `Engine::set_max_expr_depths` method. There are two limits to set, +one for the maximum depth at global level, and the other for function bodies. + +```rust +let mut engine = Engine::new(); + +engine.set_max_expr_depths(50, 5); // allow nesting up to 50 layers of expressions/statements + // at global level, but only 5 inside functions +``` + +Beware that there may be multiple layers for a simple language construct, even though it may correspond +to only one AST node. That is because the Rhai _parser_ internally runs a recursive chain of function calls +and it is important that a malignant script does not panic the parser in the first place. + +Functions are placed under stricter limits because of the multiplicative effect of recursion. +A script can effectively call itself while deep inside an expression chain within the function body, +thereby overflowing the stack even when the level of recursion is within limit. + +Make sure that `C x ( 5 + F ) + S` layered calls do not cause a stack overflow, where: + +* `C` = maximum call stack depth, +* `F` = maximum statement depth for functions, +* `S` = maximum statement depth at global level. + +A script exceeding the maximum nesting depths will terminate with a parsing error. +The malignant `AST` will not be able to get past parsing in the first place. + +The limits can be disabled via the [`unchecked`] feature for higher performance +(but higher risks as well). + ### Checked arithmetic -All arithmetic calculations in Rhai are _checked_, meaning that the script terminates with an error whenever -it detects a numeric over-flow/under-flow condition or an invalid floating-point operation, instead of -crashing the entire system. This checking can be turned off via the [`unchecked`] feature for higher performance -(but higher risks as well). +By default, all arithmetic calculations in Rhai are _checked_, meaning that the script terminates +with an error whenever it detects a numeric over-flow/under-flow condition or an invalid +floating-point operation, instead of crashing the entire system. This checking can be turned off +via the [`unchecked`] feature for higher performance (but higher risks as well). ### Blocking access to external data @@ -2383,6 +2437,8 @@ let engine = engine; // shadow the variable so that 'engi Script optimization =================== +[script optimization]: #script-optimization + Rhai includes an _optimizer_ that tries to optimize a script after parsing. This can reduce resource utilization and increase execution speed. Script optimization can be turned off via the [`no_optimize`] feature. diff --git a/RELEASES.md b/RELEASES.md index cee016d7..825105a8 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 0.15.0 +============== + +New features +------------ + +* Set limits on maximum level of nesting expressions and statements to avoid panics during parsing. + + Version 0.14.1 ============== diff --git a/src/api.rs b/src/api.rs index 26a825ab..f0e249e9 100644 --- a/src/api.rs +++ b/src/api.rs @@ -444,7 +444,14 @@ impl Engine { optimization_level: OptimizationLevel, ) -> Result> { let stream = lex(scripts); - parse(&mut stream.peekable(), self, scope, optimization_level) + + parse( + &mut stream.peekable(), + self, + scope, + optimization_level, + (self.max_expr_depth, self.max_function_expr_depth), + ) } /// Read the contents of a file into a string. @@ -571,6 +578,7 @@ impl Engine { self, &scope, OptimizationLevel::None, + self.max_expr_depth, )?; // Handle null - map to () @@ -654,7 +662,13 @@ impl Engine { { let mut peekable = stream.peekable(); - parse_global_expr(&mut peekable, self, scope, self.optimization_level) + parse_global_expr( + &mut peekable, + self, + scope, + self.optimization_level, + self.max_expr_depth, + ) } } @@ -805,8 +819,14 @@ impl Engine { ) -> Result> { let scripts = [script]; let stream = lex(&scripts); - // Since the AST will be thrown away afterwards, don't bother to optimize it - let ast = parse_global_expr(&mut stream.peekable(), self, scope, OptimizationLevel::None)?; + + let ast = parse_global_expr( + &mut stream.peekable(), + self, + scope, + self.optimization_level, + self.max_expr_depth, + )?; self.eval_ast_with_scope(scope, &ast) } @@ -931,8 +951,13 @@ impl Engine { let scripts = [script]; let stream = lex(&scripts); - // Since the AST will be thrown away afterwards, don't bother to optimize it - let ast = parse(&mut stream.peekable(), self, scope, OptimizationLevel::None)?; + let ast = parse( + &mut stream.peekable(), + self, + scope, + self.optimization_level, + (self.max_expr_depth, self.max_function_expr_depth), + )?; self.consume_ast_with_scope(scope, &ast) } diff --git a/src/engine.rs b/src/engine.rs index 3febbc6f..6bd2bafa 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -49,14 +49,30 @@ pub type Map = HashMap; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] -pub const MAX_CALL_STACK_DEPTH: usize = 28; +pub const MAX_CALL_STACK_DEPTH: usize = 8; +#[cfg(not(feature = "unchecked"))] +#[cfg(debug_assertions)] +pub const MAX_EXPR_DEPTH: usize = 32; +#[cfg(not(feature = "unchecked"))] +#[cfg(debug_assertions)] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = 16; #[cfg(not(feature = "unchecked"))] #[cfg(not(debug_assertions))] -pub const MAX_CALL_STACK_DEPTH: usize = 256; +pub const MAX_CALL_STACK_DEPTH: usize = 128; +#[cfg(not(feature = "unchecked"))] +#[cfg(not(debug_assertions))] +pub const MAX_EXPR_DEPTH: usize = 128; +#[cfg(not(feature = "unchecked"))] +#[cfg(not(debug_assertions))] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32; #[cfg(feature = "unchecked")] pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX; +#[cfg(feature = "unchecked")] +pub const MAX_EXPR_DEPTH: usize = usize::MAX; +#[cfg(feature = "unchecked")] +pub const MAX_FUNCTION_EXPR_DEPTH: usize = usize::MAX; pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; @@ -338,8 +354,12 @@ pub struct Engine { pub(crate) optimization_level: OptimizationLevel, /// Maximum levels of call-stack to prevent infinite recursion. /// - /// Defaults to 28 for debug builds and 256 for non-debug builds. + /// Defaults to 8 for debug builds and 128 for non-debug builds. pub(crate) max_call_stack_depth: usize, + /// Maximum depth of statements/expressions at global level. + pub(crate) max_expr_depth: usize, + /// Maximum depth of statements/expressions in functions. + pub(crate) max_function_expr_depth: usize, /// Maximum number of operations allowed to run. pub(crate) max_operations: Option, /// Maximum number of modules allowed to load. @@ -382,6 +402,8 @@ impl Default for Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, max_operations: None, max_modules: None, }; @@ -523,6 +545,8 @@ impl Engine { optimization_level: OptimizationLevel::Full, max_call_stack_depth: MAX_CALL_STACK_DEPTH, + max_expr_depth: MAX_EXPR_DEPTH, + max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, max_operations: None, max_modules: None, } @@ -574,6 +598,13 @@ impl Engine { self.max_modules = NonZeroU64::new(modules); } + /// Set the depth limits for expressions/statements. + #[cfg(not(feature = "unchecked"))] + pub fn set_max_expr_depths(&mut self, max_expr_depth: usize, max_function_expr_depth: usize) { + self.max_expr_depth = max_expr_depth; + self.max_function_expr_depth = max_function_expr_depth; + } + /// Set the module resolution service used by the `Engine`. /// /// Not available under the `no_module` feature. diff --git a/src/error.rs b/src/error.rs index 11b7c9e2..8e180c25 100644 --- a/src/error.rs +++ b/src/error.rs @@ -110,6 +110,10 @@ pub enum ParseErrorType { AssignmentToCopy, /// Assignment to an a constant variable. AssignmentToConstant(String), + /// Expression exceeding the maximum levels of complexity. + /// + /// Never appears under the `unchecked` feature. + ExprTooDeep, /// Break statement not inside a loop. LoopBreak, } @@ -158,7 +162,8 @@ impl ParseError { ParseErrorType::DuplicatedExport(_) => "Duplicated variable/function in export statement", ParseErrorType::WrongExport => "Export statement can only appear at global level", ParseErrorType::AssignmentToCopy => "Only a copy of the value is change with this assignment", - ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant value.", + ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant value", + ParseErrorType::ExprTooDeep => "Expression exceeds maximum complexity", ParseErrorType::LoopBreak => "Break statement should only be used inside a loop" } } diff --git a/src/parser.rs b/src/parser.rs index 29c955e2..39fa6678 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -218,21 +218,27 @@ pub enum ReturnType { Exception, } -/// A type that encapsulates a local stack with variable names to simulate an actual runtime scope. #[derive(Debug, Clone, Default)] -struct Stack(Vec<(String, ScopeEntryType)>); +struct ParseState { + /// Encapsulates a local stack with variable names to simulate an actual runtime scope. + stack: Vec<(String, ScopeEntryType)>, + max_expr_depth: usize, +} -impl Stack { - /// Create a new `Stack`. - pub fn new() -> Self { - Default::default() +impl ParseState { + /// Create a new `ParseState`. + pub fn new(max_expr_depth: usize) -> Self { + Self { + max_expr_depth, + ..Default::default() + } } - /// Find a variable by name in the `Stack`, searching in reverse. + /// Find a variable by name in the `ParseState`, searching in reverse. /// The return value is the offset to be deducted from `Stack::len`, - /// i.e. the top element of the `Stack` is offset 1. - /// Return zero when the variable name is not found in the `Stack`. + /// i.e. the top element of the `ParseState` is offset 1. + /// Return zero when the variable name is not found in the `ParseState`. pub fn find(&self, name: &str) -> Option { - self.0 + self.stack .iter() .rev() .enumerate() @@ -242,12 +248,12 @@ impl Stack { }) .and_then(|(i, _)| NonZeroUsize::new(i + 1)) } - /// Find a module by name in the `Stack`, searching in reverse. + /// Find a module by name in the `ParseState`, searching in reverse. /// The return value is the offset to be deducted from `Stack::len`, - /// i.e. the top element of the `Stack` is offset 1. - /// Return zero when the variable name is not found in the `Stack`. + /// i.e. the top element of the `ParseState` is offset 1. + /// Return zero when the variable name is not found in the `ParseState`. pub fn find_module(&self, name: &str) -> Option { - self.0 + self.stack .iter() .rev() .enumerate() @@ -259,17 +265,17 @@ impl Stack { } } -impl Deref for Stack { +impl Deref for ParseState { type Target = Vec<(String, ScopeEntryType)>; fn deref(&self) -> &Self::Target { - &self.0 + &self.stack } } -impl DerefMut for Stack { +impl DerefMut for ParseState { fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 + &mut self.stack } } @@ -691,15 +697,20 @@ fn match_token(input: &mut Peekable, token: Token) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + if match_token(input, Token::RightParen)? { return Ok(Expr::Unit(pos)); } - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; match input.next().unwrap() { // ( xxx ) @@ -718,18 +729,25 @@ fn parse_paren_expr<'a>( /// Parse a function call. fn parse_call_expr<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, id: String, #[cfg(not(feature = "no_module"))] mut modules: Option>, #[cfg(feature = "no_module")] modules: Option, begin: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + let (token, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + let mut args = StaticVec::new(); - match input.peek().unwrap() { + match token { // id - (Token::EOF, pos) => { + Token::EOF => { return Err(PERR::MissingToken( Token::RightParen.into(), format!("to close the arguments list of this function call '{}'", id), @@ -737,15 +755,15 @@ fn parse_call_expr<'a>( .into_err(*pos)) } // id - (Token::LexError(err), pos) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), + Token::LexError(err) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), // id() - (Token::RightParen, _) => { + Token::RightParen => { eat_token(input, Token::RightParen); #[cfg(not(feature = "no_module"))] let hash_fn_def = { if let Some(modules) = modules.as_mut() { - modules.set_index(stack.find_module(&modules.get(0).0)); + modules.set_index(state.find_module(&modules.get(0).0)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -775,7 +793,7 @@ fn parse_call_expr<'a>( } loop { - args.push(parse_expr(input, stack, allow_stmt_expr)?); + args.push(parse_expr(input, state, level + 1, allow_stmt_expr)?); match input.peek().unwrap() { // id(...args) @@ -786,7 +804,7 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] let hash_fn_def = { if let Some(modules) = modules.as_mut() { - modules.set_index(stack.find_module(&modules.get(0).0)); + modules.set_index(state.find_module(&modules.get(0).0)); // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, @@ -843,12 +861,17 @@ fn parse_call_expr<'a>( /// Indexing binds to the right, so this call parses all possible levels of indexing following in the input. fn parse_index_chain<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let idx_expr = parse_expr(input, stack, allow_stmt_expr)?; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let idx_expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; // Check type of indexing - must be integer or string match &idx_expr { @@ -1001,7 +1024,14 @@ fn parse_index_chain<'a>( (Token::LeftBracket, _) => { let idx_pos = eat_token(input, Token::LeftBracket); // Recursively parse the indexing chain, right-binding each - let idx = parse_index_chain(input, stack, idx_expr, idx_pos, allow_stmt_expr)?; + let idx = parse_index_chain( + input, + state, + idx_expr, + idx_pos, + level + 1, + allow_stmt_expr, + )?; // Indexing binds to right Ok(Expr::Index(Box::new((lhs, idx, pos)))) } @@ -1021,15 +1051,20 @@ fn parse_index_chain<'a>( /// Parse an array literal. fn parse_array_literal<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut arr = StaticVec::new(); if !match_token(input, Token::RightBracket)? { while !input.peek().unwrap().0.is_eof() { - arr.push(parse_expr(input, stack, allow_stmt_expr)?); + arr.push(parse_expr(input, state, level + 1, allow_stmt_expr)?); match input.peek().unwrap() { (Token::Comma, _) => eat_token(input, Token::Comma), @@ -1064,10 +1099,15 @@ fn parse_array_literal<'a>( /// Parse a map literal. fn parse_map_literal<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, pos: Position, + level: usize, allow_stmt_expr: bool, ) -> Result> { + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut map = StaticVec::new(); if !match_token(input, Token::RightBrace)? { @@ -1112,7 +1152,7 @@ fn parse_map_literal<'a>( } }; - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; map.push(((name, pos), expr)); @@ -1161,17 +1201,24 @@ fn parse_map_literal<'a>( /// Parse a primary expression. fn parse_primary<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let (token, pos) = match input.peek().unwrap() { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let (token, _) = match token { // { - block statement as expression - (Token::LeftBrace, pos) if allow_stmt_expr => { - let pos = *pos; - return parse_block(input, stack, false, allow_stmt_expr) + Token::LeftBrace if allow_stmt_expr => { + return parse_block(input, state, false, level + 1, allow_stmt_expr) .map(|block| Expr::Stmt(Box::new((block, pos)))); } - (Token::EOF, pos) => return Err(PERR::UnexpectedEOF.into_err(*pos)), + Token::EOF => return Err(PERR::UnexpectedEOF.into_err(pos)), _ => input.next().unwrap(), }; @@ -1182,14 +1229,14 @@ fn parse_primary<'a>( Token::CharConstant(c) => Expr::CharConstant(Box::new((c, pos))), Token::StringConst(s) => Expr::StringConstant(Box::new((s, pos))), Token::Identifier(s) => { - let index = stack.find(&s); + let index = state.find(&s); Expr::Variable(Box::new(((s, pos), None, 0, index))) } - Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, + Token::LeftParen => parse_paren_expr(input, state, pos, level + 1, allow_stmt_expr)?, #[cfg(not(feature = "no_index"))] - Token::LeftBracket => parse_array_literal(input, stack, pos, allow_stmt_expr)?, + Token::LeftBracket => parse_array_literal(input, state, pos, level + 1, allow_stmt_expr)?, #[cfg(not(feature = "no_object"))] - Token::MapStart => parse_map_literal(input, stack, pos, allow_stmt_expr)?, + Token::MapStart => parse_map_literal(input, state, pos, level + 1, allow_stmt_expr)?, Token::True => Expr::True(pos), Token::False => Expr::False(pos), Token::LexError(err) => return Err(PERR::BadInput(err.to_string()).into_err(pos)), @@ -1212,7 +1259,7 @@ fn parse_primary<'a>( // Function call (Expr::Variable(x), Token::LeftParen) => { let ((name, pos), modules, _, _) = *x; - parse_call_expr(input, stack, name, modules, pos, allow_stmt_expr)? + parse_call_expr(input, state, name, modules, pos, level + 1, allow_stmt_expr)? } (Expr::Property(_), _) => unreachable!(), // module access @@ -1235,7 +1282,7 @@ fn parse_primary<'a>( // Indexing #[cfg(not(feature = "no_index"))] (expr, Token::LeftBracket) => { - parse_index_chain(input, stack, expr, token_pos, allow_stmt_expr)? + parse_index_chain(input, state, expr, token_pos, level + 1, allow_stmt_expr)? } // Unknown postfix operator (expr, token) => panic!("unknown postfix operator {:?} for {:?}", token, expr), @@ -1251,7 +1298,7 @@ fn parse_primary<'a>( // Qualifiers + variable name *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); - modules.set_index(stack.find_module(&modules.get(0).0)); + modules.set_index(state.find_module(&modules.get(0).0)); } _ => (), } @@ -1262,23 +1309,28 @@ fn parse_primary<'a>( /// Parse a potential unary operator. fn parse_unary<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - match input.peek().unwrap() { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + match token { // If statement is allowed to act as expressions - (Token::If, pos) => { - let pos = *pos; - Ok(Expr::Stmt(Box::new(( - parse_if(input, stack, false, allow_stmt_expr)?, - pos, - )))) - } + Token::If => Ok(Expr::Stmt(Box::new(( + parse_if(input, state, false, level + 1, allow_stmt_expr)?, + pos, + )))), // -expr - (Token::UnaryMinus, _) => { + Token::UnaryMinus => { let pos = eat_token(input, Token::UnaryMinus); - match parse_unary(input, stack, allow_stmt_expr)? { + match parse_unary(input, state, level + 1, allow_stmt_expr)? { // Negative integer Expr::IntegerConstant(x) => { let (num, pos) = *x; @@ -1325,15 +1377,15 @@ fn parse_unary<'a>( } } // +expr - (Token::UnaryPlus, _) => { + Token::UnaryPlus => { eat_token(input, Token::UnaryPlus); - parse_unary(input, stack, allow_stmt_expr) + parse_unary(input, state, level + 1, allow_stmt_expr) } // !expr - (Token::Bang, _) => { + Token::Bang => { let pos = eat_token(input, Token::Bang); let mut args = StaticVec::new(); - args.push(parse_primary(input, stack, allow_stmt_expr)?); + args.push(parse_primary(input, state, level + 1, allow_stmt_expr)?); let op = "!"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); @@ -1347,14 +1399,14 @@ fn parse_unary<'a>( )))) } // - (Token::EOF, pos) => Err(PERR::UnexpectedEOF.into_err(*pos)), + Token::EOF => Err(PERR::UnexpectedEOF.into_err(pos)), // All other tokens - _ => parse_primary(input, stack, allow_stmt_expr), + _ => parse_primary(input, state, level + 1, allow_stmt_expr), } } fn make_assignment_stmt<'a>( - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, rhs: Expr, pos: Position, @@ -1363,7 +1415,7 @@ fn make_assignment_stmt<'a>( Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); - match stack[(stack.len() - index.unwrap().get())].1 { + match state.stack[(state.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { @@ -1376,7 +1428,7 @@ fn make_assignment_stmt<'a>( Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { let ((name, name_pos), _, _, index) = x.as_ref(); - match stack[(stack.len() - index.unwrap().get())].1 { + match state.stack[(state.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { @@ -1397,44 +1449,53 @@ fn make_assignment_stmt<'a>( /// Parse an operator-assignment expression. fn parse_op_assignment_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, lhs: Expr, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let (op, pos) = match *input.peek().unwrap() { - (Token::Equals, _) => { + let (token, pos) = input.peek().unwrap(); + let pos = *pos; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + + let op = match token { + Token::Equals => { let pos = eat_token(input, Token::Equals); - let rhs = parse_expr(input, stack, allow_stmt_expr)?; - return make_assignment_stmt(stack, lhs, rhs, pos); + let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; + return make_assignment_stmt(state, lhs, rhs, pos); } - (Token::PlusAssign, pos) => ("+", pos), - (Token::MinusAssign, pos) => ("-", pos), - (Token::MultiplyAssign, pos) => ("*", pos), - (Token::DivideAssign, pos) => ("/", pos), - (Token::LeftShiftAssign, pos) => ("<<", pos), - (Token::RightShiftAssign, pos) => (">>", pos), - (Token::ModuloAssign, pos) => ("%", pos), - (Token::PowerOfAssign, pos) => ("~", pos), - (Token::AndAssign, pos) => ("&", pos), - (Token::OrAssign, pos) => ("|", pos), - (Token::XOrAssign, pos) => ("^", pos), - (_, _) => return Ok(lhs), + Token::PlusAssign => Token::Plus.syntax(), + Token::MinusAssign => Token::Minus.syntax(), + Token::MultiplyAssign => Token::Multiply.syntax(), + Token::DivideAssign => Token::Divide.syntax(), + Token::LeftShiftAssign => Token::LeftShift.syntax(), + Token::RightShiftAssign => Token::RightShift.syntax(), + Token::ModuloAssign => Token::Modulo.syntax(), + Token::PowerOfAssign => Token::PowerOf.syntax(), + Token::AndAssign => Token::Ampersand.syntax(), + Token::OrAssign => Token::Pipe.syntax(), + Token::XOrAssign => Token::XOr.syntax(), + + _ => return Ok(lhs), }; input.next(); let lhs_copy = lhs.clone(); - let rhs = parse_expr(input, stack, allow_stmt_expr)?; + let rhs = parse_expr(input, state, level + 1, allow_stmt_expr)?; // lhs op= rhs -> lhs = op(lhs, rhs) let mut args = StaticVec::new(); args.push(lhs_copy); args.push(rhs); - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); - let rhs_expr = Expr::FnCall(Box::new(((op.into(), pos), None, hash, args, None))); + let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(args.len())); + let rhs_expr = Expr::FnCall(Box::new(((op, pos), None, hash, args, None))); - make_assignment_stmt(stack, lhs, rhs_expr, pos) + make_assignment_stmt(state, lhs, rhs_expr, pos) } /// Make a dot expression. @@ -1673,53 +1734,59 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, parent_precedence: u8, lhs: Expr, + mut level: usize, allow_stmt_expr: bool, ) -> Result> { - let mut current_lhs = lhs; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(lhs.position())); + } + + let mut root = lhs; loop { - let (current_precedence, bind_right) = input.peek().map_or_else( - || (0, false), - |(current_op, _)| (current_op.precedence(), current_op.is_bind_right()), - ); + let (current_op, _) = input.peek().unwrap(); + let precedence = current_op.precedence(); + let bind_right = current_op.is_bind_right(); // Bind left to the parent lhs expression if precedence is higher // If same precedence, then check if the operator binds right - if current_precedence < parent_precedence - || (current_precedence == parent_precedence && !bind_right) - { - return Ok(current_lhs); + if precedence < parent_precedence || (precedence == parent_precedence && !bind_right) { + return Ok(root); } let (op_token, pos) = input.next().unwrap(); - let rhs = parse_unary(input, stack, allow_stmt_expr)?; + let rhs = parse_unary(input, state, level, allow_stmt_expr)?; let next_precedence = input.peek().unwrap().0.precedence(); // Bind to right if the next operator has higher precedence // If same precedence, then check if the operator binds right - let rhs = if (current_precedence == next_precedence && bind_right) - || current_precedence < next_precedence - { - parse_binary_op(input, stack, current_precedence, rhs, allow_stmt_expr)? + let rhs = if (precedence == next_precedence && bind_right) || precedence < next_precedence { + parse_binary_op(input, state, precedence, rhs, level, allow_stmt_expr)? } else { // Otherwise bind to left (even if next operator has the same precedence) rhs }; + level += 1; + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let cmp_def = Some(false.into()); let op = op_token.syntax(); let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(2)); let mut args = StaticVec::new(); - args.push(current_lhs); + args.push(root); args.push(rhs); - current_lhs = match op_token { + root = match op_token { Token::Plus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::Minus => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::Multiply => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), @@ -1789,11 +1856,18 @@ fn parse_binary_op<'a>( /// Parse an expression. fn parse_expr<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let lhs = parse_unary(input, stack, allow_stmt_expr)?; - parse_binary_op(input, stack, 1, lhs, allow_stmt_expr) + let (_, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + + let lhs = parse_unary(input, state, level + 1, allow_stmt_expr)?; + parse_binary_op(input, state, 1, lhs, level + 1, allow_stmt_expr) } /// Make sure that the expression is not a statement expression (i.e. wrapped in `{}`). @@ -1843,27 +1917,32 @@ fn ensure_not_assignment<'a>( /// Parse an if statement. fn parse_if<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { // if ... - eat_token(input, Token::If); + let pos = eat_token(input, Token::If); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // if guard { if_body } ensure_not_statement_expr(input, "a boolean")?; - let guard = parse_expr(input, stack, allow_stmt_expr)?; + let guard = parse_expr(input, state, level + 1, allow_stmt_expr)?; ensure_not_assignment(input)?; - let if_body = parse_block(input, stack, breakable, allow_stmt_expr)?; + let if_body = parse_block(input, state, breakable, level + 1, allow_stmt_expr)?; // if guard { if_body } else ... let else_body = if match_token(input, Token::Else).unwrap_or(false) { Some(if let (Token::If, _) = input.peek().unwrap() { // if guard { if_body } else if ... - parse_if(input, stack, breakable, allow_stmt_expr)? + parse_if(input, state, breakable, level + 1, allow_stmt_expr)? } else { // if guard { if_body } else { else-body } - parse_block(input, stack, breakable, allow_stmt_expr)? + parse_block(input, state, breakable, level + 1, allow_stmt_expr)? }) } else { None @@ -1875,17 +1954,22 @@ fn parse_if<'a>( /// Parse a while loop. fn parse_while<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // while ... - eat_token(input, Token::While); + let pos = eat_token(input, Token::While); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // while guard { body } ensure_not_statement_expr(input, "a boolean")?; - let guard = parse_expr(input, stack, allow_stmt_expr)?; + let guard = parse_expr(input, state, level + 1, allow_stmt_expr)?; ensure_not_assignment(input)?; - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; Ok(Stmt::While(Box::new((guard, body)))) } @@ -1893,14 +1977,19 @@ fn parse_while<'a>( /// Parse a loop statement. fn parse_loop<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // loop ... - eat_token(input, Token::Loop); + let pos = eat_token(input, Token::Loop); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // loop { body } - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; Ok(Stmt::Loop(Box::new(body))) } @@ -1908,11 +1997,16 @@ fn parse_loop<'a>( /// Parse a for loop. fn parse_for<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // for ... - eat_token(input, Token::For); + let pos = eat_token(input, Token::For); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // for name ... let name = match input.next().unwrap() { @@ -1940,14 +2034,14 @@ fn parse_for<'a>( // for name in expr { body } ensure_not_statement_expr(input, "a boolean")?; - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; - let prev_len = stack.len(); - stack.push((name.clone(), ScopeEntryType::Normal)); + let prev_len = state.len(); + state.push((name.clone(), ScopeEntryType::Normal)); - let body = parse_block(input, stack, true, allow_stmt_expr)?; + let body = parse_block(input, state, true, level + 1, allow_stmt_expr)?; - stack.truncate(prev_len); + state.truncate(prev_len); Ok(Stmt::For(Box::new((name, expr, body)))) } @@ -1955,12 +2049,17 @@ fn parse_for<'a>( /// Parse a variable definition statement. fn parse_let<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, var_type: ScopeEntryType, + level: usize, allow_stmt_expr: bool, ) -> Result> { // let/const... (specified in `var_type`) - input.next(); + let (_, pos) = input.next().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } // let name ... let (name, pos) = match input.next().unwrap() { @@ -1972,17 +2071,17 @@ fn parse_let<'a>( // let name = ... if match_token(input, Token::Equals)? { // let name = expr - let init_value = parse_expr(input, stack, allow_stmt_expr)?; + let init_value = parse_expr(input, state, level + 1, allow_stmt_expr)?; match var_type { // let name = expr ScopeEntryType::Normal => { - stack.push((name.clone(), ScopeEntryType::Normal)); + state.push((name.clone(), ScopeEntryType::Normal)); Ok(Stmt::Let(Box::new(((name, pos), Some(init_value))))) } // const name = { expr:constant } ScopeEntryType::Constant if init_value.is_constant() => { - stack.push((name.clone(), ScopeEntryType::Constant)); + state.push((name.clone(), ScopeEntryType::Constant)); Ok(Stmt::Const(Box::new(((name, pos), init_value)))) } // const name = expr - error @@ -1996,11 +2095,11 @@ fn parse_let<'a>( // let name match var_type { ScopeEntryType::Normal => { - stack.push((name.clone(), ScopeEntryType::Normal)); + state.push((name.clone(), ScopeEntryType::Normal)); Ok(Stmt::Let(Box::new(((name, pos), None)))) } ScopeEntryType::Constant => { - stack.push((name.clone(), ScopeEntryType::Constant)); + state.push((name.clone(), ScopeEntryType::Constant)); Ok(Stmt::Const(Box::new(((name, pos), Expr::Unit(pos))))) } // Variable cannot be a module @@ -2012,14 +2111,19 @@ fn parse_let<'a>( /// Parse an import statement. fn parse_import<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { // import ... let pos = eat_token(input, Token::Import); + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + // import expr ... - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; // import expr as ... match input.next().unwrap() { @@ -2039,13 +2143,21 @@ fn parse_import<'a>( (_, pos) => return Err(PERR::VariableExpected.into_err(pos)), }; - stack.push((name.clone(), ScopeEntryType::Module)); + state.push((name.clone(), ScopeEntryType::Module)); Ok(Stmt::Import(Box::new((expr, (name, pos))))) } /// Parse an export statement. -fn parse_export<'a>(input: &mut Peekable>) -> Result> { - eat_token(input, Token::Export); +fn parse_export<'a>( + input: &mut Peekable>, + state: &mut ParseState, + level: usize, +) -> Result> { + let pos = eat_token(input, Token::Export); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } let mut exports = StaticVec::new(); @@ -2103,8 +2215,9 @@ fn parse_export<'a>(input: &mut Peekable>) -> Result( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { // Must start with { @@ -2120,12 +2233,16 @@ fn parse_block<'a>( } }; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let mut statements = StaticVec::new(); - let prev_len = stack.len(); + let prev_len = state.len(); while !match_token(input, Token::RightBrace)? { // Parse statements inside the block - let stmt = parse_stmt(input, stack, breakable, false, allow_stmt_expr)?; + let stmt = parse_stmt(input, state, breakable, false, level + 1, allow_stmt_expr)?; // See if it needs a terminating semicolon let need_semicolon = !stmt.is_self_terminated(); @@ -2162,7 +2279,7 @@ fn parse_block<'a>( } } - stack.truncate(prev_len); + state.truncate(prev_len); Ok(Stmt::Block(Box::new((statements, pos)))) } @@ -2170,41 +2287,55 @@ fn parse_block<'a>( /// Parse an expression as a statement. fn parse_expr_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, + level: usize, allow_stmt_expr: bool, ) -> Result> { - let expr = parse_expr(input, stack, allow_stmt_expr)?; - let expr = parse_op_assignment_stmt(input, stack, expr, allow_stmt_expr)?; + let (_, pos) = input.peek().unwrap(); + + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; + let expr = parse_op_assignment_stmt(input, state, expr, level + 1, allow_stmt_expr)?; Ok(Stmt::Expr(Box::new(expr))) } /// Parse a single statement. fn parse_stmt<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, breakable: bool, is_global: bool, + level: usize, allow_stmt_expr: bool, ) -> Result> { + use ScopeEntryType::{Constant, Normal}; + let (token, pos) = match input.peek().unwrap() { (Token::EOF, pos) => return Ok(Stmt::Noop(*pos)), x => x, }; + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(*pos)); + } + match token { // Semicolon - empty statement Token::SemiColon => Ok(Stmt::Noop(*pos)), - Token::LeftBrace => parse_block(input, stack, breakable, allow_stmt_expr), + Token::LeftBrace => parse_block(input, state, breakable, level + 1, allow_stmt_expr), // fn ... Token::Fn if !is_global => Err(PERR::WrongFnDefinition.into_err(*pos)), Token::Fn => unreachable!(), - Token::If => parse_if(input, stack, breakable, allow_stmt_expr), - Token::While => parse_while(input, stack, allow_stmt_expr), - Token::Loop => parse_loop(input, stack, allow_stmt_expr), - Token::For => parse_for(input, stack, allow_stmt_expr), + Token::If => parse_if(input, state, breakable, level + 1, allow_stmt_expr), + Token::While => parse_while(input, state, level + 1, allow_stmt_expr), + Token::Loop => parse_loop(input, state, level + 1, allow_stmt_expr), + Token::For => parse_for(input, state, level + 1, allow_stmt_expr), Token::Continue if breakable => { let pos = eat_token(input, Token::Continue); @@ -2234,7 +2365,7 @@ fn parse_stmt<'a>( } // `return` or `throw` with expression (_, _) => { - let expr = parse_expr(input, stack, allow_stmt_expr)?; + let expr = parse_expr(input, state, level + 1, allow_stmt_expr)?; let pos = expr.position(); Ok(Stmt::ReturnWithVal(Box::new(( @@ -2245,31 +2376,36 @@ fn parse_stmt<'a>( } } - Token::Let => parse_let(input, stack, ScopeEntryType::Normal, allow_stmt_expr), - Token::Const => parse_let(input, stack, ScopeEntryType::Constant, allow_stmt_expr), + Token::Let => parse_let(input, state, Normal, level + 1, allow_stmt_expr), + Token::Const => parse_let(input, state, Constant, level + 1, allow_stmt_expr), #[cfg(not(feature = "no_module"))] - Token::Import => parse_import(input, stack, allow_stmt_expr), + Token::Import => parse_import(input, state, level + 1, allow_stmt_expr), #[cfg(not(feature = "no_module"))] Token::Export if !is_global => Err(PERR::WrongExport.into_err(*pos)), #[cfg(not(feature = "no_module"))] - Token::Export => parse_export(input), + Token::Export => parse_export(input, state, level + 1), - _ => parse_expr_stmt(input, stack, allow_stmt_expr), + _ => parse_expr_stmt(input, state, level + 1, allow_stmt_expr), } } /// Parse a function definition. fn parse_fn<'a>( input: &mut Peekable>, - stack: &mut Stack, + state: &mut ParseState, access: FnAccess, + level: usize, allow_stmt_expr: bool, ) -> Result> { let pos = eat_token(input, Token::Fn); + if level > state.max_expr_depth { + return Err(PERR::ExprTooDeep.into_err(pos)); + } + let name = match input.next().unwrap() { (Token::Identifier(s), _) => s, (_, pos) => return Err(PERR::FnMissingName.into_err(pos)), @@ -2289,7 +2425,7 @@ fn parse_fn<'a>( loop { match input.next().unwrap() { (Token::Identifier(s), pos) => { - stack.push((s.clone(), ScopeEntryType::Normal)); + state.push((s.clone(), ScopeEntryType::Normal)); params.push((s, pos)) } (Token::LexError(err), pos) => { @@ -2333,7 +2469,7 @@ fn parse_fn<'a>( // Parse function body let body = match input.peek().unwrap() { - (Token::LeftBrace, _) => parse_block(input, stack, false, allow_stmt_expr)?, + (Token::LeftBrace, _) => parse_block(input, state, false, level + 1, allow_stmt_expr)?, (_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)), }; @@ -2353,9 +2489,10 @@ pub fn parse_global_expr<'a>( engine: &Engine, scope: &Scope, optimization_level: OptimizationLevel, + max_expr_depth: usize, ) -> Result> { - let mut stack = Stack::new(); - let expr = parse_expr(input, &mut stack, false)?; + let mut state = ParseState::new(max_expr_depth); + let expr = parse_expr(input, &mut state, 0, false)?; match input.peek().unwrap() { (Token::EOF, _) => (), @@ -2380,10 +2517,11 @@ pub fn parse_global_expr<'a>( /// Parse the global level statements. fn parse_global_level<'a>( input: &mut Peekable>, + max_expr_depth: (usize, usize), ) -> Result<(Vec, HashMap), Box> { let mut statements = Vec::::new(); let mut functions = HashMap::::new(); - let mut stack = Stack::new(); + let mut state = ParseState::new(max_expr_depth.0); while !input.peek().unwrap().0.is_eof() { // Collect all the function definitions @@ -2399,8 +2537,8 @@ fn parse_global_level<'a>( match input.peek().unwrap() { (Token::Fn, _) => { - let mut stack = Stack::new(); - let func = parse_fn(input, &mut stack, access, true)?; + let mut state = ParseState::new(max_expr_depth.1); + let func = parse_fn(input, &mut state, access, 0, true)?; // Qualifiers (none) + function name + argument `TypeId`'s let hash = calc_fn_hash( @@ -2423,7 +2561,7 @@ fn parse_global_level<'a>( } } // Actual statement - let stmt = parse_stmt(input, &mut stack, false, true, true)?; + let stmt = parse_stmt(input, &mut state, false, true, 0, true)?; let need_semicolon = !stmt.is_self_terminated(); @@ -2465,8 +2603,9 @@ pub fn parse<'a>( engine: &Engine, scope: &Scope, optimization_level: OptimizationLevel, + max_expr_depth: (usize, usize), ) -> Result> { - let (statements, functions) = parse_global_level(input)?; + let (statements, functions) = parse_global_level(input, max_expr_depth)?; let fn_lib = functions.into_iter().map(|(_, v)| v).collect(); Ok( diff --git a/tests/stack.rs b/tests/stack.rs index 5ebb550b..3087e558 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -1,18 +1,18 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Engine, EvalAltResult}; +use rhai::{Engine, EvalAltResult, ParseErrorType}; #[test] -fn test_stack_overflow() -> Result<(), Box> { +fn test_stack_overflow_fn_calls() -> Result<(), Box> { let engine = Engine::new(); assert_eq!( engine.eval::( r" - fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } } - foo(25) + fn foo(n) { if n <= 1 { 0 } else { n + foo(n-1) } } + foo(8) ", )?, - 325 + 35 ); #[cfg(not(feature = "unchecked"))] @@ -32,3 +32,53 @@ fn test_stack_overflow() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_stack_overflow_parsing() -> Result<(), Box> { + let mut engine = Engine::new(); + + assert!(matches!( + *engine.compile(r" + let a = (1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) + ").expect_err("should error"), + err if err.error_type() == &ParseErrorType::ExprTooDeep + )); + + engine.set_max_expr_depths(100, 6); + + engine.compile("1 + 2")?; + engine.compile( + r" + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 0 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + ", + )?; + + assert!(matches!( + *engine.compile(r" + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + ").expect_err("should error"), + err if err.error_type() == &ParseErrorType::ExprTooDeep + )); + + engine.compile("fn abc(x) { x + 1 }")?; + + Ok(()) +} From 6b8c6bda424a46cd32bf9c4fea27a03298a44861 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 10:08:27 +0800 Subject: [PATCH 27/36] Use u64 for operations counter. --- README.md | 2 +- src/engine.rs | 38 +++++++++++++++++++------------------- tests/modules.rs | 12 ++++++------ tests/stack.rs | 15 +++++---------- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 6edb1ca7..f6db9187 100644 --- a/README.md +++ b/README.md @@ -2347,7 +2347,7 @@ engine.set_max_modules(0); // allow unlimited modules ### Maximum call stack depth -Rhai by default limits function calls to a maximum depth of 128 levels (8 levels in debug build). +Rhai by default limits function calls to a maximum depth of 128 levels (16 levels in debug build). This limit may be changed via the `Engine::set_max_call_levels` method. When setting this limit, care must be also taken to the evaluation depth of each _statement_ diff --git a/src/engine.rs b/src/engine.rs index 6bd2bafa..bd28ccf1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -27,7 +27,7 @@ use crate::stdlib::{ format, iter::{empty, once, repeat}, mem, - num::{NonZeroU64, NonZeroUsize}, + num::NonZeroUsize, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, @@ -49,7 +49,7 @@ pub type Map = HashMap; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] -pub const MAX_CALL_STACK_DEPTH: usize = 8; +pub const MAX_CALL_STACK_DEPTH: usize = 16; #[cfg(not(feature = "unchecked"))] #[cfg(debug_assertions)] pub const MAX_EXPR_DEPTH: usize = 32; @@ -354,16 +354,16 @@ pub struct Engine { pub(crate) optimization_level: OptimizationLevel, /// Maximum levels of call-stack to prevent infinite recursion. /// - /// Defaults to 8 for debug builds and 128 for non-debug builds. + /// Defaults to 16 for debug builds and 128 for non-debug builds. pub(crate) max_call_stack_depth: usize, /// Maximum depth of statements/expressions at global level. pub(crate) max_expr_depth: usize, /// Maximum depth of statements/expressions in functions. pub(crate) max_function_expr_depth: usize, /// Maximum number of operations allowed to run. - pub(crate) max_operations: Option, + pub(crate) max_operations: u64, /// Maximum number of modules allowed to load. - pub(crate) max_modules: Option, + pub(crate) max_modules: u64, } impl Default for Engine { @@ -404,8 +404,8 @@ impl Default for Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: None, - max_modules: None, + max_operations: u64::MAX, + max_modules: u64::MAX, }; #[cfg(feature = "no_stdlib")] @@ -547,8 +547,8 @@ impl Engine { max_call_stack_depth: MAX_CALL_STACK_DEPTH, max_expr_depth: MAX_EXPR_DEPTH, max_function_expr_depth: MAX_FUNCTION_EXPR_DEPTH, - max_operations: None, - max_modules: None, + max_operations: u64::MAX, + max_modules: u64::MAX, } } @@ -589,13 +589,17 @@ impl Engine { /// consuming too much resources (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_operations(&mut self, operations: u64) { - self.max_operations = NonZeroU64::new(operations); + self.max_operations = if operations == 0 { + u64::MAX + } else { + operations + }; } /// Set the maximum number of imported modules allowed for a script (0 for unlimited). #[cfg(not(feature = "unchecked"))] pub fn set_max_modules(&mut self, modules: u64) { - self.max_modules = NonZeroU64::new(modules); + self.max_modules = if modules == 0 { u64::MAX } else { modules }; } /// Set the depth limits for expressions/statements. @@ -1835,10 +1839,8 @@ impl Engine { let (expr, (name, pos)) = x.as_ref(); // Guard against too many modules - if let Some(max) = self.max_modules { - if state.modules >= max.get() { - return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); - } + if state.modules >= self.max_modules { + return Err(Box::new(EvalAltResult::ErrorTooManyModules(*pos))); } if let Some(path) = self @@ -1902,10 +1904,8 @@ impl Engine { #[cfg(not(feature = "unchecked"))] { // Guard against too many operations - if let Some(max) = self.max_operations { - if state.operations > max.get() { - return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos))); - } + if state.operations > self.max_operations { + return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos))); } } diff --git a/tests/modules.rs b/tests/modules.rs index 9c6c8ec4..4e475d1c 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -86,14 +86,14 @@ fn test_module_resolver() -> Result<(), Box> { *engine .eval::( r#" - let x = 0; + let sum = 0; for x in range(0, 10) { import "hello" as h; - x += h::answer; + sum += h::answer; } - x + sum "# ) .expect_err("should error"), @@ -105,18 +105,18 @@ fn test_module_resolver() -> Result<(), Box> { *engine .eval::( r#" - let x = 0; + let sum = 0; fn foo() { import "hello" as h; - x += h::answer; + sum += h::answer; } for x in range(0, 10) { foo(); } - x + sum "# ) .expect_err("should error"), diff --git a/tests/stack.rs b/tests/stack.rs index 3087e558..ee7eedc4 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -16,19 +16,14 @@ fn test_stack_overflow_fn_calls() -> Result<(), Box> { ); #[cfg(not(feature = "unchecked"))] - match engine.eval::<()>( + assert!(matches!( + *engine.eval::<()>( r" fn foo(n) { if n == 0 { 0 } else { n + foo(n-1) } } foo(1000) - ", - ) { - Ok(_) => panic!("should be stack overflow"), - Err(err) => match *err { - EvalAltResult::ErrorInFunctionCall(name, _, _) - if name.starts_with("foo > foo > foo") => {} - _ => panic!("should be stack overflow"), - }, - } + ").expect_err("should error"), + EvalAltResult::ErrorInFunctionCall(name, _, _) if name.starts_with("foo > foo > foo") + )); Ok(()) } From a22f338b03857742f6a650b77885a4a5fb6ac94a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 10:13:37 +0800 Subject: [PATCH 28/36] Back out NativeCallable trait. --- src/fn_native.rs | 12 +++++++----- src/lib.rs | 2 +- src/module.rs | 10 +++++----- src/optimize.rs | 1 - src/packages/mod.rs | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/fn_native.rs b/src/fn_native.rs index 7a1dd0e6..ccc458d3 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -82,6 +82,7 @@ pub enum NativeFunctionABI { Method, } +/* /// Trait implemented by all native Rust functions that are callable by Rhai. #[cfg(not(feature = "sync"))] pub trait NativeCallable { @@ -99,15 +100,16 @@ pub trait NativeCallable: Send + Sync { /// Call a native Rust function. fn call(&self, args: &mut FnCallArgs) -> Result>; } +*/ /// A type encapsulating a native Rust function callable by Rhai. pub struct NativeFunction(Box, NativeFunctionABI); -impl NativeCallable for NativeFunction { - fn abi(&self) -> NativeFunctionABI { +impl NativeFunction { + pub fn abi(&self) -> NativeFunctionABI { self.1 } - fn call(&self, args: &mut FnCallArgs) -> Result> { + pub fn call(&self, args: &mut FnCallArgs) -> Result> { (self.0)(args) } } @@ -126,10 +128,10 @@ impl NativeFunction { /// An external native Rust function. #[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc>; +pub type SharedNativeFunction = Rc; /// An external native Rust function. #[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc>; +pub type SharedNativeFunction = Arc; /// A type iterator function. #[cfg(not(feature = "sync"))] diff --git a/src/lib.rs b/src/lib.rs index 5c7a10ae..2494d71f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -pub use fn_native::NativeCallable; +//pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; diff --git a/src/module.rs b/src/module.rs index 377a4357..f28c87fe 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,8 +4,8 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, IteratorFn, NativeCallable, NativeFunction, NativeFunctionABI, - NativeFunctionABI::*, SharedIteratorFunction, SharedNativeFunction, + FnAny, FnCallArgs, IteratorFn, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, + SharedIteratorFunction, SharedNativeFunction, }; use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; use crate::result::EvalAltResult; @@ -285,7 +285,7 @@ impl Module { ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = Box::new(NativeFunction::from((func, abi))) as Box; + let f = NativeFunction::from((func, abi)); #[cfg(not(feature = "sync"))] let func = Rc::new(f); @@ -531,7 +531,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&Box> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&NativeFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } @@ -543,7 +543,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&Box, Box> { + ) -> Result<&NativeFunction, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) diff --git a/src/optimize.rs b/src/optimize.rs index 671415e3..5fc6d608 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -15,7 +15,6 @@ use crate::utils::StaticVec; use crate::stdlib::{ boxed::Box, iter::empty, - mem, string::{String, ToString}, vec, vec::Vec, diff --git a/src/packages/mod.rs b/src/packages/mod.rs index e56843f0..ad61ae19 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{NativeCallable, SharedIteratorFunction}; +use crate::fn_native::{NativeFunction, SharedIteratorFunction}; use crate::module::Module; use crate::utils::StaticVec; @@ -69,7 +69,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_fn(&self, hash: u64) -> Option<&Box> { + pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> { self.packages .iter() .map(|p| p.get_fn(hash)) From 3295060dba35440239732ef87b8aa05df9bce63c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 19:03:06 +0800 Subject: [PATCH 29/36] Unify all functions under CallableFunction type. --- src/engine.rs | 113 +++++++++++++++----------------- src/fn_native.rs | 155 ++++++++++++++++++++++++++------------------ src/fn_register.rs | 26 ++++---- src/lib.rs | 1 - src/module.rs | 86 +++++++++--------------- src/optimize.rs | 8 +-- src/packages/mod.rs | 6 +- src/result.rs | 24 ------- 8 files changed, 199 insertions(+), 220 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index bd28ccf1..85c0c9f4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, NativeFunctionABI, PrintCallback, ProgressCallback}; +use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; @@ -229,15 +229,7 @@ impl FunctionsLib { // Qualifiers (none) + function name + placeholders (one for each parameter). let args_iter = repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()); let hash = calc_fn_hash(empty(), &fn_def.name, args_iter); - - #[cfg(feature = "sync")] - { - (hash, Arc::new(fn_def)) - } - #[cfg(not(feature = "sync"))] - { - (hash, Rc::new(fn_def)) - } + (hash, fn_def.into()) }) .collect(), ) @@ -268,8 +260,7 @@ impl FunctionsLib { match fn_def.as_ref().map(|f| f.access) { None => None, Some(FnAccess::Private) if public_only => None, - Some(FnAccess::Private) => fn_def, - Some(FnAccess::Public) => fn_def, + Some(FnAccess::Private) | Some(FnAccess::Public) => fn_def, } } /// Merge another `FunctionsLib` into this `FunctionsLib`. @@ -659,25 +650,20 @@ impl Engine { .get_fn(hashes.0) .or_else(|| self.packages.get_fn(hashes.0)) { - let mut backup: Dynamic = Default::default(); - - let (updated, restore) = match func.abi() { - // Calling pure function in method-call - NativeFunctionABI::Pure if is_ref && args.len() > 0 => { - // Backup the original value. It'll be consumed because the function - // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) - backup = args[0].clone(); - (false, true) - } - NativeFunctionABI::Pure => (false, false), - NativeFunctionABI::Method => (true, false), + // Calling pure function in method-call? + let backup: Option = if func.is_pure() && is_ref && args.len() > 0 { + // Backup the original value. It'll be consumed because the function + // is pure and doesn't know that the first value is a reference (i.e. `is_ref`) + Some(args[0].clone()) + } else { + None }; // Run external function - let result = match func.call(args) { + let result = match func.get_native_fn()(args) { Ok(r) => { // Restore the backup value for the first argument since it has been consumed! - if restore { + if let Some(backup) = backup { *args[0] = backup; } r @@ -709,7 +695,7 @@ impl Engine { .into(), false, ), - _ => (result, updated), + _ => (result, func.is_method()), }); } @@ -780,14 +766,12 @@ impl Engine { let scope_len = scope.len(); // Put arguments into scope as variables + // Actually consume the arguments instead of cloning them scope.extend( fn_def .params .iter() - .zip( - // Actually consume the arguments instead of cloning them - args.into_iter().map(|v| mem::take(*v)), - ) + .zip(args.iter_mut().map(|v| mem::take(*v))) .map(|(name, value)| { let var_name = unsafe_cast_var_name_to_lifetime(name.as_str(), &mut state); @@ -826,14 +810,12 @@ impl Engine { let mut scope = Scope::new(); // Put arguments into scope as variables + // Actually consume the arguments instead of cloning them scope.extend( fn_def .params .iter() - .zip( - // Actually consume the arguments instead of cloning them - args.into_iter().map(|v| mem::take(*v)), - ) + .zip(args.iter_mut().map(|v| mem::take(*v))) .map(|(name, value)| (name, ScopeEntryType::Normal, value)), ); @@ -1558,32 +1540,43 @@ impl Engine { }; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { - let args = args.as_mut(); - let (result, state2) = - self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; - *state = state2; - Ok(result) - } else { - // Then search in Rust functions - self.inc_operations(state, *pos)?; + let func = match module.get_qualified_fn(name, *hash_fn_def) { + Err(err) if matches!(*err, EvalAltResult::ErrorFunctionNotFound(_, _)) => { + // Then search in Rust functions + self.inc_operations(state, *pos)?; - // Rust functions are indexed in two steps: - // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s - let hash_fn_args = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); - // 3) The final hash is the XOR of the two hashes. - let hash_fn_native = *hash_fn_def ^ hash_fn_args; + // Rust functions are indexed in two steps: + // 1) Calculate a hash in a similar manner to script-defined functions, + // i.e. qualifiers + function name + dummy parameter types (one for each parameter). + // 2) Calculate a second hash with no qualifiers, empty function name, and + // the actual list of parameter `TypeId`'.s + let hash_fn_args = + calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + // 3) The final hash is the XOR of the two hashes. + let hash_fn_native = *hash_fn_def ^ hash_fn_args; - match module.get_qualified_fn(name, hash_fn_native) { - Ok(func) => func - .call(args.as_mut()) - .map_err(|err| err.new_position(*pos)), - Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), - Err(err) => Err(err), + module.get_qualified_fn(name, hash_fn_native) } + r => r, + }; + + match func { + Ok(x) if x.is_script() => { + let args = args.as_mut(); + let fn_def = x.get_fn_def(); + let (result, state2) = + self.call_script_fn(None, *state, name, fn_def, args, *pos, level)?; + *state = state2; + Ok(result) + } + Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)), + Err(err) + if def_val.is_some() + && matches!(*err, EvalAltResult::ErrorFunctionNotFound(_, _)) => + { + Ok(def_val.clone().unwrap()) + } + Err(err) => Err(err), } } @@ -1733,7 +1726,7 @@ impl Engine { let iter_type = self.eval_expr(scope, state, expr, level)?; let tid = iter_type.type_id(); - if let Some(iter_fn) = self + if let Some(func) = self .global_module .get_iter(tid) .or_else(|| self.packages.get_iter(tid)) @@ -1744,7 +1737,7 @@ impl Engine { let index = scope.len() - 1; state.scope_level += 1; - for loop_var in iter_fn(iter_type) { + for loop_var in func.get_iter_fn()(iter_type) { *scope.get_mut(index).0 = loop_var; self.inc_operations(state, stmt.position())?; diff --git a/src/fn_native.rs b/src/fn_native.rs index ccc458d3..328372b9 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,4 +1,5 @@ use crate::any::Dynamic; +use crate::parser::{FnDef, SharedFnDef}; use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; @@ -72,70 +73,98 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box> + ' #[cfg(not(feature = "sync"))] impl Box> + 'static> IteratorCallback for F {} -/// A type representing the type of ABI of a native Rust function. -#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] -pub enum NativeFunctionABI { - /// A pure function where all arguments are passed by value. - Pure, - /// An object method where the first argument is the object passed by mutable reference. - /// All other arguments are passed by value. - Method, +/// A type encapsulating a function callable by Rhai. +pub enum CallableFunction { + /// A pure native Rust function with all arguments passed by value. + Pure(Box), + /// A native Rust object method with the first argument passed by reference, + /// and the rest passed by value. + Method(Box), + /// An iterator function. + Iterator(Box), + /// A script-defined function. + Script(SharedFnDef), } -/* -/// Trait implemented by all native Rust functions that are callable by Rhai. +impl CallableFunction { + /// Is this a pure native Rust function? + pub fn is_pure(&self) -> bool { + match self { + CallableFunction::Pure(_) => true, + CallableFunction::Method(_) + | CallableFunction::Iterator(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this a pure native Rust method-call? + pub fn is_method(&self) -> bool { + match self { + CallableFunction::Method(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Iterator(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this an iterator function? + pub fn is_iter(&self) -> bool { + match self { + CallableFunction::Iterator(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Script(_) => false, + } + } + /// Is this a Rhai-scripted function? + pub fn is_script(&self) -> bool { + match self { + CallableFunction::Script(_) => true, + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Iterator(_) => false, + } + } + /// Get a reference to a native Rust function. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Pure` or `Method`. + pub fn get_native_fn(&self) -> &Box { + match self { + CallableFunction::Pure(f) | CallableFunction::Method(f) => f, + CallableFunction::Iterator(_) | CallableFunction::Script(_) => panic!(), + } + } + /// Get a reference to a script-defined function definition. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Script`. + pub fn get_fn_def(&self) -> &FnDef { + match self { + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Iterator(_) => panic!(), + CallableFunction::Script(f) => f, + } + } + /// Get a reference to an iterator function. + /// + /// # Panics + /// + /// Panics if the `CallableFunction` is not `Iterator`. + pub fn get_iter_fn(&self) -> &Box { + match self { + CallableFunction::Pure(_) + | CallableFunction::Method(_) + | CallableFunction::Script(_) => panic!(), + CallableFunction::Iterator(f) => f, + } + } +} + +/// A callable function. #[cfg(not(feature = "sync"))] -pub trait NativeCallable { - /// Get the ABI type of a native Rust function. - fn abi(&self) -> NativeFunctionABI; - /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs) -> Result>; -} - -/// Trait implemented by all native Rust functions that are callable by Rhai. +pub type SharedFunction = Rc; +/// A callable function. #[cfg(feature = "sync")] -pub trait NativeCallable: Send + Sync { - /// Get the ABI type of a native Rust function. - fn abi(&self) -> NativeFunctionABI; - /// Call a native Rust function. - fn call(&self, args: &mut FnCallArgs) -> Result>; -} -*/ - -/// A type encapsulating a native Rust function callable by Rhai. -pub struct NativeFunction(Box, NativeFunctionABI); - -impl NativeFunction { - pub fn abi(&self) -> NativeFunctionABI { - self.1 - } - pub fn call(&self, args: &mut FnCallArgs) -> Result> { - (self.0)(args) - } -} - -impl From<(Box, NativeFunctionABI)> for NativeFunction { - fn from(func: (Box, NativeFunctionABI)) -> Self { - Self::new(func.0, func.1) - } -} -impl NativeFunction { - /// Create a new `NativeFunction`. - pub fn new(func: Box, abi: NativeFunctionABI) -> Self { - Self(func, abi) - } -} - -/// An external native Rust function. -#[cfg(not(feature = "sync"))] -pub type SharedNativeFunction = Rc; -/// An external native Rust function. -#[cfg(feature = "sync")] -pub type SharedNativeFunction = Arc; - -/// A type iterator function. -#[cfg(not(feature = "sync"))] -pub type SharedIteratorFunction = Rc>; -/// An external native Rust function. -#[cfg(feature = "sync")] -pub type SharedIteratorFunction = Arc>; +pub type SharedFunction = Arc; diff --git a/src/fn_register.rs b/src/fn_register.rs index 42e81c9b..9636e557 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,7 +4,10 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{FnCallArgs, NativeFunctionABI::*}; +use crate::fn_native::{ + CallableFunction::{Method, Pure}, + FnCallArgs, +}; use crate::parser::FnAccess; use crate::result::EvalAltResult; @@ -181,9 +184,9 @@ pub fn map_result( macro_rules! def_register { () => { - def_register!(imp Pure;); + def_register!(imp Pure :); }; - (imp $abi:expr ; $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { + (imp $abi:ident : $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { // ^ function ABI type // ^ function parameter generic type name (A, B, C etc.) // ^ function parameter marker type (T, Ref or Mut) @@ -202,9 +205,9 @@ macro_rules! def_register { > RegisterFn for Engine { fn register_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_dynamic ; $($par => $clone),*) + $abi(make_func!(f : map_dynamic ; $($par => $clone),*)) ); } } @@ -220,9 +223,9 @@ macro_rules! def_register { > RegisterDynamicFn for Engine { fn register_dynamic_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_identity ; $($par => $clone),*) + $abi(make_func!(f : map_identity ; $($par => $clone),*)) ); } } @@ -239,9 +242,9 @@ macro_rules! def_register { > RegisterResultFn for Engine { fn register_result_fn(&mut self, name: &str, f: FN) { - self.global_module.set_fn(name.to_string(), $abi, FnAccess::Public, + self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - make_func!(f : map_result ; $($par => $clone),*) + $abi(make_func!(f : map_result ; $($par => $clone),*)) ); } } @@ -249,8 +252,9 @@ macro_rules! def_register { //def_register!(imp_pop $($par => $mark => $param),*); }; ($p0:ident $(, $p:ident)*) => { - def_register!(imp Pure ; $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); - def_register!(imp Method ; $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + def_register!(imp Pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); + def_register!(imp Method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + // ^ CallableFunction // handle the first parameter ^ first parameter passed through // ^ others passed by value (by_value) diff --git a/src/lib.rs b/src/lib.rs index 2494d71f..b4e71a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,6 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; -//pub use fn_native::NativeCallable; pub use fn_register::{RegisterDynamicFn, RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{AST, INT}; diff --git a/src/module.rs b/src/module.rs index f28c87fe..06d0cbbd 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,10 +4,11 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::fn_native::{ - FnAny, FnCallArgs, IteratorFn, NativeFunction, NativeFunctionABI, NativeFunctionABI::*, - SharedIteratorFunction, SharedNativeFunction, + CallableFunction, + CallableFunction::{Method, Pure}, + FnCallArgs, IteratorFn, SharedFunction, }; -use crate::parser::{FnAccess, FnDef, SharedFnDef, AST}; +use crate::parser::{FnAccess, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; @@ -51,19 +52,17 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, SharedNativeFunction)>, - - /// Flattened collection of all external Rust functions, including those in sub-modules. - all_functions: HashMap, + functions: HashMap, SharedFunction)>, /// Script-defined functions. fn_lib: FunctionsLib, - /// Flattened collection of all script-defined functions, including those in sub-modules. - all_fn_lib: FunctionsLib, - /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, + + /// Flattened collection of all external Rust functions, native or scripted, + /// including those in sub-modules. + all_functions: HashMap, } impl fmt::Debug for Module { @@ -278,23 +277,16 @@ impl Module { pub fn set_fn( &mut self, name: String, - abi: NativeFunctionABI, access: FnAccess, params: &[TypeId], - func: Box, + func: CallableFunction, ) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); - let f = NativeFunction::from((func, abi)); - - #[cfg(not(feature = "sync"))] - let func = Rc::new(f); - #[cfg(feature = "sync")] - let func = Arc::new(f); - let params = params.into_iter().cloned().collect(); - self.functions.insert(hash_fn, (name, access, params, func)); + self.functions + .insert(hash_fn, (name, access, params, func.into())); hash_fn } @@ -320,7 +312,7 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let arg_types = []; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -345,7 +337,7 @@ impl Module { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::
()).map(Dynamic::from); let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -371,7 +363,7 @@ impl Module { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -402,7 +394,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -437,7 +429,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -475,7 +467,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Pure, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -514,7 +506,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Method, DEF_ACCESS, &arg_types, Box::new(f)) + self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) } /// Get a Rust function. @@ -531,7 +523,7 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&NativeFunction> { + pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) } @@ -543,7 +535,7 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&NativeFunction, Box> { + ) -> Result<&CallableFunction, Box> { self.all_functions .get(&hash_fn_native) .map(|f| f.as_ref()) @@ -555,13 +547,6 @@ impl Module { }) } - /// Get a modules-qualified script-defined functions. - /// - /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. - pub(crate) fn get_qualified_scripted_fn(&mut self, hash_fn_def: u64) -> Option<&FnDef> { - self.all_fn_lib.get_function(hash_fn_def) - } - /// Create a new `Module` by evaluating an `AST`. /// /// # Examples @@ -620,13 +605,12 @@ impl Module { module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, SharedNativeFunction)>, - fn_lib: &mut Vec<(u64, SharedFnDef)>, + functions: &mut Vec<(u64, SharedFunction)>, ) { for (name, m) in &module.modules { // Index all the sub-modules first. qualifiers.push(name); - index_module(m, qualifiers, variables, functions, fn_lib); + index_module(m, qualifiers, variables, functions); qualifiers.pop(); } @@ -672,25 +656,17 @@ impl Module { &fn_def.name, repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); - fn_lib.push((hash_fn_def, fn_def.clone())); + functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); } } let mut variables = Vec::new(); let mut functions = Vec::new(); - let mut fn_lib = Vec::new(); - index_module( - self, - &mut vec!["root"], - &mut variables, - &mut functions, - &mut fn_lib, - ); + index_module(self, &mut vec!["root"], &mut variables, &mut functions); self.all_variables = variables.into_iter().collect(); self.all_functions = functions.into_iter().collect(); - self.all_fn_lib = fn_lib.into(); } /// Does a type iterator exist in the module? @@ -701,14 +677,16 @@ impl Module { /// Set a type iterator into the module. pub fn set_iter(&mut self, typ: TypeId, func: Box) { #[cfg(not(feature = "sync"))] - self.type_iterators.insert(typ, Rc::new(func)); + self.type_iterators + .insert(typ, Rc::new(CallableFunction::Iterator(func))); #[cfg(feature = "sync")] - self.type_iterators.insert(typ, Arc::new(func)); + self.type_iterators + .insert(typ, Arc::new(CallableFunction::Iterator(func))); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { - self.type_iterators.get(&id) + pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + self.type_iterators.get(&id).map(|v| v.as_ref()) } } diff --git a/src/optimize.rs b/src/optimize.rs index 5fc6d608..d320d4bf 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -119,12 +119,12 @@ fn call_fn( pos: Position, ) -> Result, Box> { // Search built-in's and external functions - let hash = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); global_module - .get_fn(hash) - .or_else(|| packages.get_fn(hash)) - .map(|func| func.call(args)) + .get_fn(hash_fn) + .or_else(|| packages.get_fn(hash_fn)) + .map(|func| func.get_native_fn()(args)) .transpose() .map_err(|err| err.new_position(pos)) } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index ad61ae19..39ce601c 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::{NativeFunction, SharedIteratorFunction}; +use crate::fn_native::CallableFunction; use crate::module::Module; use crate::utils::StaticVec; @@ -69,7 +69,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_fn(hash)) } /// Get specified function via its hash key. - pub fn get_fn(&self, hash: u64) -> Option<&NativeFunction> { + pub fn get_fn(&self, hash: u64) -> Option<&CallableFunction> { self.packages .iter() .map(|p| p.get_fn(hash)) @@ -81,7 +81,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&SharedIteratorFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { self.packages .iter() .map(|p| p.get_iter(id)) diff --git a/src/result.rs b/src/result.rs index 4ae0d9e5..af55535a 100644 --- a/src/result.rs +++ b/src/result.rs @@ -36,10 +36,6 @@ pub enum EvalAltResult { /// An error has occurred inside a called function. /// Wrapped values re the name of the function and the interior error. ErrorInFunctionCall(String, Box, Position), - /// Function call has incorrect number of arguments. - /// Wrapped values are the name of the function, the number of parameters required - /// and the actual number of arguments passed. - ErrorFunctionArgsMismatch(String, usize, usize, Position), /// Non-boolean operand encountered for boolean operator. Wrapped value is the operator. ErrorBooleanArgMismatch(String, Position), /// Non-character value encountered where a character is required. @@ -108,9 +104,6 @@ impl EvalAltResult { Self::ErrorParsing(p) => p.desc(), Self::ErrorInFunctionCall(_, _, _) => "Error in called function", Self::ErrorFunctionNotFound(_, _) => "Function not found", - Self::ErrorFunctionArgsMismatch(_, _, _, _) => { - "Function call with wrong number of arguments" - } Self::ErrorBooleanArgMismatch(_, _) => "Boolean operator expects boolean operands", Self::ErrorCharMismatch(_) => "Character expected", Self::ErrorNumericIndexExpr(_) => { @@ -208,21 +201,6 @@ impl fmt::Display for EvalAltResult { Self::ErrorLoopBreak(_, pos) => write!(f, "{} ({})", desc, pos), Self::Return(_, pos) => write!(f, "{} ({})", desc, pos), - Self::ErrorFunctionArgsMismatch(fn_name, 0, n, pos) => write!( - f, - "Function '{}' expects no argument but {} found ({})", - fn_name, n, pos - ), - Self::ErrorFunctionArgsMismatch(fn_name, 1, n, pos) => write!( - f, - "Function '{}' expects one argument but {} found ({})", - fn_name, n, pos - ), - Self::ErrorFunctionArgsMismatch(fn_name, need, n, pos) => write!( - f, - "Function '{}' expects {} argument(s) but {} found ({})", - fn_name, need, n, pos - ), Self::ErrorBooleanArgMismatch(op, pos) => { write!(f, "{} operator expects boolean operands ({})", op, pos) } @@ -292,7 +270,6 @@ impl EvalAltResult { Self::ErrorFunctionNotFound(_, pos) | Self::ErrorInFunctionCall(_, _, pos) - | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) | Self::ErrorArrayBounds(_, _, pos) @@ -331,7 +308,6 @@ impl EvalAltResult { Self::ErrorFunctionNotFound(_, pos) | Self::ErrorInFunctionCall(_, _, pos) - | Self::ErrorFunctionArgsMismatch(_, _, _, pos) | Self::ErrorBooleanArgMismatch(_, pos) | Self::ErrorCharMismatch(pos) | Self::ErrorArrayBounds(_, _, pos) From ab76a69b1242bda3dc3e3cfe851a3855bcb1c995 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 20:07:51 +0800 Subject: [PATCH 30/36] Avoid repeating empty TypeId's when calculating hash. --- src/engine.rs | 40 ++++++++++++++++++--------------- src/module.rs | 59 ++++++++++++++++++++++++------------------------- src/optimize.rs | 7 +++++- src/parser.rs | 48 ++++++++++++++++++---------------------- src/utils.rs | 7 ++---- 5 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 85c0c9f4..d45d1099 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -12,7 +12,7 @@ use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; #[cfg(not(feature = "no_module"))] use crate::module::{resolvers, ModuleRef, ModuleResolver}; @@ -25,7 +25,7 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, format, - iter::{empty, once, repeat}, + iter::{empty, once}, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -215,8 +215,7 @@ impl<'a> State<'a> { /// Since script-defined functions have `Dynamic` parameters, functions with the same name /// and number of parameters are considered equivalent. /// -/// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash` -/// with dummy parameter types `EMPTY_TYPE_ID()` repeated the correct number of times. +/// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_hash`. #[derive(Debug, Clone, Default)] pub struct FunctionsLib(HashMap); @@ -226,9 +225,8 @@ impl FunctionsLib { FunctionsLib( vec.into_iter() .map(|fn_def| { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let args_iter = repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()); - let hash = calc_fn_hash(empty(), &fn_def.name, args_iter); + // Qualifiers (none) + function name + number of arguments. + let hash = calc_fn_hash(empty(), &fn_def.name, fn_def.params.len(), empty()); (hash, fn_def.into()) }) .collect(), @@ -253,8 +251,8 @@ impl FunctionsLib { params: usize, public_only: bool, ) -> Option<&FnDef> { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash_fn_def = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + // Qualifiers (none) + function name + number of arguments. + let hash_fn_def = calc_fn_hash(empty(), name, params, empty()); let fn_def = self.get_function(hash_fn_def); match fn_def.as_ref().map(|f| f.access) { @@ -873,8 +871,13 @@ impl Engine { pos: Position, level: usize, ) -> Result<(Dynamic, bool), Box> { - // Qualifiers (none) + function name + argument `TypeId`'s. - let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. + let hash_fn = calc_fn_hash( + empty(), + fn_name, + args.len(), + args.iter().map(|a| a.type_id()), + ); let hashes = (hash_fn, hash_fn_def); match fn_name { @@ -1322,7 +1325,7 @@ impl Engine { Dynamic(Union::Array(mut rhs_value)) => { let op = "=="; let def_value = false.into(); - let hash_fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash_fn_def = calc_fn_hash(empty(), op, 2, empty()); // Call the `==` operator to compare each value for value in rhs_value.iter_mut() { @@ -1331,7 +1334,8 @@ impl Engine { let pos = rhs.position(); // Qualifiers (none) + function name + argument `TypeId`'s. - let hash_fn = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); + let hash_fn = + calc_fn_hash(empty(), op, args.len(), args.iter().map(|a| a.type_id())); let hashes = (hash_fn, hash_fn_def); let (r, _) = self @@ -1486,7 +1490,7 @@ impl Engine { let mut args: StaticVec<_> = arg_values.iter_mut().collect(); if name == KEYWORD_EVAL && args.len() == 1 && args.get(0).is::() { - let hash_fn = calc_fn_hash(empty(), name, once(TypeId::of::())); + let hash_fn = calc_fn_hash(empty(), name, 1, once(TypeId::of::())); if !self.has_override(state, (hash_fn, *hash_fn_def)) { // eval - only in function call style @@ -1547,11 +1551,11 @@ impl Engine { // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // i.e. qualifiers + function name + number of arguments. + // 2) Calculate a second hash with no qualifiers, empty function name, + // zero number of arguments, and the actual list of argument `TypeId`'.s let hash_fn_args = - calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + calc_fn_hash(empty(), "", 0, args.iter().map(|a| a.type_id())); // 3) The final hash is the XOR of the two hashes. let hash_fn_native = *hash_fn_def ^ hash_fn_args; diff --git a/src/module.rs b/src/module.rs index 06d0cbbd..59a0d0f1 100644 --- a/src/module.rs +++ b/src/module.rs @@ -8,18 +8,22 @@ use crate::fn_native::{ CallableFunction::{Method, Pure}, FnCallArgs, IteratorFn, SharedFunction, }; -use crate::parser::{FnAccess, AST}; +use crate::parser::{ + FnAccess, + FnAccess::{Private, Public}, + AST, +}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; use crate::stdlib::{ any::TypeId, boxed::Box, collections::HashMap, fmt, - iter::{empty, repeat}, + iter::empty, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -30,9 +34,6 @@ use crate::stdlib::{ vec::Vec, }; -/// Default function access mode. -const DEF_ACCESS: FnAccess = FnAccess::Public; - /// Return type of module-level Rust function. pub type FuncReturn = Result>; @@ -281,7 +282,7 @@ impl Module { params: &[TypeId], func: CallableFunction, ) -> u64 { - let hash_fn = calc_fn_hash(empty(), &name, params.iter().cloned()); + let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned()); let params = params.into_iter().cloned().collect(); @@ -312,7 +313,7 @@ impl Module { ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); let arg_types = []; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -337,7 +338,7 @@ impl Module { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -363,7 +364,7 @@ impl Module { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -394,7 +395,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -429,7 +430,7 @@ impl Module { func(a, b).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -467,7 +468,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Pure(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -506,7 +507,7 @@ impl Module { func(a, b, c).map(Dynamic::from) }; let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), DEF_ACCESS, &arg_types, Method(Box::new(f))) + self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) } /// Get a Rust function. @@ -617,27 +618,24 @@ impl Module { // Index all variables for (var_name, value) in &module.variables { // Qualifiers + variable name - let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, empty()); + let hash_var = calc_fn_hash(qualifiers.iter().map(|&v| v), var_name, 0, empty()); variables.push((hash_var, value.clone())); } // Index all Rust functions for (name, access, params, func) in module.functions.values() { match access { // Private functions are not exported - FnAccess::Private => continue, - FnAccess::Public => (), + Private => continue, + Public => (), } // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - let hash_fn_def = calc_fn_hash( - qualifiers.iter().map(|&v| v), - name, - repeat(EMPTY_TYPE_ID()).take(params.len()), - ); - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s - let hash_fn_args = calc_fn_hash(empty(), "", params.iter().cloned()); + // i.e. qualifiers + function name + number of arguments. + let hash_fn_def = + calc_fn_hash(qualifiers.iter().map(|&v| v), name, params.len(), empty()); + // 2) Calculate a second hash with no qualifiers, empty function name, + // zero number of arguments, and the actual list of argument `TypeId`'.s + let hash_fn_args = calc_fn_hash(empty(), "", 0, params.iter().cloned()); // 3) The final hash is the XOR of the two hashes. let hash_fn_native = hash_fn_def ^ hash_fn_args; @@ -647,14 +645,15 @@ impl Module { for fn_def in module.fn_lib.values() { match fn_def.access { // Private functions are not exported - FnAccess::Private => continue, - DEF_ACCESS => (), + Private => continue, + Public => (), } - // Qualifiers + function name + placeholders (one for each parameter) + // Qualifiers + function name + number of arguments. let hash_fn_def = calc_fn_hash( qualifiers.iter().map(|&v| v), &fn_def.name, - repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), + fn_def.params.len(), + empty(), ); functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); } diff --git a/src/optimize.rs b/src/optimize.rs index d320d4bf..1bba1196 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -119,7 +119,12 @@ fn call_fn( pos: Position, ) -> Result, Box> { // Search built-in's and external functions - let hash_fn = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + let hash_fn = calc_fn_hash( + empty(), + fn_name, + args.len(), + args.iter().map(|a| a.type_id()), + ); global_module .get_fn(hash_fn) diff --git a/src/parser.rs b/src/parser.rs index 39fa6678..1c013666 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -7,7 +7,7 @@ use crate::error::{LexError, ParseError, ParseErrorType}; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::{StaticVec, EMPTY_TYPE_ID}; +use crate::utils::StaticVec; #[cfg(not(feature = "no_module"))] use crate::module::ModuleRef; @@ -22,7 +22,7 @@ use crate::stdlib::{ char, collections::HashMap, format, - iter::{empty, repeat, Peekable}, + iter::{empty, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, rc::Rc, @@ -767,13 +767,14 @@ fn parse_call_expr<'a>( // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + no parameters. - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // i.e. qualifiers + function name + number of arguments. + // 2) Calculate a second hash with no qualifiers, empty function name, + // zero number of arguments, and the actual list of argument `TypeId`'s. // 3) The final hash is the XOR of the two hashes. - calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()) + let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + calc_fn_hash(qualifiers, &id, 0, empty()) } else { - calc_fn_hash(empty(), &id, empty()) + calc_fn_hash(empty(), &id, 0, empty()) } }; // Qualifiers (none) + function name + no parameters. @@ -799,7 +800,6 @@ fn parse_call_expr<'a>( // id(...args) (Token::RightParen, _) => { eat_token(input, Token::RightParen); - let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len()); #[cfg(not(feature = "no_module"))] let hash_fn_def = { @@ -808,13 +808,14 @@ fn parse_call_expr<'a>( // Rust functions are indexed in two steps: // 1) Calculate a hash in a similar manner to script-defined functions, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - // 2) Calculate a second hash with no qualifiers, empty function name, and - // the actual list of parameter `TypeId`'.s + // i.e. qualifiers + function name + number of arguments. + // 2) Calculate a second hash with no qualifiers, empty function name, + // zero number of arguments, and the actual list of argument `TypeId`'s. // 3) The final hash is the XOR of the two hashes. - calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, args_iter) + let qualifiers = modules.iter().map(|(m, _)| m.as_str()); + calc_fn_hash(qualifiers, &id, args.len(), empty()) } else { - calc_fn_hash(empty(), &id, args_iter) + calc_fn_hash(empty(), &id, args.len(), empty()) } }; // Qualifiers (none) + function name + dummy parameter types (one for each parameter). @@ -1297,7 +1298,7 @@ fn parse_primary<'a>( let modules = modules.as_mut().unwrap(); // Qualifiers + variable name - *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); + *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0, empty()); modules.set_index(state.find_module(&modules.get(0).0)); } _ => (), @@ -1362,7 +1363,7 @@ fn parse_unary<'a>( // Call negative function expr => { let op = "-"; - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), op, 2, empty()); let mut args = StaticVec::new(); args.push(expr); @@ -1388,7 +1389,7 @@ fn parse_unary<'a>( args.push(parse_primary(input, state, level + 1, allow_stmt_expr)?); let op = "!"; - let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), op, 2, empty()); Ok(Expr::FnCall(Box::new(( (op.into(), pos), @@ -1492,7 +1493,7 @@ fn parse_op_assignment_stmt<'a>( args.push(lhs_copy); args.push(rhs); - let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(args.len())); + let hash = calc_fn_hash(empty(), &op, args.len(), empty()); let rhs_expr = Expr::FnCall(Box::new(((op, pos), None, hash, args, None))); make_assignment_stmt(state, lhs, rhs_expr, pos) @@ -1780,7 +1781,7 @@ fn parse_binary_op<'a>( let cmp_def = Some(false.into()); let op = op_token.syntax(); - let hash = calc_fn_hash(empty(), &op, repeat(EMPTY_TYPE_ID()).take(2)); + let hash = calc_fn_hash(empty(), &op, 2, empty()); let mut args = StaticVec::new(); args.push(root); @@ -1839,8 +1840,7 @@ fn parse_binary_op<'a>( Expr::FnCall(x) => { let ((id, _), _, hash, args, _) = x.as_mut(); // Recalculate function call hash because there is an additional argument - let args_iter = repeat(EMPTY_TYPE_ID()).take(args.len() + 1); - *hash = calc_fn_hash(empty(), id, args_iter); + *hash = calc_fn_hash(empty(), id, args.len() + 1, empty()); } _ => (), } @@ -2540,12 +2540,8 @@ fn parse_global_level<'a>( let mut state = ParseState::new(max_expr_depth.1); let func = parse_fn(input, &mut state, access, 0, true)?; - // Qualifiers (none) + function name + argument `TypeId`'s - let hash = calc_fn_hash( - empty(), - &func.name, - repeat(EMPTY_TYPE_ID()).take(func.params.len()), - ); + // Qualifiers (none) + function name + number of arguments. + let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty()); functions.insert(hash, func); continue; diff --git a/src/utils.rs b/src/utils.rs index a8f73390..126f6809 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,11 +21,6 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; -#[inline(always)] -pub fn EMPTY_TYPE_ID() -> TypeId { - TypeId::of::<()>() -} - /// Calculate a `u64` hash key from a module-qualified function name and parameter types. /// /// Module names are passed in via `&str` references from an iterator. @@ -37,6 +32,7 @@ pub fn EMPTY_TYPE_ID() -> TypeId { pub fn calc_fn_spec<'a>( modules: impl Iterator, fn_name: &str, + num: usize, params: impl Iterator, ) -> u64 { #[cfg(feature = "no_std")] @@ -47,6 +43,7 @@ pub fn calc_fn_spec<'a>( // We always skip the first module modules.skip(1).for_each(|m| m.hash(&mut s)); s.write(fn_name.as_bytes()); + s.write_usize(num); params.for_each(|t| t.hash(&mut s)); s.finish() } From 4a1fd66b9f1d5d1e2f357aba56748b10095a9cbf Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 19 May 2020 22:25:57 +0800 Subject: [PATCH 31/36] Reduce Rc/Arc wrapping for functions. --- src/engine.rs | 6 ++-- src/fn_native.rs | 83 +++++++++++++++++++++++--------------------- src/fn_register.rs | 23 ++++++------- src/module.rs | 84 +++++++++++++++++---------------------------- src/packages/mod.rs | 4 +-- src/parser.rs | 35 ++++--------------- 6 files changed, 97 insertions(+), 138 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index d45d1099..cb3b0e8c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,11 +3,11 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback}; +use crate::fn_native::{FnCallArgs, PrintCallback, ProgressCallback, SharedFnDef}; use crate::module::Module; use crate::optimize::OptimizationLevel; use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage}; -use crate::parser::{Expr, FnAccess, FnDef, ReturnType, SharedFnDef, Stmt, AST}; +use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -1741,7 +1741,7 @@ impl Engine { let index = scope.len() - 1; state.scope_level += 1; - for loop_var in func.get_iter_fn()(iter_type) { + for loop_var in func(iter_type) { *scope.get_mut(index).0 = loop_var; self.inc_operations(state, stmt.position())?; diff --git a/src/fn_native.rs b/src/fn_native.rs index 328372b9..619fb102 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,5 +1,5 @@ use crate::any::Dynamic; -use crate::parser::{FnDef, SharedFnDef}; +use crate::parser::FnDef; use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc}; @@ -73,15 +73,31 @@ pub trait IteratorCallback: Fn(Dynamic) -> Box> + ' #[cfg(not(feature = "sync"))] impl Box> + 'static> IteratorCallback for F {} +#[cfg(not(feature = "sync"))] +pub type SharedNativeFunction = Rc; +#[cfg(feature = "sync")] +pub type SharedNativeFunction = Arc; + +#[cfg(not(feature = "sync"))] +pub type SharedIteratorFn = Rc; +#[cfg(feature = "sync")] +pub type SharedIteratorFn = Arc; + +#[cfg(feature = "sync")] +pub type SharedFnDef = Arc; +#[cfg(not(feature = "sync"))] +pub type SharedFnDef = Rc; + /// A type encapsulating a function callable by Rhai. +#[derive(Clone)] pub enum CallableFunction { /// A pure native Rust function with all arguments passed by value. - Pure(Box), + Pure(SharedNativeFunction), /// A native Rust object method with the first argument passed by reference, /// and the rest passed by value. - Method(Box), + Method(SharedNativeFunction), /// An iterator function. - Iterator(Box), + Iterator(SharedIteratorFn), /// A script-defined function. Script(SharedFnDef), } @@ -90,37 +106,29 @@ impl CallableFunction { /// Is this a pure native Rust function? pub fn is_pure(&self) -> bool { match self { - CallableFunction::Pure(_) => true, - CallableFunction::Method(_) - | CallableFunction::Iterator(_) - | CallableFunction::Script(_) => false, + Self::Pure(_) => true, + Self::Method(_) | Self::Iterator(_) | Self::Script(_) => false, } } /// Is this a pure native Rust method-call? pub fn is_method(&self) -> bool { match self { - CallableFunction::Method(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Iterator(_) - | CallableFunction::Script(_) => false, + Self::Method(_) => true, + Self::Pure(_) | Self::Iterator(_) | Self::Script(_) => false, } } /// Is this an iterator function? pub fn is_iter(&self) -> bool { match self { - CallableFunction::Iterator(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Script(_) => false, + Self::Iterator(_) => true, + Self::Pure(_) | Self::Method(_) | Self::Script(_) => false, } } /// Is this a Rhai-scripted function? pub fn is_script(&self) -> bool { match self { - CallableFunction::Script(_) => true, - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => false, + Self::Script(_) => true, + Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false, } } /// Get a reference to a native Rust function. @@ -128,10 +136,10 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Pure` or `Method`. - pub fn get_native_fn(&self) -> &Box { + pub fn get_native_fn(&self) -> &FnAny { match self { - CallableFunction::Pure(f) | CallableFunction::Method(f) => f, - CallableFunction::Iterator(_) | CallableFunction::Script(_) => panic!(), + Self::Pure(f) | Self::Method(f) => f.as_ref(), + Self::Iterator(_) | Self::Script(_) => panic!(), } } /// Get a reference to a script-defined function definition. @@ -141,10 +149,8 @@ impl CallableFunction { /// Panics if the `CallableFunction` is not `Script`. pub fn get_fn_def(&self) -> &FnDef { match self { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => panic!(), - CallableFunction::Script(f) => f, + Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(), + Self::Script(f) => f, } } /// Get a reference to an iterator function. @@ -152,19 +158,18 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Iterator`. - pub fn get_iter_fn(&self) -> &Box { + pub fn get_iter_fn(&self) -> &IteratorFn { match self { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Script(_) => panic!(), - CallableFunction::Iterator(f) => f, + Self::Iterator(f) => f.as_ref(), + Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(), } } + /// Create a new `CallableFunction::Pure`. + pub fn from_pure(func: Box) -> Self { + Self::Pure(func.into()) + } + /// Create a new `CallableFunction::Method`. + pub fn from_method(func: Box) -> Self { + Self::Method(func.into()) + } } - -/// A callable function. -#[cfg(not(feature = "sync"))] -pub type SharedFunction = Rc; -/// A callable function. -#[cfg(feature = "sync")] -pub type SharedFunction = Arc; diff --git a/src/fn_register.rs b/src/fn_register.rs index 9636e557..46dbd166 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -4,10 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::Engine; -use crate::fn_native::{ - CallableFunction::{Method, Pure}, - FnCallArgs, -}; +use crate::fn_native::{CallableFunction, FnAny, FnCallArgs}; use crate::parser::FnAccess; use crate::result::EvalAltResult; @@ -158,7 +155,7 @@ macro_rules! make_func { // Map the result $map(r) - }) + }) as Box }; } @@ -184,7 +181,7 @@ pub fn map_result( macro_rules! def_register { () => { - def_register!(imp Pure :); + def_register!(imp from_pure :); }; (imp $abi:ident : $($par:ident => $mark:ty => $param:ty => $clone:expr),*) => { // ^ function ABI type @@ -207,7 +204,7 @@ macro_rules! def_register { fn register_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_dynamic ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_dynamic ; $($par => $clone),*)) ); } } @@ -225,7 +222,7 @@ macro_rules! def_register { fn register_dynamic_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_identity ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_identity ; $($par => $clone),*)) ); } } @@ -244,7 +241,7 @@ macro_rules! def_register { fn register_result_fn(&mut self, name: &str, f: FN) { self.global_module.set_fn(name.to_string(), FnAccess::Public, &[$(TypeId::of::<$par>()),*], - $abi(make_func!(f : map_result ; $($par => $clone),*)) + CallableFunction::$abi(make_func!(f : map_result ; $($par => $clone),*)) ); } } @@ -252,11 +249,11 @@ macro_rules! def_register { //def_register!(imp_pop $($par => $mark => $param),*); }; ($p0:ident $(, $p:ident)*) => { - def_register!(imp Pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); - def_register!(imp Method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); + def_register!(imp from_pure : $p0 => $p0 => $p0 => by_value $(, $p => $p => $p => by_value)*); + def_register!(imp from_method : $p0 => Mut<$p0> => &mut $p0 => by_ref $(, $p => $p => $p => by_value)*); // ^ CallableFunction - // handle the first parameter ^ first parameter passed through - // ^ others passed by value (by_value) + // handle the first parameter ^ 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 diff --git a/src/module.rs b/src/module.rs index 59a0d0f1..5e751df8 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,11 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{ - CallableFunction, - CallableFunction::{Method, Pure}, - FnCallArgs, IteratorFn, SharedFunction, -}; +use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn, SharedIteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -27,9 +23,7 @@ use crate::stdlib::{ mem, num::NonZeroUsize, ops::{Deref, DerefMut}, - rc::Rc, string::{String, ToString}, - sync::Arc, vec, vec::Vec, }; @@ -53,17 +47,17 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, SharedFunction)>, + functions: HashMap, CF)>, /// Script-defined functions. fn_lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. - all_functions: HashMap, + all_functions: HashMap, } impl fmt::Debug for Module { @@ -275,13 +269,7 @@ impl Module { /// Set a Rust function into the module, returning a hash key. /// /// If there is an existing Rust function of the same hash, it is replaced. - pub fn set_fn( - &mut self, - name: String, - access: FnAccess, - params: &[TypeId], - func: CallableFunction, - ) -> u64 { + pub fn set_fn(&mut self, name: String, access: FnAccess, params: &[TypeId], func: CF) -> u64 { let hash_fn = calc_fn_hash(empty(), &name, params.len(), params.iter().cloned()); let params = params.into_iter().cloned().collect(); @@ -312,8 +300,8 @@ impl Module { #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { let f = move |_: &mut FnCallArgs| func().map(Dynamic::from); - let arg_types = []; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = []; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -337,8 +325,8 @@ impl Module { ) -> u64 { let f = move |args: &mut FnCallArgs| func(mem::take(args[0]).cast::()).map(Dynamic::from); - let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -363,8 +351,8 @@ impl Module { let f = move |args: &mut FnCallArgs| { func(args[0].downcast_mut::().unwrap()).map(Dynamic::from) }; - let arg_types = [TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -394,8 +382,8 @@ impl Module { func(a, b).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -429,8 +417,8 @@ impl Module { func(a, b).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -467,8 +455,8 @@ impl Module { func(a, b, c).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Pure(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_pure(Box::new(f))) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -506,8 +494,8 @@ impl Module { func(a, b, c).map(Dynamic::from) }; - let arg_types = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(name.into(), Public, &arg_types, Method(Box::new(f))) + let args = [TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(name.into(), Public, &args, CF::from_method(Box::new(f))) } /// Get a Rust function. @@ -524,8 +512,8 @@ impl Module { /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { - self.functions.get(&hash_fn).map(|(_, _, _, v)| v.as_ref()) + pub fn get_fn(&self, hash_fn: u64) -> Option<&CF> { + self.functions.get(&hash_fn).map(|(_, _, _, v)| v) } /// Get a modules-qualified function. @@ -536,16 +524,13 @@ impl Module { &mut self, name: &str, hash_fn_native: u64, - ) -> Result<&CallableFunction, Box> { - self.all_functions - .get(&hash_fn_native) - .map(|f| f.as_ref()) - .ok_or_else(|| { - Box::new(EvalAltResult::ErrorFunctionNotFound( - name.to_string(), - Position::none(), - )) - }) + ) -> Result<&CF, Box> { + self.all_functions.get(&hash_fn_native).ok_or_else(|| { + Box::new(EvalAltResult::ErrorFunctionNotFound( + name.to_string(), + Position::none(), + )) + }) } /// Create a new `Module` by evaluating an `AST`. @@ -606,7 +591,7 @@ impl Module { module: &'a Module, qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, - functions: &mut Vec<(u64, SharedFunction)>, + functions: &mut Vec<(u64, CF)>, ) { for (name, m) in &module.modules { // Index all the sub-modules first. @@ -655,7 +640,7 @@ impl Module { fn_def.params.len(), empty(), ); - functions.push((hash_fn_def, CallableFunction::Script(fn_def.clone()).into())); + functions.push((hash_fn_def, CF::Script(fn_def.clone()).into())); } } @@ -675,16 +660,11 @@ impl Module { /// Set a type iterator into the module. pub fn set_iter(&mut self, typ: TypeId, func: Box) { - #[cfg(not(feature = "sync"))] - self.type_iterators - .insert(typ, Rc::new(CallableFunction::Iterator(func))); - #[cfg(feature = "sync")] - self.type_iterators - .insert(typ, Arc::new(CallableFunction::Iterator(func))); + self.type_iterators.insert(typ, func.into()); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { self.type_iterators.get(&id).map(|v| v.as_ref()) } } diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 39ce601c..3e8a4f04 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -1,6 +1,6 @@ //! Module containing all built-in _packages_ available to Rhai, plus facilities to define custom packages. -use crate::fn_native::CallableFunction; +use crate::fn_native::{CallableFunction, IteratorFn}; use crate::module::Module; use crate::utils::StaticVec; @@ -81,7 +81,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&CallableFunction> { + pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { self.packages .iter() .map(|p| p.get_iter(id)) diff --git a/src/parser.rs b/src/parser.rs index 1c013666..e03f3686 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -25,9 +25,7 @@ use crate::stdlib::{ iter::{empty, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, - rc::Rc, string::{String, ToString}, - sync::Arc, vec, vec::Vec, }; @@ -59,21 +57,14 @@ type PERR = ParseErrorType; pub struct AST( /// Global statements. Vec, - /// Script-defined functions, wrapped in an `Arc` for shared access. - #[cfg(feature = "sync")] - Arc, - /// Script-defined functions, wrapped in an `Rc` for shared access. - #[cfg(not(feature = "sync"))] - Rc, + /// Script-defined functions. + FunctionsLib, ); impl AST { /// Create a new `AST`. pub fn new(statements: Vec, fn_lib: FunctionsLib) -> Self { - #[cfg(feature = "sync")] - return Self(statements, Arc::new(fn_lib)); - #[cfg(not(feature = "sync"))] - return Self(statements, Rc::new(fn_lib)); + Self(statements, fn_lib) } /// Get the statements. @@ -88,7 +79,7 @@ impl AST { /// Get the script-defined functions. pub(crate) fn fn_lib(&self) -> &FunctionsLib { - self.1.as_ref() + &self.1 } /// Merge two `AST` into one. Both `AST`'s are untouched and a new, merged, version @@ -147,20 +138,13 @@ impl AST { (true, true) => vec![], }; - Self::new(ast, functions.merge(other.1.as_ref())) + Self::new(ast, functions.merge(&other.1)) } /// Clear all function definitions in the `AST`. #[cfg(not(feature = "no_function"))] pub fn clear_functions(&mut self) { - #[cfg(feature = "sync")] - { - self.1 = Arc::new(Default::default()); - } - #[cfg(not(feature = "sync"))] - { - self.1 = Rc::new(Default::default()); - } + self.1 = Default::default(); } /// Clear all statements in the `AST`, leaving only function definitions. @@ -202,13 +186,6 @@ pub struct FnDef { pub pos: Position, } -/// A sharable script-defined function. -#[cfg(feature = "sync")] -pub type SharedFnDef = Arc; -/// A sharable script-defined function. -#[cfg(not(feature = "sync"))] -pub type SharedFnDef = Rc; - /// `return`/`throw` statement. #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] pub enum ReturnType { From fad60c0a7d1a7abd974d5e1f046345af30cf49ae Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 00:06:07 +0800 Subject: [PATCH 32/36] Bump version. --- Cargo.toml | 2 +- README.md | 4 ++-- RELEASES.md | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68837a2d..981f1241 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai" -version = "0.14.1" +version = "0.14.2" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung"] description = "Embedded scripting for Rust" diff --git a/README.md b/README.md index f6db9187..b824d178 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Rhai's current features set: to do checked arithmetic operations); for [`no-std`](#optional-features) builds, a number of additional dependencies are pulled in to provide for functionalities that used to be in `std`. -**Note:** Currently, the version is 0.14.1, so the language and API's may change before they stabilize. +**Note:** Currently, the version is 0.14.2, so the language and API's may change before they stabilize. Installation ------------ @@ -45,7 +45,7 @@ Install the Rhai crate by adding this line to `dependencies`: ```toml [dependencies] -rhai = "0.14.1" +rhai = "0.14.2" ``` Use the latest released crate version on [`crates.io`](https::/crates.io/crates/rhai/): diff --git a/RELEASES.md b/RELEASES.md index 825105a8..8d01f9f5 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,9 +1,14 @@ Rhai Release Notes ================== -Version 0.15.0 +Version 0.14.2 ============== +Regression +---------- + +* Do not optimize script with `eval_expression` - it is assumed to be one-off and short. + New features ------------ From 5db1fd37122ee587da732300ed48f79535d95e1a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 00:06:19 +0800 Subject: [PATCH 33/36] Do not optimize eval_expression scripts. --- src/api.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index f0e249e9..874aaa4e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -824,9 +824,10 @@ impl Engine { &mut stream.peekable(), self, scope, - self.optimization_level, + OptimizationLevel::None, // No need to optimize a lone expression self.max_expr_depth, )?; + self.eval_ast_with_scope(scope, &ast) } From c98633dd2bf3761a29719ede3b03efe85305dedd Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 11:12:22 +0800 Subject: [PATCH 34/36] Add EvalPackage. --- README.md | 10 +++++++++- src/packages/eval.rs | 12 ++++++++++++ src/packages/mod.rs | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/packages/eval.rs diff --git a/README.md b/README.md index b824d178..8673aba5 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Disable script-defined functions (`no_function`) only when the feature is not ne [`Engine::new_raw`](#raw-engine) creates a _raw_ engine which does not register _any_ utility functions. This makes the scripting language quite useless as even basic arithmetic operators are not supported. -Selectively include the necessary functionalities by loading specific [packages](#packages) to minimize the footprint. +Selectively include the necessary functionalities by loading specific [packages] to minimize the footprint. Packages are sharable (even across threads via the [`sync`] feature), so they only have to be created once. Related @@ -380,6 +380,9 @@ Use `Engine::new_raw` to create a _raw_ `Engine`, in which _nothing_ is added, n ### Packages +[package]: #packages +[packages]: #packages + Rhai functional features are provided in different _packages_ that can be loaded via a call to `Engine::load_package`. Packages reside under `rhai::packages::*` and the trait `rhai::packages::Package` must be loaded in order for packages to be used. @@ -408,6 +411,7 @@ The follow packages are available: | `BasicMathPackage` | Basic math functions (e.g. `sin`, `sqrt`) | No | Yes | | `BasicArrayPackage` | Basic [array] functions | No | Yes | | `BasicMapPackage` | Basic [object map] functions | No | Yes | +| `EvalPackage` | Disable [`eval`] | No | No | | `CorePackage` | Basic essentials | | | | `StandardPackage` | Standard library | | | @@ -2669,6 +2673,8 @@ engine.set_optimization_level(rhai::OptimizationLevel::None); `eval` - or "How to Shoot Yourself in the Foot even Easier" --------------------------------------------------------- +[`eval`]: #eval---or-how-to-shoot-yourself-in-the-foot-even-easier + Saving the best for last: in addition to script optimizations, there is the ever-dreaded... `eval` function! ```rust @@ -2730,3 +2736,5 @@ fn alt_eval(script: String) -> Result<(), Box> { engine.register_result_fn("eval", alt_eval); ``` + +There is even a [package] named `EvalPackage` which implements the disabling override. diff --git a/src/packages/eval.rs b/src/packages/eval.rs new file mode 100644 index 00000000..a6756d96 --- /dev/null +++ b/src/packages/eval.rs @@ -0,0 +1,12 @@ +use crate::def_package; +use crate::module::FuncReturn; +use crate::stdlib::string::String; + +def_package!(crate:EvalPackage:"Disable 'eval'.", lib, { + lib.set_fn_1_mut( + "eval", + |_: &mut String| -> FuncReturn<()> { + Err("eval is evil!".into()) + }, + ); +}); diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 3e8a4f04..62df57a6 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -8,6 +8,7 @@ use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync: mod arithmetic; mod array_basic; +mod eval; mod iter_basic; mod logic; mod map_basic; @@ -21,6 +22,7 @@ mod time_basic; pub use arithmetic::ArithmeticPackage; #[cfg(not(feature = "no_index"))] pub use array_basic::BasicArrayPackage; +pub use eval::EvalPackage; pub use iter_basic::BasicIteratorPackage; pub use logic::LogicPackage; #[cfg(not(feature = "no_object"))] From 55ee4d6a199bc8f19b6808cf476c7b93fee72224 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 11:12:35 +0800 Subject: [PATCH 35/36] More benchmarks. --- benches/eval_expression.rs | 64 ++++++++++++++++++++++++++++++++++++++ benches/parsing.rs | 32 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index 131d3bb5..c647c1b5 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -41,3 +41,67 @@ fn bench_eval_expression_number_operators(bench: &mut Bencher) { bench.iter(|| engine.consume_ast(&ast).unwrap()); } + +#[bench] +fn bench_eval_expression_optimized_simple(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Simple); + let ast = engine.compile_expression(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_expression_optimized_full(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Full); + let ast = engine.compile_expression(script).unwrap(); + + bench.iter(|| engine.consume_ast(&ast).unwrap()); +} + +#[bench] +fn bench_eval_call_expression(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + + bench.iter(|| engine.eval_expression::(script).unwrap()); +} + +#[bench] +fn bench_eval_call(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + + bench.iter(|| engine.eval::(script).unwrap()); +} diff --git a/benches/parsing.rs b/benches/parsing.rs index c4486bd5..01011877 100644 --- a/benches/parsing.rs +++ b/benches/parsing.rs @@ -102,3 +102,35 @@ fn bench_parse_primes(bench: &mut Bencher) { bench.iter(|| engine.compile(script).unwrap()); } + +#[bench] +fn bench_parse_optimize_simple(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Simple); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} + +#[bench] +fn bench_parse_optimize_full(bench: &mut Bencher) { + let script = r#" + 2 > 1 && + "something" != "nothing" || + "2014-01-20" < "Wed Jul 8 23:07:35 MDT 2015" && + [array, with, spaces].len() <= #{prop:name}.len() && + modifierTest + 1000 / 2 > (80 * 100 % 2) + "#; + + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Full); + + bench.iter(|| engine.compile_expression(script).unwrap()); +} From 80fcc40710fb9607b949a31a8b24eb48ee05bae9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 May 2020 19:27:23 +0800 Subject: [PATCH 36/36] Use function pointers for iterators. --- src/api.rs | 8 +++----- src/fn_native.rs | 32 ++++---------------------------- src/module.rs | 12 ++++++------ src/packages/array_basic.rs | 4 +--- src/packages/iter_basic.rs | 22 ++++++++-------------- src/packages/mod.rs | 2 +- 6 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/api.rs b/src/api.rs index 874aaa4e..48052e85 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,9 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::engine::{make_getter, make_setter, Engine, State, FUNC_INDEXER}; use crate::error::ParseError; use crate::fn_call::FuncArgs; -use crate::fn_native::{ - IteratorCallback, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback, -}; +use crate::fn_native::{IteratorFn, ObjectGetCallback, ObjectIndexerCallback, ObjectSetCallback}; use crate::fn_register::RegisterFn; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::parser::{parse, parse_global_expr, AST}; @@ -123,8 +121,8 @@ impl Engine { /// Register an iterator adapter for a type with the `Engine`. /// This is an advanced feature. - pub fn register_iterator(&mut self, f: F) { - self.global_module.set_iter(TypeId::of::(), Box::new(f)); + pub fn register_iterator(&mut self, f: IteratorFn) { + self.global_module.set_iter(TypeId::of::(), f); } /// Register a getter function for a member of a registered type with the `Engine`. diff --git a/src/fn_native.rs b/src/fn_native.rs index 619fb102..262bad7c 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -11,10 +11,7 @@ pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result> #[cfg(not(feature = "sync"))] pub type FnAny = dyn Fn(&mut FnCallArgs) -> Result>; -#[cfg(feature = "sync")] -pub type IteratorFn = dyn Fn(Dynamic) -> Box> + Send + Sync; -#[cfg(not(feature = "sync"))] -pub type IteratorFn = dyn Fn(Dynamic) -> Box>; +pub type IteratorFn = fn(Dynamic) -> Box>; #[cfg(feature = "sync")] pub type PrintCallback = dyn Fn(&str) + Send + Sync + 'static; @@ -57,32 +54,11 @@ pub trait ObjectIndexerCallback: Fn(&mut T, X) -> U + 'static {} #[cfg(not(feature = "sync"))] impl U + 'static, T, X, U> ObjectIndexerCallback for F {} -#[cfg(feature = "sync")] -pub trait IteratorCallback: - Fn(Dynamic) -> Box> + Send + Sync + 'static -{ -} -#[cfg(feature = "sync")] -impl Box> + Send + Sync + 'static> IteratorCallback - for F -{ -} - -#[cfg(not(feature = "sync"))] -pub trait IteratorCallback: Fn(Dynamic) -> Box> + 'static {} -#[cfg(not(feature = "sync"))] -impl Box> + 'static> IteratorCallback for F {} - #[cfg(not(feature = "sync"))] pub type SharedNativeFunction = Rc; #[cfg(feature = "sync")] pub type SharedNativeFunction = Arc; -#[cfg(not(feature = "sync"))] -pub type SharedIteratorFn = Rc; -#[cfg(feature = "sync")] -pub type SharedIteratorFn = Arc; - #[cfg(feature = "sync")] pub type SharedFnDef = Arc; #[cfg(not(feature = "sync"))] @@ -97,7 +73,7 @@ pub enum CallableFunction { /// and the rest passed by value. Method(SharedNativeFunction), /// An iterator function. - Iterator(SharedIteratorFn), + Iterator(IteratorFn), /// A script-defined function. Script(SharedFnDef), } @@ -158,9 +134,9 @@ impl CallableFunction { /// # Panics /// /// Panics if the `CallableFunction` is not `Iterator`. - pub fn get_iter_fn(&self) -> &IteratorFn { + pub fn get_iter_fn(&self) -> IteratorFn { match self { - Self::Iterator(f) => f.as_ref(), + Self::Iterator(f) => *f, Self::Pure(_) | Self::Method(_) | Self::Script(_) => panic!(), } } diff --git a/src/module.rs b/src/module.rs index 5e751df8..48e6e936 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,7 +3,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; -use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn, SharedIteratorFn}; +use crate::fn_native::{CallableFunction as CF, FnCallArgs, IteratorFn}; use crate::parser::{ FnAccess, FnAccess::{Private, Public}, @@ -53,7 +53,7 @@ pub struct Module { fn_lib: FunctionsLib, /// Iterator functions, keyed by the type producing the iterator. - type_iterators: HashMap, + type_iterators: HashMap, /// Flattened collection of all external Rust functions, native or scripted, /// including those in sub-modules. @@ -659,13 +659,13 @@ impl Module { } /// Set a type iterator into the module. - pub fn set_iter(&mut self, typ: TypeId, func: Box) { - self.type_iterators.insert(typ, func.into()); + pub fn set_iter(&mut self, typ: TypeId, func: IteratorFn) { + self.type_iterators.insert(typ, func); } /// Get the specified type iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { - self.type_iterators.get(&id).map(|v| v.as_ref()) + pub fn get_iter(&self, id: TypeId) -> Option { + self.type_iterators.get(&id).cloned() } } diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index bdfd03aa..eb56bbf4 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -120,8 +120,6 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, { // Register array iterator lib.set_iter( TypeId::of::(), - Box::new(|arr| Box::new( - arr.cast::().into_iter()) as Box> - ), + |arr| Box::new(arr.cast::().into_iter()) as Box>, ); }); diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 553873e7..ebf55992 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -14,13 +14,10 @@ fn reg_range(lib: &mut Module) where Range: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_range(from: T, to: T) -> FuncReturn> { @@ -58,13 +55,10 @@ where T: Variant + Clone + PartialOrd, StepRange: Iterator, { - lib.set_iter( - TypeId::of::>(), - Box::new(|source| { - Box::new(source.cast::>().map(|x| x.into_dynamic())) - as Box> - }), - ); + lib.set_iter(TypeId::of::>(), |source| { + Box::new(source.cast::>().map(|x| x.into_dynamic())) + as Box> + }); } fn get_step_range(from: T, to: T, step: T) -> FuncReturn> diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 62df57a6..54b6ecea 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -83,7 +83,7 @@ impl PackagesCollection { self.packages.iter().any(|p| p.contains_iter(id)) } /// Get the specified TypeId iterator. - pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> { + pub fn get_iter(&self, id: TypeId) -> Option { self.packages .iter() .map(|p| p.get_iter(id))