From 59eaad1fdf140912c6f0a59a2b722d28dc756dfa Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 6 May 2020 22:26:52 +0800 Subject: [PATCH 01/20] Create module from file. --- src/module.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/module.rs b/src/module.rs index bdb545b1..1fa704c8 100644 --- a/src/module.rs +++ b/src/module.rs @@ -740,6 +740,15 @@ mod file { pub fn new() -> Self { Default::default() } + + /// Create a `Module` from a file path. + pub fn create_module>( + &self, + engine: &Engine, + path: &str, + ) -> Result> { + self.resolve(engine, path, Default::default()) + } } impl ModuleResolver for FileModuleResolver { From f1abd115716bf3192594ece15793cd665b71dd42 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 6 May 2020 23:00:26 +0800 Subject: [PATCH 02/20] Add docs and test on creating module from AST and file. --- README.md | 51 ++++++++++++++++++++++++++++++++---- tests/modules.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cd95c92f..95c0d274 100644 --- a/README.md +++ b/README.md @@ -2080,7 +2080,7 @@ crypto::encrypt(others); // <- this causes a run-time error because the 'cryp ### Creating custom modules from Rust -To load a custom module into an [`Engine`], first create a `Module` type, add variables/functions into it, +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 at the beginning of any script run. @@ -2105,6 +2105,47 @@ engine.eval_expression_with_scope::(&scope, "question::answer + 1")? == 42; engine.eval_expression_with_scope::(&scope, "question::inc(question::answer)")? == 42; ``` +### 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`: + +```rust +use rhai::{Engine, Module}; + +let engine = Engine::new(); + +// Compile a script into an 'AST' +let ast = engine.compile(r#" + // Functions become module functions + fn calc(x) { + x + 1 + } + fn add_len(x, y) { + x + y.len() + } + + // Imported modules become sub-modules + import "another module" as extra; + + // Variables defined at global level become module variables + const x = 123; + let foo = 41; + let hello; + + // Final variable values become constant module variable values + foo = calc(foo); + hello = "hello, " + foo + " worlds!"; +"#)?; + +// Convert the 'AST' into a module, using the 'Engine' to evaluate it first +let module = Module::eval_ast_as_new(&ast, &engine)?; + +// 'module' now can be loaded into a custom 'Scope' for future use. It contains: +// - sub-module: 'extra' +// - functions: 'calc', 'add_len' +// - variables: 'x', 'foo', 'hello' +``` + ### Module resolvers When encountering an `import` statement, Rhai attempts to _resolve_ the module based on the path string. @@ -2114,10 +2155,10 @@ which simply loads a script file based on the path (with `.rhai` extension attac Built-in module resolvers are grouped under the `rhai::module_resolvers` module namespace. -| Module Resolver | Description | -| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `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. | -| `StaticModuleResolver` | Loads modules that are statically added. This can be used when the [`no_std`] feature is turned on. | +| Module Resolver | Description | +| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `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`: diff --git a/tests/modules.rs b/tests/modules.rs index e6028179..e7475c5c 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -81,3 +81,71 @@ fn test_module_resolver() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_module_from_ast() -> Result<(), Box> { + let mut engine = Engine::new(); + + let mut resolver = rhai::module_resolvers::StaticModuleResolver::new(); + let mut sub_module = Module::new(); + sub_module.set_var("foo", true); + resolver.insert("another module".to_string(), sub_module); + + engine.set_module_resolver(Some(resolver)); + + let ast = engine.compile( + r#" + // Functions become module functions + fn calc(x) { + x + 1 + } + fn add_len(x, y) { + x + y.len() + } + + // Imported modules become sub-modules + import "another module" as extra; + + // Variables defined at global level become module variables + const x = 123; + let foo = 41; + let hello; + + // Final variable values become constant module variable values + foo = calc(foo); + hello = "hello, " + foo + " worlds!"; + "#, + )?; + + let module = Module::eval_ast_as_new(&ast, &engine)?; + + let mut scope = Scope::new(); + scope.push_module("testing", module); + + assert_eq!( + engine.eval_expression_with_scope::(&mut scope, "testing::x")?, + 123 + ); + assert_eq!( + engine.eval_expression_with_scope::(&mut scope, "testing::foo")?, + 42 + ); + assert!(engine.eval_expression_with_scope::(&mut scope, "testing::extra::foo")?); + assert_eq!( + engine.eval_expression_with_scope::(&mut scope, "testing::hello")?, + "hello, 42 worlds!" + ); + assert_eq!( + engine.eval_expression_with_scope::(&mut scope, "testing::calc(999)")?, + 1000 + ); + assert_eq!( + engine.eval_expression_with_scope::( + &mut scope, + "testing::add_len(testing::foo, testing::hello)" + )?, + 59 + ); + + Ok(()) +} From ae776f1e1144bb8bd5d59033fed4bdd435c91b1e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 6 May 2020 23:19:55 +0800 Subject: [PATCH 03/20] Avoid recreating array in loop. --- src/engine.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/engine.rs b/src/engine.rs index b35419cb..3634788c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1112,10 +1112,11 @@ impl Engine { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(mut rhs_value)) => { let def_value = false.into(); + let args = &mut [&mut lhs_value, &mut Default::default()]; // Call the '==' operator to compare each value for value in rhs_value.iter_mut() { - let args = &mut [&mut lhs_value, value]; + args[1] = value; let def_value = Some(&def_value); let pos = rhs.position(); From 34327f6e54388babe886b0ef33971348bbcde9c2 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 6 May 2020 23:52:47 +0800 Subject: [PATCH 04/20] Avoid copying arguments in packages. --- src/engine.rs | 31 ++++++++++++++++++++++++++----- src/packages/utils.rs | 29 +++++++++++++++-------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index b35419cb..1375e73a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -502,6 +502,12 @@ impl Engine { } /// Universal method for calling functions either registered with the `Engine` or written in Rhai. + /// + /// ## WARNING + /// + /// 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_fn_raw( &self, scope: Option<&mut Scope>, @@ -596,6 +602,12 @@ impl Engine { } /// Call a script-defined function. + /// + /// ## WARNING + /// + /// 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_fn_from_lib( &self, scope: Option<&mut Scope>, @@ -678,6 +690,12 @@ impl Engine { } // Perform an actual function call, taking care of special functions + /// + /// ## WARNING + /// + /// 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 `()`! fn exec_fn_call( &self, fn_lib: &FunctionsLib, @@ -1105,17 +1123,20 @@ impl Engine { rhs: &Expr, level: usize, ) -> Result> { - let mut lhs_value = self.eval_expr(scope, state, fn_lib, lhs, level)?; + let lhs_value = self.eval_expr(scope, state, fn_lib, lhs, level)?; let rhs_value = self.eval_expr(scope, state, fn_lib, rhs, level)?; match rhs_value { #[cfg(not(feature = "no_index"))] - Dynamic(Union::Array(mut rhs_value)) => { + Dynamic(Union::Array(rhs_value)) => { let def_value = false.into(); - // Call the '==' operator to compare each value - for value in rhs_value.iter_mut() { - let args = &mut [&mut lhs_value, value]; + // 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()]; let def_value = Some(&def_value); let pos = rhs.position(); diff --git a/src/packages/utils.rs b/src/packages/utils.rs index fd631b22..a02e475c 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -9,6 +9,7 @@ use crate::token::Position; use crate::stdlib::{ any::TypeId, boxed::Box, + mem, string::{String, ToString}, }; @@ -170,9 +171,9 @@ pub fn reg_unary( check_num_args(fn_name, 1, args, pos)?; let mut drain = args.iter_mut(); - let x: &mut T = drain.next().unwrap().downcast_mut().unwrap(); + let x = mem::take(*drain.next().unwrap()).cast::(); - let r = func(x.clone()); + let r = func(x); map_result(r, pos) }); @@ -286,10 +287,10 @@ pub fn reg_binary( 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: &mut B = drain.next().unwrap().downcast_mut().unwrap(); + let x = mem::take(*drain.next().unwrap()).cast::(); + let y = mem::take(*drain.next().unwrap()).cast::(); - let r = func(x.clone(), y.clone()); + let r = func(x, y); map_result(r, pos) }); @@ -351,9 +352,9 @@ pub fn reg_binary_mut( let mut drain = args.iter_mut(); let x: &mut A = drain.next().unwrap().downcast_mut().unwrap(); - let y: &mut B = drain.next().unwrap().downcast_mut().unwrap(); + let y = mem::take(*drain.next().unwrap()).cast::(); - let r = func(x, y.clone()); + let r = func(x, y); map_result(r, pos) }); @@ -390,11 +391,11 @@ pub fn reg_trinary(); + let y = mem::take(*drain.next().unwrap()).cast::(); + let z = mem::take(*drain.next().unwrap()).cast::(); - let r = func(x.clone(), y.clone(), z.clone()); + let r = func(x, y, z); map_result(r, pos) }); @@ -432,10 +433,10 @@ pub fn reg_trinary_mut(); + let z = mem::take(*drain.next().unwrap()).cast::(); - let r = func(x, y.clone(), z.clone()); + let r = func(x, y, z); map_result(r, pos) }); From fb64adca93197fa0cd4d95479558d7d9a68a9dc3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 7 May 2020 10:00:10 +0800 Subject: [PATCH 05/20] Move fn_lib into State, and use StaticVec for function call arguments in dotting/indexing chains. --- src/api.rs | 12 +-- src/engine.rs | 208 +++++++++++++++++++++++++------------------------- src/utils.rs | 10 +++ 3 files changed, 123 insertions(+), 107 deletions(-) diff --git a/src/api.rs b/src/api.rs index d0193549..b5865ed6 100644 --- a/src/api.rs +++ b/src/api.rs @@ -867,12 +867,12 @@ impl Engine { scope: &mut Scope, ast: &AST, ) -> Result> { - let mut state = State::new(); + let mut state = State::new(ast.fn_lib()); ast.statements() .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, &mut state, ast.fn_lib(), stmt, 0) + self.eval_stmt(scope, &mut state, stmt, 0) }) .or_else(|err| match *err { EvalAltResult::Return(out, _) => Ok(out), @@ -932,12 +932,12 @@ impl Engine { scope: &mut Scope, ast: &AST, ) -> Result<(), Box> { - let mut state = State::new(); + let mut state = State::new(ast.fn_lib()); ast.statements() .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, &mut state, ast.fn_lib(), stmt, 0) + self.eval_stmt(scope, &mut state, stmt, 0) }) .map_or_else( |err| match *err { @@ -1000,7 +1000,9 @@ impl Engine { .get_function(name, args.len()) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos)))?; - let result = self.call_fn_from_lib(Some(scope), fn_lib, fn_def, &mut args, pos, 0)?; + let state = State::new(fn_lib); + + let result = self.call_script_fn(Some(scope), &state, fn_def, &mut args, pos, 0)?; let return_type = self.map_type_name(result.type_name()); diff --git a/src/engine.rs b/src/engine.rs index 1375e73a..5969ca59 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -141,21 +141,35 @@ impl> From for Target<'_> { } /// A type that holds all the current states of the Engine. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct State { +#[derive(Debug, Clone, Copy)] +pub struct State<'a> { + /// Global script-defined functions. + pub fn_lib: &'a FunctionsLib, + /// Normally, access to variables are parsed with a relative offset into the scope to avoid a lookup. /// 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, } -impl State { +impl<'a> State<'a> { /// Create a new `State`. - pub fn new() -> Self { + pub fn new(fn_lib: &'a FunctionsLib) -> Self { Self { always_search: false, + fn_lib, } } + /// Does a certain script-defined function exist in the `State`? + pub fn has_function(&self, name: &str, params: usize) -> bool { + self.fn_lib.contains_key(&calc_fn_def(name, params)) + } + /// Get a script-defined function definition from the `State`. + pub fn get_function(&self, name: &str, params: usize) -> Option<&FnDef> { + self.fn_lib + .get(&calc_fn_def(name, params)) + .map(|f| f.as_ref()) + } } /// A type that holds a library (`HashMap`) of script-defined functions. @@ -511,7 +525,7 @@ impl Engine { pub(crate) fn call_fn_raw( &self, scope: Option<&mut Scope>, - fn_lib: &FunctionsLib, + state: &State, fn_name: &str, args: &mut FnCallArgs, def_val: Option<&Dynamic>, @@ -524,8 +538,8 @@ impl Engine { } // First search in script-defined functions (can override built-in) - if let Some(fn_def) = fn_lib.get_function(fn_name, args.len()) { - return self.call_fn_from_lib(scope, fn_lib, fn_def, args, pos, level); + if let Some(fn_def) = state.get_function(fn_name, args.len()) { + return self.call_script_fn(scope, state, fn_def, args, pos, level); } // Search built-in's and external functions @@ -608,10 +622,10 @@ 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_fn_from_lib( + pub(crate) fn call_script_fn( &self, scope: Option<&mut Scope>, - fn_lib: &FunctionsLib, + state: &State, fn_def: &FnDef, args: &mut FnCallArgs, pos: Position, @@ -621,7 +635,7 @@ impl Engine { // Extern scope passed in which is not empty Some(scope) if scope.len() > 0 => { let scope_len = scope.len(); - let mut state = State::new(); + let mut state = State::new(state.fn_lib); // Put arguments into scope as variables - variable name is copied scope.extend( @@ -637,7 +651,7 @@ impl Engine { // Evaluate the function at one higher level of call depth let result = self - .eval_stmt(scope, &mut state, fn_lib, &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), @@ -651,7 +665,7 @@ impl Engine { // No new scope - create internal scope _ => { let mut scope = Scope::new(); - let mut state = State::new(); + let mut state = State::new(state.fn_lib); // Put arguments into scope as variables scope.extend( @@ -667,7 +681,7 @@ impl Engine { // Evaluate the function at one higher level of call depth return self - .eval_stmt(&mut scope, &mut state, fn_lib, &fn_def.body, level + 1) + .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), @@ -678,7 +692,7 @@ impl Engine { } // Has a system function an override? - fn has_override(&self, fn_lib: &FunctionsLib, name: &str) -> bool { + fn has_override(&self, state: &State, name: &str) -> bool { let hash = calc_fn_hash(name, once(TypeId::of::())); // First check registered functions @@ -686,7 +700,7 @@ impl Engine { // Then check packages || self.packages.iter().any(|p| p.functions.contains_key(&hash)) // Then check script-defined functions - || fn_lib.has_function(name, 1) + || state.has_function(name, 1) } // Perform an actual function call, taking care of special functions @@ -698,7 +712,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, - fn_lib: &FunctionsLib, + state: &State, fn_name: &str, args: &mut FnCallArgs, def_val: Option<&Dynamic>, @@ -707,19 +721,19 @@ impl Engine { ) -> Result> { match fn_name { // type_of - KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(fn_lib, KEYWORD_TYPE_OF) => { + KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, KEYWORD_TYPE_OF) => { Ok(self.map_type_name(args[0].type_name()).to_string().into()) } // eval - reaching this point it must be a method-style call - KEYWORD_EVAL if args.len() == 1 && !self.has_override(fn_lib, KEYWORD_EVAL) => { + KEYWORD_EVAL if args.len() == 1 && !self.has_override(state, KEYWORD_EVAL) => { Err(Box::new(EvalAltResult::ErrorRuntime( "'eval' should not be called in method style. Try eval(...);".into(), pos, ))) } // Normal method call - _ => self.call_fn_raw(None, fn_lib, fn_name, args, def_val, pos, level), + _ => self.call_fn_raw(None, state, fn_name, args, def_val, pos, level), } } @@ -727,7 +741,7 @@ impl Engine { fn eval_script_expr( &self, scope: &mut Scope, - fn_lib: &FunctionsLib, + state: &State, script: &Dynamic, pos: Position, ) -> Result> { @@ -751,7 +765,7 @@ impl Engine { } let statements = mem::take(ast.statements_mut()); - let ast = AST::new(statements, fn_lib.clone()); + let ast = AST::new(statements, state.fn_lib.clone()); // Evaluate the AST self.eval_ast_with_scope_raw(scope, &ast) @@ -761,7 +775,7 @@ impl Engine { /// Chain-evaluate a dot/index chain. fn eval_dot_index_chain_helper( &self, - fn_lib: &FunctionsLib, + state: &State, mut target: Target, rhs: &Expr, idx_values: &mut StaticVec, @@ -788,20 +802,20 @@ impl Engine { Expr::Index(idx, idx_rhs, pos) => { let is_index = matches!(rhs, Expr::Index(_,_,_)); - let indexed_val = self.get_indexed_mut(fn_lib, obj, idx_val, idx.position(), op_pos, false)?; + let indexed_val = self.get_indexed_mut(state, obj, idx_val, idx.position(), op_pos, false)?; self.eval_dot_index_chain_helper( - fn_lib, indexed_val, idx_rhs.as_ref(), idx_values, is_index, *pos, level, new_val + state, indexed_val, idx_rhs.as_ref(), idx_values, is_index, *pos, level, new_val ) } // xxx[rhs] = new_val _ if new_val.is_some() => { - let mut indexed_val = self.get_indexed_mut(fn_lib, obj, idx_val, rhs.position(), op_pos, true)?; + let mut indexed_val = self.get_indexed_mut(state, obj, 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(fn_lib, obj, idx_val, rhs.position(), op_pos, false) + .get_indexed_mut(state, obj, idx_val, rhs.position(), op_pos, false) .map(|v| (v.clone_into_dynamic(), false)) } } else { @@ -809,12 +823,12 @@ impl Engine { // xxx.fn_name(arg_expr_list) Expr::FnCall(fn_name, None,_, def_val, pos) => { let mut args: Vec<_> = once(obj) - .chain(idx_val.downcast_mut::>().unwrap().iter_mut()) + .chain(idx_val.downcast_mut::>().unwrap().iter_mut()) .collect(); let def_val = def_val.as_deref(); // 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(fn_lib, fn_name, &mut args, def_val, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, fn_name, &mut args, def_val, *pos, 0).map(|v| (v, true)) } // xxx.module::fn_name(...) - syntax error Expr::FnCall(_,_,_,_,_) => unreachable!(), @@ -822,7 +836,7 @@ impl Engine { #[cfg(not(feature = "no_object"))] Expr::Property(id, pos) if obj.is::() && new_val.is_some() => { let mut indexed_val = - self.get_indexed_mut(fn_lib, obj, id.to_string().into(), *pos, op_pos, true)?; + self.get_indexed_mut(state, obj, id.to_string().into(), *pos, op_pos, true)?; indexed_val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } @@ -830,20 +844,20 @@ impl Engine { #[cfg(not(feature = "no_object"))] Expr::Property(id, pos) if obj.is::() => { let indexed_val = - self.get_indexed_mut(fn_lib, obj, id.to_string().into(), *pos, op_pos, false)?; + self.get_indexed_mut(state, obj, id.to_string().into(), *pos, op_pos, false)?; Ok((indexed_val.clone_into_dynamic(), false)) } // xxx.id = ??? a Expr::Property(id, pos) if new_val.is_some() => { let fn_name = make_setter(id); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 0).map(|v| (v, true)) } // xxx.id Expr::Property(id, pos) => { let fn_name = make_getter(id); let mut args = [obj]; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).map(|v| (v, false)) + self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 0).map(|v| (v, false)) } #[cfg(not(feature = "no_object"))] // {xxx:map}.idx_lhs[idx_expr] @@ -853,7 +867,7 @@ impl Engine { let is_index = matches!(rhs, Expr::Index(_,_,_)); let indexed_val = if let Expr::Property(id, pos) = dot_lhs.as_ref() { - self.get_indexed_mut(fn_lib, obj, id.to_string().into(), *pos, op_pos, false)? + self.get_indexed_mut(state, obj, id.to_string().into(), *pos, op_pos, false)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -862,7 +876,7 @@ impl Engine { ))); }; self.eval_dot_index_chain_helper( - fn_lib, indexed_val, dot_rhs, idx_values, is_index, *pos, level, new_val + state, indexed_val, dot_rhs, idx_values, is_index, *pos, level, new_val ) } // xxx.idx_lhs[idx_expr] @@ -874,7 +888,7 @@ impl Engine { let indexed_val = &mut (if let Expr::Property(id, pos) = dot_lhs.as_ref() { let fn_name = make_getter(id); - self.exec_fn_call(fn_lib, &fn_name, &mut args[..1], None, *pos, 0)? + self.exec_fn_call(state, &fn_name, &mut args[..1], None, *pos, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -883,7 +897,7 @@ impl Engine { ))); }); let (result, may_be_changed) = self.eval_dot_index_chain_helper( - fn_lib, indexed_val.into(), dot_rhs, idx_values, is_index, *pos, level, new_val + state, indexed_val.into(), dot_rhs, idx_values, is_index, *pos, level, new_val )?; // Feed the value back via a setter just in case it has been updated @@ -892,7 +906,7 @@ impl Engine { let fn_name = make_setter(id); // Re-use args because the first &mut parameter will not be consumed args[1] = indexed_val; - self.exec_fn_call(fn_lib, &fn_name, &mut args, None, *pos, 0).or_else(|err| match *err { + self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 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()), err => Err(Box::new(err)) @@ -916,7 +930,6 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, - fn_lib: &FunctionsLib, dot_lhs: &Expr, dot_rhs: &Expr, is_index: bool, @@ -926,7 +939,7 @@ impl Engine { ) -> Result> { let idx_values = &mut StaticVec::new(); - self.eval_indexed_chain(scope, state, fn_lib, dot_rhs, idx_values, 0, level)?; + self.eval_indexed_chain(scope, state, dot_rhs, idx_values, 0, level)?; match dot_lhs { // id.??? or id[???] @@ -948,7 +961,7 @@ impl Engine { let this_ptr = target.into(); self.eval_dot_index_chain_helper( - fn_lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -960,10 +973,10 @@ impl Engine { } // {expr}.??? or {expr}[???] expr => { - let val = self.eval_expr(scope, state, fn_lib, expr, level)?; + let val = self.eval_expr(scope, state, expr, level)?; let this_ptr = val.into(); self.eval_dot_index_chain_helper( - fn_lib, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, + state, this_ptr, dot_rhs, idx_values, is_index, op_pos, level, new_val, ) .map(|(v, _)| v) } @@ -979,7 +992,6 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, - fn_lib: &FunctionsLib, expr: &Expr, idx_values: &mut StaticVec, size: usize, @@ -987,14 +999,12 @@ impl Engine { ) -> Result<(), Box> { match expr { Expr::FnCall(_, None, arg_exprs, _, _) => { - let arg_values = arg_exprs - .iter() - .map(|arg_expr| self.eval_expr(scope, state, fn_lib, arg_expr, level)) - .collect::, _>>()?; + let mut arg_values = StaticVec::::new(); + + for arg_expr in arg_exprs.iter() { + arg_values.push(self.eval_expr(scope, state, arg_expr, level)?); + } - #[cfg(not(feature = "no_index"))] - idx_values.push(arg_values); - #[cfg(feature = "no_index")] idx_values.push(Dynamic::from(arg_values)); } Expr::FnCall(_, _, _, _, _) => unreachable!(), @@ -1003,15 +1013,15 @@ impl Engine { // Evaluate in left-to-right order let lhs_val = match lhs.as_ref() { Expr::Property(_, _) => Default::default(), // Store a placeholder in case of a property - _ => self.eval_expr(scope, state, fn_lib, lhs, level)?, + _ => self.eval_expr(scope, state, lhs, level)?, }; // Push in reverse order - self.eval_indexed_chain(scope, state, fn_lib, rhs, idx_values, size, level)?; + self.eval_indexed_chain(scope, state, rhs, idx_values, size, level)?; idx_values.push(lhs_val); } - _ => idx_values.push(self.eval_expr(scope, state, fn_lib, expr, level)?), + _ => idx_values.push(self.eval_expr(scope, state, expr, level)?), } Ok(()) @@ -1020,7 +1030,7 @@ impl Engine { /// Get the value at the indexed position of a base type fn get_indexed_mut<'a>( &self, - fn_lib: &FunctionsLib, + state: &State, val: &'a mut Dynamic, mut idx: Dynamic, idx_pos: Position, @@ -1100,7 +1110,7 @@ impl Engine { _ => { let args = &mut [val, &mut idx]; - self.exec_fn_call(fn_lib, FUNC_INDEXER, args, None, op_pos, 0) + self.exec_fn_call(state, FUNC_INDEXER, args, None, op_pos, 0) .map(|v| v.into()) .map_err(|_| { Box::new(EvalAltResult::ErrorIndexingType( @@ -1118,13 +1128,12 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, - fn_lib: &FunctionsLib, lhs: &Expr, rhs: &Expr, level: usize, ) -> Result> { - let lhs_value = self.eval_expr(scope, state, fn_lib, lhs, level)?; - let rhs_value = self.eval_expr(scope, state, fn_lib, rhs, level)?; + let 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"))] @@ -1141,7 +1150,7 @@ impl Engine { let pos = rhs.position(); if self - .call_fn_raw(None, fn_lib, "==", args, def_value, pos, level)? + .call_fn_raw(None, state, "==", args, def_value, pos, level)? .as_bool() .unwrap_or(false) { @@ -1173,7 +1182,6 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, - fn_lib: &FunctionsLib, expr: &Expr, level: usize, ) -> Result> { @@ -1191,11 +1199,11 @@ impl Engine { Expr::Property(_, _) => unreachable!(), // Statement block - Expr::Stmt(stmt, _) => self.eval_stmt(scope, state, fn_lib, stmt, level), + Expr::Stmt(stmt, _) => self.eval_stmt(scope, state, stmt, level), // lhs = rhs Expr::Assignment(lhs, rhs, op_pos) => { - let rhs_val = self.eval_expr(scope, state, fn_lib, rhs, level)?; + let rhs_val = self.eval_expr(scope, state, rhs, level)?; match lhs.as_ref() { // name = rhs @@ -1218,7 +1226,7 @@ impl Engine { Expr::Index(idx_lhs, idx_expr, op_pos) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, fn_lib, idx_lhs, idx_expr, true, *op_pos, level, new_val, + scope, state, idx_lhs, idx_expr, true, *op_pos, level, new_val, ) } // dot_lhs.dot_rhs = rhs @@ -1226,7 +1234,7 @@ impl Engine { Expr::Dot(dot_lhs, dot_rhs, _) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, fn_lib, dot_lhs, dot_rhs, false, *op_pos, level, new_val, + scope, state, dot_lhs, dot_rhs, false, *op_pos, level, new_val, ) } // Error assignment to constant @@ -1245,21 +1253,21 @@ impl Engine { // lhs[idx_expr] #[cfg(not(feature = "no_index"))] - Expr::Index(lhs, idx_expr, op_pos) => self.eval_dot_index_chain( - scope, state, fn_lib, lhs, idx_expr, true, *op_pos, level, None, - ), + Expr::Index(lhs, idx_expr, op_pos) => { + self.eval_dot_index_chain(scope, state, lhs, idx_expr, true, *op_pos, level, None) + } // lhs.dot_rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(lhs, dot_rhs, op_pos) => self.eval_dot_index_chain( - scope, state, fn_lib, lhs, dot_rhs, false, *op_pos, level, None, - ), + Expr::Dot(lhs, dot_rhs, op_pos) => { + self.eval_dot_index_chain(scope, state, lhs, dot_rhs, false, *op_pos, level, None) + } #[cfg(not(feature = "no_index"))] Expr::Array(contents, _) => Ok(Dynamic(Union::Array(Box::new( contents .iter() - .map(|item| self.eval_expr(scope, state, fn_lib, item, level)) + .map(|item| self.eval_expr(scope, state, item, level)) .collect::, _>>()?, )))), @@ -1268,7 +1276,7 @@ impl Engine { contents .iter() .map(|(key, expr, _)| { - self.eval_expr(scope, state, fn_lib, expr, level) + self.eval_expr(scope, state, expr, level) .map(|val| (key.clone(), val)) }) .collect::, _>>()?, @@ -1278,21 +1286,21 @@ impl Engine { Expr::FnCall(fn_name, None, arg_exprs, def_val, pos) => { let mut arg_values = arg_exprs .iter() - .map(|expr| self.eval_expr(scope, state, fn_lib, expr, level)) + .map(|expr| self.eval_expr(scope, state, expr, level)) .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); if fn_name.as_ref() == KEYWORD_EVAL && args.len() == 1 - && !self.has_override(fn_lib, KEYWORD_EVAL) + && !self.has_override(state, KEYWORD_EVAL) { // 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, fn_lib, args[0], arg_exprs[0].position()); + self.eval_script_expr(scope, state, args[0], arg_exprs[0].position()); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1304,7 +1312,7 @@ impl Engine { } else { // Normal function call - except for eval (handled above) let def_value = def_val.as_deref(); - self.exec_fn_call(fn_lib, fn_name, &mut args, def_value, *pos, level) + self.exec_fn_call(state, fn_name, &mut args, def_value, *pos, level) } } @@ -1315,7 +1323,7 @@ impl Engine { let mut arg_values = arg_exprs .iter() - .map(|expr| self.eval_expr(scope, state, fn_lib, expr, level)) + .map(|expr| self.eval_expr(scope, state, expr, level)) .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); @@ -1328,7 +1336,7 @@ impl Engine { // First search in script-defined functions (can override built-in) if let Some(fn_def) = module.get_qualified_fn_lib(fn_name, args.len(), modules)? { - self.call_fn_from_lib(None, fn_lib, fn_def, &mut args, *pos, level) + self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions let hash = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); @@ -1342,18 +1350,18 @@ impl Engine { } Expr::In(lhs, rhs, _) => { - self.eval_in_expr(scope, state, fn_lib, lhs.as_ref(), rhs.as_ref(), level) + self.eval_in_expr(scope, state, lhs.as_ref(), rhs.as_ref(), level) } Expr::And(lhs, rhs, _) => Ok((self - .eval_expr(scope, state, fn_lib, lhs.as_ref(), level)? + .eval_expr(scope, state, lhs.as_ref(), level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position()) })? && // Short-circuit using && self - .eval_expr(scope, state, fn_lib, rhs.as_ref(), level)? + .eval_expr(scope, state, rhs.as_ref(), level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position()) @@ -1361,14 +1369,14 @@ impl Engine { .into()), Expr::Or(lhs, rhs, _) => Ok((self - .eval_expr(scope, state, fn_lib, lhs.as_ref(), level)? + .eval_expr(scope, state, lhs.as_ref(), level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position()) })? || // Short-circuit using || self - .eval_expr(scope, state, fn_lib, rhs.as_ref(), level)? + .eval_expr(scope, state, rhs.as_ref(), level)? .as_bool() .map_err(|_| { EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position()) @@ -1388,7 +1396,6 @@ impl Engine { &self, scope: &mut Scope, state: &mut State, - fn_lib: &FunctionsLib, stmt: &Stmt, level: usize, ) -> Result> { @@ -1398,7 +1405,7 @@ impl Engine { // Expression as statement Stmt::Expr(expr) => { - let result = self.eval_expr(scope, state, fn_lib, expr, level)?; + let result = self.eval_expr(scope, state, expr, level)?; Ok(if let Expr::Assignment(_, _, _) = *expr.as_ref() { // If it is an assignment, erase the result at the root @@ -1413,7 +1420,7 @@ impl Engine { let prev_len = scope.len(); let result = block.iter().try_fold(Default::default(), |_, stmt| { - self.eval_stmt(scope, state, fn_lib, stmt, level) + self.eval_stmt(scope, state, stmt, level) }); scope.rewind(prev_len); @@ -1427,14 +1434,14 @@ impl Engine { // If-else statement Stmt::IfThenElse(guard, if_body, else_body) => self - .eval_expr(scope, state, fn_lib, guard, level)? + .eval_expr(scope, state, guard, level)? .as_bool() .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(guard.position()))) .and_then(|guard_val| { if guard_val { - self.eval_stmt(scope, state, fn_lib, if_body, level) + self.eval_stmt(scope, state, if_body, level) } else if let Some(stmt) = else_body { - self.eval_stmt(scope, state, fn_lib, stmt.as_ref(), level) + self.eval_stmt(scope, state, stmt.as_ref(), level) } else { Ok(Default::default()) } @@ -1442,11 +1449,8 @@ impl Engine { // While loop Stmt::While(guard, body) => loop { - match self - .eval_expr(scope, state, fn_lib, guard, level)? - .as_bool() - { - Ok(true) => match self.eval_stmt(scope, state, fn_lib, body, level) { + match self.eval_expr(scope, state, guard, level)?.as_bool() { + Ok(true) => match self.eval_stmt(scope, state, body, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1463,7 +1467,7 @@ impl Engine { // Loop statement Stmt::Loop(body) => loop { - match self.eval_stmt(scope, state, fn_lib, body, level) { + match self.eval_stmt(scope, state, body, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1475,7 +1479,7 @@ impl Engine { // For loop Stmt::For(name, expr, body) => { - let arr = self.eval_expr(scope, state, fn_lib, expr, level)?; + let arr = self.eval_expr(scope, state, expr, level)?; let tid = arr.type_id(); if let Some(iter_fn) = self.type_iterators.get(&tid).or_else(|| { @@ -1492,7 +1496,7 @@ impl Engine { for a in iter_fn(arr) { *scope.get_mut(index).0 = a; - match self.eval_stmt(scope, state, fn_lib, body, level) { + match self.eval_stmt(scope, state, body, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1522,7 +1526,7 @@ impl Engine { // Return value Stmt::ReturnWithVal(Some(a), ReturnType::Return, pos) => Err(Box::new( - EvalAltResult::Return(self.eval_expr(scope, state, fn_lib, a, level)?, *pos), + EvalAltResult::Return(self.eval_expr(scope, state, a, level)?, *pos), )), // Empty throw @@ -1532,7 +1536,7 @@ impl Engine { // Throw value Stmt::ReturnWithVal(Some(a), ReturnType::Exception, pos) => { - let val = self.eval_expr(scope, state, fn_lib, a, level)?; + let val = self.eval_expr(scope, state, a, level)?; Err(Box::new(EvalAltResult::ErrorRuntime( val.take_string().unwrap_or_else(|_| "".to_string()), *pos, @@ -1541,7 +1545,7 @@ impl Engine { // Let statement Stmt::Let(name, Some(expr), _) => { - let val = self.eval_expr(scope, state, fn_lib, expr, level)?; + let val = self.eval_expr(scope, state, expr, level)?; // TODO - avoid copying variable name in inner block? let var_name = name.as_ref().clone(); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); @@ -1557,7 +1561,7 @@ impl Engine { // Const statement Stmt::Const(name, expr, _) if expr.is_constant() => { - let val = self.eval_expr(scope, state, fn_lib, expr, level)?; + let val = self.eval_expr(scope, state, expr, level)?; // TODO - avoid copying variable name in inner block? let var_name = name.as_ref().clone(); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); @@ -1575,7 +1579,7 @@ impl Engine { #[cfg(not(feature = "no_module"))] { if let Some(path) = self - .eval_expr(scope, state, fn_lib, expr, level)? + .eval_expr(scope, state, expr, level)? .try_cast::() { if let Some(resolver) = self.module_resolver.as_ref() { diff --git a/src/utils.rs b/src/utils.rs index 4a9479f1..6e11e39b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -117,6 +117,16 @@ impl StaticVec { self.list[..num].iter().chain(self.more.iter()) } + /// Get a mutable iterator to entries in the `StaticVec`. + pub fn iter_mut(&mut self) -> impl Iterator { + let num = if self.len >= self.list.len() { + self.list.len() + } else { + self.len + }; + + self.list[..num].iter_mut().chain(self.more.iter_mut()) + } } impl fmt::Debug for StaticVec { From f3c0609377de54df7f3bce45da038dace96d01d6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 7 May 2020 12:25:09 +0800 Subject: [PATCH 06/20] Catch more assignment errors at parse time. --- src/error.rs | 14 ++++++------ src/parser.rs | 56 +++++++++++++++++++++++++++++++++++++--------- tests/constants.rs | 6 ++--- 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index b08e5975..eed98271 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use crate::token::Position; use crate::stdlib::{boxed::Box, char, error::Error, fmt, string::String}; /// Error when tokenizing the script text. -#[derive(Debug, Eq, PartialEq, Hash, Clone)] +#[derive(Debug, Eq, PartialEq, Clone, Hash)] pub enum LexError { /// An unexpected character is encountered when tokenizing the script text. UnexpectedChar(char), @@ -44,7 +44,7 @@ impl fmt::Display for LexError { /// Some errors never appear when certain features are turned on. /// They still exist so that the application can turn features on and off without going through /// massive code changes to remove/add back enum variants in match statements. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Eq, PartialEq, Clone, Hash)] pub enum ParseErrorType { /// Error in the script text. Wrapped value is the error message. BadInput(String), @@ -98,8 +98,8 @@ pub enum ParseErrorType { /// /// Never appears under the `no_function` feature. FnMissingBody(String), - /// Assignment to an inappropriate LHS (left-hand-side) expression. - AssignmentToInvalidLHS, + /// Assignment to a copy of a value. + AssignmentToCopy, /// Assignment to an a constant variable. AssignmentToConstant(String), /// Break statement not inside a loop. @@ -114,7 +114,7 @@ impl ParseErrorType { } /// Error when parsing a script. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Hash)] pub struct ParseError(pub(crate) ParseErrorType, pub(crate) Position); impl ParseError { @@ -147,8 +147,8 @@ impl ParseError { ParseErrorType::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration", ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration", ParseErrorType::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function", - ParseErrorType::AssignmentToInvalidLHS => "Cannot assign to this expression", - ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant variable.", + ParseErrorType::AssignmentToCopy => "Only a copy of the value is change with this assignment", + ParseErrorType::AssignmentToConstant(_) => "Cannot assign to a constant value.", ParseErrorType::LoopBreak => "Break statement should only be used inside a loop" } } diff --git a/src/parser.rs b/src/parser.rs index 35b2d2e8..953c12a0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -152,11 +152,11 @@ impl AST { pub fn clear_functions(&mut self) { #[cfg(feature = "sync")] { - self.1 = Arc::new(FunctionsLib::new()); + self.1 = Arc::new(Default::default()); } #[cfg(not(feature = "sync"))] { - self.1 = Rc::new(FunctionsLib::new()); + self.1 = Rc::new(Default::default()); } } @@ -1212,15 +1212,47 @@ fn parse_unary<'a>( } } -fn parse_assignment_stmt<'a>( - input: &mut Peekable>, +fn make_assignment_stmt<'a>( stack: &mut Stack, lhs: Expr, - allow_stmt_expr: bool, + rhs: Expr, + pos: Position, ) -> Result> { - let pos = eat_token(input, Token::Equals); - let rhs = parse_expr(input, stack, allow_stmt_expr)?; - Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) + match &lhs { + Expr::Variable(_, _, None, _) => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + Expr::Variable(name, _, Some(index), var_pos) => { + match stack[(stack.len() - index.get())].1 { + ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + // Constant values cannot be assigned to + ScopeEntryType::Constant => { + Err(PERR::AssignmentToConstant(name.to_string()).into_err(*var_pos)) + } + ScopeEntryType::Module => unreachable!(), + } + } + Expr::Index(idx_lhs, _, _) | Expr::Dot(idx_lhs, _, _) => match idx_lhs.as_ref() { + Expr::Variable(_, _, None, _) => { + Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) + } + Expr::Variable(name, _, Some(index), var_pos) => { + match stack[(stack.len() - index.get())].1 { + ScopeEntryType::Normal => { + Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) + } + // Constant values cannot be assigned to + ScopeEntryType::Constant => { + Err(PERR::AssignmentToConstant(name.to_string()).into_err(*var_pos)) + } + ScopeEntryType::Module => unreachable!(), + } + } + _ => Err(PERR::AssignmentToCopy.into_err(idx_lhs.position())), + }, + expr if expr.is_constant() => { + Err(PERR::AssignmentToConstant("".into()).into_err(lhs.position())) + } + _ => Err(PERR::AssignmentToCopy.into_err(lhs.position())), + } } /// Parse an operator-assignment expression. @@ -1231,7 +1263,11 @@ fn parse_op_assignment_stmt<'a>( allow_stmt_expr: bool, ) -> Result> { let (op, pos) = match *input.peek().unwrap() { - (Token::Equals, _) => return parse_assignment_stmt(input, stack, lhs, allow_stmt_expr), + (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); + } (Token::PlusAssign, pos) => ("+", pos), (Token::MinusAssign, pos) => ("-", pos), (Token::MultiplyAssign, pos) => ("*", pos), @@ -1254,7 +1290,7 @@ fn parse_op_assignment_stmt<'a>( // lhs op= rhs -> lhs = op(lhs, rhs) let args = vec![lhs_copy, rhs]; let rhs_expr = Expr::FnCall(Box::new(op.into()), None, Box::new(args), None, pos); - Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs_expr), pos)) + make_assignment_stmt(stack, lhs, rhs_expr, pos) } /// Make a dot expression. diff --git a/tests/constants.rs b/tests/constants.rs index 55752d28..d5e0820a 100644 --- a/tests/constants.rs +++ b/tests/constants.rs @@ -1,4 +1,4 @@ -use rhai::{Engine, EvalAltResult, INT}; +use rhai::{Engine, EvalAltResult, ParseErrorType, INT}; #[test] fn test_constant() -> Result<(), Box> { @@ -8,13 +8,13 @@ fn test_constant() -> Result<(), Box> { assert!(matches!( *engine.eval::("const x = 123; x = 42;").expect_err("expects error"), - EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x" + EvalAltResult::ErrorParsing(err) if err.error_type() == &ParseErrorType::AssignmentToConstant("x".to_string()) )); #[cfg(not(feature = "no_index"))] assert!(matches!( *engine.eval::("const x = [1, 2, 3, 4, 5]; x[2] = 42;").expect_err("expects error"), - EvalAltResult::ErrorAssignmentToConstant(var, _) if var == "x" + EvalAltResult::ErrorParsing(err) if err.error_type() == &ParseErrorType::AssignmentToConstant("x".to_string()) )); Ok(()) From c607c7c4285023e3cfe7783f7afa8a47e19225e8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 7 May 2020 15:25:50 +0800 Subject: [PATCH 07/20] Move Engine functions and iterators lib to Packages API. --- src/api.rs | 4 ++- src/engine.rs | 62 ++++++++++++++------------------------ src/fn_register.rs | 6 ++-- src/optimize.rs | 19 +++++------- src/packages/mod.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 106 insertions(+), 57 deletions(-) diff --git a/src/api.rs b/src/api.rs index b5865ed6..68be4e59 100644 --- a/src/api.rs +++ b/src/api.rs @@ -168,7 +168,9 @@ 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.type_iterators.insert(TypeId::of::(), Box::new(f)); + self.base_package + .type_iterators + .insert(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 5969ca59..f10d77a2 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -4,7 +4,9 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; use crate::optimize::OptimizationLevel; -use crate::packages::{CorePackage, Package, PackageLibrary, StandardPackage}; +use crate::packages::{ + CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, +}; use crate::parser::{Expr, FnDef, ModuleRef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; @@ -60,13 +62,6 @@ pub const MAX_CALL_STACK_DEPTH: usize = 28; #[cfg(not(debug_assertions))] pub const MAX_CALL_STACK_DEPTH: usize = 256; -#[cfg(not(feature = "only_i32"))] -#[cfg(not(feature = "only_i64"))] -const FUNCTIONS_COUNT: usize = 512; - -#[cfg(any(feature = "only_i32", feature = "only_i64"))] -const FUNCTIONS_COUNT: usize = 256; - pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; pub const KEYWORD_TYPE_OF: &str = "type_of"; @@ -185,10 +180,6 @@ pub struct FunctionsLib( ); impl FunctionsLib { - /// Create a new `FunctionsLib`. - pub fn new() -> Self { - Default::default() - } /// Create a new `FunctionsLib` from a collection of `FnDef`. pub fn from_vec(vec: Vec) -> Self { FunctionsLib( @@ -270,14 +261,9 @@ 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. - pub(crate) packages: Vec, - /// A `HashMap` containing all compiled functions known to the engine. - /// - /// The key of the `HashMap` is a `u64` hash calculated by the function `crate::calc_fn_hash`. - pub(crate) functions: HashMap>, - - /// A hashmap containing all iterators known to the engine. - pub(crate) type_iterators: HashMap>, + 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"))] @@ -314,8 +300,7 @@ impl Default for Engine { // Create the new scripting Engine let mut engine = Self { packages: Default::default(), - functions: HashMap::with_capacity(FUNCTIONS_COUNT), - type_iterators: Default::default(), + base_package: Default::default(), #[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_std"))] @@ -459,8 +444,7 @@ impl Engine { pub fn new_raw() -> Self { Self { packages: Default::default(), - functions: HashMap::with_capacity(FUNCTIONS_COUNT / 2), - type_iterators: Default::default(), + base_package: Default::default(), #[cfg(not(feature = "no_module"))] module_resolver: None, @@ -490,7 +474,7 @@ impl Engine { /// In other words, loaded packages are searched in reverse order. pub fn load_package(&mut self, package: PackageLibrary) { // Push the package to the top - packages are searched in reverse order - self.packages.insert(0, package); + self.packages.push(package); } /// Control whether and how the `Engine` will optimize an AST after compilation. @@ -545,12 +529,11 @@ impl Engine { // Search built-in's and external functions let fn_spec = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); - if let Some(func) = self.functions.get(&fn_spec).or_else(|| { - self.packages - .iter() - .find(|pkg| pkg.functions.contains_key(&fn_spec)) - .and_then(|pkg| pkg.functions.get(&fn_spec)) - }) { + if let Some(func) = self + .base_package + .get_function(fn_spec) + .or_else(|| self.packages.get_function(fn_spec)) + { // Run external function let result = func(args, pos)?; @@ -696,9 +679,9 @@ impl Engine { let hash = calc_fn_hash(name, once(TypeId::of::())); // First check registered functions - self.functions.contains_key(&hash) + self.base_package.contains_function(hash) // Then check packages - || self.packages.iter().any(|p| p.functions.contains_key(&hash)) + || self.packages.contains_function(hash) // Then check script-defined functions || state.has_function(name, 1) } @@ -1211,7 +1194,7 @@ impl Engine { let index = if state.always_search { None } else { *index }; match search_scope(scope, name, modules, index, *pos)? { (_, ScopeEntryType::Constant) => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(name.to_string(), *op_pos), + EvalAltResult::ErrorAssignmentToConstant(name.to_string(), *pos), )), (value_ptr, ScopeEntryType::Normal) => { *value_ptr = rhs_val; @@ -1482,12 +1465,11 @@ impl Engine { let arr = self.eval_expr(scope, state, expr, level)?; let tid = arr.type_id(); - if let Some(iter_fn) = self.type_iterators.get(&tid).or_else(|| { - self.packages - .iter() - .find(|pkg| pkg.type_iterators.contains_key(&tid)) - .and_then(|pkg| pkg.type_iterators.get(&tid)) - }) { + if let Some(iter_fn) = self + .base_package + .get_iterator(tid) + .or_else(|| self.packages.get_iterator(tid)) + { // Add the loop variable let var_name = name.as_ref().clone(); scope.push(var_name, ()); diff --git a/src/fn_register.rs b/src/fn_register.rs index 9f798af1..2e9a5325 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -221,7 +221,7 @@ macro_rules! def_register { let fn_name = name.to_string(); let func = make_func!(fn_name : f : map_dynamic ; $($par => $clone),*); let hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.functions.insert(hash, Box::new(func)); + self.base_package.functions.insert(hash, Box::new(func)); } } @@ -239,7 +239,7 @@ macro_rules! def_register { let fn_name = name.to_string(); let func = make_func!(fn_name : f : map_identity ; $($par => $clone),*); let hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.functions.insert(hash, Box::new(func)); + self.base_package.functions.insert(hash, Box::new(func)); } } @@ -258,7 +258,7 @@ macro_rules! def_register { let fn_name = name.to_string(); let func = make_func!(fn_name : f : map_result ; $($par => $clone),*); let hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); - self.functions.insert(hash, Box::new(func)); + self.base_package.functions.insert(hash, Box::new(func)); } } diff --git a/src/optimize.rs b/src/optimize.rs index 33765a56..018a210e 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -4,7 +4,7 @@ use crate::engine::{ Engine, FnAny, FnCallArgs, FunctionsLib, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_PRINT, KEYWORD_TYPE_OF, }; -use crate::packages::PackageLibrary; +use crate::packages::{PackageStore, 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}; @@ -110,8 +110,8 @@ impl<'a> State<'a> { /// Call a registered function fn call_fn( - packages: &Vec, - functions: &HashMap>, + packages: &PackagesCollection, + base_package: &PackageStore, fn_name: &str, args: &mut FnCallArgs, pos: Position, @@ -119,14 +119,9 @@ fn call_fn( // Search built-in's and external functions let hash = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); - functions - .get(&hash) - .or_else(|| { - packages - .iter() - .find(|p| p.functions.contains_key(&hash)) - .and_then(|p| p.functions.get(&hash)) - }) + base_package + .get_function(hash) + .or_else(|| packages.get_function(hash)) .map(|func| func(args, pos)) .transpose() } @@ -577,7 +572,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { "" }; - call_fn(&state.engine.packages, &state.engine.functions, &id, &mut call_args, pos).ok() + call_fn(&state.engine.packages, &state.engine.base_package, &id, &mut call_args, pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { diff --git a/src/packages/mod.rs b/src/packages/mod.rs index 3734bd25..c9be4487 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -2,7 +2,7 @@ use crate::engine::{FnAny, IteratorFn}; -use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc}; +use crate::stdlib::{any::TypeId, boxed::Box, collections::HashMap, rc::Rc, sync::Arc, vec::Vec}; mod arithmetic; mod array_basic; @@ -61,6 +61,30 @@ impl PackageStore { pub fn new() -> Self { Default::default() } + /// Get an iterator over the keys of the functions in the `PackageStore`. + pub fn function_keys(&self) -> impl Iterator { + self.functions.keys() + } + /// Get an iterator over the `TypeId` of the type iterators in the `PackageStore`. + pub fn type_iterator_keys(&self) -> impl Iterator { + self.type_iterators.keys() + } + /// 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) + } } /// Type which `Rc`-wraps a `PackageStore` to facilitate sharing library instances. @@ -70,3 +94,49 @@ pub type PackageLibrary = Rc; /// Type which `Arc`-wraps a `PackageStore` to facilitate sharing library instances. #[cfg(feature = "sync")] pub type PackageLibrary = Arc; + +#[derive(Default)] +/// Type containing a collection of `PackageLibrary` instances. +/// All function and type iterator keys in the loaded packages are indexed for fast access. +pub(crate) struct PackagesCollection { + /// Collection of `PackageLibrary` instances. + packages: Vec, + /// Index of all function keys, pointing to the offset in `packages`. + function_keys: HashMap, + /// Index of all type iterator `TypeId`'s, pointing to the offset in `packages`. + iterator_types: HashMap, +} + +impl PackagesCollection { + /// Add a `PackageLibrary` into the `PackagesCollection`. + pub fn push(&mut self, package: PackageLibrary) { + let index = self.packages.len(); + package.function_keys().for_each(|&hash| { + self.function_keys.insert(hash, index); + }); + package.type_iterator_keys().for_each(|&id| { + self.iterator_types.insert(id, index); + }); + self.packages.push(package); + } + /// Does the specified function hash key exist in the `PackagesCollection`? + pub fn contains_function(&self, hash: u64) -> bool { + self.function_keys.contains_key(&hash) + } + /// Get specified function via its hash key. + pub fn get_function(&self, hash: u64) -> Option<&Box> { + self.function_keys + .get(&hash) + .and_then(|&index| self.packages[index].functions.get(&hash)) + } + /// Does the specified TypeId iterator exist in the `PackagesCollection`? + pub fn contains_iterator(&self, id: TypeId) -> bool { + self.iterator_types.contains_key(&id) + } + /// Get the specified TypeId iterator. + pub fn get_iterator(&self, id: TypeId) -> Option<&Box> { + self.iterator_types + .get(&id) + .and_then(|&index| self.packages[index].type_iterators.get(&id)) + } +} From 7f6ce29447de808c22caeacf84d8e33ccd36a668 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 7 May 2020 19:16:50 +0800 Subject: [PATCH 08/20] Add try_cast to cast any type to another. --- src/any.rs | 56 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/src/any.rs b/src/any.rs index 16bb8ae7..156e8cba 100644 --- a/src/any.rs +++ b/src/any.rs @@ -18,7 +18,7 @@ use crate::stdlib::{ any::{type_name, Any, TypeId}, boxed::Box, collections::HashMap, - fmt, + fmt, mem, ptr, string::String, vec::Vec, }; @@ -38,6 +38,9 @@ pub trait Variant: Any { /// Convert this `Variant` trait object to `&mut dyn Any`. 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) -> Box; + /// Get the name of this type. fn type_name(&self) -> &'static str; @@ -60,6 +63,9 @@ impl Variant for T { fn as_mut_any(&mut self) -> &mut dyn Any { self as &mut dyn Any } + fn as_box_any(self: Box) -> Box { + self as Box + } fn type_name(&self) -> &'static str { type_name::() } @@ -86,6 +92,9 @@ pub trait Variant: Any + Send + Sync { /// Convert this `Variant` trait object to `&mut dyn Any`. 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; + /// Get the name of this type. fn type_name(&self) -> &'static str; @@ -108,6 +117,9 @@ impl Variant for T { fn as_mut_any(&mut self) -> &mut dyn Any { self as &mut dyn Any } + fn as_box_any(self: Box) -> Box { + self as Box + } fn type_name(&self) -> &'static str { type_name::() } @@ -284,6 +296,22 @@ impl Default for Dynamic { } } +/// Cast a type into another type. +fn 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 cast_box(item: Box) -> Result, Box> { // Only allow casting to the exact same type @@ -388,26 +416,26 @@ impl Dynamic { /// /// assert_eq!(x.try_cast::().unwrap(), 42); /// ``` - pub fn try_cast(self) -> Option { + pub fn try_cast(self) -> Option { if TypeId::of::() == TypeId::of::() { return cast_box::<_, T>(Box::new(self)).ok().map(|v| *v); } match self.0 { - Union::Unit(ref value) => (value as &dyn Any).downcast_ref::().cloned(), - Union::Bool(ref value) => (value as &dyn Any).downcast_ref::().cloned(), + 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(ref value) => (value as &dyn Any).downcast_ref::().cloned(), - Union::Int(ref value) => (value as &dyn Any).downcast_ref::().cloned(), + Union::Char(value) => try_cast(value), + Union::Int(value) => try_cast(value), #[cfg(not(feature = "no_float"))] - Union::Float(ref value) => (value as &dyn Any).downcast_ref::().cloned(), + Union::Float(value) => try_cast(value), #[cfg(not(feature = "no_index"))] Union::Array(value) => cast_box::<_, T>(value).ok().map(|v| *v), #[cfg(not(feature = "no_object"))] Union::Map(value) => 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::Variant(value) => value.as_any().downcast_ref::().cloned(), + Union::Variant(value) => (*value).as_box_any().downcast().map(|x| *x).ok(), } } @@ -435,20 +463,20 @@ impl Dynamic { } match self.0 { - Union::Unit(ref value) => (value as &dyn Any).downcast_ref::().unwrap().clone(), - Union::Bool(ref value) => (value as &dyn Any).downcast_ref::().unwrap().clone(), + Union::Unit(value) => try_cast(value).unwrap(), + Union::Bool(value) => try_cast(value).unwrap(), Union::Str(value) => *cast_box::<_, T>(value).unwrap(), - Union::Char(ref value) => (value as &dyn Any).downcast_ref::().unwrap().clone(), - Union::Int(ref value) => (value as &dyn Any).downcast_ref::().unwrap().clone(), + Union::Char(value) => try_cast(value).unwrap(), + Union::Int(value) => try_cast(value).unwrap(), #[cfg(not(feature = "no_float"))] - Union::Float(ref value) => (value as &dyn Any).downcast_ref::().unwrap().clone(), + Union::Float(value) => try_cast(value).unwrap(), #[cfg(not(feature = "no_index"))] Union::Array(value) => *cast_box::<_, T>(value).unwrap(), #[cfg(not(feature = "no_object"))] Union::Map(value) => *cast_box::<_, T>(value).unwrap(), #[cfg(not(feature = "no_module"))] Union::Module(value) => *cast_box::<_, T>(value).unwrap(), - Union::Variant(value) => value.as_any().downcast_ref::().unwrap().clone(), + Union::Variant(value) => (*value).as_box_any().downcast().map(|x| *x).unwrap(), } } From 5f12391ec6c6a35e5fa81d96197683c15c5c9829 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 00:19:08 +0800 Subject: [PATCH 09/20] Use hashed lookup for module-qualified functions and variables. --- src/engine.rs | 47 ++++---- src/fn_register.rs | 8 +- src/module.rs | 250 ++++++++++++++++++++++++++++++------------ src/optimize.rs | 9 +- src/packages/utils.rs | 11 +- src/parser.rs | 40 ++++--- src/scope.rs | 4 +- src/utils.rs | 20 +++- 8 files changed, 268 insertions(+), 121 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index f10d77a2..136a3b17 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,14 +3,15 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; +use crate::module::ModuleRef; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, }; -use crate::parser::{Expr, FnDef, ModuleRef, ReturnType, Stmt, AST}; +use crate::parser::{Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; -use crate::token::Position; +use crate::token::{Position, Token}; use crate::utils::{calc_fn_def, StaticVec}; #[cfg(not(feature = "no_module"))] @@ -21,7 +22,7 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, format, - iter::once, + iter::{empty, once}, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -392,14 +393,14 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - modules: &ModuleRef, + modules: &Option>, index: Option, pos: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { #[cfg(not(feature = "no_module"))] { if let Some(modules) = modules { - let (id, root_pos) = modules.get(0); // First module + let (id, root_pos) = modules.get(0); let module = if let Some(index) = index { scope @@ -409,12 +410,15 @@ fn search_scope<'a>( .unwrap() } else { scope.find_module(id).ok_or_else(|| { - Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *root_pos)) + Box::new(EvalAltResult::ErrorModuleNotFound( + id.to_string(), + *root_pos, + )) })? }; return Ok(( - module.get_qualified_var_mut(name, modules.as_ref(), pos)?, + module.get_qualified_var_mut(name, modules.key(), pos)?, // Module variables are constant ScopeEntryType::Constant, )); @@ -527,7 +531,7 @@ impl Engine { } // Search built-in's and external functions - let fn_spec = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); + let fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); if let Some(func) = self .base_package @@ -676,7 +680,7 @@ impl Engine { // Has a system function an override? fn has_override(&self, state: &State, name: &str) -> bool { - let hash = calc_fn_hash(name, once(TypeId::of::())); + let hash = calc_fn_hash(empty(), name, once(TypeId::of::())); // First check registered functions self.base_package.contains_function(hash) @@ -1176,8 +1180,8 @@ impl Engine { Expr::CharConstant(c, _) => Ok((*c).into()), Expr::Variable(id, modules, index, pos) => { let index = if state.always_search { None } else { *index }; - let val = search_scope(scope, id, modules, index, *pos)?; - Ok(val.0.clone()) + let (val, _) = search_scope(scope, id, modules, index, *pos)?; + Ok(val.clone()) } Expr::Property(_, _) => unreachable!(), @@ -1190,18 +1194,19 @@ impl Engine { match lhs.as_ref() { // name = rhs - Expr::Variable(name, modules, index, pos) => { + Expr::Variable(id, modules, index, pos) => { let index = if state.always_search { None } else { *index }; - match search_scope(scope, name, modules, index, *pos)? { - (_, ScopeEntryType::Constant) => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(name.to_string(), *pos), + let (value_ptr, typ) = search_scope(scope, id, modules, index, *pos)?; + match typ { + ScopeEntryType::Constant => Err(Box::new( + EvalAltResult::ErrorAssignmentToConstant(id.to_string(), *pos), )), - (value_ptr, ScopeEntryType::Normal) => { + ScopeEntryType::Normal => { *value_ptr = rhs_val; Ok(Default::default()) } // End variable cannot be a module - (_, ScopeEntryType::Module) => unreachable!(), + ScopeEntryType::Module => unreachable!(), } } // idx_lhs[idx_expr] = rhs @@ -1322,9 +1327,13 @@ impl Engine { self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions - let hash = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); + let hash = calc_fn_hash( + modules.iter().map(|(m, _)| m.as_str()), + fn_name, + args.iter().map(|a| a.type_id()), + ); - match module.get_qualified_fn(fn_name, hash, modules, *pos) { + match module.get_qualified_fn(fn_name, hash, *pos) { Ok(func) => func(&mut args, *pos), Err(_) if def_val.is_some() => Ok(def_val.as_deref().unwrap().clone()), Err(err) => Err(err), diff --git a/src/fn_register.rs b/src/fn_register.rs index 2e9a5325..c1549185 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -8,7 +8,7 @@ use crate::result::EvalAltResult; use crate::token::Position; use crate::utils::calc_fn_spec; -use crate::stdlib::{any::TypeId, boxed::Box, mem, string::ToString}; +use crate::stdlib::{any::TypeId, boxed::Box, iter::empty, mem, string::ToString}; /// A trait to register custom functions with the `Engine`. pub trait RegisterFn { @@ -220,7 +220,7 @@ macro_rules! def_register { 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 hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); + let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } } @@ -238,7 +238,7 @@ macro_rules! def_register { 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 hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); + let hash = calc_fn_spec(empty(), name, [$(TypeId::of::<$par>()),*].iter().cloned()); self.base_package.functions.insert(hash, Box::new(func)); } } @@ -257,7 +257,7 @@ macro_rules! def_register { 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 hash = calc_fn_spec(name, [$(TypeId::of::<$par>()),*].iter().cloned()); + 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/module.rs b/src/module.rs index 1fa704c8..374f22d6 100644 --- a/src/module.rs +++ b/src/module.rs @@ -7,21 +7,29 @@ use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib}; use crate::parser::{FnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; -use crate::token::Position; -use crate::token::Token; +use crate::token::{Position, Token}; use crate::utils::StaticVec; use crate::stdlib::{ any::TypeId, boxed::Box, collections::HashMap, - fmt, mem, + fmt, + iter::{empty, once}, + mem, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, sync::Arc, + vec, + vec::Vec, }; +#[cfg(not(feature = "sync"))] +type NativeFunction = Rc>; +#[cfg(feature = "sync")] +type NativeFunction = Arc>; + /// A trait that encapsulates a module resolution service. pub trait ModuleResolver { /// Resolve a module based on a path string. @@ -44,15 +52,18 @@ type FuncReturn = Result>; pub struct Module { /// Sub-modules. modules: HashMap, - /// Module variables, including sub-modules. + + /// Module variables. variables: HashMap, + /// Flattened collection of all module variables, including those in sub-modules. + all_variables: HashMap, + /// External Rust functions. - #[cfg(not(feature = "sync"))] - functions: HashMap>>, - /// External Rust functions. - #[cfg(feature = "sync")] - functions: HashMap>>, + functions: HashMap, NativeFunction)>, + + /// Flattened collection of all external Rust functions, including those in sub-modules. + all_functions: HashMap, /// Script-defined functions. fn_lib: FunctionsLib, @@ -113,7 +124,7 @@ impl Module { /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); /// ``` pub fn get_var_value(&self, name: &str) -> Option { - self.get_var(name).and_then(|v| v.try_cast::()) + self.get_var(name).and_then(Dynamic::try_cast::) } /// Get a module variable as a `Dynamic`. @@ -131,11 +142,6 @@ impl Module { self.variables.get(name).cloned() } - /// Get a mutable reference to a module variable. - pub fn get_var_mut(&mut self, name: &str) -> Option<&mut Dynamic> { - self.variables.get_mut(name) - } - /// Set a variable into the module. /// /// If there is an existing variable of the same name, it is replaced. @@ -154,16 +160,17 @@ impl Module { } /// Get a mutable reference to a modules-qualified variable. + /// + /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. pub(crate) fn get_qualified_var_mut( &mut self, name: &str, - modules: &StaticVec<(String, Position)>, + hash: u64, pos: Position, ) -> Result<&mut Dynamic, Box> { - Ok(self - .get_qualified_module_mut(modules)? - .get_var_mut(name) - .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), pos)))?) + self.all_variables + .get_mut(&hash) + .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.to_string(), pos))) } /// Does a sub-module exist in the module? @@ -273,13 +280,15 @@ 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, fn_name: &str, params: &[TypeId], func: Box) -> u64 { - let hash = calc_fn_hash(fn_name, params.iter().cloned()); + pub fn set_fn(&mut self, fn_name: String, params: Vec, func: Box) -> u64 { + let hash = calc_fn_hash(empty(), &fn_name, params.iter().cloned()); #[cfg(not(feature = "sync"))] - self.functions.insert(hash, Rc::new(func)); + self.functions + .insert(hash, (fn_name, params, Rc::new(func))); #[cfg(feature = "sync")] - self.functions.insert(hash, Arc::new(func)); + self.functions + .insert(hash, (fn_name, params, Arc::new(func))); hash } @@ -297,9 +306,9 @@ impl Module { /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_0>( + pub fn set_fn_0, T: Into>( &mut self, - fn_name: &str, + fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -308,8 +317,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -325,9 +334,9 @@ 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>( + pub fn set_fn_1, A: Variant + Clone, T: Into>( &mut self, - fn_name: &str, + fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, ) -> u64 { @@ -336,8 +345,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -353,9 +362,9 @@ 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>( + pub fn set_fn_1_mut, A: Variant + Clone, T: Into>( &mut self, - fn_name: &str, + fn_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 { @@ -364,8 +373,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -383,9 +392,9 @@ impl Module { /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_2>( + pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Into>( &mut self, - fn_name: &str, + fn_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 { @@ -397,8 +406,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::(), TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -415,9 +424,14 @@ impl Module { /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` - pub fn set_fn_2_mut>( + pub fn set_fn_2_mut< + K: Into, + A: Variant + Clone, + B: Variant + Clone, + T: Into, + >( &mut self, - fn_name: &str, + fn_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 { @@ -429,8 +443,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::(), TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -449,13 +463,14 @@ impl Module { /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3< + K: Into, A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Into, >( &mut self, - fn_name: &str, + fn_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 { @@ -468,8 +483,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -489,13 +504,14 @@ impl Module { /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3_mut< + K: Into, A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Into, >( &mut self, - fn_name: &str, + fn_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 { @@ -508,8 +524,8 @@ impl Module { .map(|v| v.into()) .map_err(|err| EvalAltResult::set_position(err, pos)) }; - let arg_types = &[TypeId::of::(), TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name, arg_types, Box::new(f)) + let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; + self.set_fn(fn_name.into(), arg_types, Box::new(f)) } /// Get a Rust function. @@ -527,7 +543,7 @@ impl Module { /// 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()) + self.functions.get(&hash).map(|(_, _, v)| v.as_ref()) } /// Get a modules-qualified function. @@ -538,24 +554,12 @@ impl Module { &mut self, name: &str, hash: u64, - modules: &StaticVec<(String, Position)>, pos: Position, ) -> Result<&Box, Box> { - Ok(self - .get_qualified_module_mut(modules)? - .get_fn(hash) - .ok_or_else(|| { - let mut fn_name: String = Default::default(); - - modules.iter().for_each(|(n, _)| { - fn_name.push_str(n); - fn_name.push_str(Token::DoubleColon.syntax().as_ref()); - }); - - fn_name.push_str(name); - - Box::new(EvalAltResult::ErrorFunctionNotFound(fn_name, pos)) - })?) + self.all_functions + .get(&hash) + .map(|f| f.as_ref()) + .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos))) } /// Get the script-defined functions. @@ -634,6 +638,52 @@ impl Module { Ok(module) } + + /// Scan through all the sub-modules in the `Module` build an index of all + /// variables and external Rust functions via hashing. + pub(crate) fn collect_all_sub_modules(&mut self) { + // Collect a particular module. + fn collect<'a>( + module: &'a mut Module, + names: &mut Vec<&'a str>, + variables: &mut Vec<(u64, Dynamic)>, + functions: &mut Vec<(u64, NativeFunction)>, + ) { + for (n, m) in module.modules.iter_mut() { + // Collect all the sub-modules first. + names.push(n); + collect(m, names, variables, functions); + names.pop(); + } + + // Collect all variables + for (var_name, value) in module.variables.iter() { + let hash = calc_fn_hash(names.iter().map(|v| *v), var_name, empty()); + variables.push((hash, value.clone())); + } + // Collect all functions + for (fn_name, params, func) in module.functions.values() { + let hash = calc_fn_hash(names.iter().map(|v| *v), fn_name, params.iter().cloned()); + functions.push((hash, func.clone())); + } + } + + let mut variables = Vec::new(); + let mut functions = Vec::new(); + + collect(self, &mut vec!["root"], &mut variables, &mut functions); + + self.all_variables.clear(); + self.all_functions.clear(); + + for (key, value) in variables { + self.all_variables.insert(key, value); + } + + for (key, value) in functions { + self.all_functions.insert(key, value); + } + } } /// Re-export module resolvers. @@ -669,12 +719,18 @@ mod file { /// let mut engine = Engine::new(); /// engine.set_module_resolver(Some(resolver)); /// ``` - #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)] + #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct FileModuleResolver { path: PathBuf, extension: String, } + impl Default for FileModuleResolver { + fn default() -> Self { + Self::new_with_path(PathBuf::default()) + } + } + impl FileModuleResolver { /// Create a new `FileModuleResolver` with a specific base path. /// @@ -774,6 +830,64 @@ 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, Hash, Default)] +pub struct ModuleRef(StaticVec<(String, Position)>, u64); + +impl fmt::Debug for ModuleRef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f)?; + + if self.1 > 0 { + write!(f, " -> {}", self.1) + } 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, 0) + } +} + +impl ModuleRef { + pub fn key(&self) -> u64 { + self.1 + } + pub fn set_key(&mut self, key: u64) { + self.1 = key + } +} + /// Static module resolver. mod stat { use super::*; diff --git a/src/optimize.rs b/src/optimize.rs index 018a210e..2c4e183a 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -13,6 +13,7 @@ use crate::token::Position; use crate::stdlib::{ boxed::Box, collections::HashMap, + iter::empty, string::{String, ToString}, vec, vec::Vec, @@ -117,7 +118,7 @@ fn call_fn( pos: Position, ) -> Result, Box> { // Search built-in's and external functions - let hash = calc_fn_hash(fn_name, args.iter().map(|a| a.type_id())); + let hash = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); base_package .get_function(hash) @@ -376,11 +377,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // id1 = id2 = expr2 (id1, id2) => Expr::Assignment( Box::new(id1), - Box::new(Expr::Assignment( - Box::new(id2), - Box::new(optimize_expr(*expr2, state)), - pos2, - )), + Box::new(Expr::Assignment(Box::new(id2), Box::new(optimize_expr(*expr2, state)), pos2)), pos, ), }, diff --git a/src/packages/utils.rs b/src/packages/utils.rs index a02e475c..f8c60f2c 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -9,6 +9,7 @@ use crate::token::Position; use crate::stdlib::{ any::TypeId, boxed::Box, + iter::empty, mem, string::{String, ToString}, }; @@ -115,7 +116,7 @@ pub fn reg_none( + Sync + 'static, ) { - let hash = calc_fn_hash(fn_name, ([] as [TypeId; 0]).iter().cloned()); + 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)?; @@ -165,7 +166,7 @@ pub fn reg_unary( ) { //println!("register {}({})", fn_name, crate::std::any::type_name::()); - let hash = calc_fn_hash(fn_name, [TypeId::of::()].iter().cloned()); + 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)?; @@ -225,7 +226,7 @@ pub fn reg_unary_mut( ) { //println!("register {}(&mut {})", fn_name, crate::std::any::type_name::()); - let hash = calc_fn_hash(fn_name, [TypeId::of::()].iter().cloned()); + 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)?; @@ -279,6 +280,7 @@ pub fn reg_binary( //println!("register {}({}, {})", fn_name, crate::std::any::type_name::(), crate::std::any::type_name::()); let hash = calc_fn_hash( + empty(), fn_name, [TypeId::of::(), TypeId::of::()].iter().cloned(), ); @@ -343,6 +345,7 @@ 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( + empty(), fn_name, [TypeId::of::(), TypeId::of::()].iter().cloned(), ); @@ -381,6 +384,7 @@ pub fn reg_trinary(), crate::std::any::type_name::(), crate::std::any::type_name::()); let hash = calc_fn_hash( + empty(), fn_name, [TypeId::of::(), TypeId::of::(), TypeId::of::()] .iter() @@ -422,6 +426,7 @@ pub fn reg_trinary_mut(), crate::std::any::type_name::(), crate::std::any::type_name::()); let hash = calc_fn_hash( + empty(), fn_name, [TypeId::of::(), TypeId::of::(), TypeId::of::()] .iter() diff --git a/src/parser.rs b/src/parser.rs index 953c12a0..d19a7e07 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,8 +1,10 @@ //! Main module defining the lexer and parser. use crate::any::{Dynamic, Union}; +use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::error::{LexError, ParseError, ParseErrorType}; +use crate::module::ModuleRef; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; @@ -14,7 +16,7 @@ use crate::stdlib::{ char, collections::HashMap, format, - iter::Peekable, + iter::{empty, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, rc::Rc, @@ -44,11 +46,6 @@ pub type FLOAT = f64; type PERR = ParseErrorType; -/// A chain of module names to qualify a variable or function call. -/// 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. -pub type ModuleRef = Option>>; - /// Compiled AST (abstract syntax tree) of a Rhai script. /// /// Currently, `AST` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`. @@ -357,8 +354,13 @@ pub enum Expr { CharConstant(char, Position), /// String constant. StringConstant(String, Position), - /// Variable access - (variable name, optional modules, optional index, position) - Variable(Box, ModuleRef, Option, Position), + /// Variable access - (variable name, optional modules, hash, optional index, position) + Variable( + Box, + Option>, + Option, + Position, + ), /// Property access. Property(String, Position), /// { stmt } @@ -368,7 +370,7 @@ pub enum Expr { /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box>, - ModuleRef, + Option>, Box>, Option>, Position, @@ -675,7 +677,7 @@ fn parse_call_expr<'a>( input: &mut Peekable>, stack: &mut Stack, id: String, - modules: ModuleRef, + modules: Option>, begin: Position, allow_stmt_expr: bool, ) -> Result> { @@ -1110,12 +1112,11 @@ fn parse_primary<'a>( if let Some(ref mut modules) = modules { modules.push((*id, pos)); } else { + index = stack.find_module(id.as_ref()); + let mut vec = StaticVec::new(); vec.push((*id, pos)); - modules = Some(Box::new(vec)); - - let root = modules.as_ref().unwrap().iter().next().unwrap(); - index = stack.find_module(&root.0); + modules = Some(Box::new(vec.into())); } Expr::Variable(Box::new(id2), modules, index, pos2) @@ -1133,6 +1134,15 @@ fn parse_primary<'a>( } } + match &mut root_expr { + // Calculate hash key for module-qualified variables + Expr::Variable(id, Some(modules), _, _) => { + let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); + modules.set_key(hash); + } + _ => (), + } + Ok(root_expr) } @@ -1315,7 +1325,7 @@ fn make_dot_expr( } // lhs.module::id - syntax error (_, Expr::Variable(_, Some(modules), _, _)) => { - return Err(PERR::PropertyExpected.into_err(modules.iter().next().unwrap().1)) + return Err(PERR::PropertyExpected.into_err(modules.get(0).1)) } // lhs.dot_lhs.dot_rhs (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot( diff --git a/src/scope.rs b/src/scope.rs index 2411dcc1..04e447bd 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -175,7 +175,9 @@ impl<'a> Scope<'a> { /// /// Modules are used for accessing member variables, functions and plugins under a namespace. #[cfg(not(feature = "no_module"))] - pub fn push_module>>(&mut self, name: K, value: Module) { + pub fn push_module>>(&mut self, name: K, mut value: Module) { + value.collect_all_sub_modules(); + self.push_dynamic_value( name, EntryType::Module, diff --git a/src/utils.rs b/src/utils.rs index 6e11e39b..e4aeb17a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,16 +14,26 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; -/// Calculate a `u64` hash key from a function name and parameter types. +/// Calculate a `u64` hash key from a module-qualified function name and parameter types. /// -/// Parameter types are passed in via `TypeId` values from an iterator -/// which can come from any source. -pub fn calc_fn_spec(fn_name: &str, params: impl Iterator) -> u64 { +/// Module names are passed in via `&str` references from an iterator. +/// Parameter types are passed in via `TypeId` values from an iterator. +/// +/// ### Note +/// +/// The first module name is skipped. Hashing starts from the _second_ module in the chain. +pub fn calc_fn_spec<'a>( + modules: impl Iterator, + fn_name: &str, + params: impl Iterator, +) -> u64 { #[cfg(feature = "no_std")] let mut s: AHasher = Default::default(); #[cfg(not(feature = "no_std"))] let mut s = DefaultHasher::new(); + // We always skip the first module + modules.skip(1).for_each(|m| m.hash(&mut s)); s.write(fn_name.as_bytes()); params.for_each(|t| t.hash(&mut s)); s.finish() @@ -45,7 +55,7 @@ pub(crate) fn calc_fn_def(fn_name: &str, num_params: usize) -> u64 { /// /// 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. -#[derive(Clone, Default)] +#[derive(Clone, Hash, Default)] pub struct StaticVec { /// Total number of values held. len: usize, From e6fabe58cc048b68eb2254debdaacfc147f39c0a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 11:34:56 +0800 Subject: [PATCH 10/20] Unify function hash calculations, pre-hash module-qualified function calls. --- src/api.rs | 2 +- src/engine.rs | 72 ++++++++++++++++++++++++++++------------ src/module.rs | 92 +++++++++++++++++++++++---------------------------- src/parser.rs | 37 ++++++++++++++++++--- src/utils.rs | 16 +++------ 5 files changed, 130 insertions(+), 89 deletions(-) diff --git a/src/api.rs b/src/api.rs index 68be4e59..6f624b76 100644 --- a/src/api.rs +++ b/src/api.rs @@ -999,7 +999,7 @@ impl Engine { let pos = Position::none(); let fn_def = fn_lib - .get_function(name, args.len()) + .get_function_by_signature(name, args.len()) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos)))?; let state = State::new(fn_lib); diff --git a/src/engine.rs b/src/engine.rs index 136a3b17..6f6c6009 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -11,8 +11,8 @@ use crate::packages::{ use crate::parser::{Expr, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; -use crate::token::{Position, Token}; -use crate::utils::{calc_fn_def, StaticVec}; +use crate::token::Position; +use crate::utils::{StaticVec, EMPTY_TYPE_ID}; #[cfg(not(feature = "no_module"))] use crate::module::{resolvers, Module, ModuleResolver}; @@ -22,7 +22,7 @@ use crate::stdlib::{ boxed::Box, collections::HashMap, format, - iter::{empty, once}, + iter::{empty, once, repeat}, mem, num::NonZeroUsize, ops::{Deref, DerefMut}, @@ -158,27 +158,39 @@ impl<'a> State<'a> { } /// Does a certain script-defined function exist in the `State`? pub fn has_function(&self, name: &str, params: usize) -> bool { - self.fn_lib.contains_key(&calc_fn_def(name, params)) + self.fn_lib.contains_key(&calc_fn_hash( + empty(), + name, + repeat(EMPTY_TYPE_ID()).take(params), + )) } /// Get a script-defined function definition from the `State`. pub fn get_function(&self, name: &str, params: usize) -> Option<&FnDef> { self.fn_lib - .get(&calc_fn_def(name, params)) + .get(&calc_fn_hash( + empty(), + name, + repeat(EMPTY_TYPE_ID()).take(params), + )) .map(|f| f.as_ref()) } } +/// A sharable script-defined function. +#[cfg(feature = "sync")] +pub type ScriptedFunction = Arc; +#[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 /// and number of parameters are considered equivalent. /// -/// The key of the `HashMap` is a `u64` hash calculated by the function `calc_fn_def`. +/// 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( - #[cfg(feature = "sync")] HashMap>, - #[cfg(not(feature = "sync"))] HashMap>, -); +pub struct FunctionsLib(HashMap); impl FunctionsLib { /// Create a new `FunctionsLib` from a collection of `FnDef`. @@ -186,7 +198,11 @@ impl FunctionsLib { FunctionsLib( vec.into_iter() .map(|f| { - let hash = calc_fn_def(&f.name, f.params.len()); + let hash = calc_fn_hash( + empty(), + &f.name, + repeat(EMPTY_TYPE_ID()).take(f.params.len()), + ); #[cfg(feature = "sync")] { @@ -201,12 +217,21 @@ impl FunctionsLib { ) } /// Does a certain function exist in the `FunctionsLib`? - pub fn has_function(&self, name: &str, params: usize) -> bool { - self.contains_key(&calc_fn_def(name, params)) + /// + /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. + pub fn has_function(&self, hash: u64) -> bool { + self.contains_key(&hash) } /// Get a function definition from the `FunctionsLib`. - pub fn get_function(&self, name: &str, params: usize) -> Option<&FnDef> { - self.get(&calc_fn_def(name, params)).map(|f| f.as_ref()) + /// + /// 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()) + } + /// Get a function definition from the `FunctionsLib`. + pub fn get_function_by_signature(&self, name: &str, params: usize) -> Option<&FnDef> { + let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + self.get_function(hash) } /// Merge another `FunctionsLib` into this `FunctionsLib`. pub fn merge(&self, other: &Self) -> Self { @@ -222,6 +247,12 @@ impl FunctionsLib { } } +impl From> for FunctionsLib { + fn from(values: Vec<(u64, ScriptedFunction)>) -> Self { + FunctionsLib(values.into_iter().collect()) + } +} + impl Deref for FunctionsLib { #[cfg(feature = "sync")] type Target = HashMap>; @@ -1323,17 +1354,14 @@ impl Engine { })?; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_fn_lib(fn_name, args.len(), modules)? { + if let Some(fn_def) = module.get_qualified_scripted_fn(modules.key()) { self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions - let hash = calc_fn_hash( - modules.iter().map(|(m, _)| m.as_str()), - fn_name, - args.iter().map(|a| a.type_id()), - ); + let hash1 = modules.key(); + let hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); - match module.get_qualified_fn(fn_name, hash, *pos) { + match module.get_qualified_fn(fn_name, hash1 ^ hash2, *pos) { Ok(func) => func(&mut args, *pos), Err(_) if def_val.is_some() => Ok(def_val.as_deref().unwrap().clone()), Err(err) => Err(err), diff --git a/src/module.rs b/src/module.rs index 374f22d6..53af8e4d 100644 --- a/src/module.rs +++ b/src/module.rs @@ -3,19 +3,19 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; -use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib}; +use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, ScriptedFunction}; use crate::parser::{FnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; -use crate::utils::StaticVec; +use crate::utils::{StaticVec, EMPTY_TYPE_ID}; use crate::stdlib::{ any::TypeId, boxed::Box, collections::HashMap, fmt, - iter::{empty, once}, + iter::{empty, repeat}, mem, ops::{Deref, DerefMut}, rc::Rc, @@ -67,6 +67,9 @@ pub struct Module { /// Script-defined functions. fn_lib: FunctionsLib, + + /// Flattened collection of all script-defined functions, including those in sub-modules. + all_fn_lib: FunctionsLib, } impl fmt::Debug for Module { @@ -239,26 +242,6 @@ impl Module { self.modules.insert(name.into(), sub_module.into()); } - /// Get a mutable reference to a modules chain. - /// The first module is always skipped and assumed to be the same as `self`. - pub(crate) fn get_qualified_module_mut( - &mut self, - modules: &StaticVec<(String, Position)>, - ) -> Result<&mut Module, Box> { - let mut drain = modules.iter(); - drain.next().unwrap(); // Skip first module - - let mut module = self; - - for (id, id_pos) in drain { - module = module - .get_sub_module_mut(id) - .ok_or_else(|| Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *id_pos)))?; - } - - Ok(module) - } - /// Does the particular Rust function exist in the module? /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. @@ -576,17 +559,11 @@ impl Module { &self.fn_lib } - /// Get a modules-qualified functions library. - pub(crate) fn get_qualified_fn_lib( - &mut self, - name: &str, - args: usize, - modules: &StaticVec<(String, Position)>, - ) -> Result, Box> { - Ok(self - .get_qualified_module_mut(modules)? - .fn_lib - .get_function(name, args)) + /// 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) } /// Create a new `Module` by evaluating an `AST`. @@ -648,11 +625,12 @@ impl Module { names: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, functions: &mut Vec<(u64, NativeFunction)>, + fn_lib: &mut Vec<(u64, ScriptedFunction)>, ) { for (n, m) in module.modules.iter_mut() { // Collect all the sub-modules first. names.push(n); - collect(m, names, variables, functions); + collect(m, names, variables, functions, fn_lib); names.pop(); } @@ -661,28 +639,42 @@ impl Module { let hash = calc_fn_hash(names.iter().map(|v| *v), var_name, empty()); variables.push((hash, value.clone())); } - // Collect all functions + // Collect all Rust functions for (fn_name, params, func) in module.functions.values() { - let hash = calc_fn_hash(names.iter().map(|v| *v), fn_name, params.iter().cloned()); - functions.push((hash, func.clone())); + let hash1 = calc_fn_hash( + names.iter().map(|v| *v), + fn_name, + repeat(EMPTY_TYPE_ID()).take(params.len()), + ); + let hash2 = calc_fn_hash(empty(), "", params.iter().cloned()); + functions.push((hash1 ^ hash2, func.clone())); + } + // Collect all script-defined functions + for fn_def in module.fn_lib.values() { + let hash = calc_fn_hash( + names.iter().map(|v| *v), + &fn_def.name, + repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), + ); + fn_lib.push((hash, fn_def.clone())); } } let mut variables = Vec::new(); let mut functions = Vec::new(); + let mut fn_lib = Vec::new(); - collect(self, &mut vec!["root"], &mut variables, &mut functions); + collect( + self, + &mut vec!["root"], + &mut variables, + &mut functions, + &mut fn_lib, + ); - self.all_variables.clear(); - self.all_functions.clear(); - - for (key, value) in variables { - self.all_variables.insert(key, value); - } - - for (key, value) in functions { - self.all_functions.insert(key, value); - } + self.all_variables = variables.into_iter().collect(); + self.all_functions = functions.into_iter().collect(); + self.all_fn_lib = fn_lib.into(); } } @@ -843,7 +835,7 @@ impl fmt::Debug for ModuleRef { fmt::Debug::fmt(&self.0, f)?; if self.1 > 0 { - write!(f, " -> {}", self.1) + write!(f, " -> {:0>16x}", self.1) } else { Ok(()) } diff --git a/src/parser.rs b/src/parser.rs index d19a7e07..3e648828 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,7 +8,7 @@ use crate::module::ModuleRef; use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token, TokenIterator}; -use crate::utils::{calc_fn_def, StaticVec}; +use crate::utils::{StaticVec, EMPTY_TYPE_ID}; use crate::stdlib::{ borrow::Cow, @@ -16,7 +16,7 @@ use crate::stdlib::{ char, collections::HashMap, format, - iter::{empty, Peekable}, + iter::{empty, repeat, Peekable}, num::NonZeroUsize, ops::{Add, Deref, DerefMut}, rc::Rc, @@ -677,7 +677,7 @@ fn parse_call_expr<'a>( input: &mut Peekable>, stack: &mut Stack, id: String, - modules: Option>, + mut modules: Option>, begin: Position, allow_stmt_expr: bool, ) -> Result> { @@ -697,6 +697,13 @@ fn parse_call_expr<'a>( // id() (Token::RightParen, _) => { eat_token(input, Token::RightParen); + + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); + modules.set_key(hash); + } + return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -713,9 +720,20 @@ fn parse_call_expr<'a>( args.push(parse_expr(input, stack, allow_stmt_expr)?); match input.peek().unwrap() { + // id(...args) (Token::RightParen, _) => { eat_token(input, Token::RightParen); + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash( + modules.iter().map(|(m, _)| m.as_str()), + &id, + repeat(EMPTY_TYPE_ID()).take(args.len()), + ); + modules.set_key(hash); + } + return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -724,9 +742,11 @@ fn parse_call_expr<'a>( begin, )); } + // id(...args, (Token::Comma, _) => { eat_token(input, Token::Comma); } + // id(...args (Token::EOF, pos) => { return Err(PERR::MissingToken( Token::RightParen.into(), @@ -734,9 +754,11 @@ fn parse_call_expr<'a>( ) .into_err(*pos)) } + // id(...args (Token::LexError(err), pos) => { return Err(PERR::BadInput(err.to_string()).into_err(*pos)) } + // id(...args ??? (_, pos) => { return Err(PERR::MissingToken( Token::Comma.into(), @@ -2207,7 +2229,14 @@ fn parse_global_level<'a>( if let (Token::Fn, _) = input.peek().unwrap() { let mut stack = Stack::new(); let f = parse_fn(input, &mut stack, true)?; - functions.insert(calc_fn_def(&f.name, f.params.len()), f); + functions.insert( + calc_fn_hash( + empty(), + &f.name, + repeat(EMPTY_TYPE_ID()).take(f.params.len()), + ), + f, + ); continue; } } diff --git a/src/utils.rs b/src/utils.rs index e4aeb17a..2d0ae237 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,6 +14,10 @@ use crate::stdlib::collections::hash_map::DefaultHasher; #[cfg(feature = "no_std")] use ahash::AHasher; +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. @@ -39,18 +43,6 @@ pub fn calc_fn_spec<'a>( s.finish() } -/// Calculate a `u64` hash key from a function name and number of parameters (without regard to types). -pub(crate) fn calc_fn_def(fn_name: &str, num_params: usize) -> u64 { - #[cfg(feature = "no_std")] - let mut s: AHasher = Default::default(); - #[cfg(not(feature = "no_std"))] - let mut s = DefaultHasher::new(); - - s.write(fn_name.as_bytes()); - s.write_usize(num_params); - s.finish() -} - /// 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. From e0745ef069e389b157402033579bf1898a8f18b6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 13:27:51 +0800 Subject: [PATCH 11/20] Do not build index for multiple packages to avoid Engine creation regression. --- src/engine.rs | 17 +++++++++++++ src/module.rs | 7 +---- src/packages/mod.rs | 59 +++++++++++++++++++------------------------ src/packages/utils.rs | 14 +++++----- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 6f6c6009..00532895 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -176,9 +176,17 @@ 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; @@ -512,6 +520,15 @@ impl Engine { self.packages.push(package); } + /// Load a new package into the `Engine`. + /// + /// When searching for functions, packages loaded later are preferred. + /// In other words, loaded packages are searched in reverse order. + pub fn load_packages(&mut self, package: PackageLibrary) { + // Push the package to the top - packages are searched in reverse order + self.packages.push(package); + } + /// Control whether and how the `Engine` will optimize an AST after compilation. /// /// Not available under the `no_optimize` feature. diff --git a/src/module.rs b/src/module.rs index 53af8e4d..a1721835 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, FnAny, FnCallArgs, FunctionsLib, ScriptedFunction}; +use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, NativeFunction, ScriptedFunction}; use crate::parser::{FnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; @@ -25,11 +25,6 @@ use crate::stdlib::{ vec::Vec, }; -#[cfg(not(feature = "sync"))] -type NativeFunction = Rc>; -#[cfg(feature = "sync")] -type NativeFunction = Arc>; - /// A trait that encapsulates a module resolution service. pub trait ModuleResolver { /// Resolve a module based on a path string. diff --git a/src/packages/mod.rs b/src/packages/mod.rs index c9be4487..0d935e40 100644 --- a/src/packages/mod.rs +++ b/src/packages/mod.rs @@ -34,11 +34,10 @@ pub use time_basic::BasicTimePackage; pub use utils::*; +const NUM_NATIVE_FUNCTIONS: usize = 512; + /// Trait that all packages must implement. pub trait Package { - /// Create a new instance of a package. - fn new() -> Self; - /// Register all the functions in a package into a store. fn init(lib: &mut PackageStore); @@ -47,7 +46,6 @@ pub trait Package { } /// Type to store all functions in the package. -#[derive(Default)] pub struct PackageStore { /// All functions, keyed by a hash created from the function name and parameter types. pub functions: HashMap>, @@ -61,14 +59,6 @@ impl PackageStore { pub fn new() -> Self { Default::default() } - /// Get an iterator over the keys of the functions in the `PackageStore`. - pub fn function_keys(&self) -> impl Iterator { - self.functions.keys() - } - /// Get an iterator over the `TypeId` of the type iterators in the `PackageStore`. - pub fn type_iterator_keys(&self) -> impl Iterator { - self.type_iterators.keys() - } /// Does the specified function hash key exist in the `PackageStore`? pub fn contains_function(&self, hash: u64) -> bool { self.functions.contains_key(&hash) @@ -87,6 +77,15 @@ impl PackageStore { } } +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. #[cfg(not(feature = "sync"))] pub type PackageLibrary = Rc; @@ -95,48 +94,42 @@ pub type PackageLibrary = Rc; #[cfg(feature = "sync")] pub type PackageLibrary = Arc; -#[derive(Default)] /// Type containing a collection of `PackageLibrary` instances. /// All function and type iterator keys in the loaded packages are indexed for fast access. +#[derive(Clone, Default)] pub(crate) struct PackagesCollection { /// Collection of `PackageLibrary` instances. packages: Vec, - /// Index of all function keys, pointing to the offset in `packages`. - function_keys: HashMap, - /// Index of all type iterator `TypeId`'s, pointing to the offset in `packages`. - iterator_types: HashMap, } impl PackagesCollection { /// Add a `PackageLibrary` into the `PackagesCollection`. pub fn push(&mut self, package: PackageLibrary) { - let index = self.packages.len(); - package.function_keys().for_each(|&hash| { - self.function_keys.insert(hash, index); - }); - package.type_iterator_keys().for_each(|&id| { - self.iterator_types.insert(id, index); - }); - self.packages.push(package); + // Later packages override previous ones. + self.packages.insert(0, package); } /// Does the specified function hash key exist in the `PackagesCollection`? pub fn contains_function(&self, hash: u64) -> bool { - self.function_keys.contains_key(&hash) + 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> { - self.function_keys - .get(&hash) - .and_then(|&index| self.packages[index].functions.get(&hash)) + self.packages + .iter() + .map(|p| p.get_function(hash)) + .find(|f| f.is_some()) + .flatten() } /// Does the specified TypeId iterator exist in the `PackagesCollection`? pub fn contains_iterator(&self, id: TypeId) -> bool { - self.iterator_types.contains_key(&id) + self.packages.iter().any(|p| p.contains_iterator(id)) } /// Get the specified TypeId iterator. pub fn get_iterator(&self, id: TypeId) -> Option<&Box> { - self.iterator_types - .get(&id) - .and_then(|&index| self.packages[index].type_iterators.get(&id)) + self.packages + .iter() + .map(|p| p.get_iterator(id)) + .find(|f| f.is_some()) + .flatten() } } diff --git a/src/packages/utils.rs b/src/packages/utils.rs index f8c60f2c..fbb8c31d 100644 --- a/src/packages/utils.rs +++ b/src/packages/utils.rs @@ -44,12 +44,6 @@ macro_rules! def_package { pub struct $package($root::packages::PackageLibrary); impl $root::packages::Package for $package { - fn new() -> Self { - let mut pkg = $root::packages::PackageStore::new(); - Self::init(&mut pkg); - Self(pkg.into()) - } - fn get(&self) -> $root::packages::PackageLibrary { self.0.clone() } @@ -58,6 +52,14 @@ macro_rules! def_package { $block } } + + impl $package { + pub fn new() -> Self { + let mut pkg = $root::packages::PackageStore::new(); + ::init(&mut pkg); + Self(pkg.into()) + } + } }; } From 89d75b1b11d3f80ea2f780e3725dbd50b5251a0d Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 14:50:48 +0800 Subject: [PATCH 12/20] Fix compilation errors for no_module. --- src/engine.rs | 9 +++++--- src/parser.rs | 63 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 00532895..0029684d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -3,7 +3,6 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::error::ParseErrorType; -use crate::module::ModuleRef; use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, @@ -15,7 +14,10 @@ use crate::token::Position; use crate::utils::{StaticVec, EMPTY_TYPE_ID}; #[cfg(not(feature = "no_module"))] -use crate::module::{resolvers, Module, ModuleResolver}; +use crate::module::{resolvers, Module, ModuleRef, ModuleResolver}; + +#[cfg(feature = "no_module")] +use crate::parser::ModuleRef; use crate::stdlib::{ any::TypeId, @@ -432,7 +434,8 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - modules: &Option>, + #[cfg(not(feature = "no_module"))] mut modules: &Option>, + #[cfg(feature = "no_module")] _: &Option, index: Option, pos: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { diff --git a/src/parser.rs b/src/parser.rs index 3e648828..2499cdca 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,11 +4,17 @@ use crate::any::{Dynamic, Union}; use crate::calc_fn_hash; use crate::engine::{Engine, FunctionsLib}; use crate::error::{LexError, ParseError, ParseErrorType}; -use crate::module::ModuleRef; 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::EMPTY_TYPE_ID; + +#[cfg(not(feature = "no_module"))] +use crate::module::ModuleRef; + +#[cfg(feature = "no_module")] +#[derive(Debug, Clone, Copy)] +pub struct ModuleRef; use crate::stdlib::{ borrow::Cow, @@ -357,7 +363,8 @@ pub enum Expr { /// Variable access - (variable name, optional modules, hash, optional index, position) Variable( Box, - Option>, + #[cfg(not(feature = "no_module"))] Option>, + #[cfg(feature = "no_module")] Option, Option, Position, ), @@ -370,7 +377,8 @@ pub enum Expr { /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box>, - Option>, + #[cfg(not(feature = "no_module"))] Option>, + #[cfg(feature = "no_module")] Option, Box>, Option>, Position, @@ -677,7 +685,8 @@ fn parse_call_expr<'a>( input: &mut Peekable>, stack: &mut Stack, id: String, - mut modules: Option>, + #[cfg(not(feature = "no_module"))] mut modules: Option>, + #[cfg(feature = "no_module")] modules: Option, begin: Position, allow_stmt_expr: bool, ) -> Result> { @@ -698,12 +707,14 @@ fn parse_call_expr<'a>( (Token::RightParen, _) => { eat_token(input, Token::RightParen); - if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); - modules.set_key(hash); + #[cfg(not(feature = "no_module"))] + { + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); + modules.set_key(hash); + } } - return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -724,16 +735,18 @@ fn parse_call_expr<'a>( (Token::RightParen, _) => { eat_token(input, Token::RightParen); - if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash( - modules.iter().map(|(m, _)| m.as_str()), - &id, - repeat(EMPTY_TYPE_ID()).take(args.len()), - ); - modules.set_key(hash); + #[cfg(not(feature = "no_module"))] + { + if let Some(modules) = modules.as_mut() { + // Calculate hash + let hash = calc_fn_hash( + modules.iter().map(|(m, _)| m.as_str()), + &id, + repeat(EMPTY_TYPE_ID()).take(args.len()), + ); + modules.set_key(hash); + } } - return Ok(Expr::FnCall( Box::new(id.into()), modules, @@ -1136,9 +1149,9 @@ fn parse_primary<'a>( } else { index = stack.find_module(id.as_ref()); - let mut vec = StaticVec::new(); - vec.push((*id, pos)); - modules = Some(Box::new(vec.into())); + let mut m: ModuleRef = Default::default(); + m.push((*id, pos)); + modules = Some(Box::new(m)); } Expr::Variable(Box::new(id2), modules, index, pos2) @@ -1158,6 +1171,7 @@ fn parse_primary<'a>( match &mut root_expr { // Calculate hash key for module-qualified variables + #[cfg(not(feature = "no_module"))] Expr::Variable(id, Some(modules), _, _) => { let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); modules.set_key(hash); @@ -1347,7 +1361,10 @@ fn make_dot_expr( } // lhs.module::id - syntax error (_, Expr::Variable(_, Some(modules), _, _)) => { - return Err(PERR::PropertyExpected.into_err(modules.get(0).1)) + #[cfg(feature = "no_module")] + unreachable!(); + #[cfg(not(feature = "no_module"))] + return Err(PERR::PropertyExpected.into_err(modules.get(0).1)); } // lhs.dot_lhs.dot_rhs (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot( From eb52bfa28a55c8030e835724219648df67cb2872 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 16:49:24 +0800 Subject: [PATCH 13/20] Add export statement. --- README.md | 26 ++++++++++++++++-- src/engine.rs | 35 +++++++++++++++++++++++- src/error.rs | 16 +++++++++++ src/module.rs | 30 ++++++++++++-------- src/parser.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/scope.rs | 17 +++++++----- 6 files changed, 175 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 95c0d274..bb5f3597 100644 --- a/README.md +++ b/README.md @@ -2044,10 +2044,30 @@ Using external modules [module]: #using-external-modules [modules]: #using-external-modules -Rhai allows organizing code (functions and variables) into _modules_. A module is a single script file -with `export` statements that _exports_ certain global variables and functions as contents of the module. +Rhai allows organizing code (functions and variables) into _modules_. +Modules can be disabled via the [`no_module`] feature. -Everything exported as part of a module is constant and read-only. +### Exporting 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. Everything exported from a module is **constant** (**read-only**). + +```rust +// This is a module script. + +fn inc(x) { x + 1 } // function + +let private = 123; // variable not exported - invisible to outside +let x = 42; // this will be exported below + +export x; // the variable 'x' is exported under its own name + +export x as answer; // the variable 'x' is exported under the alias 'answer' + // another script can load this module and access 'x' as 'module::answer' +``` ### Importing modules diff --git a/src/engine.rs b/src/engine.rs index 0029684d..d026ecb0 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1622,7 +1622,10 @@ impl Engine { .try_cast::() { if let Some(resolver) = self.module_resolver.as_ref() { - let module = resolver.resolve(self, &path, expr.position())?; + // Use an empty scope to create a module + let mut mod_scope = Scope::new(); + let module = + resolver.resolve(self, mod_scope, &path, expr.position())?; // TODO - avoid copying module name in inner block? let mod_name = name.as_ref().clone(); @@ -1639,6 +1642,36 @@ impl Engine { } } } + + // Export statement + Stmt::Export(list) => { + for (id, id_pos, rename) in list { + let mut found = false; + + // Mark scope variables as public + match scope.get_index(id) { + Some((index, ScopeEntryType::Normal)) + | Some((index, ScopeEntryType::Constant)) => { + let alias = rename + .as_ref() + .map(|(n, _)| n.clone()) + .unwrap_or_else(|| id.clone()); + scope.set_entry_alias(index, alias); + found = true; + } + Some((_, ScopeEntryType::Module)) => unreachable!(), + _ => (), + } + + if !found { + return Err(Box::new(EvalAltResult::ErrorVariableNotFound( + id.into(), + *id_pos, + ))); + } + } + Ok(Default::default()) + } } } diff --git a/src/error.rs b/src/error.rs index eed98271..ee3f87f7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -98,6 +98,14 @@ pub enum ParseErrorType { /// /// Never appears under the `no_function` feature. FnMissingBody(String), + /// An export statement has duplicated names. + /// + /// Never appears under the `no_module` feature. + DuplicatedExport(String), + /// Export statement not at global level. + /// + /// Never appears under the `no_module` feature. + WrongExport, /// Assignment to a copy of a value. AssignmentToCopy, /// Assignment to an a constant variable. @@ -147,6 +155,8 @@ impl ParseError { ParseErrorType::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration", ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration", ParseErrorType::WrongFnDefinition => "Function definitions must be at global level and cannot be inside a block or another function", + 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::LoopBreak => "Break statement should only be used inside a loop" @@ -193,6 +203,12 @@ impl fmt::Display for ParseError { write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)? } + ParseErrorType::DuplicatedExport(s) => write!( + f, + "Duplicated variable/function '{}' in export statement", + s + )?, + ParseErrorType::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s)?, ParseErrorType::AssignmentToConstant(s) if s.is_empty() => { diff --git a/src/module.rs b/src/module.rs index a1721835..4eac6dfa 100644 --- a/src/module.rs +++ b/src/module.rs @@ -31,6 +31,7 @@ pub trait ModuleResolver { fn resolve( &self, engine: &Engine, + scope: Scope, path: &str, pos: Position, ) -> Result>; @@ -570,17 +571,15 @@ impl Module { /// use rhai::{Engine, Module}; /// /// let engine = Engine::new(); + /// let mut scope = Scope::new(); /// let ast = engine.compile("let answer = 42;")?; - /// let module = Module::eval_ast_as_new(&ast, &engine)?; + /// let module = Module::eval_ast_as_new(scope, &ast, &engine)?; /// assert!(module.contains_var("answer")); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); /// # Ok(()) /// # } /// ``` - pub fn eval_ast_as_new(ast: &AST, engine: &Engine) -> FuncReturn { - // Use new scope - let mut scope = Scope::new(); - + 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)?; @@ -589,13 +588,19 @@ impl Module { scope.into_iter().for_each( |ScopeEntry { - name, typ, value, .. + name, + typ, + value, + alias, + .. }| { match typ { - // Variables left in the scope become module variables - ScopeEntryType::Normal | ScopeEntryType::Constant => { - module.variables.insert(name.into_owned(), value); + // Variables with an alias left in the scope become module variables + ScopeEntryType::Normal | ScopeEntryType::Constant if alias.is_some() => { + module.variables.insert(*alias.unwrap(), value); } + // Variables with no alias are private and not exported + ScopeEntryType::Normal | ScopeEntryType::Constant => (), // Modules left in the scope become sub-modules ScopeEntryType::Module => { module @@ -788,9 +793,10 @@ mod file { pub fn create_module>( &self, engine: &Engine, + scope: Scope, path: &str, ) -> Result> { - self.resolve(engine, path, Default::default()) + self.resolve(engine, scope, path, Default::default()) } } @@ -798,6 +804,7 @@ mod file { fn resolve( &self, engine: &Engine, + scope: Scope, path: &str, pos: Position, ) -> Result> { @@ -811,7 +818,7 @@ mod file { .compile_file(file_path) .map_err(|err| EvalAltResult::set_position(err, pos))?; - Module::eval_ast_as_new(&ast, engine) + Module::eval_ast_as_new(scope, &ast, engine) .map_err(|err| EvalAltResult::set_position(err, pos)) } } @@ -938,6 +945,7 @@ mod stat { fn resolve( &self, _: &Engine, + _: Scope, path: &str, pos: Position, ) -> Result> { diff --git a/src/parser.rs b/src/parser.rs index 2499cdca..ad93943c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -284,6 +284,8 @@ pub enum Stmt { ReturnWithVal(Option>, ReturnType, Position), /// import expr as module Import(Box, Box, Position), + /// expr id as name, ... + Export(Vec<(String, Position, Option<(String, Position)>)>), } impl Stmt { @@ -302,6 +304,8 @@ impl Stmt { Stmt::IfThenElse(expr, _, _) | Stmt::Expr(expr) => expr.position(), Stmt::While(_, stmt) | Stmt::Loop(stmt) | Stmt::For(_, _, stmt) => stmt.position(), + + Stmt::Export(list) => list.get(0).unwrap().1, } } @@ -320,6 +324,7 @@ impl Stmt { Stmt::Let(_, _, _) | Stmt::Const(_, _, _) | Stmt::Import(_, _, _) + | Stmt::Export(_) | Stmt::Expr(_) | Stmt::Continue(_) | Stmt::Break(_) @@ -344,6 +349,7 @@ impl Stmt { Stmt::Block(statements, _) => statements.iter().all(Stmt::is_pure), Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_, _, _) => false, Stmt::Import(_, _, _) => false, + Stmt::Export(_) => false, } } } @@ -1970,6 +1976,63 @@ fn parse_import<'a>( Ok(Stmt::Import(Box::new(expr), Box::new(name), pos)) } +/// Parse an export statement. +fn parse_export<'a>(input: &mut Peekable>) -> Result> { + eat_token(input, Token::Export); + + let mut exports = Vec::new(); + + loop { + let (id, id_pos) = match input.next().unwrap() { + (Token::Identifier(s), pos) => (s.clone(), pos), + (Token::LexError(err), pos) => { + return Err(PERR::BadInput(err.to_string()).into_err(pos)) + } + (_, pos) => return Err(PERR::VariableExpected.into_err(pos)), + }; + + let rename = if match_token(input, Token::As)? { + match input.next().unwrap() { + (Token::Identifier(s), pos) => Some((s.clone(), pos)), + (_, pos) => return Err(PERR::VariableExpected.into_err(pos)), + } + } else { + None + }; + + exports.push((id, id_pos, rename)); + + match input.peek().unwrap() { + (Token::Comma, _) => { + eat_token(input, Token::Comma); + } + (Token::Identifier(_), pos) => { + return Err(PERR::MissingToken( + Token::Comma.into(), + "to separate the list of exports".into(), + ) + .into_err(*pos)) + } + _ => break, + } + } + + // Check for duplicating parameters + exports + .iter() + .enumerate() + .try_for_each(|(i, (p1, _, _))| { + exports + .iter() + .skip(i + 1) + .find(|(p2, _, _)| p2 == p1) + .map_or_else(|| Ok(()), |(p2, pos, _)| Err((p2, *pos))) + }) + .map_err(|(p, pos)| PERR::DuplicatedExport(p.to_string()).into_err(pos))?; + + Ok(Stmt::Export(exports)) +} + /// Parse a statement block. fn parse_block<'a>( input: &mut Peekable>, @@ -1995,7 +2058,7 @@ fn parse_block<'a>( while !match_token(input, Token::RightBrace)? { // Parse statements inside the block - let stmt = parse_stmt(input, stack, breakable, allow_stmt_expr)?; + let stmt = parse_stmt(input, stack, breakable, false, allow_stmt_expr)?; // See if it needs a terminating semicolon let need_semicolon = !stmt.is_self_terminated(); @@ -2053,6 +2116,7 @@ fn parse_stmt<'a>( input: &mut Peekable>, stack: &mut Stack, breakable: bool, + is_global: bool, allow_stmt_expr: bool, ) -> Result> { let (token, pos) = match input.peek().unwrap() { @@ -2113,6 +2177,12 @@ fn parse_stmt<'a>( #[cfg(not(feature = "no_module"))] Token::Import => parse_import(input, stack, 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), + _ => parse_expr_stmt(input, stack, allow_stmt_expr), } } @@ -2123,7 +2193,7 @@ fn parse_fn<'a>( stack: &mut Stack, allow_stmt_expr: bool, ) -> Result> { - let pos = input.next().expect("should be fn").1; + let pos = eat_token(input, Token::Fn); let name = match input.next().unwrap() { (Token::Identifier(s), _) => s, @@ -2258,7 +2328,7 @@ fn parse_global_level<'a>( } } // Actual statement - let stmt = parse_stmt(input, &mut stack, false, true)?; + let stmt = parse_stmt(input, &mut stack, false, true, true)?; let need_semicolon = !stmt.is_self_terminated(); diff --git a/src/scope.rs b/src/scope.rs index 04e447bd..c15c0581 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -30,6 +30,8 @@ pub struct Entry<'a> { pub typ: EntryType, /// Current value of the entry. pub value: Dynamic, + /// Alias of the entry. + pub alias: Option>, /// A constant expression if the initial value matches one of the recognized types. pub expr: Option>, } @@ -248,6 +250,7 @@ impl<'a> Scope<'a> { self.0.push(Entry { name: name.into(), typ: entry_type, + alias: None, value: value.into(), expr, }); @@ -412,16 +415,15 @@ impl<'a> Scope<'a> { /// Get a mutable reference to an entry in the Scope. pub(crate) fn get_mut(&mut self, index: usize) -> (&mut Dynamic, EntryType) { let entry = self.0.get_mut(index).expect("invalid index in Scope"); - - // assert_ne!( - // entry.typ, - // EntryType::Constant, - // "get mut of constant entry" - // ); - (&mut entry.value, entry.typ) } + /// Update the access type of an entry in the Scope. + pub(crate) fn set_entry_alias(&mut self, index: usize, alias: String) { + let entry = self.0.get_mut(index).expect("invalid index in Scope"); + entry.alias = Some(Box::new(alias)); + } + /// Get an iterator to entries in the Scope. pub(crate) fn into_iter(self) -> impl Iterator> { self.0.into_iter() @@ -439,6 +441,7 @@ impl<'a, K: Into>> iter::Extend<(K, EntryType, Dynamic)> for Scope< .extend(iter.into_iter().map(|(name, typ, value)| Entry { name: name.into(), typ, + alias: None, value: value.into(), expr: None, })); From e50fcc385f3271456729d5db518173730e434be0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 8 May 2020 22:38:56 +0800 Subject: [PATCH 14/20] Pre-calculate index for module-qualified calls. --- README.md | 2 +- src/engine.rs | 45 ++++++++++++++++++++++++++------------------- src/module.rs | 40 +++++++++++++++++++++++++--------------- src/parser.rs | 10 ++++++---- tests/modules.rs | 8 +++++--- 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index bb5f3597..5a4ebd16 100644 --- a/README.md +++ b/README.md @@ -2158,7 +2158,7 @@ let ast = engine.compile(r#" "#)?; // Convert the 'AST' into a module, using the 'Engine' to evaluate it first -let module = Module::eval_ast_as_new(&ast, &engine)?; +let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; // 'module' now can be loaded into a custom 'Scope' for future use. It contains: // - sub-module: 'extra' diff --git a/src/engine.rs b/src/engine.rs index d026ecb0..0832152b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -434,7 +434,7 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - #[cfg(not(feature = "no_module"))] mut modules: &Option>, + #[cfg(not(feature = "no_module"))] modules: &Option>, #[cfg(feature = "no_module")] _: &Option, index: Option, pos: Position, @@ -444,7 +444,7 @@ fn search_scope<'a>( if let Some(modules) = modules { let (id, root_pos) = modules.get(0); - let module = if let Some(index) = index { + let module = if let Some(index) = modules.index() { scope .get_mut(scope.len() - index.get()) .0 @@ -1369,9 +1369,17 @@ impl Engine { let (id, root_pos) = modules.get(0); // First module - let module = scope.find_module(id).ok_or_else(|| { - Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *root_pos)) - })?; + let module = if let Some(index) = modules.index() { + scope + .get_mut(scope.len() - index.get()) + .0 + .downcast_mut::() + .unwrap() + } else { + scope.find_module(id).ok_or_else(|| { + Box::new(EvalAltResult::ErrorModuleNotFound(id.into(), *root_pos)) + })? + }; // First search in script-defined functions (can override built-in) if let Some(fn_def) = module.get_qualified_scripted_fn(modules.key()) { @@ -1623,9 +1631,8 @@ impl Engine { { if let Some(resolver) = self.module_resolver.as_ref() { // Use an empty scope to create a module - let mut mod_scope = Scope::new(); let module = - resolver.resolve(self, mod_scope, &path, expr.position())?; + resolver.resolve(self, Scope::new(), &path, expr.position())?; // TODO - avoid copying module name in inner block? let mod_name = name.as_ref().clone(); @@ -1649,18 +1656,18 @@ impl Engine { let mut found = false; // Mark scope variables as public - match scope.get_index(id) { - Some((index, ScopeEntryType::Normal)) - | Some((index, ScopeEntryType::Constant)) => { - let alias = rename - .as_ref() - .map(|(n, _)| n.clone()) - .unwrap_or_else(|| id.clone()); - scope.set_entry_alias(index, alias); - found = true; - } - Some((_, ScopeEntryType::Module)) => unreachable!(), - _ => (), + if let Some(index) = scope + .get_index(id) + .map(|(i, _)| i) + .or_else(|| scope.get_module_index(id)) + { + let alias = rename + .as_ref() + .map(|(n, _)| n.clone()) + .unwrap_or_else(|| id.clone()); + + scope.set_entry_alias(index, alias); + found = true; } if !found { diff --git a/src/module.rs b/src/module.rs index 4eac6dfa..10636ee8 100644 --- a/src/module.rs +++ b/src/module.rs @@ -17,6 +17,7 @@ use crate::stdlib::{ fmt, iter::{empty, repeat}, mem, + num::NonZeroUsize, ops::{Deref, DerefMut}, rc::Rc, string::{String, ToString}, @@ -568,12 +569,11 @@ impl Module { /// /// ``` /// # fn main() -> Result<(), Box> { - /// use rhai::{Engine, Module}; + /// use rhai::{Engine, Module, Scope}; /// /// let engine = Engine::new(); - /// let mut scope = Scope::new(); - /// let ast = engine.compile("let answer = 42;")?; - /// let module = Module::eval_ast_as_new(scope, &ast, &engine)?; + /// let ast = engine.compile("let answer = 42; export answer;")?; + /// let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; /// assert!(module.contains_var("answer")); /// assert_eq!(module.get_var_value::("answer").unwrap(), 42); /// # Ok(()) @@ -599,14 +599,14 @@ impl Module { ScopeEntryType::Normal | ScopeEntryType::Constant if alias.is_some() => { module.variables.insert(*alias.unwrap(), value); } - // Variables with no alias are private and not exported - ScopeEntryType::Normal | ScopeEntryType::Constant => (), // Modules left in the scope become sub-modules - ScopeEntryType::Module => { + ScopeEntryType::Module if alias.is_some() => { module .modules - .insert(name.into_owned(), value.cast::()); + .insert(*alias.unwrap(), value.cast::()); } + // Variables and modules with no alias are private and not exported + _ => (), } }, ); @@ -830,14 +830,18 @@ 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)] -pub struct ModuleRef(StaticVec<(String, Position)>, u64); +pub struct ModuleRef(StaticVec<(String, Position)>, Option, u64); impl fmt::Debug for ModuleRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f)?; - if self.1 > 0 { - write!(f, " -> {:0>16x}", self.1) + if self.2 > 0 { + if let Some(index) = self.1 { + write!(f, " -> {},{:0>16x}", index, self.2) + } else { + write!(f, " -> {:0>16x}", self.2) + } } else { Ok(()) } @@ -869,16 +873,22 @@ impl fmt::Display for ModuleRef { impl From> for ModuleRef { fn from(modules: StaticVec<(String, Position)>) -> Self { - Self(modules, 0) + Self(modules, None, 0) } } impl ModuleRef { - pub fn key(&self) -> u64 { + pub(crate) fn key(&self) -> u64 { + self.2 + } + pub(crate) fn set_key(&mut self, key: u64) { + self.2 = key + } + pub(crate) fn index(&self) -> Option { self.1 } - pub fn set_key(&mut self, key: u64) { - self.1 = key + pub(crate) fn set_index(&mut self, index: Option) { + self.1 = index } } diff --git a/src/parser.rs b/src/parser.rs index ad93943c..274854b4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -719,6 +719,7 @@ fn parse_call_expr<'a>( // Calculate hash let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); modules.set_key(hash); + modules.set_index(stack.find_module(&modules.get(0).0)); } } return Ok(Expr::FnCall( @@ -751,6 +752,7 @@ fn parse_call_expr<'a>( repeat(EMPTY_TYPE_ID()).take(args.len()), ); modules.set_key(hash); + modules.set_index(stack.find_module(&modules.get(0).0)); } } return Ok(Expr::FnCall( @@ -1147,14 +1149,12 @@ fn parse_primary<'a>( } // module access #[cfg(not(feature = "no_module"))] - (Expr::Variable(id, mut modules, mut index, pos), Token::DoubleColon) => { + (Expr::Variable(id, mut modules, index, pos), Token::DoubleColon) => { match input.next().unwrap() { (Token::Identifier(id2), pos2) => { if let Some(ref mut modules) = modules { modules.push((*id, pos)); } else { - index = stack.find_module(id.as_ref()); - let mut m: ModuleRef = Default::default(); m.push((*id, pos)); modules = Some(Box::new(m)); @@ -1181,6 +1181,7 @@ fn parse_primary<'a>( Expr::Variable(id, Some(modules), _, _) => { let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); modules.set_key(hash); + modules.set_index(stack.find_module(&modules.get(0).0)); } _ => (), } @@ -2131,7 +2132,8 @@ fn parse_stmt<'a>( Token::LeftBrace => parse_block(input, stack, breakable, allow_stmt_expr), // fn ... - Token::Fn => Err(PERR::WrongFnDefinition.into_err(*pos)), + 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), diff --git a/tests/modules.rs b/tests/modules.rs index e7475c5c..4876e9fb 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -114,23 +114,25 @@ fn test_module_from_ast() -> Result<(), Box> { // Final variable values become constant module variable values foo = calc(foo); hello = "hello, " + foo + " worlds!"; + + export x as abc, foo, hello, extra as foobar; "#, )?; - let module = Module::eval_ast_as_new(&ast, &engine)?; + let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; let mut scope = Scope::new(); scope.push_module("testing", module); assert_eq!( - engine.eval_expression_with_scope::(&mut scope, "testing::x")?, + engine.eval_expression_with_scope::(&mut scope, "testing::abc")?, 123 ); assert_eq!( engine.eval_expression_with_scope::(&mut scope, "testing::foo")?, 42 ); - assert!(engine.eval_expression_with_scope::(&mut scope, "testing::extra::foo")?); + assert!(engine.eval_expression_with_scope::(&mut scope, "testing::foobar::foo")?); assert_eq!( engine.eval_expression_with_scope::(&mut scope, "testing::hello")?, "hello, 42 worlds!" From d1de84fdd2366005c6e34d6f00728db15a94c308 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 May 2020 10:00:59 +0800 Subject: [PATCH 15/20] Add comments and fix documentation. --- README.md | 20 ++++++++++++++------ src/engine.rs | 46 ++++++++++++++++++++++++++-------------------- src/module.rs | 42 ++++++++++++++++++++++++++---------------- src/parser.rs | 46 +++++++++++++++++++++++++++++++--------------- src/scope.rs | 2 +- tests/modules.rs | 6 +++++- 6 files changed, 103 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 5a4ebd16..4ed22d10 100644 --- a/README.md +++ b/README.md @@ -2127,7 +2127,8 @@ 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`: +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! ```rust use rhai::{Engine, Module}; @@ -2144,26 +2145,33 @@ let ast = engine.compile(r#" x + y.len() } - // Imported modules become sub-modules + // Imported modules can become sub-modules import "another module" as extra; - // Variables defined at global level become module variables + // Variables defined at global level can become module variables const x = 123; let foo = 41; let hello; - // Final variable values become constant module variable values + // Variable values become constant module variable values foo = calc(foo); hello = "hello, " + foo + " worlds!"; + + // Finally, export the variables and modules + export + x as abc, // aliased variable name + foo, + hello, + extra as foobar; // export sub-module "#)?; // Convert the 'AST' into a module, using the 'Engine' to evaluate it first let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?; // 'module' now can be loaded into a custom 'Scope' for future use. It contains: -// - sub-module: 'extra' +// - sub-module: 'foobar' (renamed from 'extra') // - functions: 'calc', 'add_len' -// - variables: 'x', 'foo', 'hello' +// - variables: 'abc' (renamed from 'x'), 'foo', 'hello' ``` ### Module resolvers diff --git a/src/engine.rs b/src/engine.rs index 0832152b..6a52dcf4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -160,21 +160,15 @@ impl<'a> State<'a> { } /// Does a certain script-defined function exist in the `State`? pub fn has_function(&self, name: &str, params: usize) -> bool { - self.fn_lib.contains_key(&calc_fn_hash( - empty(), - name, - repeat(EMPTY_TYPE_ID()).take(params), - )) + // Qualifiers (none) + function name + placeholders (one for each parameter). + let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + self.fn_lib.contains_key(&hash) } /// Get a script-defined function definition from the `State`. pub fn get_function(&self, name: &str, params: usize) -> Option<&FnDef> { - self.fn_lib - .get(&calc_fn_hash( - empty(), - name, - repeat(EMPTY_TYPE_ID()).take(params), - )) - .map(|f| f.as_ref()) + // Qualifiers (none) + function name + placeholders (one for each parameter). + let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + self.fn_lib.get(&hash).map(|f| f.as_ref()) } } @@ -207,20 +201,21 @@ impl FunctionsLib { pub fn from_vec(vec: Vec) -> Self { FunctionsLib( vec.into_iter() - .map(|f| { + .map(|fn_def| { + // Qualifiers (none) + function name + placeholders (one for each parameter). let hash = calc_fn_hash( empty(), - &f.name, - repeat(EMPTY_TYPE_ID()).take(f.params.len()), + &fn_def.name, + repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); #[cfg(feature = "sync")] { - (hash, Arc::new(f)) + (hash, Arc::new(fn_def)) } #[cfg(not(feature = "sync"))] { - (hash, Rc::new(f)) + (hash, Rc::new(fn_def)) } }) .collect(), @@ -240,6 +235,7 @@ impl FunctionsLib { } /// Get a function definition from the `FunctionsLib`. pub fn get_function_by_signature(&self, name: &str, params: usize) -> Option<&FnDef> { + // Qualifiers (none) + function name + placeholders (one for each parameter). let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); self.get_function(hash) } @@ -582,6 +578,7 @@ impl Engine { } // Search built-in's and external functions + // Qualifiers (none) + function name + argument `TypeId`'s. let fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); if let Some(func) = self @@ -731,6 +728,7 @@ impl Engine { // Has a system function an override? fn has_override(&self, state: &State, name: &str) -> bool { + // Qualifiers (none) + function name + argument `TypeId`'s. let hash = calc_fn_hash(empty(), name, once(TypeId::of::())); // First check registered functions @@ -1386,10 +1384,18 @@ impl Engine { self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions - let hash1 = modules.key(); - let hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); - match module.get_qualified_fn(fn_name, hash1 ^ hash2, *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). + let hash1 = modules.key(); + // 2) Calculate a second hash with no qualifiers, empty function name, and + // the actual list of parameter `TypeId`'.s + let hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + // 3) The final hash is the XOR of the two hashes. + let hash = hash1 ^ hash2; + + match module.get_qualified_fn(fn_name, hash, *pos) { Ok(func) => func(&mut args, *pos), Err(_) if def_val.is_some() => Ok(def_val.as_deref().unwrap().clone()), Err(err) => Err(err), diff --git a/src/module.rs b/src/module.rs index 10636ee8..ef74ec6a 100644 --- a/src/module.rs +++ b/src/module.rs @@ -618,41 +618,51 @@ impl Module { /// Scan through all the sub-modules in the `Module` build an index of all /// variables and external Rust functions via hashing. - pub(crate) fn collect_all_sub_modules(&mut self) { + pub(crate) fn index_all_sub_modules(&mut self) { // Collect a particular module. - fn collect<'a>( + fn index_module<'a>( module: &'a mut Module, - names: &mut Vec<&'a str>, + qualifiers: &mut Vec<&'a str>, variables: &mut Vec<(u64, Dynamic)>, functions: &mut Vec<(u64, NativeFunction)>, fn_lib: &mut Vec<(u64, ScriptedFunction)>, ) { - for (n, m) in module.modules.iter_mut() { - // Collect all the sub-modules first. - names.push(n); - collect(m, names, variables, functions, fn_lib); - names.pop(); + for (name, m) in module.modules.iter_mut() { + // Index all the sub-modules first. + qualifiers.push(name); + index_module(m, qualifiers, variables, functions, fn_lib); + qualifiers.pop(); } - // Collect all variables + // Index all variables for (var_name, value) in module.variables.iter() { - let hash = calc_fn_hash(names.iter().map(|v| *v), var_name, empty()); + // Qualifiers + variable name + let hash = calc_fn_hash(qualifiers.iter().map(|v| *v), var_name, empty()); variables.push((hash, value.clone())); } - // Collect all Rust functions + // Index all Rust functions for (fn_name, params, func) in module.functions.values() { + // 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 hash1 = calc_fn_hash( - names.iter().map(|v| *v), + qualifiers.iter().map(|v| *v), fn_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 hash2 = calc_fn_hash(empty(), "", params.iter().cloned()); - functions.push((hash1 ^ hash2, func.clone())); + // 3) The final hash is the XOR of the two hashes. + let hash = hash1 ^ hash2; + + functions.push((hash, func.clone())); } - // Collect all script-defined functions + // Index all script-defined functions for fn_def in module.fn_lib.values() { + // Qualifiers + function name + placeholders (one for each parameter) let hash = calc_fn_hash( - names.iter().map(|v| *v), + qualifiers.iter().map(|v| *v), &fn_def.name, repeat(EMPTY_TYPE_ID()).take(fn_def.params.len()), ); @@ -664,7 +674,7 @@ impl Module { let mut functions = Vec::new(); let mut fn_lib = Vec::new(); - collect( + index_module( self, &mut vec!["root"], &mut variables, diff --git a/src/parser.rs b/src/parser.rs index 274854b4..b3b05e1b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -716,9 +716,16 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] { if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); - modules.set_key(hash); + // 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 hash1 = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); + // 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. + + // Cache the first hash + modules.set_key(hash1); modules.set_index(stack.find_module(&modules.get(0).0)); } } @@ -745,13 +752,20 @@ fn parse_call_expr<'a>( #[cfg(not(feature = "no_module"))] { if let Some(modules) = modules.as_mut() { - // Calculate hash - let hash = calc_fn_hash( + // 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 hash1 = calc_fn_hash( modules.iter().map(|(m, _)| m.as_str()), &id, repeat(EMPTY_TYPE_ID()).take(args.len()), ); - modules.set_key(hash); + // 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. + + // Cache the first hash + modules.set_key(hash1); modules.set_index(stack.find_module(&modules.get(0).0)); } } @@ -1176,9 +1190,10 @@ fn parse_primary<'a>( } match &mut root_expr { - // Calculate hash key for module-qualified variables + // Cache the hash key for module-qualified variables #[cfg(not(feature = "no_module"))] Expr::Variable(id, Some(modules), _, _) => { + // Qualifiers + variable name let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); modules.set_key(hash); modules.set_index(stack.find_module(&modules.get(0).0)); @@ -2317,15 +2332,16 @@ fn parse_global_level<'a>( { if let (Token::Fn, _) = input.peek().unwrap() { let mut stack = Stack::new(); - let f = parse_fn(input, &mut stack, true)?; - functions.insert( - calc_fn_hash( - empty(), - &f.name, - repeat(EMPTY_TYPE_ID()).take(f.params.len()), - ), - f, + let func = parse_fn(input, &mut stack, true)?; + + // Qualifiers (none) + function name + argument `TypeId`'s + let hash = calc_fn_hash( + empty(), + &func.name, + repeat(EMPTY_TYPE_ID()).take(func.params.len()), ); + + functions.insert(hash, func); continue; } } diff --git a/src/scope.rs b/src/scope.rs index c15c0581..04e11fe6 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -178,7 +178,7 @@ impl<'a> Scope<'a> { /// Modules are used for accessing member variables, functions and plugins under a namespace. #[cfg(not(feature = "no_module"))] pub fn push_module>>(&mut self, name: K, mut value: Module) { - value.collect_all_sub_modules(); + value.index_all_sub_modules(); self.push_dynamic_value( name, diff --git a/tests/modules.rs b/tests/modules.rs index 4876e9fb..7ec17483 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -115,7 +115,11 @@ fn test_module_from_ast() -> Result<(), Box> { foo = calc(foo); hello = "hello, " + foo + " worlds!"; - export x as abc, foo, hello, extra as foobar; + export + x as abc, + foo, + hello, + extra as foobar; "#, )?; From 0d20137d6cb24fccc09d9261081731c61f821f39 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 May 2020 11:29:30 +0800 Subject: [PATCH 16/20] Implement private modifier for functions. --- README.md | 22 ++++++++-- src/api.rs | 2 +- src/engine.rs | 18 ++++++-- src/module.rs | 106 +++++++++++++++++++++++++++++++++++------------ src/parser.rs | 55 ++++++++++++++++++------ src/token.rs | 3 ++ tests/modules.rs | 26 ++++++++++-- 7 files changed, 183 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 4ed22d10..a8329aa3 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,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`. +Functions declared with `private` are hidden and cannot be called from Rust (see also [modules]). ```rust // Define functions in a script. @@ -287,6 +288,11 @@ let ast = engine.compile(true, fn hello() { 42 } + + // this one is private and cannot be called by 'call_fn' + private hidden() { + throw "you shouldn't see me!"; + } ")?; // A custom scope can also contain any variables/constants available to the functions @@ -300,11 +306,15 @@ let result: i64 = engine.call_fn(&mut scope, &ast, "hello", ( String::from("abc" // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // put arguments in a tuple -let result: i64 = engine.call_fn(&mut scope, &ast, "hello", (123_i64,) )? +let result: i64 = engine.call_fn(&mut scope, &ast, "hello", (123_i64,) )?; // ^^^^^^^^^^ tuple of one -let result: i64 = engine.call_fn(&mut scope, &ast, "hello", () )? +let result: i64 = engine.call_fn(&mut scope, &ast, "hello", () )?; // ^^ unit = tuple of zero + +// The following call will return a function-not-found error because +// 'hidden' is declared with 'private'. +let result: () = engine.call_fn(&mut scope, &ast, "hidden", ())?; ``` ### Creating Rust anonymous functions from Rhai script @@ -2052,13 +2062,17 @@ Modules can be disabled via the [`no_module`] feature. 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`. +Functions declared `private` are invisible to the outside. -All functions are automatically exported. Everything exported from a module is **constant** (**read-only**). +Everything exported from a module is **constant** (**read-only**). ```rust // This is a module script. -fn inc(x) { x + 1 } // function +fn inc(x) { x + 1 } // public function + +private fn foo() {} // private function - invisible to outside let private = 123; // variable not exported - invisible to outside let x = 42; // this will be exported below diff --git a/src/api.rs b/src/api.rs index 6f624b76..db4b00c8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -999,7 +999,7 @@ impl Engine { let pos = Position::none(); let fn_def = fn_lib - .get_function_by_signature(name, args.len()) + .get_function_by_signature(name, args.len(), true) .ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.to_string(), pos)))?; let state = State::new(fn_lib); diff --git a/src/engine.rs b/src/engine.rs index 6a52dcf4..c438602d 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -7,7 +7,7 @@ use crate::optimize::OptimizationLevel; use crate::packages::{ CorePackage, Package, PackageLibrary, PackageStore, PackagesCollection, StandardPackage, }; -use crate::parser::{Expr, FnDef, ReturnType, Stmt, AST}; +use crate::parser::{Expr, FnAccess, FnDef, ReturnType, Stmt, AST}; use crate::result::EvalAltResult; use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::token::Position; @@ -234,10 +234,22 @@ impl FunctionsLib { self.get(&hash).map(|fn_def| fn_def.as_ref()) } /// Get a function definition from the `FunctionsLib`. - pub fn get_function_by_signature(&self, name: &str, params: usize) -> Option<&FnDef> { + pub fn get_function_by_signature( + &self, + name: &str, + params: usize, + 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)); - self.get_function(hash) + let fn_def = self.get_function(hash); + + 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, + } } /// Merge another `FunctionsLib` into this `FunctionsLib`. pub fn merge(&self, other: &Self) -> Self { diff --git a/src/module.rs b/src/module.rs index ef74ec6a..32ae0874 100644 --- a/src/module.rs +++ b/src/module.rs @@ -4,7 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::calc_fn_hash; use crate::engine::{Engine, FnAny, FnCallArgs, FunctionsLib, NativeFunction, ScriptedFunction}; -use crate::parser::{FnDef, AST}; +use crate::parser::{FnAccess, FnDef, AST}; use crate::result::EvalAltResult; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope}; use crate::token::{Position, Token}; @@ -57,7 +57,7 @@ pub struct Module { all_variables: HashMap, /// External Rust functions. - functions: HashMap, NativeFunction)>, + functions: HashMap, NativeFunction)>, /// Flattened collection of all external Rust functions, including those in sub-modules. all_functions: HashMap, @@ -260,15 +260,21 @@ 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, fn_name: String, params: Vec, func: Box) -> u64 { + pub fn set_fn( + &mut self, + fn_name: String, + access: FnAccess, + params: Vec, + func: Box, + ) -> u64 { let hash = calc_fn_hash(empty(), &fn_name, params.iter().cloned()); #[cfg(not(feature = "sync"))] self.functions - .insert(hash, (fn_name, params, Rc::new(func))); + .insert(hash, (fn_name, access, params, Rc::new(func))); #[cfg(feature = "sync")] self.functions - .insert(hash, (fn_name, params, Arc::new(func))); + .insert(hash, (fn_name, access, params, Arc::new(func))); hash } @@ -283,7 +289,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_0("calc", || Ok(42_i64)); + /// let hash = module.set_fn_0("calc", || Ok(42_i64), false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_0, T: Into>( @@ -291,6 +297,7 @@ impl Module { fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, + is_private: bool, ) -> u64 { let f = move |_: &mut FnCallArgs, pos| { func() @@ -298,7 +305,12 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![]; - self.set_fn(fn_name.into(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -311,7 +323,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); + /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_1, A: Variant + Clone, T: Into>( @@ -319,6 +331,7 @@ impl Module { fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(mem::take(args[0]).cast::()) @@ -326,7 +339,12 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - self.set_fn(fn_name.into(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -339,7 +357,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }); + /// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }, false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_1_mut, A: Variant + Clone, T: Into>( @@ -347,6 +365,7 @@ impl Module { fn_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, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(args[0].downcast_mut::().unwrap()) @@ -354,7 +373,12 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - self.set_fn(fn_name.into(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -369,7 +393,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_2("calc", |x: i64, y: String| { /// Ok(x + y.len() as i64) - /// }); + /// }, false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Into>( @@ -377,6 +401,7 @@ impl Module { fn_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, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let a = mem::take(args[0]).cast::(); @@ -387,7 +412,12 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -401,7 +431,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: String| { /// *x += y.len() as i64; Ok(*x) - /// }); + /// }, false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_2_mut< @@ -414,6 +444,7 @@ impl Module { fn_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, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let b = mem::take(args[1]).cast::(); @@ -424,7 +455,12 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - self.set_fn(fn_name.into(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -439,7 +475,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64| { /// Ok(x + y.len() as i64 + z) - /// }); + /// }, false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3< @@ -453,6 +489,7 @@ impl Module { fn_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, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let a = mem::take(args[0]).cast::(); @@ -464,7 +501,12 @@ 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(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -480,7 +522,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64| { /// *x += y.len() as i64 + z; Ok(*x) - /// }); + /// }, false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3_mut< @@ -494,6 +536,7 @@ impl Module { fn_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, + is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let b = mem::take(args[1]).cast::(); @@ -505,7 +548,12 @@ 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(), arg_types, Box::new(f)) + let access = if is_private { + FnAccess::Private + } else { + FnAccess::Public + }; + self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) } /// Get a Rust function. @@ -523,7 +571,7 @@ impl Module { /// 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()) + self.functions.get(&hash).map(|(_, _, _, v)| v.as_ref()) } /// Get a modules-qualified function. @@ -588,11 +636,7 @@ impl Module { scope.into_iter().for_each( |ScopeEntry { - name, - typ, - value, - alias, - .. + typ, value, alias, .. }| { match typ { // Variables with an alias left in the scope become module variables @@ -641,7 +685,12 @@ impl Module { variables.push((hash, value.clone())); } // Index all Rust functions - for (fn_name, params, func) in module.functions.values() { + for (fn_name, access, params, func) in module.functions.values() { + match access { + // Private functions are not exported + FnAccess::Private => continue, + FnAccess::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). @@ -660,6 +709,11 @@ impl Module { } // Index all script-defined functions for fn_def in module.fn_lib.values() { + match fn_def.access { + // Private functions are not exported + FnAccess::Private => continue, + FnAccess::Public => (), + } // Qualifiers + function name + placeholders (one for each parameter) let hash = calc_fn_hash( qualifiers.iter().map(|v| *v), diff --git a/src/parser.rs b/src/parser.rs index b3b05e1b..9585b53a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -178,11 +178,22 @@ impl Add for &AST { } } -/// A script-function definition. +/// A type representing the access mode of a scripted function. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum FnAccess { + /// Private function. + Private, + /// Public function. + Public, +} + +/// A scripted function definition. #[derive(Debug, Clone)] pub struct FnDef { /// Function name. pub name: String, + /// Function access mode. + pub access: FnAccess, /// Names of function parameters. pub params: Vec, /// Function body. @@ -2208,6 +2219,7 @@ fn parse_stmt<'a>( fn parse_fn<'a>( input: &mut Peekable>, stack: &mut Stack, + access: FnAccess, allow_stmt_expr: bool, ) -> Result> { let pos = eat_token(input, Token::Fn); @@ -2283,6 +2295,7 @@ fn parse_fn<'a>( Ok(FnDef { name, + access, params, body, pos, @@ -2330,19 +2343,37 @@ fn parse_global_level<'a>( // Collect all the function definitions #[cfg(not(feature = "no_function"))] { - if let (Token::Fn, _) = input.peek().unwrap() { - let mut stack = Stack::new(); - let func = parse_fn(input, &mut stack, true)?; + let mut access = FnAccess::Public; + let mut must_be_fn = false; - // Qualifiers (none) + function name + argument `TypeId`'s - let hash = calc_fn_hash( - empty(), - &func.name, - repeat(EMPTY_TYPE_ID()).take(func.params.len()), - ); + if match_token(input, Token::Private)? { + access = FnAccess::Private; + must_be_fn = true; + } - functions.insert(hash, func); - continue; + match input.peek().unwrap() { + (Token::Fn, _) => { + let mut stack = Stack::new(); + let func = parse_fn(input, &mut stack, access, true)?; + + // Qualifiers (none) + function name + argument `TypeId`'s + let hash = calc_fn_hash( + empty(), + &func.name, + repeat(EMPTY_TYPE_ID()).take(func.params.len()), + ); + + functions.insert(hash, func); + continue; + } + (_, pos) if must_be_fn => { + return Err(PERR::MissingToken( + Token::Fn.into(), + format!("following '{}'", Token::Private.syntax()), + ) + .into_err(*pos)) + } + _ => (), } } // Actual statement diff --git a/src/token.rs b/src/token.rs index f67a8e14..9692b112 100644 --- a/src/token.rs +++ b/src/token.rs @@ -196,6 +196,7 @@ pub enum Token { XOrAssign, ModuloAssign, PowerOfAssign, + Private, Import, Export, As, @@ -279,6 +280,7 @@ impl Token { ModuloAssign => "%=", PowerOf => "~", PowerOfAssign => "~=", + Private => "private", Import => "import", Export => "export", As => "as", @@ -750,6 +752,7 @@ impl<'a> TokenIterator<'a> { "throw" => Token::Throw, "for" => Token::For, "in" => Token::In, + "private" => Token::Private, #[cfg(not(feature = "no_module"))] "import" => Token::Import, diff --git a/tests/modules.rs b/tests/modules.rs index 7ec17483..1fc4d895 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -18,7 +18,12 @@ fn test_module_sub_module() -> Result<(), Box> { let mut sub_module2 = Module::new(); sub_module2.set_var("answer", 41 as INT); - let hash = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1)); + let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1), false); + let hash_hidden = sub_module2.set_fn_0( + "hidden", + || Err("shouldn't see me!".into()) as Result<(), Box>, + true, + ); sub_module.set_sub_module("universe", sub_module2); module.set_sub_module("life", sub_module); @@ -30,11 +35,12 @@ fn test_module_sub_module() -> Result<(), Box> { let m2 = m.get_sub_module("universe").unwrap(); assert!(m2.contains_var("answer")); - assert!(m2.contains_fn(hash)); + assert!(m2.contains_fn(hash_inc)); + assert!(m2.contains_fn(hash_hidden)); assert_eq!(m2.get_var_value::("answer").unwrap(), 41); - let mut engine = Engine::new(); + let engine = Engine::new(); let mut scope = Scope::new(); scope.push_module("question", module); @@ -53,6 +59,11 @@ fn test_module_sub_module() -> Result<(), Box> { )?, 42 ); + assert!(matches!( + *engine.eval_expression_with_scope::<()>(&mut scope, "question::life::universe::hidden()") + .expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden" + )); Ok(()) } @@ -102,6 +113,9 @@ fn test_module_from_ast() -> Result<(), Box> { fn add_len(x, y) { x + y.len() } + private fn hidden() { + throw "you shouldn't see me!"; + } // Imported modules become sub-modules import "another module" as extra; @@ -152,6 +166,12 @@ fn test_module_from_ast() -> Result<(), Box> { )?, 59 ); + assert!(matches!( + *engine + .eval_expression_with_scope::<()>(&mut scope, "testing::hidden()") + .expect_err("should error"), + EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden" + )); Ok(()) } From 79f39bd70252113f518e9eedad741167c93b57f9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 May 2020 16:15:50 +0800 Subject: [PATCH 17/20] Pre-calculate function call hashes. --- src/engine.rs | 127 +++++++++++++++++++++++++++++------------------- src/module.rs | 22 +++------ src/optimize.rs | 25 ++++++---- src/parser.rs | 127 ++++++++++++++++++++++++++++++++---------------- 4 files changed, 182 insertions(+), 119 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index c438602d..5b8e379e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -159,15 +159,11 @@ impl<'a> State<'a> { } } /// Does a certain script-defined function exist in the `State`? - pub fn has_function(&self, name: &str, params: usize) -> bool { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + pub fn has_function(&self, hash: u64) -> bool { self.fn_lib.contains_key(&hash) } /// Get a script-defined function definition from the `State`. - pub fn get_function(&self, name: &str, params: usize) -> Option<&FnDef> { - // Qualifiers (none) + function name + placeholders (one for each parameter). - let hash = calc_fn_hash(empty(), name, repeat(EMPTY_TYPE_ID()).take(params)); + pub fn get_function(&self, hash: u64) -> Option<&FnDef> { self.fn_lib.get(&hash).map(|f| f.as_ref()) } } @@ -442,14 +438,14 @@ fn default_print(s: &str) { fn search_scope<'a>( scope: &'a mut Scope, name: &str, - #[cfg(not(feature = "no_module"))] modules: &Option>, - #[cfg(feature = "no_module")] _: &Option, + #[cfg(not(feature = "no_module"))] modules: Option<(&Box, u64)>, + #[cfg(feature = "no_module")] _: Option<(&ModuleRef, u64)>, index: Option, pos: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { #[cfg(not(feature = "no_module"))] { - if let Some(modules) = modules { + if let Some((modules, hash)) = modules { let (id, root_pos) = modules.get(0); let module = if let Some(index) = modules.index() { @@ -468,7 +464,7 @@ fn search_scope<'a>( }; return Ok(( - module.get_qualified_var_mut(name, modules.key(), pos)?, + module.get_qualified_var_mut(name, hash, pos)?, // Module variables are constant ScopeEntryType::Constant, )); @@ -574,6 +570,8 @@ impl Engine { scope: Option<&mut Scope>, state: &State, fn_name: &str, + hash_fn_spec: u64, + hash_fn_def: u64, args: &mut FnCallArgs, def_val: Option<&Dynamic>, pos: Position, @@ -585,18 +583,17 @@ impl Engine { } // First search in script-defined functions (can override built-in) - if let Some(fn_def) = state.get_function(fn_name, args.len()) { - return self.call_script_fn(scope, state, fn_def, args, pos, level); + 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); + } } // Search built-in's and external functions - // Qualifiers (none) + function name + argument `TypeId`'s. - let fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); - if let Some(func) = self .base_package - .get_function(fn_spec) - .or_else(|| self.packages.get_function(fn_spec)) + .get_function(hash_fn_spec) + .or_else(|| self.packages.get_function(hash_fn_spec)) { // Run external function let result = func(args, pos)?; @@ -739,16 +736,13 @@ impl Engine { } // Has a system function an override? - fn has_override(&self, state: &State, name: &str) -> bool { - // Qualifiers (none) + function name + argument `TypeId`'s. - let hash = calc_fn_hash(empty(), name, once(TypeId::of::())); - + fn has_override(&self, state: &State, hash_fn_spec: u64, hash_fn_def: u64) -> bool { // First check registered functions - self.base_package.contains_function(hash) + self.base_package.contains_function(hash_fn_spec) // Then check packages - || self.packages.contains_function(hash) + || self.packages.contains_function(hash_fn_spec) // Then check script-defined functions - || state.has_function(name, 1) + || state.has_function(hash_fn_def) } // Perform an actual function call, taking care of special functions @@ -762,26 +756,45 @@ impl Engine { &self, state: &State, fn_name: &str, + hash_fn_def: u64, args: &mut FnCallArgs, def_val: Option<&Dynamic>, pos: Position, level: usize, ) -> Result> { + // Qualifiers (none) + function name + argument `TypeId`'s. + let hash_fn_spec = calc_fn_hash(empty(), fn_name, args.iter().map(|a| a.type_id())); + match fn_name { // type_of - KEYWORD_TYPE_OF if args.len() == 1 && !self.has_override(state, KEYWORD_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()) } // eval - reaching this point it must be a method-style call - KEYWORD_EVAL if args.len() == 1 && !self.has_override(state, KEYWORD_EVAL) => { + KEYWORD_EVAL + if args.len() == 1 && !self.has_override(state, hash_fn_spec, hash_fn_def) => + { Err(Box::new(EvalAltResult::ErrorRuntime( "'eval' should not be called in method style. Try eval(...);".into(), pos, ))) } + // Normal method call - _ => self.call_fn_raw(None, state, fn_name, args, def_val, pos, level), + _ => self.call_fn_raw( + None, + state, + fn_name, + hash_fn_spec, + hash_fn_def, + args, + def_val, + pos, + level, + ), } } @@ -869,17 +882,17 @@ impl Engine { } else { match rhs { // xxx.fn_name(arg_expr_list) - Expr::FnCall(fn_name, None,_, def_val, pos) => { + Expr::FnCall(fn_name, None, hash, _, def_val, pos) => { let mut args: Vec<_> = once(obj) .chain(idx_val.downcast_mut::>().unwrap().iter_mut()) .collect(); let def_val = def_val.as_deref(); // 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, fn_name, &mut args, def_val, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, fn_name, *hash, &mut args, def_val, *pos, 0).map(|v| (v, true)) } // xxx.module::fn_name(...) - syntax error - Expr::FnCall(_,_,_,_,_) => unreachable!(), + Expr::FnCall(_, _, _, _, _, _) => unreachable!(), // {xxx:map}.id = ??? #[cfg(not(feature = "no_object"))] Expr::Property(id, pos) if obj.is::() && new_val.is_some() => { @@ -899,13 +912,13 @@ impl Engine { Expr::Property(id, pos) if new_val.is_some() => { let fn_name = make_setter(id); let mut args = [obj, new_val.as_mut().unwrap()]; - self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, &fn_name, 0, &mut args, None, *pos, 0).map(|v| (v, true)) } // xxx.id Expr::Property(id, pos) => { let fn_name = make_getter(id); let mut args = [obj]; - self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 0).map(|v| (v, false)) + self.exec_fn_call(state, &fn_name, 0, &mut args, None, *pos, 0).map(|v| (v, false)) } #[cfg(not(feature = "no_object"))] // {xxx:map}.idx_lhs[idx_expr] @@ -936,7 +949,7 @@ impl Engine { let indexed_val = &mut (if let Expr::Property(id, pos) = dot_lhs.as_ref() { let fn_name = make_getter(id); - self.exec_fn_call(state, &fn_name, &mut args[..1], None, *pos, 0)? + self.exec_fn_call(state, &fn_name, 0, &mut args[..1], None, *pos, 0)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -954,7 +967,7 @@ impl Engine { let fn_name = make_setter(id); // Re-use args because the first &mut parameter will not be consumed args[1] = indexed_val; - self.exec_fn_call(state, &fn_name, &mut args, None, *pos, 0).or_else(|err| match *err { + self.exec_fn_call(state, &fn_name, 0, &mut args, None, *pos, 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()), err => Err(Box::new(err)) @@ -991,9 +1004,10 @@ impl Engine { match dot_lhs { // id.??? or id[???] - Expr::Variable(id, modules, index, pos) => { + Expr::Variable(id, modules, hash, index, pos) => { let index = if state.always_search { None } else { *index }; - let (target, typ) = search_scope(scope, id, modules, index, *pos)?; + let (target, typ) = + search_scope(scope, id, modules.as_ref().map(|m| (m, *hash)), index, *pos)?; // Constants cannot be modified match typ { @@ -1046,7 +1060,7 @@ impl Engine { level: usize, ) -> Result<(), Box> { match expr { - Expr::FnCall(_, None, arg_exprs, _, _) => { + Expr::FnCall(_, None, _, arg_exprs, _, _) => { let mut arg_values = StaticVec::::new(); for arg_expr in arg_exprs.iter() { @@ -1055,7 +1069,7 @@ impl Engine { idx_values.push(Dynamic::from(arg_values)); } - Expr::FnCall(_, _, _, _, _) => unreachable!(), + Expr::FnCall(_, _, _, _, _, _) => unreachable!(), Expr::Property(_, _) => idx_values.push(()), // Store a placeholder - no need to copy the property name Expr::Index(lhs, rhs, _) | Expr::Dot(lhs, rhs, _) => { // Evaluate in left-to-right order @@ -1158,7 +1172,7 @@ impl Engine { _ => { let args = &mut [val, &mut idx]; - self.exec_fn_call(state, FUNC_INDEXER, args, None, op_pos, 0) + self.exec_fn_call(state, FUNC_INDEXER, 0, args, None, op_pos, 0) .map(|v| v.into()) .map_err(|_| { Box::new(EvalAltResult::ErrorIndexingType( @@ -1196,9 +1210,16 @@ impl Engine { let args = &mut [&mut lhs_value.clone(), &mut value.clone()]; let def_value = Some(&def_value); let pos = rhs.position(); + let op = "=="; + + // Qualifiers (none) + function name + argument `TypeId`'s. + let fn_spec = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); + let fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); if self - .call_fn_raw(None, state, "==", args, def_value, pos, level)? + .call_fn_raw( + None, state, op, fn_spec, fn_def, args, def_value, pos, level, + )? .as_bool() .unwrap_or(false) { @@ -1239,9 +1260,10 @@ impl Engine { Expr::FloatConstant(f, _) => Ok((*f).into()), Expr::StringConstant(s, _) => Ok(s.to_string().into()), Expr::CharConstant(c, _) => Ok((*c).into()), - Expr::Variable(id, modules, index, pos) => { + Expr::Variable(id, modules, hash, index, pos) => { let index = if state.always_search { None } else { *index }; - let (val, _) = search_scope(scope, id, modules, index, *pos)?; + let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); + let (val, _) = search_scope(scope, id, mod_and_hash, index, *pos)?; Ok(val.clone()) } Expr::Property(_, _) => unreachable!(), @@ -1255,9 +1277,10 @@ impl Engine { match lhs.as_ref() { // name = rhs - Expr::Variable(id, modules, index, pos) => { + Expr::Variable(id, modules, hash, index, pos) => { let index = if state.always_search { None } else { *index }; - let (value_ptr, typ) = search_scope(scope, id, modules, index, *pos)?; + let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); + let (value_ptr, typ) = search_scope(scope, id, mod_and_hash, index, *pos)?; match typ { ScopeEntryType::Constant => Err(Box::new( EvalAltResult::ErrorAssignmentToConstant(id.to_string(), *pos), @@ -1332,7 +1355,7 @@ impl Engine { )))), // Normal function call - Expr::FnCall(fn_name, None, arg_exprs, def_val, pos) => { + Expr::FnCall(fn_name, None, hash, arg_exprs, def_val, pos) => { let mut arg_values = arg_exprs .iter() .map(|expr| self.eval_expr(scope, state, expr, level)) @@ -1340,9 +1363,12 @@ impl Engine { let mut args: Vec<_> = arg_values.iter_mut().collect(); + let hash_fn_spec = + calc_fn_hash(empty(), KEYWORD_EVAL, once(TypeId::of::())); + if fn_name.as_ref() == KEYWORD_EVAL && args.len() == 1 - && !self.has_override(state, KEYWORD_EVAL) + && !self.has_override(state, hash_fn_spec, *hash) { // eval - only in function call style let prev_len = scope.len(); @@ -1361,13 +1387,13 @@ impl Engine { } else { // Normal function call - except for eval (handled above) let def_value = def_val.as_deref(); - self.exec_fn_call(state, fn_name, &mut args, def_value, *pos, level) + self.exec_fn_call(state, fn_name, *hash, &mut args, def_value, *pos, level) } } // Module-qualified function call #[cfg(not(feature = "no_module"))] - Expr::FnCall(fn_name, Some(modules), arg_exprs, def_val, pos) => { + Expr::FnCall(fn_name, Some(modules), hash1, arg_exprs, def_val, pos) => { let modules = modules.as_ref(); let mut arg_values = arg_exprs @@ -1392,7 +1418,7 @@ impl Engine { }; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_scripted_fn(modules.key()) { + if let Some(fn_def) = module.get_qualified_scripted_fn(*hash1) { self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions @@ -1400,12 +1426,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). - let hash1 = modules.key(); // 2) Calculate a second hash with no qualifiers, empty function name, and // the actual list of parameter `TypeId`'.s let hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); // 3) The final hash is the XOR of the two hashes. - let hash = hash1 ^ hash2; + let hash = *hash1 ^ hash2; match module.get_qualified_fn(fn_name, hash, *pos) { Ok(func) => func(&mut args, *pos), diff --git a/src/module.rs b/src/module.rs index 32ae0874..75c2b126 100644 --- a/src/module.rs +++ b/src/module.rs @@ -250,7 +250,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_0("calc", || Ok(42_i64)); + /// let hash = module.set_fn_0("calc", || Ok(42_i64), false); /// assert!(module.contains_fn(hash)); /// ``` pub fn contains_fn(&self, hash: u64) -> bool { @@ -567,7 +567,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); + /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn get_fn(&self, hash: u64) -> Option<&Box> { @@ -894,18 +894,14 @@ 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)] -pub struct ModuleRef(StaticVec<(String, Position)>, Option, u64); +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 self.2 > 0 { - if let Some(index) = self.1 { - write!(f, " -> {},{:0>16x}", index, self.2) - } else { - write!(f, " -> {:0>16x}", self.2) - } + if let Some(index) = self.1 { + write!(f, " -> {}", index) } else { Ok(()) } @@ -937,17 +933,11 @@ impl fmt::Display for ModuleRef { impl From> for ModuleRef { fn from(modules: StaticVec<(String, Position)>) -> Self { - Self(modules, None, 0) + Self(modules, None) } } impl ModuleRef { - pub(crate) fn key(&self) -> u64 { - self.2 - } - pub(crate) fn set_key(&mut self, key: u64) { - self.2 = key - } pub(crate) fn index(&self) -> Option { self.1 } diff --git a/src/optimize.rs b/src/optimize.rs index 2c4e183a..8a1ad745 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -367,12 +367,17 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { //id = id2 = expr2 Expr::Assignment(id2, expr2, pos2) => match (*id, *id2) { // var = var = expr2 -> var = expr2 - (Expr::Variable(var, None, sp, _), Expr::Variable(var2, None, sp2, _)) + (Expr::Variable(var, None, index, sp, _), Expr::Variable(var2, None, _, sp2, _)) if var == var2 && sp == sp2 => { // Assignment to the same variable - fold state.set_dirty(); - Expr::Assignment(Box::new(Expr::Variable(var, None, sp, pos)), Box::new(optimize_expr(*expr2, state)), pos) + + Expr::Assignment( + Box::new(Expr::Variable(var, None, index, sp, pos)), + Box::new(optimize_expr(*expr2, state)) + , pos + ) } // id1 = id2 = expr2 (id1, id2) => Expr::Assignment( @@ -544,18 +549,18 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }, // Do not call some special keywords - Expr::FnCall(id, None, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref().as_ref())=> - Expr::FnCall(id, None, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), + Expr::FnCall(id, None, index, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref().as_ref())=> + Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), // Eagerly call functions - Expr::FnCall(id, None, args, def_value, pos) + Expr::FnCall(id, None, index, args, def_value, pos) if state.optimization_level == OptimizationLevel::Full // full optimizations && args.iter().all(|expr| expr.is_constant()) // all arguments are constants => { // First search in script-defined functions (can override built-in) if state.fn_lib.iter().find(|(name, len)| name == id.as_ref() && *len == args.len()).is_some() { // A script-defined function overrides the built-in function - do not make the call - return Expr::FnCall(id, None, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos); + return Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos); } let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect(); @@ -586,16 +591,16 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { }) ).unwrap_or_else(|| // Optimize function call arguments - Expr::FnCall(id, None, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos) + Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos) ) } // id(args ..) -> optimize function call arguments - Expr::FnCall(id, modules, args, def_value, pos) => - Expr::FnCall(id, modules, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), + Expr::FnCall(id, modules, index, args, def_value, pos) => + Expr::FnCall(id, modules, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), // constant-name - Expr::Variable(name, None, _, pos) if state.contains_constant(&name) => { + Expr::Variable(name, None, _, _, pos) if state.contains_constant(&name) => { state.set_dirty(); // Replace constant with value diff --git a/src/parser.rs b/src/parser.rs index 9585b53a..fbca086e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -382,6 +382,7 @@ pub enum Expr { Box, #[cfg(not(feature = "no_module"))] Option>, #[cfg(feature = "no_module")] Option, + u64, Option, Position, ), @@ -389,13 +390,14 @@ pub enum Expr { Property(String, Position), /// { stmt } Stmt(Box, Position), - /// func(expr, ... ) - (function name, optional modules, arguments, optional default value, position) + /// func(expr, ... ) - (function name, optional modules, hash, arguments, optional default value, position) /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box>, #[cfg(not(feature = "no_module"))] Option>, #[cfg(feature = "no_module")] Option, + u64, Box>, Option>, Position, @@ -499,10 +501,10 @@ impl Expr { | Self::StringConstant(_, pos) | Self::Array(_, pos) | Self::Map(_, pos) - | Self::Variable(_, _, _, pos) + | Self::Variable(_, _, _, _, pos) | Self::Property(_, pos) | Self::Stmt(_, pos) - | Self::FnCall(_, _, _, _, pos) + | Self::FnCall(_, _, _, _, _, pos) | Self::And(_, _, pos) | Self::Or(_, _, pos) | Self::In(_, _, pos) @@ -527,10 +529,10 @@ impl Expr { | Self::StringConstant(_, pos) | Self::Array(_, pos) | Self::Map(_, pos) - | Self::Variable(_, _, _, pos) + | Self::Variable(_, _, _, _, pos) | Self::Property(_, pos) | Self::Stmt(_, pos) - | Self::FnCall(_, _, _, _, pos) + | Self::FnCall(_, _, _, _, _, pos) | Self::And(_, _, pos) | Self::Or(_, _, pos) | Self::In(_, _, pos) @@ -558,7 +560,7 @@ impl Expr { Self::Stmt(stmt, _) => stmt.is_pure(), - Self::Variable(_, _, _, _) => true, + Self::Variable(_, _, _, _, _) => true, expr => expr.is_constant(), } @@ -611,7 +613,7 @@ impl Expr { Self::StringConstant(_, _) | Self::Stmt(_, _) - | Self::FnCall(_, _, _, _, _) + | Self::FnCall(_, _, _, _, _, _) | Self::Assignment(_, _, _) | Self::Dot(_, _, _) | Self::Index(_, _, _) @@ -621,7 +623,7 @@ impl Expr { _ => false, }, - Self::Variable(_, _, _, _) => match token { + Self::Variable(_, _, _, _, _) => match token { Token::LeftBracket | Token::LeftParen => true, #[cfg(not(feature = "no_module"))] Token::DoubleColon => true, @@ -638,7 +640,7 @@ impl Expr { /// Convert a `Variable` into a `Property`. All other variants are untouched. pub(crate) fn into_property(self) -> Self { match self { - Self::Variable(id, None, _, pos) => Self::Property(*id, pos), + Self::Variable(id, None, _, _, pos) => Self::Property(*id, pos), _ => self, } } @@ -725,24 +727,31 @@ fn parse_call_expr<'a>( eat_token(input, Token::RightParen); #[cfg(not(feature = "no_module"))] - { + let hash1 = { if let Some(modules) = modules.as_mut() { + 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, - // i.e. qualifiers + function name + dummy parameter types (one for each parameter). + // i.e. qualifiers + function name + no parameters. let hash1 = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); // 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. - // Cache the first hash - modules.set_key(hash1); - modules.set_index(stack.find_module(&modules.get(0).0)); + hash1 + } else { + calc_fn_hash(empty(), &id, empty()) } - } + }; + // Qualifiers (none) + function name + no parameters. + #[cfg(feature = "no_module")] + let hash1 = calc_fn_hash(empty(), &id, empty()); + return Ok(Expr::FnCall( Box::new(id.into()), modules, + hash1, Box::new(args), None, begin, @@ -761,8 +770,10 @@ fn parse_call_expr<'a>( eat_token(input, Token::RightParen); #[cfg(not(feature = "no_module"))] - { + let hash1 = { if let Some(modules) = modules.as_mut() { + 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, // i.e. qualifiers + function name + dummy parameter types (one for each parameter). @@ -775,14 +786,19 @@ fn parse_call_expr<'a>( // the actual list of parameter `TypeId`'.s // 3) The final hash is the XOR of the two hashes. - // Cache the first hash - modules.set_key(hash1); - modules.set_index(stack.find_module(&modules.get(0).0)); + hash1 + } else { + calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())) } - } + }; + // Qualifiers (none) + function name + dummy parameter types (one for each parameter). + #[cfg(feature = "no_module")] + let hash1 = calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())); + return Ok(Expr::FnCall( Box::new(id.into()), modules, + hash1, Box::new(args), None, begin, @@ -1139,7 +1155,7 @@ fn parse_primary<'a>( Token::StringConst(s) => Expr::StringConstant(s, pos), Token::Identifier(s) => { let index = stack.find(&s); - Expr::Variable(Box::new(s), None, index, pos) + Expr::Variable(Box::new(s), None, 0, index, pos) } Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, #[cfg(not(feature = "no_index"))] @@ -1166,7 +1182,7 @@ fn parse_primary<'a>( root_expr = match (root_expr, token) { // Function call - (Expr::Variable(id, modules, _, pos), Token::LeftParen) => { + (Expr::Variable(id, modules, _, _, pos), Token::LeftParen) => { parse_call_expr(input, stack, *id, modules, pos, allow_stmt_expr)? } (Expr::Property(id, pos), Token::LeftParen) => { @@ -1174,7 +1190,7 @@ fn parse_primary<'a>( } // module access #[cfg(not(feature = "no_module"))] - (Expr::Variable(id, mut modules, index, pos), Token::DoubleColon) => { + (Expr::Variable(id, mut modules, _, index, pos), Token::DoubleColon) => { match input.next().unwrap() { (Token::Identifier(id2), pos2) => { if let Some(ref mut modules) = modules { @@ -1185,7 +1201,7 @@ fn parse_primary<'a>( modules = Some(Box::new(m)); } - Expr::Variable(Box::new(id2), modules, index, pos2) + Expr::Variable(Box::new(id2), modules, 0, index, pos2) } (_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)), } @@ -1203,10 +1219,9 @@ fn parse_primary<'a>( match &mut root_expr { // Cache the hash key for module-qualified variables #[cfg(not(feature = "no_module"))] - Expr::Variable(id, Some(modules), _, _) => { + Expr::Variable(id, Some(modules), hash, _, _) => { // Qualifiers + variable name - let hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); - modules.set_key(hash); + *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); modules.set_index(stack.find_module(&modules.get(0).0)); } _ => (), @@ -1259,13 +1274,19 @@ fn parse_unary<'a>( Expr::FloatConstant(f, pos) => Ok(Expr::FloatConstant(-f, pos)), // Call negative function - e => Ok(Expr::FnCall( - Box::new("-".into()), - None, - Box::new(vec![e]), - None, - pos, - )), + e => { + let op = "-"; + let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + + Ok(Expr::FnCall( + Box::new(op.into()), + None, + hash, + Box::new(vec![e]), + None, + pos, + )) + } } } // +expr @@ -1276,9 +1297,13 @@ fn parse_unary<'a>( // !expr (Token::Bang, _) => { let pos = eat_token(input, Token::Bang); + let op = "!"; + let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); + Ok(Expr::FnCall( - Box::new("!".into()), + Box::new(op.into()), None, + hash, Box::new(vec![parse_primary(input, stack, allow_stmt_expr)?]), Some(Box::new(false.into())), // NOT operator, when operating on invalid operand, defaults to false pos, @@ -1298,8 +1323,8 @@ fn make_assignment_stmt<'a>( pos: Position, ) -> Result> { match &lhs { - Expr::Variable(_, _, None, _) => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), - Expr::Variable(name, _, Some(index), var_pos) => { + Expr::Variable(_, _, _, None, _) => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + Expr::Variable(name, _, _, Some(index), var_pos) => { match stack[(stack.len() - index.get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), // Constant values cannot be assigned to @@ -1310,10 +1335,10 @@ fn make_assignment_stmt<'a>( } } Expr::Index(idx_lhs, _, _) | Expr::Dot(idx_lhs, _, _) => match idx_lhs.as_ref() { - Expr::Variable(_, _, None, _) => { + Expr::Variable(_, _, _, None, _) => { Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) } - Expr::Variable(name, _, Some(index), var_pos) => { + Expr::Variable(name, _, _, Some(index), var_pos) => { match stack[(stack.len() - index.get())].1 { ScopeEntryType::Normal => { Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) @@ -1368,7 +1393,8 @@ fn parse_op_assignment_stmt<'a>( // lhs op= rhs -> lhs = op(lhs, rhs) let args = vec![lhs_copy, rhs]; - let rhs_expr = Expr::FnCall(Box::new(op.into()), None, Box::new(args), None, pos); + let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); + let rhs_expr = Expr::FnCall(Box::new(op.into()), None, hash, Box::new(args), None, pos); make_assignment_stmt(stack, lhs, rhs_expr, pos) } @@ -1388,12 +1414,12 @@ fn make_dot_expr( idx_pos, ), // lhs.id - (lhs, rhs @ Expr::Variable(_, None, _, _)) | (lhs, rhs @ Expr::Property(_, _)) => { + (lhs, rhs @ Expr::Variable(_, None, _, _, _)) | (lhs, rhs @ Expr::Property(_, _)) => { let lhs = if is_index { lhs.into_property() } else { lhs }; Expr::Dot(Box::new(lhs), Box::new(rhs.into_property()), op_pos) } // lhs.module::id - syntax error - (_, Expr::Variable(_, Some(modules), _, _)) => { + (_, Expr::Variable(_, Some(modules), _, _, _)) => { #[cfg(feature = "no_module")] unreachable!(); #[cfg(not(feature = "no_module"))] @@ -1609,6 +1635,7 @@ fn parse_binary_op<'a>( Token::Plus => Expr::FnCall( Box::new("+".into()), None, + calc_fn_hash(empty(), "+", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1616,6 +1643,7 @@ fn parse_binary_op<'a>( Token::Minus => Expr::FnCall( Box::new("-".into()), None, + calc_fn_hash(empty(), "-", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1623,6 +1651,7 @@ fn parse_binary_op<'a>( Token::Multiply => Expr::FnCall( Box::new("*".into()), None, + calc_fn_hash(empty(), "*", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1630,6 +1659,7 @@ fn parse_binary_op<'a>( Token::Divide => Expr::FnCall( Box::new("/".into()), None, + calc_fn_hash(empty(), "/", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1638,6 +1668,7 @@ fn parse_binary_op<'a>( Token::LeftShift => Expr::FnCall( Box::new("<<".into()), None, + calc_fn_hash(empty(), "<<", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1645,6 +1676,7 @@ fn parse_binary_op<'a>( Token::RightShift => Expr::FnCall( Box::new(">>".into()), None, + calc_fn_hash(empty(), ">>", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1652,6 +1684,7 @@ fn parse_binary_op<'a>( Token::Modulo => Expr::FnCall( Box::new("%".into()), None, + calc_fn_hash(empty(), "%", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1659,6 +1692,7 @@ fn parse_binary_op<'a>( Token::PowerOf => Expr::FnCall( Box::new("~".into()), None, + calc_fn_hash(empty(), "~", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1668,6 +1702,7 @@ fn parse_binary_op<'a>( Token::EqualsTo => Expr::FnCall( Box::new("==".into()), None, + calc_fn_hash(empty(), "==", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1675,6 +1710,7 @@ fn parse_binary_op<'a>( Token::NotEqualsTo => Expr::FnCall( Box::new("!=".into()), None, + calc_fn_hash(empty(), "!=", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1682,6 +1718,7 @@ fn parse_binary_op<'a>( Token::LessThan => Expr::FnCall( Box::new("<".into()), None, + calc_fn_hash(empty(), "<", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1689,6 +1726,7 @@ fn parse_binary_op<'a>( Token::LessThanEqualsTo => Expr::FnCall( Box::new("<=".into()), None, + calc_fn_hash(empty(), "<=", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1696,6 +1734,7 @@ fn parse_binary_op<'a>( Token::GreaterThan => Expr::FnCall( Box::new(">".into()), None, + calc_fn_hash(empty(), ">", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1703,6 +1742,7 @@ fn parse_binary_op<'a>( Token::GreaterThanEqualsTo => Expr::FnCall( Box::new(">=".into()), None, + calc_fn_hash(empty(), ">=", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), cmp_default, pos, @@ -1713,6 +1753,7 @@ fn parse_binary_op<'a>( Token::Ampersand => Expr::FnCall( Box::new("&".into()), None, + calc_fn_hash(empty(), "&", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1720,6 +1761,7 @@ fn parse_binary_op<'a>( Token::Pipe => Expr::FnCall( Box::new("|".into()), None, + calc_fn_hash(empty(), "|", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, @@ -1727,6 +1769,7 @@ fn parse_binary_op<'a>( Token::XOr => Expr::FnCall( Box::new("^".into()), None, + calc_fn_hash(empty(), "^", repeat(EMPTY_TYPE_ID()).take(2)), Box::new(vec![current_lhs, rhs]), None, pos, From 17e4adc049dd48df6e9c447409881c5c5daa037e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 May 2020 16:21:11 +0800 Subject: [PATCH 18/20] Move hash calculation out of loop. --- src/engine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 5b8e379e..3efc0067 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1200,7 +1200,9 @@ impl Engine { match rhs_value { #[cfg(not(feature = "no_index"))] Dynamic(Union::Array(rhs_value)) => { + let op = "=="; let def_value = false.into(); + let 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() { @@ -1210,11 +1212,9 @@ impl Engine { let args = &mut [&mut lhs_value.clone(), &mut value.clone()]; let def_value = Some(&def_value); let pos = rhs.position(); - let op = "=="; // Qualifiers (none) + function name + argument `TypeId`'s. let fn_spec = calc_fn_hash(empty(), op, args.iter().map(|a| a.type_id())); - let fn_def = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); if self .call_fn_raw( From a7bfac21bd748905c8a4e9af123c2180bfe77737 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 9 May 2020 21:46:38 +0800 Subject: [PATCH 19/20] Reducing boxing and sizes of Expr/Stmt. --- src/engine.rs | 408 ++++++++++--------- src/optimize.rs | 327 ++++++++-------- src/parser.rs | 1001 ++++++++++++++++++++++------------------------- src/scope.rs | 2 +- src/token.rs | 4 +- 5 files changed, 851 insertions(+), 891 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 3efc0067..110b29ee 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -857,78 +857,94 @@ impl Engine { if is_index { match rhs { - // xxx[idx].dot_rhs... - Expr::Dot(idx, idx_rhs, pos) | - // xxx[idx][dot_rhs]... - Expr::Index(idx, idx_rhs, pos) => { - let is_index = matches!(rhs, Expr::Index(_,_,_)); + // xxx[idx].dot_rhs... | xxx[idx][dot_rhs]... + Expr::Dot(x) | Expr::Index(x) => { + let is_index = matches!(rhs, Expr::Index(_)); - let indexed_val = self.get_indexed_mut(state, obj, idx_val, idx.position(), op_pos, false)?; + let indexed_val = + self.get_indexed_mut(state, obj, idx_val, x.0.position(), op_pos, false)?; self.eval_dot_index_chain_helper( - state, indexed_val, idx_rhs.as_ref(), idx_values, is_index, *pos, level, new_val + state, + indexed_val, + &x.1, + idx_values, + is_index, + x.2, + level, + new_val, ) } // 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, 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) - .map(|v| (v.clone_into_dynamic(), false)) + .map(|v| (v.clone_into_dynamic(), false)), } } else { match rhs { // xxx.fn_name(arg_expr_list) - Expr::FnCall(fn_name, None, hash, _, def_val, pos) => { + Expr::FnCall(x) if x.1.is_none() => { let mut args: Vec<_> = once(obj) - .chain(idx_val.downcast_mut::>().unwrap().iter_mut()) + .chain( + idx_val + .downcast_mut::>() + .unwrap() + .iter_mut(), + ) .collect(); - let def_val = def_val.as_deref(); + let def_val = x.4.as_deref(); // 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, fn_name, *hash, &mut args, def_val, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, &x.0, x.2, &mut args, def_val, x.5, 0) + .map(|v| (v, true)) } // xxx.module::fn_name(...) - syntax error - Expr::FnCall(_, _, _, _, _, _) => unreachable!(), + Expr::FnCall(_) => unreachable!(), // {xxx:map}.id = ??? #[cfg(not(feature = "no_object"))] - Expr::Property(id, pos) if obj.is::() && new_val.is_some() => { + 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, id.to_string().into(), *pos, op_pos, true)?; + self.get_indexed_mut(state, obj, index, x.1, op_pos, true)?; indexed_val.set_value(new_val.unwrap(), rhs.position())?; Ok((Default::default(), true)) } // {xxx:map}.id #[cfg(not(feature = "no_object"))] - Expr::Property(id, pos) if obj.is::() => { + Expr::Property(x) if obj.is::() => { + let index = x.0.clone().into(); let indexed_val = - self.get_indexed_mut(state, obj, id.to_string().into(), *pos, op_pos, false)?; + self.get_indexed_mut(state, obj, index, x.1, op_pos, false)?; Ok((indexed_val.clone_into_dynamic(), false)) } // xxx.id = ??? a - Expr::Property(id, pos) if new_val.is_some() => { - let fn_name = make_setter(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, 0, &mut args, None, *pos, 0).map(|v| (v, true)) + self.exec_fn_call(state, &fn_name, 0, &mut args, None, x.1, 0) + .map(|v| (v, true)) } // xxx.id - Expr::Property(id, pos) => { - let fn_name = make_getter(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, *pos, 0).map(|v| (v, false)) + self.exec_fn_call(state, &fn_name, 0, &mut args, None, x.1, 0) + .map(|v| (v, false)) } #[cfg(not(feature = "no_object"))] - // {xxx:map}.idx_lhs[idx_expr] - Expr::Index(dot_lhs, dot_rhs, pos) | - // {xxx:map}.dot_lhs.rhs - Expr::Dot(dot_lhs, dot_rhs, pos) if obj.is::() => { - let is_index = matches!(rhs, Expr::Index(_,_,_)); + // {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 indexed_val = if let Expr::Property(id, pos) = dot_lhs.as_ref() { - self.get_indexed_mut(state, obj, id.to_string().into(), *pos, op_pos, false)? + 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)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -937,19 +953,24 @@ impl Engine { ))); }; self.eval_dot_index_chain_helper( - state, indexed_val, dot_rhs, idx_values, is_index, *pos, level, new_val + state, + indexed_val, + &x.1, + idx_values, + is_index, + x.2, + level, + new_val, ) } - // xxx.idx_lhs[idx_expr] - Expr::Index(dot_lhs, dot_rhs, pos) | - // xxx.dot_lhs.rhs - Expr::Dot(dot_lhs, dot_rhs, pos) => { - let is_index = matches!(rhs, Expr::Index(_,_,_)); + // 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 indexed_val = &mut (if let Expr::Property(id, pos) = dot_lhs.as_ref() { - let fn_name = make_getter(id); - self.exec_fn_call(state, &fn_name, 0, &mut args[..1], None, *pos, 0)? + let indexed_val = &mut (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)? } else { // Syntax error return Err(Box::new(EvalAltResult::ErrorDotExpr( @@ -958,20 +979,28 @@ impl Engine { ))); }); let (result, may_be_changed) = self.eval_dot_index_chain_helper( - state, indexed_val.into(), dot_rhs, idx_values, is_index, *pos, level, new_val + state, + indexed_val.into(), + &x.1, + idx_values, + is_index, + x.2, + level, + new_val, )?; // Feed the value back via a setter just in case it has been updated if may_be_changed { - if let Expr::Property(id, pos) = dot_lhs.as_ref() { - let fn_name = make_setter(id); + 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, *pos, 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()), - err => Err(Box::new(err)) - })?; + self.exec_fn_call(state, &fn_name, 0, &mut 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()), + err => Err(Box::new(err)), + })?; } } @@ -1004,18 +1033,18 @@ impl Engine { match dot_lhs { // id.??? or id[???] - Expr::Variable(id, modules, hash, index, pos) => { - let index = if state.always_search { None } else { *index }; + Expr::Variable(x) => { + let index = if state.always_search { None } else { x.3 }; let (target, typ) = - search_scope(scope, id, modules.as_ref().map(|m| (m, *hash)), index, *pos)?; + search_scope(scope, &x.0, x.1.as_ref().map(|m| (m, x.2)), index, x.4)?; // Constants cannot be modified match typ { ScopeEntryType::Module => unreachable!(), ScopeEntryType::Constant if new_val.is_some() => { return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - id.to_string(), - *pos, + x.0.clone(), + x.4, ))); } ScopeEntryType::Constant | ScopeEntryType::Normal => (), @@ -1060,26 +1089,26 @@ impl Engine { level: usize, ) -> Result<(), Box> { match expr { - Expr::FnCall(_, None, _, arg_exprs, _, _) => { + Expr::FnCall(x) if x.1.is_none() => { let mut arg_values = StaticVec::::new(); - for arg_expr in arg_exprs.iter() { + for arg_expr in x.3.iter() { arg_values.push(self.eval_expr(scope, state, arg_expr, level)?); } idx_values.push(Dynamic::from(arg_values)); } - Expr::FnCall(_, _, _, _, _, _) => unreachable!(), - Expr::Property(_, _) => idx_values.push(()), // Store a placeholder - no need to copy the property name - Expr::Index(lhs, rhs, _) | Expr::Dot(lhs, rhs, _) => { + Expr::FnCall(_) => unreachable!(), + Expr::Property(_) => idx_values.push(()), // Store a placeholder - no need to copy the property name + Expr::Index(x) | Expr::Dot(x) => { // Evaluate in left-to-right order - let lhs_val = match lhs.as_ref() { - Expr::Property(_, _) => Default::default(), // Store a placeholder in case of a property - _ => self.eval_expr(scope, state, lhs, level)?, + let lhs_val = match x.0 { + Expr::Property(_) => Default::default(), // Store a placeholder in case of a property + _ => self.eval_expr(scope, state, &x.0, level)?, }; // Push in reverse order - self.eval_indexed_chain(scope, state, rhs, idx_values, size, level)?; + self.eval_indexed_chain(scope, state, &x.1, idx_values, size, level)?; idx_values.push(lhs_val); } @@ -1255,35 +1284,36 @@ impl Engine { level: usize, ) -> Result> { match expr { - Expr::IntegerConstant(i, _) => Ok((*i).into()), + Expr::IntegerConstant(x) => Ok(x.0.into()), #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(f, _) => Ok((*f).into()), - Expr::StringConstant(s, _) => Ok(s.to_string().into()), - Expr::CharConstant(c, _) => Ok((*c).into()), - Expr::Variable(id, modules, hash, index, pos) => { - let index = if state.always_search { None } else { *index }; - let mod_and_hash = modules.as_ref().map(|m| (m, *hash)); - let (val, _) = search_scope(scope, id, mod_and_hash, index, *pos)?; + Expr::FloatConstant(x) => Ok(x.0.into()), + Expr::StringConstant(x) => Ok(x.0.to_string().into()), + Expr::CharConstant(x) => Ok(x.0.into()), + Expr::Variable(x) => { + let index = if state.always_search { None } else { x.3 }; + let mod_and_hash = x.1.as_ref().map(|m| (m, x.2)); + let (val, _) = search_scope(scope, &x.0, mod_and_hash, index, x.4)?; Ok(val.clone()) } - Expr::Property(_, _) => unreachable!(), + Expr::Property(_) => unreachable!(), // Statement block - Expr::Stmt(stmt, _) => self.eval_stmt(scope, state, stmt, level), + Expr::Stmt(stmt) => self.eval_stmt(scope, state, &stmt.0, level), // lhs = rhs - Expr::Assignment(lhs, rhs, op_pos) => { - let rhs_val = self.eval_expr(scope, state, rhs, level)?; + Expr::Assignment(x) => { + let op_pos = x.2; + let rhs_val = self.eval_expr(scope, state, &x.1, level)?; - match lhs.as_ref() { + match &x.0 { // name = rhs - Expr::Variable(id, modules, hash, index, pos) => { - 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, id, mod_and_hash, index, *pos)?; + Expr::Variable(x) => { + let index = if state.always_search { None } else { x.3 }; + let mod_and_hash = x.1.as_ref().map(|m| (m, x.2)); + let (value_ptr, typ) = search_scope(scope, &x.0, mod_and_hash, index, x.4)?; match typ { ScopeEntryType::Constant => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(id.to_string(), *pos), + EvalAltResult::ErrorAssignmentToConstant(x.0.clone(), x.4), )), ScopeEntryType::Normal => { *value_ptr = rhs_val; @@ -1295,58 +1325,56 @@ impl Engine { } // idx_lhs[idx_expr] = rhs #[cfg(not(feature = "no_index"))] - Expr::Index(idx_lhs, idx_expr, op_pos) => { + Expr::Index(x) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, idx_lhs, idx_expr, true, *op_pos, level, new_val, + scope, state, &x.0, &x.1, true, x.2, level, new_val, ) } // dot_lhs.dot_rhs = rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(dot_lhs, dot_rhs, _) => { + Expr::Dot(x) => { let new_val = Some(rhs_val); self.eval_dot_index_chain( - scope, state, dot_lhs, dot_rhs, false, *op_pos, level, new_val, + scope, state, &x.0, &x.1, false, op_pos, level, new_val, ) } // Error assignment to constant expr if expr.is_constant() => { Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( expr.get_constant_str(), - lhs.position(), + expr.position(), ))) } // Syntax error - _ => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( - lhs.position(), + expr => Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS( + expr.position(), ))), } } // lhs[idx_expr] #[cfg(not(feature = "no_index"))] - Expr::Index(lhs, idx_expr, op_pos) => { - self.eval_dot_index_chain(scope, state, lhs, idx_expr, true, *op_pos, level, None) + Expr::Index(x) => { + self.eval_dot_index_chain(scope, state, &x.0, &x.1, true, x.2, level, None) } // lhs.dot_rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(lhs, dot_rhs, op_pos) => { - self.eval_dot_index_chain(scope, state, lhs, dot_rhs, false, *op_pos, level, None) + Expr::Dot(x) => { + self.eval_dot_index_chain(scope, state, &x.0, &x.1, false, x.2, level, None) } #[cfg(not(feature = "no_index"))] - Expr::Array(contents, _) => Ok(Dynamic(Union::Array(Box::new( - contents - .iter() + Expr::Array(x) => Ok(Dynamic(Union::Array(Box::new( + x.0.iter() .map(|item| self.eval_expr(scope, state, item, level)) .collect::, _>>()?, )))), #[cfg(not(feature = "no_object"))] - Expr::Map(contents, _) => Ok(Dynamic(Union::Map(Box::new( - contents - .iter() + Expr::Map(x) => Ok(Dynamic(Union::Map(Box::new( + x.0.iter() .map(|(key, expr, _)| { self.eval_expr(scope, state, expr, level) .map(|val| (key.clone(), val)) @@ -1355,27 +1383,26 @@ impl Engine { )))), // Normal function call - Expr::FnCall(fn_name, None, hash, arg_exprs, def_val, pos) => { - let mut arg_values = arg_exprs - .iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) - .collect::, _>>()?; + Expr::FnCall(x) if x.1.is_none() => { + let mut arg_values = + x.3.iter() + .map(|expr| self.eval_expr(scope, state, expr, level)) + .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); let hash_fn_spec = calc_fn_hash(empty(), KEYWORD_EVAL, once(TypeId::of::())); - if fn_name.as_ref() == KEYWORD_EVAL + if x.0 == KEYWORD_EVAL && args.len() == 1 - && !self.has_override(state, hash_fn_spec, *hash) + && !self.has_override(state, hash_fn_spec, x.2) { // 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[0], arg_exprs[0].position()); + let result = self.eval_script_expr(scope, state, args[0], x.3[0].position()); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1386,20 +1413,20 @@ impl Engine { result } else { // Normal function call - except for eval (handled above) - let def_value = def_val.as_deref(); - self.exec_fn_call(state, fn_name, *hash, &mut args, def_value, *pos, level) + let def_value = x.4.as_deref(); + self.exec_fn_call(state, &x.0, x.2, &mut args, def_value, x.5, level) } } // Module-qualified function call #[cfg(not(feature = "no_module"))] - Expr::FnCall(fn_name, Some(modules), hash1, arg_exprs, def_val, pos) => { - let modules = modules.as_ref(); + Expr::FnCall(x) if x.1.is_some() => { + let modules = x.1.as_ref().unwrap(); - let mut arg_values = arg_exprs - .iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) - .collect::, _>>()?; + let mut arg_values = + x.3.iter() + .map(|expr| self.eval_expr(scope, state, expr, level)) + .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); @@ -1418,8 +1445,8 @@ impl Engine { }; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_scripted_fn(*hash1) { - self.call_script_fn(None, state, fn_def, &mut args, *pos, level) + if let Some(fn_def) = module.get_qualified_scripted_fn(x.2) { + self.call_script_fn(None, state, fn_def, &mut args, x.5, level) } else { // Then search in Rust functions @@ -1430,47 +1457,45 @@ impl Engine { // the actual list of parameter `TypeId`'.s let hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); // 3) The final hash is the XOR of the two hashes. - let hash = *hash1 ^ hash2; + let hash = x.2 ^ hash2; - match module.get_qualified_fn(fn_name, hash, *pos) { - Ok(func) => func(&mut args, *pos), - Err(_) if def_val.is_some() => Ok(def_val.as_deref().unwrap().clone()), + match module.get_qualified_fn(&x.0, hash, x.5) { + Ok(func) => func(&mut args, x.5), + Err(_) if x.4.is_some() => Ok(x.4.as_deref().unwrap().clone()), Err(err) => Err(err), } } } - Expr::In(lhs, rhs, _) => { - self.eval_in_expr(scope, state, lhs.as_ref(), rhs.as_ref(), level) - } + Expr::In(x) => self.eval_in_expr(scope, state, &x.0, &x.1, level), - Expr::And(lhs, rhs, _) => Ok((self - .eval_expr(scope, state, lhs.as_ref(), level)? + Expr::And(x) => Ok((self + .eval_expr(scope, state, &x.0, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position()) + EvalAltResult::ErrorBooleanArgMismatch("AND".into(), x.0.position()) })? && // Short-circuit using && self - .eval_expr(scope, state, rhs.as_ref(), level)? + .eval_expr(scope, state, &x.1, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position()) + EvalAltResult::ErrorBooleanArgMismatch("AND".into(), x.1.position()) })?) .into()), - Expr::Or(lhs, rhs, _) => Ok((self - .eval_expr(scope, state, lhs.as_ref(), level)? + Expr::Or(x) => Ok((self + .eval_expr(scope, state, &x.0, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position()) + EvalAltResult::ErrorBooleanArgMismatch("OR".into(), x.0.position()) })? || // Short-circuit using || self - .eval_expr(scope, state, rhs.as_ref(), level)? + .eval_expr(scope, state, &x.1, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position()) + EvalAltResult::ErrorBooleanArgMismatch("OR".into(), x.1.position()) })?) .into()), @@ -1498,7 +1523,7 @@ impl Engine { Stmt::Expr(expr) => { let result = self.eval_expr(scope, state, expr, level)?; - Ok(if let Expr::Assignment(_, _, _) = *expr.as_ref() { + Ok(if let Expr::Assignment(_) = *expr.as_ref() { // If it is an assignment, erase the result at the root Default::default() } else { @@ -1507,10 +1532,10 @@ impl Engine { } // Block scope - Stmt::Block(block, _) => { + Stmt::Block(x) => { let prev_len = scope.len(); - let result = block.iter().try_fold(Default::default(), |_, stmt| { + let result = x.0.iter().try_fold(Default::default(), |_, stmt| { self.eval_stmt(scope, state, stmt, level) }); @@ -1524,24 +1549,24 @@ impl Engine { } // If-else statement - Stmt::IfThenElse(guard, if_body, else_body) => self - .eval_expr(scope, state, guard, level)? + Stmt::IfThenElse(x) => self + .eval_expr(scope, state, &x.0, level)? .as_bool() - .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(guard.position()))) + .map_err(|_| Box::new(EvalAltResult::ErrorLogicGuard(x.0.position()))) .and_then(|guard_val| { if guard_val { - self.eval_stmt(scope, state, if_body, level) - } else if let Some(stmt) = else_body { - self.eval_stmt(scope, state, stmt.as_ref(), level) + 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()) } }), // While loop - Stmt::While(guard, body) => loop { - match self.eval_expr(scope, state, guard, level)?.as_bool() { - Ok(true) => match self.eval_stmt(scope, state, body, level) { + 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(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1550,9 +1575,7 @@ impl Engine { }, }, Ok(false) => return Ok(Default::default()), - Err(_) => { - return Err(Box::new(EvalAltResult::ErrorLogicGuard(guard.position()))) - } + Err(_) => return Err(Box::new(EvalAltResult::ErrorLogicGuard(x.0.position()))), } }, @@ -1569,9 +1592,9 @@ impl Engine { }, // For loop - Stmt::For(name, expr, body) => { - let arr = self.eval_expr(scope, state, expr, level)?; - let tid = arr.type_id(); + Stmt::For(x) => { + let iter_type = self.eval_expr(scope, state, &x.1, level)?; + let tid = iter_type.type_id(); if let Some(iter_fn) = self .base_package @@ -1579,14 +1602,14 @@ impl Engine { .or_else(|| self.packages.get_iterator(tid)) { // Add the loop variable - let var_name = name.as_ref().clone(); - scope.push(var_name, ()); + let name = x.0.clone(); + scope.push(name, ()); let index = scope.len() - 1; - for a in iter_fn(arr) { + for a in iter_fn(iter_type) { *scope.get_mut(index).0 = a; - match self.eval_stmt(scope, state, body, level) { + match self.eval_stmt(scope, state, &x.2, level) { Ok(_) => (), Err(err) => match *err { EvalAltResult::ErrorLoopBreak(false, _) => (), @@ -1599,7 +1622,7 @@ impl Engine { scope.rewind(scope.len() - 1); Ok(Default::default()) } else { - Err(Box::new(EvalAltResult::ErrorFor(expr.position()))) + Err(Box::new(EvalAltResult::ErrorFor(x.1.position()))) } } @@ -1609,67 +1632,74 @@ impl Engine { // Break statement Stmt::Break(pos) => Err(Box::new(EvalAltResult::ErrorLoopBreak(true, *pos))), - // Empty return - Stmt::ReturnWithVal(None, ReturnType::Return, pos) => { - Err(Box::new(EvalAltResult::Return(Default::default(), *pos))) - } - // Return value - Stmt::ReturnWithVal(Some(a), ReturnType::Return, pos) => Err(Box::new( - EvalAltResult::Return(self.eval_expr(scope, state, a, level)?, *pos), - )), - - // Empty throw - Stmt::ReturnWithVal(None, ReturnType::Exception, pos) => { - Err(Box::new(EvalAltResult::ErrorRuntime("".into(), *pos))) - } - - // Throw value - Stmt::ReturnWithVal(Some(a), ReturnType::Exception, pos) => { - let val = self.eval_expr(scope, state, a, level)?; - Err(Box::new(EvalAltResult::ErrorRuntime( - val.take_string().unwrap_or_else(|_| "".to_string()), - *pos, + Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Return => { + Err(Box::new(EvalAltResult::Return( + self.eval_expr(scope, state, x.0.as_ref().unwrap(), level)?, + x.2, ))) } + // Empty return + Stmt::ReturnWithVal(x) if x.1 == ReturnType::Return => { + Err(Box::new(EvalAltResult::Return(Default::default(), x.2))) + } + + // Throw value + Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Exception => { + let val = self.eval_expr(scope, state, x.0.as_ref().unwrap(), level)?; + Err(Box::new(EvalAltResult::ErrorRuntime( + val.take_string().unwrap_or_else(|_| "".to_string()), + x.2, + ))) + } + + // Empty throw + Stmt::ReturnWithVal(x) if x.1 == ReturnType::Exception => { + Err(Box::new(EvalAltResult::ErrorRuntime("".into(), x.2))) + } + + Stmt::ReturnWithVal(_) => unreachable!(), + // Let statement - Stmt::Let(name, Some(expr), _) => { - let val = self.eval_expr(scope, state, expr, level)?; + Stmt::Let(x) if x.1.is_some() => { + let val = self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?; // TODO - avoid copying variable name in inner block? - let var_name = name.as_ref().clone(); + let var_name = x.0.clone(); scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); Ok(Default::default()) } - Stmt::Let(name, None, _) => { + Stmt::Let(x) => { // TODO - avoid copying variable name in inner block? - let var_name = name.as_ref().clone(); + let var_name = x.0.clone(); scope.push(var_name, ()); Ok(Default::default()) } // Const statement - Stmt::Const(name, expr, _) if expr.is_constant() => { - let val = self.eval_expr(scope, state, expr, level)?; + Stmt::Const(x) if x.1.is_constant() => { + let val = self.eval_expr(scope, state, &x.1, level)?; // TODO - avoid copying variable name in inner block? - let var_name = name.as_ref().clone(); + let var_name = x.0.clone(); scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); Ok(Default::default()) } // Const expression not constant - Stmt::Const(_, _, _) => unreachable!(), + Stmt::Const(_) => unreachable!(), // Import statement - Stmt::Import(expr, name, _) => { + Stmt::Import(x) => { + let (expr, name, _) = x.as_ref(); + #[cfg(feature = "no_module")] unreachable!(); #[cfg(not(feature = "no_module"))] { if let Some(path) = self - .eval_expr(scope, state, expr, level)? + .eval_expr(scope, state, &expr, level)? .try_cast::() { if let Some(resolver) = self.module_resolver.as_ref() { @@ -1678,7 +1708,7 @@ impl Engine { resolver.resolve(self, Scope::new(), &path, expr.position())?; // TODO - avoid copying module name in inner block? - let mod_name = name.as_ref().clone(); + let mod_name = name.clone(); scope.push_module(mod_name, module); Ok(Default::default()) } else { @@ -1695,7 +1725,7 @@ impl Engine { // Export statement Stmt::Export(list) => { - for (id, id_pos, rename) in list { + for (id, id_pos, rename) in list.as_ref() { let mut found = false; // Mark scope variables as public diff --git a/src/optimize.rs b/src/optimize.rs index 8a1ad745..223949d6 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -131,63 +131,63 @@ fn call_fn( fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) -> Stmt { match stmt { // if expr { Noop } - Stmt::IfThenElse(expr, if_block, None) if matches!(*if_block, Stmt::Noop(_)) => { + Stmt::IfThenElse(x) if matches!(x.1, Stmt::Noop(_)) => { state.set_dirty(); - let pos = expr.position(); - let expr = optimize_expr(*expr, state); + let pos = x.0.position(); + let expr = optimize_expr(x.0, state); if preserve_result { // -> { expr, Noop } - Stmt::Block(vec![Stmt::Expr(Box::new(expr)), *if_block], pos) + Stmt::Block(Box::new((vec![Stmt::Expr(Box::new(expr)), x.1], pos))) } else { // -> expr Stmt::Expr(Box::new(expr)) } } // if expr { if_block } - Stmt::IfThenElse(expr, if_block, None) => match *expr { + Stmt::IfThenElse(x) if x.2.is_none() => match x.0 { // if false { if_block } -> Noop Expr::False(pos) => { state.set_dirty(); Stmt::Noop(pos) } // if true { if_block } -> if_block - Expr::True(_) => optimize_stmt(*if_block, state, true), + Expr::True(_) => optimize_stmt(x.1, state, true), // if expr { if_block } - expr => Stmt::IfThenElse( - Box::new(optimize_expr(expr, state)), - Box::new(optimize_stmt(*if_block, state, true)), + expr => Stmt::IfThenElse(Box::new(( + optimize_expr(expr, state), + optimize_stmt(x.1, state, true), None, - ), + ))), }, // if expr { if_block } else { else_block } - Stmt::IfThenElse(expr, if_block, Some(else_block)) => match *expr { + Stmt::IfThenElse(x) if x.2.is_some() => match x.0 { // if false { if_block } else { else_block } -> else_block - Expr::False(_) => optimize_stmt(*else_block, state, true), + Expr::False(_) => optimize_stmt(x.2.unwrap(), state, true), // if true { if_block } else { else_block } -> if_block - Expr::True(_) => optimize_stmt(*if_block, state, true), + Expr::True(_) => optimize_stmt(x.1, state, true), // if expr { if_block } else { else_block } - expr => Stmt::IfThenElse( - Box::new(optimize_expr(expr, state)), - Box::new(optimize_stmt(*if_block, state, true)), - match optimize_stmt(*else_block, state, true) { + expr => Stmt::IfThenElse(Box::new(( + optimize_expr(expr, state), + optimize_stmt(x.1, state, true), + match optimize_stmt(x.2.unwrap(), state, true) { Stmt::Noop(_) => None, // Noop -> no else block - stmt => Some(Box::new(stmt)), + stmt => Some(stmt), }, - ), + ))), }, // while expr { block } - Stmt::While(expr, block) => match *expr { + Stmt::While(x) => match x.0 { // while false { block } -> Noop Expr::False(pos) => { state.set_dirty(); Stmt::Noop(pos) } // while true { block } -> loop { block } - Expr::True(_) => Stmt::Loop(Box::new(optimize_stmt(*block, state, false))), + Expr::True(_) => Stmt::Loop(Box::new(optimize_stmt(x.1, state, false))), // while expr { block } - expr => match optimize_stmt(*block, state, false) { + expr => match optimize_stmt(x.1, state, false) { // while expr { break; } -> { expr; } Stmt::Break(pos) => { // Only a single break statement - turn into running the guard expression once @@ -196,10 +196,10 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - if preserve_result { statements.push(Stmt::Noop(pos)) } - Stmt::Block(statements, pos) + Stmt::Block(Box::new((statements, pos))) } // while expr { block } - stmt => Stmt::While(Box::new(optimize_expr(expr, state)), Box::new(stmt)), + stmt => Stmt::While(Box::new((optimize_expr(expr, state), stmt))), }, }, // loop { block } @@ -214,38 +214,41 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - stmt => Stmt::Loop(Box::new(stmt)), }, // for id in expr { block } - Stmt::For(id, expr, block) => Stmt::For( - id, - Box::new(optimize_expr(*expr, state)), - Box::new(optimize_stmt(*block, state, false)), - ), + Stmt::For(x) => Stmt::For(Box::new(( + x.0, + optimize_expr(x.1, state), + optimize_stmt(x.2, state, false), + ))), // let id = expr; - Stmt::Let(id, Some(expr), pos) => { - Stmt::Let(id, Some(Box::new(optimize_expr(*expr, state))), pos) - } + Stmt::Let(x) if x.1.is_some() => Stmt::Let(Box::new(( + x.0, + Some(optimize_expr(x.1.unwrap(), state)), + x.2, + ))), // let id; - Stmt::Let(_, None, _) => stmt, + stmt @ Stmt::Let(_) => stmt, // import expr as id; - Stmt::Import(expr, id, pos) => Stmt::Import(Box::new(optimize_expr(*expr, state)), id, pos), + Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1, x.2))), // { block } - Stmt::Block(block, pos) => { - let orig_len = block.len(); // Original number of statements in the block, for change detection + 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<_> = block - .into_iter() - .map(|stmt| match stmt { - // Add constant into the state - Stmt::Const(name, value, pos) => { - state.push_constant(&name, *value); - state.set_dirty(); - Stmt::Noop(pos) // No need to keep constants - } - // Optimize the statement - _ => optimize_stmt(stmt, state, preserve_result), - }) - .collect(); + let mut result: Vec<_> = + x.0.into_iter() + .map(|stmt| match stmt { + // Add constant into the state + Stmt::Const(v) => { + state.push_constant(&v.0, v.1); + state.set_dirty(); + Stmt::Noop(v.2) // No need to keep constants + } + // Optimize the statement + _ => optimize_stmt(stmt, state, preserve_result), + }) + .collect(); // Remove all raw expression statements that are pure except for the very last statement let last_stmt = if preserve_result { result.pop() } else { None }; @@ -263,9 +266,9 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - while let Some(expr) = result.pop() { match expr { - Stmt::Let(_, None, _) => removed = true, - Stmt::Let(_, Some(val_expr), _) => removed = val_expr.is_pure(), - Stmt::Import(expr, _, _) => removed = expr.is_pure(), + Stmt::Let(x) if x.1.is_none() => removed = true, + Stmt::Let(x) if x.1.is_some() => removed = x.1.unwrap().is_pure(), + Stmt::Import(x) => removed = x.0.is_pure(), _ => { result.push(expr); break; @@ -297,7 +300,7 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - } match stmt { - Stmt::ReturnWithVal(_, _, _) | Stmt::Break(_) => { + Stmt::ReturnWithVal(_) | Stmt::Break(_) => { dead_code = true; } _ => (), @@ -321,21 +324,23 @@ 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(result, pos), + [Stmt::Let(_)] | [Stmt::Import(_)] => Stmt::Block(Box::new((result, pos))), // Only one statement - promote [_] => { state.set_dirty(); result.remove(0) } - _ => Stmt::Block(result, pos), + _ => Stmt::Block(Box::new((result, pos))), } } // expr; Stmt::Expr(expr) => Stmt::Expr(Box::new(optimize_expr(*expr, state))), // return expr; - Stmt::ReturnWithVal(Some(expr), is_return, pos) => { - Stmt::ReturnWithVal(Some(Box::new(optimize_expr(*expr, state))), is_return, pos) - } + Stmt::ReturnWithVal(x) if x.0.is_some() => Stmt::ReturnWithVal(Box::new(( + Some(optimize_expr(x.0.unwrap(), state)), + x.1, + x.2, + ))), // All other statements - skip stmt => stmt, } @@ -348,11 +353,11 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { match expr { // ( stmt ) - Expr::Stmt(stmt, pos) => match optimize_stmt(*stmt, state, true) { + Expr::Stmt(x) => match optimize_stmt(x.0, state, true) { // ( Noop ) -> () Stmt::Noop(_) => { state.set_dirty(); - Expr::Unit(pos) + Expr::Unit(x.1) } // ( expr ) -> expr Stmt::Expr(expr) => { @@ -360,151 +365,128 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { *expr } // ( stmt ) - stmt => Expr::Stmt(Box::new(stmt), pos), + stmt => Expr::Stmt(Box::new((stmt, x.1))), }, // id = expr - Expr::Assignment(id, expr, pos) => match *expr { + Expr::Assignment(x) => match x.1 { //id = id2 = expr2 - Expr::Assignment(id2, expr2, pos2) => match (*id, *id2) { + Expr::Assignment(x2) => match (x.0, x2.0) { // var = var = expr2 -> var = expr2 - (Expr::Variable(var, None, index, sp, _), Expr::Variable(var2, None, _, sp2, _)) - if var == var2 && sp == sp2 => + (Expr::Variable(a), Expr::Variable(b)) + if a.1.is_none() && b.1.is_none() && a.0 == b.0 && a.3 == b.3 => { // Assignment to the same variable - fold state.set_dirty(); - - Expr::Assignment( - Box::new(Expr::Variable(var, None, index, sp, pos)), - Box::new(optimize_expr(*expr2, state)) - , pos - ) + Expr::Assignment(Box::new((Expr::Variable(a), optimize_expr(x2.1, state), x.2))) } // id1 = id2 = expr2 - (id1, id2) => Expr::Assignment( - Box::new(id1), - Box::new(Expr::Assignment(Box::new(id2), Box::new(optimize_expr(*expr2, state)), pos2)), - pos, - ), + (id1, id2) => { + Expr::Assignment(Box::new(( + id1, Expr::Assignment(Box::new((id2, optimize_expr(x2.1, state), x2.2))), x.2, + ))) + } }, // id = expr - expr => Expr::Assignment(id, Box::new(optimize_expr(expr, state)), pos), + expr => Expr::Assignment(Box::new((x.0, optimize_expr(expr, state), x.2))), }, // lhs.rhs #[cfg(not(feature = "no_object"))] - Expr::Dot(lhs, rhs, pos) => match (*lhs, *rhs) { + Expr::Dot(x) => match (x.0, x.1) { // map.string - (Expr::Map(items, pos), Expr::Property(s, _)) if items.iter().all(|(_, x, _)| x.is_pure()) => { + (Expr::Map(m), Expr::Property(p)) 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(); - items.into_iter().find(|(name, _, _)| name == &s) + let pos = m.1; + m.0.into_iter().find(|(name, _, _)| name == &p.0) .map(|(_, expr, _)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // lhs.rhs - (lhs, rhs) => Expr::Dot( - Box::new(optimize_expr(lhs, state)), - Box::new(optimize_expr(rhs, state)), - pos, - ) + (lhs, rhs) => Expr::Dot(Box::new((optimize_expr(lhs, state), optimize_expr(rhs, state), x.2))) } // lhs[rhs] #[cfg(not(feature = "no_index"))] - Expr::Index(lhs, rhs, pos) => match (*lhs, *rhs) { + Expr::Index(x) => match (x.0, x.1) { // array[int] - (Expr::Array(mut items, pos), Expr::IntegerConstant(i, _)) - if i >= 0 && (i as usize) < items.len() && items.iter().all(Expr::is_pure) => + (Expr::Array(mut a), Expr::IntegerConstant(i)) + if i.0 >= 0 && (i.0 as usize) < a.0.len() && a.0.iter().all(Expr::is_pure) => { // Array literal where everything is pure - promote the indexed item. // All other items can be thrown away. state.set_dirty(); - items.remove(i as usize).set_position(pos) + a.0.remove(i.0 as usize).set_position(a.1) } // map[string] - (Expr::Map(items, pos), Expr::StringConstant(s, _)) if items.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(); - items.into_iter().find(|(name, _, _)| name == &s) + let pos = m.1; + m.0.into_iter().find(|(name, _, _)| name == &s.0) .map(|(_, expr, _)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // string[int] - (Expr::StringConstant(s, pos), Expr::IntegerConstant(i, _)) if i >= 0 && (i as usize) < s.chars().count() => { + (Expr::StringConstant(s), Expr::IntegerConstant(i)) if i.0 >= 0 && (i.0 as usize) < s.0.chars().count() => { // String literal indexing - get the character state.set_dirty(); - Expr::CharConstant(s.chars().nth(i as usize).expect("should get char"), pos) + Expr::CharConstant(Box::new((s.0.chars().nth(i.0 as usize).expect("should get char"), s.1))) } // lhs[rhs] - (lhs, rhs) => Expr::Index( - Box::new(optimize_expr(lhs, state)), - Box::new(optimize_expr(rhs, state)), - pos, - ), + (lhs, rhs) => Expr::Index(Box::new((optimize_expr(lhs, state), optimize_expr(rhs, state), x.2))), }, // [ items .. ] #[cfg(not(feature = "no_index"))] - Expr::Array(items, pos) => Expr::Array(items - .into_iter() - .map(|expr| optimize_expr(expr, state)) - .collect(), pos), + 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(items, pos) => Expr::Map(items - .into_iter() - .map(|(key, expr, pos)| (key, optimize_expr(expr, state), pos)) - .collect(), pos), + Expr::Map(m) => Expr::Map(Box::new((m.0 + .into_iter() + .map(|(key, expr, pos)| (key, optimize_expr(expr, state), pos)) + .collect(), m.1))), // lhs in rhs - Expr::In(lhs, rhs, pos) => match (*lhs, *rhs) { + Expr::In(x) => match (x.0, x.1) { // "xxx" in "xxxxx" - (Expr::StringConstant(lhs, pos), Expr::StringConstant(rhs, _)) => { + (Expr::StringConstant(a), Expr::StringConstant(b)) => { state.set_dirty(); - if rhs.contains(&lhs) { - Expr::True(pos) - } else { - Expr::False(pos) - } + if b.0.contains(&a.0) { Expr::True(a.1) } else { Expr::False(a.1) } } // 'x' in "xxxxx" - (Expr::CharConstant(lhs, pos), Expr::StringConstant(rhs, _)) => { + (Expr::CharConstant(a), Expr::StringConstant(b)) => { state.set_dirty(); - if rhs.contains(&lhs.to_string()) { - Expr::True(pos) - } else { - Expr::False(pos) - } + if b.0.contains(a.0) { Expr::True(a.1) } else { Expr::False(a.1) } } // "xxx" in #{...} - (Expr::StringConstant(lhs, pos), Expr::Map(items, _)) => { + (Expr::StringConstant(a), Expr::Map(b)) => { state.set_dirty(); - if items.iter().find(|(name, _, _)| name == &lhs).is_some() { - Expr::True(pos) + if b.0.iter().find(|(name, _, _)| name == &a.0).is_some() { + Expr::True(a.1) } else { - Expr::False(pos) + Expr::False(a.1) } } // 'x' in #{...} - (Expr::CharConstant(lhs, pos), Expr::Map(items, _)) => { + (Expr::CharConstant(a), Expr::Map(b)) => { state.set_dirty(); - let lhs = lhs.to_string(); + let ch = a.0.to_string(); - if items.iter().find(|(name, _, _)| name == &lhs).is_some() { - Expr::True(pos) + if b.0.iter().find(|(name, _, _)| name == &ch).is_some() { + Expr::True(a.1) } else { - Expr::False(pos) + Expr::False(a.1) } } // lhs in rhs - (lhs, rhs) => Expr::In( - Box::new(optimize_expr(lhs, state)), - Box::new(optimize_expr(rhs, state)), - pos - ), + (lhs, rhs) => Expr::In(Box::new((optimize_expr(lhs, state), optimize_expr(rhs, state), x.2))), }, // lhs && rhs - Expr::And(lhs, rhs, pos) => match (*lhs, *rhs) { + Expr::And(x) => match (x.0, x.1) { // true && rhs -> rhs (Expr::True(_), rhs) => { state.set_dirty(); @@ -521,14 +503,10 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { optimize_expr(lhs, state) } // lhs && rhs - (lhs, rhs) => Expr::And( - Box::new(optimize_expr(lhs, state)), - Box::new(optimize_expr(rhs, state)), - pos - ), + (lhs, rhs) => Expr::And(Box::new((optimize_expr(lhs, state), optimize_expr(rhs, state), x.2))), }, // lhs || rhs - Expr::Or(lhs, rhs, pos) => match (*lhs, *rhs) { + Expr::Or(x) => match (x.0, x.1) { // false || rhs -> rhs (Expr::False(_), rhs) => { state.set_dirty(); @@ -545,36 +523,40 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { optimize_expr(lhs, state) } // lhs || rhs - (lhs, rhs) => Expr::Or(Box::new(optimize_expr(lhs, state)), Box::new(optimize_expr(rhs, state)), pos), + (lhs, rhs) => Expr::Or(Box::new((optimize_expr(lhs, state), optimize_expr(rhs, state), x.2))), }, // Do not call some special keywords - Expr::FnCall(id, None, index, args, def_value, pos) if DONT_EVAL_KEYWORDS.contains(&id.as_ref().as_ref())=> - Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), + Expr::FnCall(mut x) if DONT_EVAL_KEYWORDS.contains(&x.0.as_ref().as_ref())=> { + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + Expr::FnCall(x) + } // Eagerly call functions - Expr::FnCall(id, None, index, args, def_value, pos) - if state.optimization_level == OptimizationLevel::Full // full optimizations - && args.iter().all(|expr| expr.is_constant()) // all arguments are constants + Expr::FnCall(mut x) + if x.1.is_none() // Non-qualified + && state.optimization_level == OptimizationLevel::Full // full optimizations + && x.3.iter().all(|expr| expr.is_constant()) // all arguments are constants => { // First search in script-defined functions (can override built-in) - if state.fn_lib.iter().find(|(name, len)| name == id.as_ref() && *len == args.len()).is_some() { + if state.fn_lib.iter().find(|(name, len)| *name == x.0 && *len == x.3.len()).is_some() { // A script-defined function overrides the built-in function - do not make the call - return Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos); + 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 arg_values: Vec<_> = x.3.iter().map(Expr::get_constant_value).collect(); let mut call_args: Vec<_> = 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 - let arg_for_type_of = if *id == KEYWORD_TYPE_OF && call_args.len() == 1 { + let arg_for_type_of = if x.0 == KEYWORD_TYPE_OF && call_args.len() == 1 { state.engine.map_type_name(call_args[0].type_name()) } else { "" }; - call_fn(&state.engine.packages, &state.engine.base_package, &id, &mut call_args, pos).ok() + call_fn(&state.engine.packages, &state.engine.base_package, &x.0, &mut call_args, x.5).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { @@ -582,29 +564,32 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { Some(arg_for_type_of.to_string().into()) } else { // Otherwise use the default value, if any - def_value.clone().map(|v| *v) + x.4.clone().map(|v| *v) } - }).and_then(|result| map_dynamic_to_expr(result, pos)) + }).and_then(|result| map_dynamic_to_expr(result, x.5)) .map(|expr| { state.set_dirty(); expr }) - ).unwrap_or_else(|| + ).unwrap_or_else(|| { // Optimize function call arguments - Expr::FnCall(id, None, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos) - ) + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + Expr::FnCall(x) + }) } // id(args ..) -> optimize function call arguments - Expr::FnCall(id, modules, index, args, def_value, pos) => - Expr::FnCall(id, modules, index, Box::new(args.into_iter().map(|a| optimize_expr(a, state)).collect()), def_value, pos), + Expr::FnCall(mut x) => { + x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect(); + Expr::FnCall(x) + } // constant-name - Expr::Variable(name, None, _, _, pos) if state.contains_constant(&name) => { + Expr::Variable(x) if x.1.is_none() && state.contains_constant(&x.0) => { state.set_dirty(); // Replace constant with value - state.find_constant(&name).expect("should find constant in scope!").clone().set_position(pos) + state.find_constant(&x.0).expect("should find constant in scope!").clone().set_position(x.4) } // All other expressions - skip @@ -657,17 +642,17 @@ fn optimize<'a>( .into_iter() .enumerate() .map(|(i, stmt)| { - match stmt { - Stmt::Const(ref name, ref value, _) => { + match &stmt { + Stmt::Const(x) => { // Load constants - state.push_constant(name.as_ref(), value.as_ref().clone()); + state.push_constant(&x.0, x.1.clone()); stmt // Keep it in the global scope } _ => { // Keep all variable declarations at this level // and always keep the last return value let keep = match stmt { - Stmt::Let(_, _, _) | Stmt::Import(_, _, _) => true, + Stmt::Let(_) | Stmt::Import(_) => true, _ => i == num_statements - 1, }; optimize_stmt(stmt, &mut state, keep) @@ -728,19 +713,21 @@ pub fn optimize_into_ast( // Optimize the function body let mut body = - optimize(vec![*fn_def.body], engine, &Scope::new(), &fn_lib, level); + optimize(vec![fn_def.body], engine, &Scope::new(), &fn_lib, level); // {} -> Noop - fn_def.body = Box::new(match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { + fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { // { return val; } -> val - Stmt::ReturnWithVal(Some(val), ReturnType::Return, _) => Stmt::Expr(val), + Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Return => { + Stmt::Expr(Box::new(x.0.unwrap())) + } // { return; } -> () - Stmt::ReturnWithVal(None, ReturnType::Return, pos) => { - Stmt::Expr(Box::new(Expr::Unit(pos))) + Stmt::ReturnWithVal(x) if x.0.is_none() && x.1 == ReturnType::Return => { + Stmt::Expr(Box::new(Expr::Unit(x.2))) } // All others stmt => stmt, - }); + }; } fn_def }) diff --git a/src/parser.rs b/src/parser.rs index fbca086e..7a88abfc 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -197,7 +197,7 @@ pub struct FnDef { /// Names of function parameters. pub params: Vec, /// Function body. - pub body: Box, + pub body: Stmt, /// Position of the function definition. pub pos: Position, } @@ -272,19 +272,19 @@ pub enum Stmt { /// No-op. Noop(Position), /// if expr { stmt } else { stmt } - IfThenElse(Box, Box, Option>), + IfThenElse(Box<(Expr, Stmt, Option)>), /// while expr { stmt } - While(Box, Box), + While(Box<(Expr, Stmt)>), /// loop { stmt } Loop(Box), /// for id in expr { stmt } - For(Box, Box, Box), + For(Box<(String, Expr, Stmt)>), /// let id = expr - Let(Box, Option>, Position), + Let(Box<(String, Option, Position)>), /// const id = expr - Const(Box, Box, Position), + Const(Box<(String, Expr, Position)>), /// { stmt; ... } - Block(Vec, Position), + Block(Box<(Vec, Position)>), /// { stmt } Expr(Box), /// continue @@ -292,54 +292,52 @@ pub enum Stmt { /// break Break(Position), /// return/throw - ReturnWithVal(Option>, ReturnType, Position), + ReturnWithVal(Box<(Option, ReturnType, Position)>), /// import expr as module - Import(Box, Box, Position), + Import(Box<(Expr, String, Position)>), /// expr id as name, ... - Export(Vec<(String, Position, Option<(String, Position)>)>), + Export(Box)>>), } impl Stmt { /// Get the `Position` of this statement. pub fn position(&self) -> Position { match self { - Stmt::Noop(pos) - | Stmt::Let(_, _, pos) - | Stmt::Const(_, _, pos) - | Stmt::Import(_, _, pos) - | Stmt::Block(_, pos) - | Stmt::Continue(pos) - | Stmt::Break(pos) - | Stmt::ReturnWithVal(_, _, pos) => *pos, - - Stmt::IfThenElse(expr, _, _) | Stmt::Expr(expr) => expr.position(), - - Stmt::While(_, stmt) | Stmt::Loop(stmt) | Stmt::For(_, _, stmt) => stmt.position(), - - Stmt::Export(list) => list.get(0).unwrap().1, + Stmt::Noop(pos) | Stmt::Continue(pos) | Stmt::Break(pos) => *pos, + Stmt::Let(x) => x.2, + Stmt::Const(x) => x.2, + Stmt::ReturnWithVal(x) => x.2, + Stmt::Block(x) => x.1, + Stmt::IfThenElse(x) => x.0.position(), + Stmt::Expr(x) => x.position(), + Stmt::While(x) => x.1.position(), + Stmt::Loop(x) => x.position(), + Stmt::For(x) => x.2.position(), + Stmt::Import(x) => x.2, + Stmt::Export(x) => x.get(0).unwrap().1, } } /// Is this statement self-terminated (i.e. no need for a semicolon terminator)? pub fn is_self_terminated(&self) -> bool { match self { - Stmt::IfThenElse(_, _, _) - | Stmt::While(_, _) + Stmt::IfThenElse(_) + | Stmt::While(_) | Stmt::Loop(_) - | Stmt::For(_, _, _) - | Stmt::Block(_, _) => true, + | Stmt::For(_) + | Stmt::Block(_) => true, // A No-op requires a semicolon in order to know it is an empty statement! Stmt::Noop(_) => false, - Stmt::Let(_, _, _) - | Stmt::Const(_, _, _) - | Stmt::Import(_, _, _) + Stmt::Let(_) + | Stmt::Const(_) + | Stmt::Import(_) | Stmt::Export(_) | Stmt::Expr(_) | Stmt::Continue(_) | Stmt::Break(_) - | Stmt::ReturnWithVal(_, _, _) => false, + | Stmt::ReturnWithVal(_) => false, } } @@ -348,76 +346,74 @@ impl Stmt { match self { Stmt::Noop(_) => true, Stmt::Expr(expr) => expr.is_pure(), - Stmt::IfThenElse(guard, if_block, Some(else_block)) => { - guard.is_pure() && if_block.is_pure() && else_block.is_pure() + Stmt::IfThenElse(x) if x.2.is_some() => { + x.0.is_pure() && x.1.is_pure() && x.2.as_ref().unwrap().is_pure() } - Stmt::IfThenElse(guard, block, None) | Stmt::While(guard, block) => { - guard.is_pure() && block.is_pure() - } - Stmt::Loop(block) => block.is_pure(), - Stmt::For(_, range, block) => range.is_pure() && block.is_pure(), - Stmt::Let(_, _, _) | Stmt::Const(_, _, _) => false, - Stmt::Block(statements, _) => statements.iter().all(Stmt::is_pure), - Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_, _, _) => false, - Stmt::Import(_, _, _) => false, + Stmt::IfThenElse(x) => x.1.is_pure(), + Stmt::While(x) => x.0.is_pure() && x.1.is_pure(), + Stmt::Loop(x) => x.is_pure(), + Stmt::For(x) => x.1.is_pure() && x.2.is_pure(), + Stmt::Let(_) | Stmt::Const(_) => false, + Stmt::Block(x) => x.0.iter().all(Stmt::is_pure), + Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_) => false, + Stmt::Import(_) => false, Stmt::Export(_) => false, } } } +#[cfg(not(feature = "no_module"))] +type MRef = Option>; +#[cfg(feature = "no_module")] +type MRef = Option; + /// An expression. #[derive(Debug, Clone)] pub enum Expr { /// Integer constant. - IntegerConstant(INT, Position), + IntegerConstant(Box<(INT, Position)>), /// Floating-point constant. #[cfg(not(feature = "no_float"))] - FloatConstant(FLOAT, Position), + FloatConstant(Box<(FLOAT, Position)>), /// Character constant. - CharConstant(char, Position), + CharConstant(Box<(char, Position)>), /// String constant. - StringConstant(String, Position), + StringConstant(Box<(String, Position)>), /// Variable access - (variable name, optional modules, hash, optional index, position) - Variable( - Box, - #[cfg(not(feature = "no_module"))] Option>, - #[cfg(feature = "no_module")] Option, - u64, - Option, - Position, - ), + Variable(Box<(String, MRef, u64, Option, Position)>), /// Property access. - Property(String, Position), + Property(Box<(String, Position)>), /// { stmt } - Stmt(Box, Position), + Stmt(Box<(Stmt, Position)>), /// func(expr, ... ) - (function name, optional modules, hash, arguments, optional default value, position) /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// and the function names are predictable, so no need to allocate a new `String`. FnCall( - Box>, - #[cfg(not(feature = "no_module"))] Option>, - #[cfg(feature = "no_module")] Option, - u64, - Box>, - Option>, - Position, + Box<( + Cow<'static, str>, + MRef, + u64, + Vec, + Option>, + Position, + )>, ), /// expr = expr - Assignment(Box, Box, Position), + Assignment(Box<(Expr, Expr, Position)>), /// lhs.rhs - Dot(Box, Box, Position), + Dot(Box<(Expr, Expr, Position)>), /// expr[expr] - Index(Box, Box, Position), + Index(Box<(Expr, Expr, Position)>), /// [ expr, ... ] - Array(Vec, Position), + Array(Box<(Vec, Position)>), /// #{ name:expr, ... } - Map(Vec<(String, Expr, Position)>, Position), + Map(Box<(Vec<(String, Expr, Position)>, Position)>), /// lhs in rhs - In(Box, Box, Position), + In(Box<(Expr, Expr, Position)>), /// lhs && rhs - And(Box, Box, Position), + And(Box<(Expr, Expr, Position)>), /// lhs || rhs - Or(Box, Box, Position), + Or(Box<(Expr, Expr, Position)>), /// true True(Position), /// false @@ -434,30 +430,24 @@ impl Expr { /// Panics when the expression is not constant. pub fn get_constant_value(&self) -> Dynamic { match self { - Self::IntegerConstant(i, _) => (*i).into(), + Self::IntegerConstant(x) => x.0.into(), #[cfg(not(feature = "no_float"))] - Self::FloatConstant(f, _) => (*f).into(), - Self::CharConstant(c, _) => (*c).into(), - Self::StringConstant(s, _) => s.clone().into(), + Self::FloatConstant(x) => x.0.into(), + Self::CharConstant(x) => x.0.into(), + Self::StringConstant(x) => x.0.clone().into(), Self::True(_) => true.into(), Self::False(_) => false.into(), Self::Unit(_) => ().into(), #[cfg(not(feature = "no_index"))] - Self::Array(items, _) if items.iter().all(Self::is_constant) => { - Dynamic(Union::Array(Box::new( - items - .iter() - .map(Self::get_constant_value) - .collect::>(), - ))) - } + Self::Array(x) if x.0.iter().all(Self::is_constant) => Dynamic(Union::Array(Box::new( + x.0.iter().map(Self::get_constant_value).collect::>(), + ))), #[cfg(not(feature = "no_object"))] - Self::Map(items, _) if items.iter().all(|(_, v, _)| v.is_constant()) => { + Self::Map(x) if x.0.iter().all(|(_, v, _)| v.is_constant()) => { Dynamic(Union::Map(Box::new( - items - .iter() + x.0.iter() .map(|(k, v, _)| (k.clone(), v.get_constant_value())) .collect::>(), ))) @@ -475,16 +465,16 @@ impl Expr { pub fn get_constant_str(&self) -> String { match self { #[cfg(not(feature = "no_float"))] - Self::FloatConstant(f, _) => f.to_string(), + Self::FloatConstant(x) => x.0.to_string(), - Self::IntegerConstant(i, _) => i.to_string(), - Self::CharConstant(c, _) => c.to_string(), - Self::StringConstant(_, _) => "string".to_string(), + Self::IntegerConstant(x) => x.0.to_string(), + Self::CharConstant(x) => x.0.to_string(), + Self::StringConstant(_) => "string".to_string(), Self::True(_) => "true".to_string(), Self::False(_) => "false".to_string(), Self::Unit(_) => "()".to_string(), - Self::Array(items, _) if items.iter().all(Self::is_constant) => "array".to_string(), + Self::Array(x) if x.0.iter().all(Self::is_constant) => "array".to_string(), _ => panic!("cannot get value of non-constant expression"), } @@ -494,27 +484,23 @@ impl Expr { pub fn position(&self) -> Position { match self { #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_, pos) => *pos, + Self::FloatConstant(x) => x.1, - Self::IntegerConstant(_, pos) - | Self::CharConstant(_, pos) - | Self::StringConstant(_, pos) - | Self::Array(_, pos) - | Self::Map(_, pos) - | Self::Variable(_, _, _, _, pos) - | Self::Property(_, pos) - | Self::Stmt(_, pos) - | Self::FnCall(_, _, _, _, _, pos) - | Self::And(_, _, pos) - | Self::Or(_, _, pos) - | Self::In(_, _, pos) - | Self::True(pos) - | Self::False(pos) - | Self::Unit(pos) => *pos, + Self::IntegerConstant(x) => x.1, + Self::CharConstant(x) => x.1, + Self::StringConstant(x) => x.1, + Self::Array(x) => x.1, + Self::Map(x) => x.1, + Self::Property(x) => x.1, + Self::Stmt(x) => x.1, + Self::Variable(x) => x.4, + Self::FnCall(x) => x.5, - Self::Assignment(expr, _, _) | Self::Dot(expr, _, _) | Self::Index(expr, _, _) => { - expr.position() - } + Self::And(x) | Self::Or(x) | Self::In(x) => x.2, + + Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos, + + Self::Assignment(x) | Self::Dot(x) | Self::Index(x) => x.0.position(), } } @@ -522,26 +508,26 @@ impl Expr { pub(crate) fn set_position(mut self, new_pos: Position) -> Self { match &mut self { #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_, pos) => *pos = new_pos, + Self::FloatConstant(x) => x.1 = new_pos, - Self::IntegerConstant(_, pos) - | Self::CharConstant(_, pos) - | Self::StringConstant(_, pos) - | Self::Array(_, pos) - | Self::Map(_, pos) - | Self::Variable(_, _, _, _, pos) - | Self::Property(_, pos) - | Self::Stmt(_, pos) - | Self::FnCall(_, _, _, _, _, pos) - | Self::And(_, _, pos) - | Self::Or(_, _, pos) - | Self::In(_, _, pos) - | Self::True(pos) - | Self::False(pos) - | Self::Unit(pos) - | Self::Assignment(_, _, pos) - | Self::Dot(_, _, pos) - | Self::Index(_, _, pos) => *pos = new_pos, + Self::IntegerConstant(x) => x.1 = new_pos, + Self::CharConstant(x) => x.1 = new_pos, + Self::StringConstant(x) => x.1 = new_pos, + Self::Array(x) => x.1 = new_pos, + Self::Map(x) => x.1 = new_pos, + Self::Variable(x) => x.4 = new_pos, + Self::Property(x) => x.1 = new_pos, + Self::Stmt(x) => x.1 = new_pos, + Self::FnCall(x) => x.5 = new_pos, + Self::And(x) => x.2 = new_pos, + Self::Or(x) => x.2 = new_pos, + Self::In(x) => x.2 = new_pos, + Self::True(pos) => *pos = new_pos, + Self::False(pos) => *pos = new_pos, + Self::Unit(pos) => *pos = new_pos, + Self::Assignment(x) => x.2 = new_pos, + Self::Dot(x) => x.2 = new_pos, + Self::Index(x) => x.2 = new_pos, } self @@ -552,15 +538,15 @@ impl Expr { /// A pure expression has no side effects. pub fn is_pure(&self) -> bool { match self { - Self::Array(expressions, _) => expressions.iter().all(Self::is_pure), + Self::Array(x) => x.0.iter().all(Self::is_pure), - Self::Index(x, y, _) | Self::And(x, y, _) | Self::Or(x, y, _) | Self::In(x, y, _) => { - x.is_pure() && y.is_pure() + Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => { + x.0.is_pure() && x.1.is_pure() } - Self::Stmt(stmt, _) => stmt.is_pure(), + Self::Stmt(x) => x.0.is_pure(), - Self::Variable(_, _, _, _, _) => true, + Self::Variable(_) => true, expr => expr.is_constant(), } @@ -570,25 +556,25 @@ impl Expr { pub fn is_constant(&self) -> bool { match self { #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_, _) => true, + Self::FloatConstant(_) => true, - Self::IntegerConstant(_, _) - | Self::CharConstant(_, _) - | Self::StringConstant(_, _) + Self::IntegerConstant(_) + | Self::CharConstant(_) + | Self::StringConstant(_) | Self::True(_) | Self::False(_) | Self::Unit(_) => true, // An array literal is constant if all items are constant - Self::Array(expressions, _) => expressions.iter().all(Self::is_constant), + Self::Array(x) => x.0.iter().all(Self::is_constant), // An map literal is constant if all items are constant - Self::Map(items, _) => items.iter().map(|(_, expr, _)| expr).all(Self::is_constant), + Self::Map(x) => x.0.iter().map(|(_, expr, _)| expr).all(Self::is_constant), // Check in expression - Self::In(lhs, rhs, _) => match (lhs.as_ref(), rhs.as_ref()) { - (Self::StringConstant(_, _), Self::StringConstant(_, _)) - | (Self::CharConstant(_, _), Self::StringConstant(_, _)) => true, + Self::In(x) => match (&x.0, &x.1) { + (Self::StringConstant(_), Self::StringConstant(_)) + | (Self::CharConstant(_), Self::StringConstant(_)) => true, _ => false, }, @@ -600,37 +586,37 @@ impl Expr { pub fn is_valid_postfix(&self, token: &Token) -> bool { match self { #[cfg(not(feature = "no_float"))] - Self::FloatConstant(_, _) => false, + Self::FloatConstant(_) => false, - Self::IntegerConstant(_, _) - | Self::CharConstant(_, _) - | Self::In(_, _, _) - | Self::And(_, _, _) - | Self::Or(_, _, _) + Self::IntegerConstant(_) + | Self::CharConstant(_) + | Self::In(_) + | Self::And(_) + | Self::Or(_) | Self::True(_) | Self::False(_) | Self::Unit(_) => false, - Self::StringConstant(_, _) - | Self::Stmt(_, _) - | Self::FnCall(_, _, _, _, _, _) - | Self::Assignment(_, _, _) - | Self::Dot(_, _, _) - | Self::Index(_, _, _) - | Self::Array(_, _) - | Self::Map(_, _) => match token { + Self::StringConstant(_) + | Self::Stmt(_) + | Self::FnCall(_) + | Self::Assignment(_) + | Self::Dot(_) + | Self::Index(_) + | Self::Array(_) + | Self::Map(_) => match token { Token::LeftBracket => true, _ => false, }, - Self::Variable(_, _, _, _, _) => match token { + Self::Variable(_) => match token { Token::LeftBracket | Token::LeftParen => true, #[cfg(not(feature = "no_module"))] Token::DoubleColon => true, _ => false, }, - Self::Property(_, _) => match token { + Self::Property(_) => match token { Token::LeftBracket | Token::LeftParen => true, _ => false, }, @@ -640,7 +626,7 @@ impl Expr { /// Convert a `Variable` into a `Property`. All other variants are untouched. pub(crate) fn into_property(self) -> Self { match self { - Self::Variable(id, None, _, _, pos) => Self::Property(*id, pos), + Self::Variable(x) if x.1.is_none() => Self::Property(Box::new((x.0.clone(), x.4))), _ => self, } } @@ -748,14 +734,14 @@ fn parse_call_expr<'a>( #[cfg(feature = "no_module")] let hash1 = calc_fn_hash(empty(), &id, empty()); - return Ok(Expr::FnCall( - Box::new(id.into()), + return Ok(Expr::FnCall(Box::new(( + id.into(), modules, hash1, - Box::new(args), + args, None, begin, - )); + )))); } // id... _ => (), @@ -795,14 +781,14 @@ fn parse_call_expr<'a>( #[cfg(feature = "no_module")] let hash1 = calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())); - return Ok(Expr::FnCall( - Box::new(id.into()), + return Ok(Expr::FnCall(Box::new(( + id.into(), modules, hash1, - Box::new(args), + args, None, begin, - )); + )))); } // id(...args, (Token::Comma, _) => { @@ -846,39 +832,44 @@ fn parse_index_chain<'a>( // Check type of indexing - must be integer or string match &idx_expr { // lhs[int] - Expr::IntegerConstant(i, pos) if *i < 0 => { + Expr::IntegerConstant(x) if x.0 < 0 => { return Err(PERR::MalformedIndexExpr(format!( "Array access expects non-negative index: {} < 0", - i + x.0 )) - .into_err(*pos)) + .into_err(x.1)) } - Expr::IntegerConstant(_, pos) => match lhs { - Expr::Array(_, _) | Expr::StringConstant(_, _) => (), + Expr::IntegerConstant(x) => match lhs { + Expr::Array(_) | Expr::StringConstant(_) => (), - Expr::Map(_, _) => { + Expr::Map(_) => { return Err(PERR::MalformedIndexExpr( "Object map access expects string index, not a number".into(), ) - .into_err(*pos)) + .into_err(x.1)) } #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(_, pos) => { + Expr::FloatConstant(x) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(pos)) + .into_err(x.1)) } - Expr::CharConstant(_, pos) - | Expr::Assignment(_, _, pos) - | Expr::And(_, _, pos) - | Expr::Or(_, _, pos) - | Expr::In(_, _, pos) - | Expr::True(pos) - | Expr::False(pos) - | Expr::Unit(pos) => { + Expr::CharConstant(x) => { + return Err(PERR::MalformedIndexExpr( + "Only arrays, object maps and strings can be indexed".into(), + ) + .into_err(x.1)) + } + Expr::Assignment(x) | Expr::And(x) | Expr::Or(x) | Expr::In(x) => { + return Err(PERR::MalformedIndexExpr( + "Only arrays, object maps and strings can be indexed".into(), + ) + .into_err(x.2)) + } + Expr::True(pos) | Expr::False(pos) | Expr::Unit(pos) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) @@ -889,32 +880,39 @@ fn parse_index_chain<'a>( }, // lhs[string] - Expr::StringConstant(_, pos) => match lhs { - Expr::Map(_, _) => (), + Expr::StringConstant(x) => match lhs { + Expr::Map(_) => (), - Expr::Array(_, _) | Expr::StringConstant(_, _) => { + Expr::Array(_) | Expr::StringConstant(_) => { return Err(PERR::MalformedIndexExpr( "Array or string expects numeric index, not a string".into(), ) - .into_err(*pos)) + .into_err(x.1)) } #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(_, pos) => { + Expr::FloatConstant(x) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) - .into_err(pos)) + .into_err(x.1)) } - Expr::CharConstant(_, pos) - | Expr::Assignment(_, _, pos) - | Expr::And(_, _, pos) - | Expr::Or(_, _, pos) - | Expr::In(_, _, pos) - | Expr::True(pos) - | Expr::False(pos) - | Expr::Unit(pos) => { + Expr::CharConstant(x) => { + return Err(PERR::MalformedIndexExpr( + "Only arrays, object maps and strings can be indexed".into(), + ) + .into_err(x.1)) + } + + Expr::Assignment(x) | Expr::And(x) | Expr::Or(x) | Expr::In(x) => { + return Err(PERR::MalformedIndexExpr( + "Only arrays, object maps and strings can be indexed".into(), + ) + .into_err(x.2)) + } + + Expr::True(pos) | Expr::False(pos) | Expr::Unit(pos) => { return Err(PERR::MalformedIndexExpr( "Only arrays, object maps and strings can be indexed".into(), ) @@ -926,32 +924,42 @@ fn parse_index_chain<'a>( // lhs[float] #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(_, pos) => { + Expr::FloatConstant(x) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a float".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // lhs[char] - Expr::CharConstant(_, pos) => { + Expr::CharConstant(x) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a character".into(), ) - .into_err(*pos)) + .into_err(x.1)) } - // lhs[??? = ??? ], lhs[()] - Expr::Assignment(_, _, pos) | Expr::Unit(pos) => { + // lhs[??? = ??? ] + Expr::Assignment(x) => { + return Err(PERR::MalformedIndexExpr( + "Array access expects integer index, not ()".into(), + ) + .into_err(x.2)) + } + // lhs[()] + Expr::Unit(pos) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not ()".into(), ) .into_err(*pos)) } - // lhs[??? && ???], lhs[??? || ???], lhs[??? in ???], lhs[true], lhs[false] - Expr::And(_, _, pos) - | Expr::Or(_, _, pos) - | Expr::In(_, _, pos) - | Expr::True(pos) - | Expr::False(pos) => { + // lhs[??? && ???], lhs[??? || ???], lhs[??? in ???] + Expr::And(x) | Expr::Or(x) | Expr::In(x) => { + return Err(PERR::MalformedIndexExpr( + "Array access expects integer index, not a boolean".into(), + ) + .into_err(x.2)) + } + // lhs[true], lhs[false] + Expr::True(pos) | Expr::False(pos) => { return Err(PERR::MalformedIndexExpr( "Array access expects integer index, not a boolean".into(), ) @@ -975,10 +983,10 @@ fn parse_index_chain<'a>( let follow = parse_index_chain(input, stack, idx_expr, follow_pos, allow_stmt_expr)?; // Indexing binds to right - Ok(Expr::Index(Box::new(lhs), Box::new(follow), pos)) + Ok(Expr::Index(Box::new((lhs, follow, pos)))) } // Otherwise terminate the indexing chain - _ => Ok(Expr::Index(Box::new(lhs), Box::new(idx_expr), pos)), + _ => Ok(Expr::Index(Box::new((lhs, idx_expr, pos)))), } } (Token::LexError(err), pos) => return Err(PERR::BadInput(err.to_string()).into_err(*pos)), @@ -1030,7 +1038,7 @@ fn parse_array_literal<'a>( } } - Ok(Expr::Array(arr, pos)) + Ok(Expr::Array(Box::new((arr, pos)))) } /// Parse a map literal. @@ -1127,7 +1135,7 @@ fn parse_map_literal<'a>( }) .map_err(|(key, pos)| PERR::DuplicatedProperty(key.to_string()).into_err(pos))?; - Ok(Expr::Map(map, pos)) + Ok(Expr::Map(Box::new((map, pos)))) } /// Parse a primary expression. @@ -1141,21 +1149,21 @@ fn parse_primary<'a>( (Token::LeftBrace, pos) if allow_stmt_expr => { let pos = *pos; return parse_block(input, stack, false, allow_stmt_expr) - .map(|block| Expr::Stmt(Box::new(block), pos)); + .map(|block| Expr::Stmt(Box::new((block, pos)))); } (Token::EOF, pos) => return Err(PERR::UnexpectedEOF.into_err(*pos)), _ => input.next().unwrap(), }; let mut root_expr = match token { - Token::IntegerConstant(x) => Expr::IntegerConstant(x, pos), + Token::IntegerConstant(x) => Expr::IntegerConstant(Box::new((x, pos))), #[cfg(not(feature = "no_float"))] - Token::FloatConstant(x) => Expr::FloatConstant(x, pos), - Token::CharConstant(c) => Expr::CharConstant(c, pos), - Token::StringConst(s) => Expr::StringConstant(s, pos), + Token::FloatConstant(x) => Expr::FloatConstant(Box::new((x, pos))), + 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); - Expr::Variable(Box::new(s), None, 0, index, pos) + Expr::Variable(Box::new((s, None, 0, index, pos))) } Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, #[cfg(not(feature = "no_index"))] @@ -1182,30 +1190,28 @@ fn parse_primary<'a>( root_expr = match (root_expr, token) { // Function call - (Expr::Variable(id, modules, _, _, pos), Token::LeftParen) => { - parse_call_expr(input, stack, *id, modules, pos, allow_stmt_expr)? + (Expr::Variable(x), Token::LeftParen) => { + parse_call_expr(input, stack, x.0, x.1, x.4, allow_stmt_expr)? } - (Expr::Property(id, pos), Token::LeftParen) => { - parse_call_expr(input, stack, id, None, pos, allow_stmt_expr)? + (Expr::Property(x), Token::LeftParen) => { + parse_call_expr(input, stack, x.0, None, x.1, allow_stmt_expr)? } // module access #[cfg(not(feature = "no_module"))] - (Expr::Variable(id, mut modules, _, index, pos), Token::DoubleColon) => { - match input.next().unwrap() { - (Token::Identifier(id2), pos2) => { - if let Some(ref mut modules) = modules { - modules.push((*id, pos)); - } else { - let mut m: ModuleRef = Default::default(); - m.push((*id, pos)); - modules = Some(Box::new(m)); - } - - Expr::Variable(Box::new(id2), modules, 0, index, pos2) + (Expr::Variable(mut x), Token::DoubleColon) => match input.next().unwrap() { + (Token::Identifier(id2), pos2) => { + if let Some(ref mut modules) = x.1 { + modules.push((x.0, x.4)); + } else { + let mut m: ModuleRef = Default::default(); + m.push((x.0, x.4)); + x.1 = Some(Box::new(m)); } - (_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)), + + Expr::Variable(Box::new((id2, x.1, 0, x.3, pos2))) } - } + (_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)), + }, // Indexing #[cfg(not(feature = "no_index"))] (expr, Token::LeftBracket) => { @@ -1219,9 +1225,11 @@ fn parse_primary<'a>( match &mut root_expr { // Cache the hash key for module-qualified variables #[cfg(not(feature = "no_module"))] - Expr::Variable(id, Some(modules), hash, _, _) => { + Expr::Variable(x) if x.1.is_some() => { + let modules = x.1.as_mut().unwrap(); + // Qualifiers + variable name - *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), id, empty()); + x.2 = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), &x.0, empty()); modules.set_index(stack.find_module(&modules.get(0).0)); } _ => (), @@ -1240,10 +1248,10 @@ fn parse_unary<'a>( // 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)?), + Ok(Expr::Stmt(Box::new(( + parse_if(input, stack, false, allow_stmt_expr)?, pos, - )) + )))) } // -expr (Token::UnaryMinus, _) => { @@ -1251,41 +1259,46 @@ fn parse_unary<'a>( match parse_unary(input, stack, allow_stmt_expr)? { // Negative integer - Expr::IntegerConstant(i, _) => i - .checked_neg() - .map(|x| Expr::IntegerConstant(x, pos)) - .or_else(|| { - #[cfg(not(feature = "no_float"))] - { - Some(Expr::FloatConstant(-(i as FLOAT), pos)) - } - #[cfg(feature = "no_float")] - { - None - } - }) - .ok_or_else(|| { - PERR::BadInput(LexError::MalformedNumber(format!("-{}", i)).to_string()) + Expr::IntegerConstant(x) => { + let (num, pos) = *x; + + num.checked_neg() + .map(|i| Expr::IntegerConstant(Box::new((i, pos)))) + .or_else(|| { + #[cfg(not(feature = "no_float"))] + { + Some(Expr::FloatConstant(Box::new((-(x.0 as FLOAT), pos)))) + } + #[cfg(feature = "no_float")] + { + None + } + }) + .ok_or_else(|| { + PERR::BadInput( + LexError::MalformedNumber(format!("-{}", x.0)).to_string(), + ) .into_err(pos) - }), + }) + } // Negative float #[cfg(not(feature = "no_float"))] - Expr::FloatConstant(f, pos) => Ok(Expr::FloatConstant(-f, pos)), + Expr::FloatConstant(x) => Ok(Expr::FloatConstant(Box::new((-x.0, x.1)))), // Call negative function e => { let op = "-"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); - Ok(Expr::FnCall( - Box::new(op.into()), + Ok(Expr::FnCall(Box::new(( + op.into(), None, hash, - Box::new(vec![e]), + vec![e], None, pos, - )) + )))) } } } @@ -1300,14 +1313,14 @@ fn parse_unary<'a>( let op = "!"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); - Ok(Expr::FnCall( - Box::new(op.into()), + Ok(Expr::FnCall(Box::new(( + op.into(), None, hash, - Box::new(vec![parse_primary(input, stack, allow_stmt_expr)?]), + vec![parse_primary(input, stack, allow_stmt_expr)?], Some(Box::new(false.into())), // NOT operator, when operating on invalid operand, defaults to false pos, - )) + )))) } // (Token::EOF, pos) => Err(PERR::UnexpectedEOF.into_err(*pos)), @@ -1323,34 +1336,30 @@ fn make_assignment_stmt<'a>( pos: Position, ) -> Result> { match &lhs { - Expr::Variable(_, _, _, None, _) => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), - Expr::Variable(name, _, _, Some(index), var_pos) => { - match stack[(stack.len() - index.get())].1 { - ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + Expr::Variable(x) => { + match stack[(stack.len() - x.3.unwrap().get())].1 { + ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { - Err(PERR::AssignmentToConstant(name.to_string()).into_err(*var_pos)) + Err(PERR::AssignmentToConstant(x.0.to_string()).into_err(x.4)) } ScopeEntryType::Module => unreachable!(), } } - Expr::Index(idx_lhs, _, _) | Expr::Dot(idx_lhs, _, _) => match idx_lhs.as_ref() { - Expr::Variable(_, _, _, None, _) => { - Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) - } - Expr::Variable(name, _, _, Some(index), var_pos) => { - match stack[(stack.len() - index.get())].1 { - ScopeEntryType::Normal => { - Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)) - } + Expr::Index(x) | Expr::Dot(x) => match &x.0 { + Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), + Expr::Variable(x) => { + match stack[(stack.len() - x.3.unwrap().get())].1 { + ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { - Err(PERR::AssignmentToConstant(name.to_string()).into_err(*var_pos)) + Err(PERR::AssignmentToConstant(x.0.to_string()).into_err(x.4)) } ScopeEntryType::Module => unreachable!(), } } - _ => Err(PERR::AssignmentToCopy.into_err(idx_lhs.position())), + _ => Err(PERR::AssignmentToCopy.into_err(x.0.position())), }, expr if expr.is_constant() => { Err(PERR::AssignmentToConstant("".into()).into_err(lhs.position())) @@ -1394,7 +1403,7 @@ fn parse_op_assignment_stmt<'a>( // lhs op= rhs -> lhs = op(lhs, rhs) let args = vec![lhs_copy, rhs]; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); - let rhs_expr = Expr::FnCall(Box::new(op.into()), None, hash, Box::new(args), None, pos); + let rhs_expr = Expr::FnCall(Box::new((op.into(), None, hash, args, None, pos))); make_assignment_stmt(stack, lhs, rhs_expr, pos) } @@ -1408,59 +1417,64 @@ fn make_dot_expr( Ok(match (lhs, rhs) { // idx_lhs[idx_rhs].rhs // Attach dot chain to the bottom level of indexing chain - (Expr::Index(idx_lhs, idx_rhs, idx_pos), rhs) => Expr::Index( - idx_lhs, - Box::new(make_dot_expr(*idx_rhs, rhs, op_pos, true)?), - idx_pos, - ), + (Expr::Index(x), rhs) => { + Expr::Index(Box::new((x.0, make_dot_expr(x.1, rhs, op_pos, true)?, x.2))) + } // lhs.id - (lhs, rhs @ Expr::Variable(_, None, _, _, _)) | (lhs, rhs @ Expr::Property(_, _)) => { + (lhs, Expr::Variable(x)) if x.1.is_none() => { let lhs = if is_index { lhs.into_property() } else { lhs }; - Expr::Dot(Box::new(lhs), Box::new(rhs.into_property()), op_pos) + Expr::Dot(Box::new(( + lhs, + Expr::Property(Box::new((x.0, x.4))), + op_pos, + ))) + } + (lhs, Expr::Property(x)) => { + let lhs = if is_index { lhs.into_property() } else { lhs }; + Expr::Dot(Box::new((lhs, Expr::Property(x), op_pos))) } // lhs.module::id - syntax error - (_, Expr::Variable(_, Some(modules), _, _, _)) => { + (_, Expr::Variable(x)) if x.1.is_some() => { #[cfg(feature = "no_module")] unreachable!(); #[cfg(not(feature = "no_module"))] - return Err(PERR::PropertyExpected.into_err(modules.get(0).1)); + return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1)); } // lhs.dot_lhs.dot_rhs - (lhs, Expr::Dot(dot_lhs, dot_rhs, dot_pos)) => Expr::Dot( - Box::new(lhs), - Box::new(Expr::Dot( - Box::new(dot_lhs.into_property()), - Box::new(dot_rhs.into_property()), - dot_pos, - )), + (lhs, Expr::Dot(x)) => Expr::Dot(Box::new(( + lhs, + Expr::Dot(Box::new((x.0.into_property(), x.1.into_property(), x.2))), op_pos, - ), + ))), // lhs.idx_lhs[idx_rhs] - (lhs, Expr::Index(idx_lhs, idx_rhs, idx_pos)) => Expr::Dot( - Box::new(lhs), - Box::new(Expr::Index( - Box::new(idx_lhs.into_property()), - Box::new(idx_rhs.into_property()), - idx_pos, - )), + (lhs, Expr::Index(x)) => Expr::Dot(Box::new(( + lhs, + Expr::Index(Box::new((x.0.into_property(), x.1.into_property(), x.2))), op_pos, - ), + ))), // lhs.rhs - (lhs, rhs) => Expr::Dot(Box::new(lhs), Box::new(rhs.into_property()), op_pos), + (lhs, rhs) => Expr::Dot(Box::new((lhs, rhs.into_property(), op_pos))), }) } /// Make an 'in' expression. fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result> { match (&lhs, &rhs) { - (_, Expr::IntegerConstant(_, pos)) - | (_, Expr::And(_, _, pos)) - | (_, Expr::Or(_, _, pos)) - | (_, Expr::In(_, _, pos)) - | (_, Expr::True(pos)) - | (_, Expr::False(pos)) - | (_, Expr::Assignment(_, _, pos)) - | (_, Expr::Unit(pos)) => { + (_, Expr::IntegerConstant(x)) => { + return Err(PERR::MalformedInExpr( + "'in' expression expects a string, array or object map".into(), + ) + .into_err(x.1)) + } + + (_, Expr::And(x)) | (_, Expr::Or(x)) | (_, Expr::In(x)) | (_, Expr::Assignment(x)) => { + return Err(PERR::MalformedInExpr( + "'in' expression expects a string, array or object map".into(), + ) + .into_err(x.2)) + } + + (_, Expr::True(pos)) | (_, Expr::False(pos)) | (_, Expr::Unit(pos)) => { return Err(PERR::MalformedInExpr( "'in' expression expects a string, array or object map".into(), ) @@ -1468,61 +1482,72 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result { + (_, Expr::FloatConstant(x)) => { return Err(PERR::MalformedInExpr( "'in' expression expects a string, array or object map".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // "xxx" in "xxxx", 'x' in "xxxx" - OK! - (Expr::StringConstant(_, _), Expr::StringConstant(_, _)) - | (Expr::CharConstant(_, _), Expr::StringConstant(_, _)) => (), + (Expr::StringConstant(_), Expr::StringConstant(_)) + | (Expr::CharConstant(_), Expr::StringConstant(_)) => (), // 123.456 in "xxxx" #[cfg(not(feature = "no_float"))] - (Expr::FloatConstant(_, pos), Expr::StringConstant(_, _)) => { + (Expr::FloatConstant(x), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a float".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // 123 in "xxxx" - (Expr::IntegerConstant(_, pos), Expr::StringConstant(_, _)) => { + (Expr::IntegerConstant(x), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a number".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // (??? && ???) in "xxxx", (??? || ???) in "xxxx", (??? in ???) in "xxxx", + (Expr::And(x), Expr::StringConstant(_)) + | (Expr::Or(x), Expr::StringConstant(_)) + | (Expr::In(x), Expr::StringConstant(_)) => { + return Err(PERR::MalformedInExpr( + "'in' expression for a string expects a string, not a boolean".into(), + ) + .into_err(x.2)) + } // true in "xxxx", false in "xxxx" - (Expr::And(_, _, pos), Expr::StringConstant(_, _)) - | (Expr::Or(_, _, pos), Expr::StringConstant(_, _)) - | (Expr::In(_, _, pos), Expr::StringConstant(_, _)) - | (Expr::True(pos), Expr::StringConstant(_, _)) - | (Expr::False(pos), Expr::StringConstant(_, _)) => { + (Expr::True(pos), Expr::StringConstant(_)) + | (Expr::False(pos), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not a boolean".into(), ) .into_err(*pos)) } // [???, ???, ???] in "xxxx" - (Expr::Array(_, pos), Expr::StringConstant(_, _)) => { + (Expr::Array(x), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not an array".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // #{...} in "xxxx" - (Expr::Map(_, pos), Expr::StringConstant(_, _)) => { + (Expr::Map(x), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not an object map".into(), ) - .into_err(*pos)) + .into_err(x.1)) } - // (??? = ???) in "xxxx", () in "xxxx" - (Expr::Assignment(_, _, pos), Expr::StringConstant(_, _)) - | (Expr::Unit(pos), Expr::StringConstant(_, _)) => { + // (??? = ???) in "xxxx" + (Expr::Assignment(x), Expr::StringConstant(_)) => { + return Err(PERR::MalformedInExpr( + "'in' expression for a string expects a string, not ()".into(), + ) + .into_err(x.2)) + } + // () in "xxxx" + (Expr::Unit(pos), Expr::StringConstant(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for a string expects a string, not ()".into(), ) @@ -1530,52 +1555,62 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result (), + (Expr::StringConstant(_), Expr::Map(_)) | (Expr::CharConstant(_), Expr::Map(_)) => (), // 123.456 in #{...} #[cfg(not(feature = "no_float"))] - (Expr::FloatConstant(_, pos), Expr::Map(_, _)) => { + (Expr::FloatConstant(x), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a float".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // 123 in #{...} - (Expr::IntegerConstant(_, pos), Expr::Map(_, _)) => { + (Expr::IntegerConstant(x), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a number".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // (??? && ???) in #{...}, (??? || ???) in #{...}, (??? in ???) in #{...}, + (Expr::And(x), Expr::Map(_)) + | (Expr::Or(x), Expr::Map(_)) + | (Expr::In(x), Expr::Map(_)) => { + return Err(PERR::MalformedInExpr( + "'in' expression for an object map expects a string, not a boolean".into(), + ) + .into_err(x.2)) + } // true in #{...}, false in #{...} - (Expr::And(_, _, pos), Expr::Map(_, _)) - | (Expr::Or(_, _, pos), Expr::Map(_, _)) - | (Expr::In(_, _, pos), Expr::Map(_, _)) - | (Expr::True(pos), Expr::Map(_, _)) - | (Expr::False(pos), Expr::Map(_, _)) => { + (Expr::True(pos), Expr::Map(_)) | (Expr::False(pos), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not a boolean".into(), ) .into_err(*pos)) } // [???, ???, ???] in #{..} - (Expr::Array(_, pos), Expr::Map(_, _)) => { + (Expr::Array(x), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not an array".into(), ) - .into_err(*pos)) + .into_err(x.1)) } // #{...} in #{..} - (Expr::Map(_, pos), Expr::Map(_, _)) => { + (Expr::Map(x), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not an object map".into(), ) - .into_err(*pos)) + .into_err(x.1)) } - // (??? = ???) in #{...}, () in #{...} - (Expr::Assignment(_, _, pos), Expr::Map(_, _)) | (Expr::Unit(pos), Expr::Map(_, _)) => { + // (??? = ???) in #{...} + (Expr::Assignment(x), Expr::Map(_)) => { + return Err(PERR::MalformedInExpr( + "'in' expression for an object map expects a string, not ()".into(), + ) + .into_err(x.2)) + } + // () in #{...} + (Expr::Unit(pos), Expr::Map(_)) => { return Err(PERR::MalformedInExpr( "'in' expression for an object map expects a string, not ()".into(), ) @@ -1585,7 +1620,7 @@ fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result (), } - Ok(Expr::In(Box::new(lhs), Box::new(rhs), op_pos)) + Ok(Expr::In(Box::new((lhs, rhs, op_pos)))) } /// Parse a binary expression. @@ -1630,155 +1665,59 @@ fn parse_binary_op<'a>( }; let cmp_default = Some(Box::new(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]; current_lhs = match op_token { - Token::Plus => Expr::FnCall( - Box::new("+".into()), - None, - calc_fn_hash(empty(), "+", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::Minus => Expr::FnCall( - Box::new("-".into()), - None, - calc_fn_hash(empty(), "-", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::Multiply => Expr::FnCall( - Box::new("*".into()), - None, - calc_fn_hash(empty(), "*", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::Divide => Expr::FnCall( - Box::new("/".into()), - None, - calc_fn_hash(empty(), "/", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), + Token::Plus => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Minus => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Multiply => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Divide => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::LeftShift => Expr::FnCall( - Box::new("<<".into()), - None, - calc_fn_hash(empty(), "<<", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::RightShift => Expr::FnCall( - Box::new(">>".into()), - None, - calc_fn_hash(empty(), ">>", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::Modulo => Expr::FnCall( - Box::new("%".into()), - None, - calc_fn_hash(empty(), "%", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::PowerOf => Expr::FnCall( - Box::new("~".into()), - None, - calc_fn_hash(empty(), "~", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), + Token::LeftShift => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::RightShift => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Modulo => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::PowerOf => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), // Comparison operators default to false when passed invalid operands - Token::EqualsTo => Expr::FnCall( - Box::new("==".into()), - None, - calc_fn_hash(empty(), "==", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), - Token::NotEqualsTo => Expr::FnCall( - Box::new("!=".into()), - None, - calc_fn_hash(empty(), "!=", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), - Token::LessThan => Expr::FnCall( - Box::new("<".into()), - None, - calc_fn_hash(empty(), "<", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), - Token::LessThanEqualsTo => Expr::FnCall( - Box::new("<=".into()), - None, - calc_fn_hash(empty(), "<=", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), - Token::GreaterThan => Expr::FnCall( - Box::new(">".into()), - None, - calc_fn_hash(empty(), ">", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), - Token::GreaterThanEqualsTo => Expr::FnCall( - Box::new(">=".into()), - None, - calc_fn_hash(empty(), ">=", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - cmp_default, - pos, - ), + Token::EqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::NotEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::LessThan => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::LessThanEqualsTo => { + Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))) + } + Token::GreaterThan => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::GreaterThanEqualsTo => { + Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))) + } - Token::Or => Expr::Or(Box::new(current_lhs), Box::new(rhs), pos), - Token::And => Expr::And(Box::new(current_lhs), Box::new(rhs), pos), - Token::Ampersand => Expr::FnCall( - Box::new("&".into()), - None, - calc_fn_hash(empty(), "&", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::Pipe => Expr::FnCall( - Box::new("|".into()), - None, - calc_fn_hash(empty(), "|", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), - Token::XOr => Expr::FnCall( - Box::new("^".into()), - None, - calc_fn_hash(empty(), "^", repeat(EMPTY_TYPE_ID()).take(2)), - Box::new(vec![current_lhs, rhs]), - None, - pos, - ), + Token::Or => { + let rhs = args.pop().unwrap(); + let current_lhs = args.pop().unwrap(); + Expr::Or(Box::new((current_lhs, rhs, pos))) + } + Token::And => { + let rhs = args.pop().unwrap(); + let current_lhs = args.pop().unwrap(); + Expr::And(Box::new((current_lhs, rhs, pos))) + } + Token::Ampersand => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Pipe => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::XOr => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::In => make_in_expr(current_lhs, rhs, pos)?, + Token::In => { + let rhs = args.pop().unwrap(); + let current_lhs = args.pop().unwrap(); + make_in_expr(current_lhs, rhs, pos)? + } #[cfg(not(feature = "no_object"))] - Token::Period => make_dot_expr(current_lhs, rhs, pos, false)?, + Token::Period => { + let rhs = args.pop().unwrap(); + let current_lhs = args.pop().unwrap(); + make_dot_expr(current_lhs, rhs, pos, false)? + } token => return Err(PERR::UnknownOperator(token.into()).into_err(pos)), }; @@ -1857,22 +1796,18 @@ fn parse_if<'a>( // if guard { if_body } else ... let else_body = if match_token(input, Token::Else).unwrap_or(false) { - Some(Box::new(if let (Token::If, _) = input.peek().unwrap() { + Some(if let (Token::If, _) = input.peek().unwrap() { // if guard { if_body } else if ... parse_if(input, stack, breakable, allow_stmt_expr)? } else { // if guard { if_body } else { else-body } parse_block(input, stack, breakable, allow_stmt_expr)? - })) + }) } else { None }; - Ok(Stmt::IfThenElse( - Box::new(guard), - Box::new(if_body), - else_body, - )) + Ok(Stmt::IfThenElse(Box::new((guard, if_body, else_body)))) } /// Parse a while loop. @@ -1890,7 +1825,7 @@ fn parse_while<'a>( ensure_not_assignment(input)?; let body = parse_block(input, stack, true, allow_stmt_expr)?; - Ok(Stmt::While(Box::new(guard), Box::new(body))) + Ok(Stmt::While(Box::new((guard, body)))) } /// Parse a loop statement. @@ -1952,7 +1887,7 @@ fn parse_for<'a>( stack.truncate(prev_len); - Ok(Stmt::For(Box::new(name), Box::new(expr), Box::new(body))) + Ok(Stmt::For(Box::new((name, expr, body)))) } /// Parse a variable definition statement. @@ -1981,12 +1916,12 @@ fn parse_let<'a>( // let name = expr ScopeEntryType::Normal => { stack.push((name.clone(), ScopeEntryType::Normal)); - Ok(Stmt::Let(Box::new(name), Some(Box::new(init_value)), pos)) + Ok(Stmt::Let(Box::new((name, Some(init_value), pos)))) } // const name = { expr:constant } ScopeEntryType::Constant if init_value.is_constant() => { stack.push((name.clone(), ScopeEntryType::Constant)); - Ok(Stmt::Const(Box::new(name), Box::new(init_value), pos)) + Ok(Stmt::Const(Box::new((name, init_value, pos)))) } // const name = expr - error ScopeEntryType::Constant => { @@ -2000,11 +1935,11 @@ fn parse_let<'a>( match var_type { ScopeEntryType::Normal => { stack.push((name.clone(), ScopeEntryType::Normal)); - Ok(Stmt::Let(Box::new(name), None, pos)) + Ok(Stmt::Let(Box::new((name, None, pos)))) } ScopeEntryType::Constant => { stack.push((name.clone(), ScopeEntryType::Constant)); - Ok(Stmt::Const(Box::new(name), Box::new(Expr::Unit(pos)), pos)) + Ok(Stmt::Const(Box::new((name, Expr::Unit(pos), pos)))) } // Variable cannot be a module ScopeEntryType::Module => unreachable!(), @@ -2043,7 +1978,7 @@ fn parse_import<'a>( }; stack.push((name.clone(), ScopeEntryType::Module)); - Ok(Stmt::Import(Box::new(expr), Box::new(name), pos)) + Ok(Stmt::Import(Box::new((expr, name, pos)))) } /// Parse an export statement. @@ -2100,7 +2035,7 @@ fn parse_export<'a>(input: &mut Peekable>) -> Result( stack.truncate(prev_len); - Ok(Stmt::Block(statements, pos)) + Ok(Stmt::Block(Box::new((statements, pos)))) } /// Parse an expression as a statement. @@ -2230,14 +2165,21 @@ fn parse_stmt<'a>( match input.peek().unwrap() { // `return`/`throw` at - (Token::EOF, pos) => Ok(Stmt::ReturnWithVal(None, return_type, *pos)), + (Token::EOF, pos) => Ok(Stmt::ReturnWithVal(Box::new((None, return_type, *pos)))), // `return;` or `throw;` - (Token::SemiColon, _) => Ok(Stmt::ReturnWithVal(None, return_type, pos)), + (Token::SemiColon, _) => { + Ok(Stmt::ReturnWithVal(Box::new((None, return_type, pos)))) + } // `return` or `throw` with expression (_, _) => { let expr = parse_expr(input, stack, allow_stmt_expr)?; let pos = expr.position(); - Ok(Stmt::ReturnWithVal(Some(Box::new(expr)), return_type, pos)) + + Ok(Stmt::ReturnWithVal(Box::new(( + Some(expr), + return_type, + pos, + )))) } } } @@ -2329,10 +2271,10 @@ fn parse_fn<'a>( })?; // Parse function body - let body = Box::new(match input.peek().unwrap() { + let body = match input.peek().unwrap() { (Token::LeftBrace, _) => parse_block(input, stack, false, allow_stmt_expr)?, (_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)), - }); + }; let params = params.into_iter().map(|(p, _)| p).collect(); @@ -2477,10 +2419,13 @@ pub fn parse<'a>( /// Returns Some(expression) if conversion is successful. Otherwise None. pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option { match value.0 { + #[cfg(not(feature = "no_float"))] + Union::Float(value) => Some(Expr::FloatConstant(Box::new((value, pos)))), + Union::Unit(_) => Some(Expr::Unit(pos)), - Union::Int(value) => Some(Expr::IntegerConstant(value, pos)), - Union::Char(value) => Some(Expr::CharConstant(value, pos)), - Union::Str(value) => Some(Expr::StringConstant((*value).clone(), pos)), + Union::Int(value) => Some(Expr::IntegerConstant(Box::new((value, pos)))), + Union::Char(value) => Some(Expr::CharConstant(Box::new((value, pos)))), + Union::Str(value) => Some(Expr::StringConstant(Box::new(((*value).clone(), pos)))), Union::Bool(true) => Some(Expr::True(pos)), Union::Bool(false) => Some(Expr::False(pos)), #[cfg(not(feature = "no_index"))] @@ -2491,10 +2436,10 @@ pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option { .collect(); if items.iter().all(Option::is_some) { - Some(Expr::Array( + Some(Expr::Array(Box::new(( items.into_iter().map(Option::unwrap).collect(), pos, - )) + )))) } else { None } @@ -2506,19 +2451,17 @@ pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option { .map(|(k, v)| (k, map_dynamic_to_expr(v, pos), pos)) .collect(); if items.iter().all(|(_, expr, _)| expr.is_some()) { - Some(Expr::Map( + Some(Expr::Map(Box::new(( items .into_iter() .map(|(k, expr, pos)| (k, expr.unwrap(), pos)) .collect(), pos, - )) + )))) } else { None } } - #[cfg(not(feature = "no_float"))] - Union::Float(value) => Some(Expr::FloatConstant(value, pos)), _ => None, } diff --git a/src/scope.rs b/src/scope.rs index 04e11fe6..542bb01b 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -7,7 +7,7 @@ use crate::token::Position; #[cfg(not(feature = "no_module"))] use crate::module::Module; -use crate::stdlib::{borrow::Cow, boxed::Box, iter, vec, vec::Vec}; +use crate::stdlib::{borrow::Cow, boxed::Box, iter, vec::Vec}; /// Type of an entry in the Scope. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] diff --git a/src/token.rs b/src/token.rs index 9692b112..25d26ff7 100644 --- a/src/token.rs +++ b/src/token.rs @@ -206,14 +206,14 @@ pub enum Token { impl Token { /// Get the syntax of the token. - pub fn syntax(&self) -> Cow { + pub fn syntax(&self) -> Cow<'static, str> { use Token::*; match self { IntegerConstant(i) => i.to_string().into(), #[cfg(not(feature = "no_float"))] FloatConstant(f) => f.to_string().into(), - Identifier(s) => s.into(), + Identifier(s) => s.clone().into(), CharConstant(c) => c.to_string().into(), LexError(err) => err.to_string().into(), From 29159b359b151e136c76237ebf2e9389f818c0d8 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 10 May 2020 00:13:49 +0800 Subject: [PATCH 20/20] Refactor. --- src/engine.rs | 158 ++++++++++++++++------------ src/module.rs | 80 ++++----------- src/optimize.rs | 79 +++++++------- src/parser.rs | 262 +++++++++++++++++++++++++---------------------- tests/modules.rs | 13 +-- 5 files changed, 291 insertions(+), 301 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 110b29ee..7b29129c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -890,6 +890,8 @@ impl Engine { match rhs { // xxx.fn_name(arg_expr_list) Expr::FnCall(x) if x.1.is_none() => { + let ((name, pos), modules, hash, args, def_val) = x.as_ref(); + let mut args: Vec<_> = once(obj) .chain( idx_val @@ -898,10 +900,9 @@ impl Engine { .iter_mut(), ) .collect(); - let def_val = x.4.as_deref(); // 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, &x.0, x.2, &mut args, def_val, x.5, 0) + self.exec_fn_call(state, name, *hash, &mut args, def_val.as_ref(), *pos, 0) .map(|v| (v, true)) } // xxx.module::fn_name(...) - syntax error @@ -1034,17 +1035,18 @@ impl Engine { match dot_lhs { // id.??? or id[???] Expr::Variable(x) => { - let index = if state.always_search { None } else { x.3 }; - let (target, typ) = - search_scope(scope, &x.0, x.1.as_ref().map(|m| (m, x.2)), index, x.4)?; + let ((name, pos), modules, hash, 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 (target, typ) = search_scope(scope, &name, mod_and_hash, index, *pos)?; // Constants cannot be modified match typ { ScopeEntryType::Module => unreachable!(), ScopeEntryType::Constant if new_val.is_some() => { return Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( - x.0.clone(), - x.4, + name.clone(), + *pos, ))); } ScopeEntryType::Constant | ScopeEntryType::Normal => (), @@ -1290,9 +1292,10 @@ impl Engine { Expr::StringConstant(x) => Ok(x.0.to_string().into()), Expr::CharConstant(x) => Ok(x.0.into()), Expr::Variable(x) => { - let index = if state.always_search { None } else { x.3 }; - let mod_and_hash = x.1.as_ref().map(|m| (m, x.2)); - let (val, _) = search_scope(scope, &x.0, mod_and_hash, index, x.4)?; + let ((name, pos), modules, hash, 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 (val, _) = search_scope(scope, name, mod_and_hash, index, *pos)?; Ok(val.clone()) } Expr::Property(_) => unreachable!(), @@ -1308,12 +1311,14 @@ impl Engine { match &x.0 { // name = rhs Expr::Variable(x) => { - let index = if state.always_search { None } else { x.3 }; - let mod_and_hash = x.1.as_ref().map(|m| (m, x.2)); - let (value_ptr, typ) = search_scope(scope, &x.0, mod_and_hash, index, x.4)?; + let ((name, pos), modules, hash, 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)?; match typ { ScopeEntryType::Constant => Err(Box::new( - EvalAltResult::ErrorAssignmentToConstant(x.0.clone(), x.4), + EvalAltResult::ErrorAssignmentToConstant(name.clone(), *pos), )), ScopeEntryType::Normal => { *value_ptr = rhs_val; @@ -1375,7 +1380,7 @@ impl Engine { #[cfg(not(feature = "no_object"))] Expr::Map(x) => Ok(Dynamic(Union::Map(Box::new( x.0.iter() - .map(|(key, expr, _)| { + .map(|((key, _), expr)| { self.eval_expr(scope, state, expr, level) .map(|val| (key.clone(), val)) }) @@ -1384,25 +1389,28 @@ impl Engine { // Normal function call Expr::FnCall(x) if x.1.is_none() => { - let mut arg_values = - x.3.iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) - .collect::, _>>()?; + let ((name, pos), _, hash_fn_def, args_expr, def_val) = x.as_ref(); + + let mut arg_values = args_expr + .iter() + .map(|expr| self.eval_expr(scope, state, expr, level)) + .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); let hash_fn_spec = calc_fn_hash(empty(), KEYWORD_EVAL, once(TypeId::of::())); - if x.0 == KEYWORD_EVAL + if name == KEYWORD_EVAL && args.len() == 1 - && !self.has_override(state, hash_fn_spec, x.2) + && !self.has_override(state, hash_fn_spec, *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[0], x.3[0].position()); + let result = + self.eval_script_expr(scope, state, args[0], args_expr[0].position()); if scope.len() != prev_len { // IMPORTANT! If the eval defines new variables in the current scope, @@ -1413,20 +1421,28 @@ impl Engine { result } else { // Normal function call - except for eval (handled above) - let def_value = x.4.as_deref(); - self.exec_fn_call(state, &x.0, x.2, &mut args, def_value, x.5, level) + self.exec_fn_call( + state, + name, + *hash_fn_def, + &mut args, + def_val.as_ref(), + *pos, + level, + ) } } // Module-qualified function call #[cfg(not(feature = "no_module"))] Expr::FnCall(x) if x.1.is_some() => { - let modules = x.1.as_ref().unwrap(); + let ((name, pos), modules, hash_fn_def, args_expr, def_val) = x.as_ref(); + let modules = modules.as_ref().unwrap(); - let mut arg_values = - x.3.iter() - .map(|expr| self.eval_expr(scope, state, expr, level)) - .collect::, _>>()?; + let mut arg_values = args_expr + .iter() + .map(|expr| self.eval_expr(scope, state, expr, level)) + .collect::, _>>()?; let mut args: Vec<_> = arg_values.iter_mut().collect(); @@ -1445,8 +1461,8 @@ impl Engine { }; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = module.get_qualified_scripted_fn(x.2) { - self.call_script_fn(None, state, fn_def, &mut args, x.5, level) + if let Some(fn_def) = module.get_qualified_scripted_fn(*hash_fn_def) { + self.call_script_fn(None, state, fn_def, &mut args, *pos, level) } else { // Then search in Rust functions @@ -1455,13 +1471,13 @@ impl Engine { // 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 hash2 = calc_fn_hash(empty(), "", args.iter().map(|a| a.type_id())); + 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 = x.2 ^ hash2; + let hash = *hash_fn_def ^ hash_fn_args; - match module.get_qualified_fn(&x.0, hash, x.5) { - Ok(func) => func(&mut args, x.5), - Err(_) if x.4.is_some() => Ok(x.4.as_deref().unwrap().clone()), + match module.get_qualified_fn(name, hash, *pos) { + Ok(func) => func(&mut args, *pos), + Err(_) if def_val.is_some() => Ok(def_val.clone().unwrap()), Err(err) => Err(err), } } @@ -1469,35 +1485,41 @@ impl Engine { Expr::In(x) => self.eval_in_expr(scope, state, &x.0, &x.1, level), - Expr::And(x) => Ok((self - .eval_expr(scope, state, &x.0, level)? + Expr::And(x) => { + let (lhs, rhs, _) = x.as_ref(); + Ok((self + .eval_expr(scope, state, lhs, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("AND".into(), x.0.position()) + EvalAltResult::ErrorBooleanArgMismatch("AND".into(), lhs.position()) })? && // Short-circuit using && self - .eval_expr(scope, state, &x.1, level)? + .eval_expr(scope, state, rhs, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("AND".into(), x.1.position()) + EvalAltResult::ErrorBooleanArgMismatch("AND".into(), rhs.position()) })?) - .into()), + .into()) + } - Expr::Or(x) => Ok((self - .eval_expr(scope, state, &x.0, level)? + Expr::Or(x) => { + let (lhs, rhs, _) = x.as_ref(); + Ok((self + .eval_expr(scope, state, lhs, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("OR".into(), x.0.position()) + EvalAltResult::ErrorBooleanArgMismatch("OR".into(), lhs.position()) })? || // Short-circuit using || self - .eval_expr(scope, state, &x.1, level)? + .eval_expr(scope, state, rhs, level)? .as_bool() .map_err(|_| { - EvalAltResult::ErrorBooleanArgMismatch("OR".into(), x.1.position()) + EvalAltResult::ErrorBooleanArgMismatch("OR".into(), rhs.position()) })?) - .into()), + .into()) + } Expr::True(_) => Ok(true.into()), Expr::False(_) => Ok(false.into()), @@ -1633,56 +1655,56 @@ impl Engine { Stmt::Break(pos) => Err(Box::new(EvalAltResult::ErrorLoopBreak(true, *pos))), // Return value - Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Return => { + Stmt::ReturnWithVal(x) if x.1.is_some() && (x.0).0 == ReturnType::Return => { Err(Box::new(EvalAltResult::Return( - self.eval_expr(scope, state, x.0.as_ref().unwrap(), level)?, - x.2, + self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?, + (x.0).1, ))) } // Empty return - Stmt::ReturnWithVal(x) if x.1 == ReturnType::Return => { - Err(Box::new(EvalAltResult::Return(Default::default(), x.2))) + Stmt::ReturnWithVal(x) if (x.0).0 == ReturnType::Return => { + Err(Box::new(EvalAltResult::Return(Default::default(), (x.0).1))) } // Throw value - Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Exception => { - let val = self.eval_expr(scope, state, x.0.as_ref().unwrap(), level)?; + 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()), - x.2, + (x.0).1, ))) } // Empty throw - Stmt::ReturnWithVal(x) if x.1 == ReturnType::Exception => { - Err(Box::new(EvalAltResult::ErrorRuntime("".into(), x.2))) + Stmt::ReturnWithVal(x) if (x.0).0 == ReturnType::Exception => { + Err(Box::new(EvalAltResult::ErrorRuntime("".into(), (x.0).1))) } Stmt::ReturnWithVal(_) => unreachable!(), // Let statement Stmt::Let(x) if x.1.is_some() => { - let val = self.eval_expr(scope, state, x.1.as_ref().unwrap(), level)?; + 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? - let var_name = x.0.clone(); - scope.push_dynamic_value(var_name, ScopeEntryType::Normal, val, false); + scope.push_dynamic_value(var_name.clone(), ScopeEntryType::Normal, val, false); Ok(Default::default()) } Stmt::Let(x) => { + let ((var_name, _), _) = x.as_ref(); // TODO - avoid copying variable name in inner block? - let var_name = x.0.clone(); - scope.push(var_name, ()); + scope.push(var_name.clone(), ()); Ok(Default::default()) } // Const statement Stmt::Const(x) if x.1.is_constant() => { - let val = self.eval_expr(scope, state, &x.1, level)?; + let ((var_name, _), expr) = x.as_ref(); + let val = self.eval_expr(scope, state, &expr, level)?; // TODO - avoid copying variable name in inner block? - let var_name = x.0.clone(); - scope.push_dynamic_value(var_name, ScopeEntryType::Constant, val, true); + scope.push_dynamic_value(var_name.clone(), ScopeEntryType::Constant, val, true); Ok(Default::default()) } @@ -1691,7 +1713,7 @@ impl Engine { // Import statement Stmt::Import(x) => { - let (expr, name, _) = x.as_ref(); + let (expr, (name, _)) = x.as_ref(); #[cfg(feature = "no_module")] unreachable!(); @@ -1725,7 +1747,7 @@ impl Engine { // Export statement Stmt::Export(list) => { - for (id, id_pos, rename) in list.as_ref() { + for ((id, id_pos), rename) in list.as_ref() { let mut found = false; // Mark scope variables as public diff --git a/src/module.rs b/src/module.rs index 75c2b126..0519a44b 100644 --- a/src/module.rs +++ b/src/module.rs @@ -250,7 +250,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_0("calc", || Ok(42_i64), false); + /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.contains_fn(hash)); /// ``` pub fn contains_fn(&self, hash: u64) -> bool { @@ -289,7 +289,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_0("calc", || Ok(42_i64), false); + /// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_0, T: Into>( @@ -297,7 +297,6 @@ impl Module { fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn() -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn() -> FuncReturn + Send + Sync + 'static, - is_private: bool, ) -> u64 { let f = move |_: &mut FnCallArgs, pos| { func() @@ -305,12 +304,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking one parameter into the module, returning a hash key. @@ -323,7 +317,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false); + /// 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>( @@ -331,7 +325,6 @@ impl Module { fn_name: K, #[cfg(not(feature = "sync"))] func: impl Fn(A) -> FuncReturn + 'static, #[cfg(feature = "sync")] func: impl Fn(A) -> FuncReturn + Send + Sync + 'static, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(mem::take(args[0]).cast::()) @@ -339,12 +332,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking one mutable parameter into the module, returning a hash key. @@ -357,7 +345,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }, false); + /// 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>( @@ -365,7 +353,6 @@ impl Module { fn_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, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { func(args[0].downcast_mut::().unwrap()) @@ -373,12 +360,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters into the module, returning a hash key. @@ -393,7 +375,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_2("calc", |x: i64, y: String| { /// Ok(x + y.len() as i64) - /// }, false); + /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_2, A: Variant + Clone, B: Variant + Clone, T: Into>( @@ -401,7 +383,6 @@ impl Module { fn_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, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let a = mem::take(args[0]).cast::(); @@ -412,12 +393,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking two parameters (the first one mutable) into the module, @@ -431,7 +407,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: String| { /// *x += y.len() as i64; Ok(*x) - /// }, false); + /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_2_mut< @@ -444,7 +420,6 @@ impl Module { fn_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, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let b = mem::take(args[1]).cast::(); @@ -455,12 +430,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters into the module, returning a hash key. @@ -475,7 +445,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_3("calc", |x: i64, y: String, z: i64| { /// Ok(x + y.len() as i64 + z) - /// }, false); + /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3< @@ -489,7 +459,6 @@ impl Module { fn_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, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let a = mem::take(args[0]).cast::(); @@ -501,12 +470,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Set a Rust function taking three parameters (the first one mutable) into the module, @@ -522,7 +486,7 @@ impl Module { /// let mut module = Module::new(); /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: String, z: i64| { /// *x += y.len() as i64 + z; Ok(*x) - /// }, false); + /// }); /// assert!(module.get_fn(hash).is_some()); /// ``` pub fn set_fn_3_mut< @@ -536,7 +500,6 @@ impl Module { fn_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, - is_private: bool, ) -> u64 { let f = move |args: &mut FnCallArgs, pos| { let b = mem::take(args[1]).cast::(); @@ -548,12 +511,7 @@ impl Module { .map_err(|err| EvalAltResult::set_position(err, pos)) }; let arg_types = vec![TypeId::of::(), TypeId::of::(), TypeId::of::()]; - let access = if is_private { - FnAccess::Private - } else { - FnAccess::Public - }; - self.set_fn(fn_name.into(), access, arg_types, Box::new(f)) + self.set_fn(fn_name.into(), FnAccess::Public, arg_types, Box::new(f)) } /// Get a Rust function. @@ -567,7 +525,7 @@ impl Module { /// use rhai::Module; /// /// let mut module = Module::new(); - /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1), false); + /// 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> { @@ -694,16 +652,16 @@ impl Module { // 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 hash1 = calc_fn_hash( + let hash_fn_def = calc_fn_hash( qualifiers.iter().map(|v| *v), fn_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 hash2 = calc_fn_hash(empty(), "", params.iter().cloned()); + let hash_fn_args = calc_fn_hash(empty(), "", params.iter().cloned()); // 3) The final hash is the XOR of the two hashes. - let hash = hash1 ^ hash2; + let hash = hash_fn_def ^ hash_fn_args; functions.push((hash, func.clone())); } diff --git a/src/optimize.rs b/src/optimize.rs index 223949d6..09f294c6 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -220,15 +220,13 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - optimize_stmt(x.2, state, false), ))), // let id = expr; - Stmt::Let(x) if x.1.is_some() => Stmt::Let(Box::new(( - x.0, - Some(optimize_expr(x.1.unwrap(), state)), - x.2, - ))), + Stmt::Let(x) if x.1.is_some() => { + Stmt::Let(Box::new((x.0, Some(optimize_expr(x.1.unwrap(), state))))) + } // let id; stmt @ Stmt::Let(_) => stmt, // import expr as id; - Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1, x.2))), + Stmt::Import(x) => Stmt::Import(Box::new((optimize_expr(x.0, state), x.1))), // { block } Stmt::Block(x) => { let orig_len = x.0.len(); // Original number of statements in the block, for change detection @@ -241,9 +239,10 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - .map(|stmt| match stmt { // Add constant into the state Stmt::Const(v) => { - state.push_constant(&v.0, v.1); + let ((name, pos), expr) = *v; + state.push_constant(&name, expr); state.set_dirty(); - Stmt::Noop(v.2) // No need to keep constants + Stmt::Noop(pos) // No need to keep constants } // Optimize the statement _ => optimize_stmt(stmt, state, preserve_result), @@ -336,11 +335,9 @@ fn optimize_stmt<'a>(stmt: Stmt, state: &mut State<'a>, preserve_result: bool) - // expr; Stmt::Expr(expr) => Stmt::Expr(Box::new(optimize_expr(*expr, state))), // return expr; - Stmt::ReturnWithVal(x) if x.0.is_some() => Stmt::ReturnWithVal(Box::new(( - Some(optimize_expr(x.0.unwrap(), state)), - x.1, - x.2, - ))), + Stmt::ReturnWithVal(x) if x.1.is_some() => { + Stmt::ReturnWithVal(Box::new((x.0, Some(optimize_expr(x.1.unwrap(), state))))) + } // All other statements - skip stmt => stmt, } @@ -394,13 +391,13 @@ 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(m), Expr::Property(p)) 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 == &p.0) - .map(|(_, expr, _)| expr.set_position(pos)) + m.0.into_iter().find(|((name, _), _)| name == &p.0) + .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // lhs.rhs @@ -420,13 +417,13 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { a.0.remove(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(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.into_iter().find(|((name, _), _)| name == &s.0) + .map(|(_, expr)| expr.set_position(pos)) .unwrap_or_else(|| Expr::Unit(pos)) } // string[int] @@ -448,7 +445,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { #[cfg(not(feature = "no_object"))] Expr::Map(m) => Expr::Map(Box::new((m.0 .into_iter() - .map(|(key, expr, pos)| (key, optimize_expr(expr, state), pos)) + .map(|((key, pos), expr)| ((key, pos), optimize_expr(expr, state))) .collect(), m.1))), // lhs in rhs Expr::In(x) => match (x.0, x.1) { @@ -465,7 +462,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { // "xxx" in #{...} (Expr::StringConstant(a), Expr::Map(b)) => { state.set_dirty(); - if b.0.iter().find(|(name, _, _)| name == &a.0).is_some() { + if b.0.iter().find(|((name, _), _)| name == &a.0).is_some() { Expr::True(a.1) } else { Expr::False(a.1) @@ -476,7 +473,7 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { state.set_dirty(); let ch = a.0.to_string(); - if b.0.iter().find(|(name, _, _)| name == &ch).is_some() { + if b.0.iter().find(|((name, _), _)| name == &ch).is_some() { Expr::True(a.1) } else { Expr::False(a.1) @@ -527,7 +524,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.as_ref().as_ref())=> { + 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(); Expr::FnCall(x) } @@ -538,25 +535,27 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { && state.optimization_level == OptimizationLevel::Full // full optimizations && x.3.iter().all(|expr| expr.is_constant()) // all arguments are constants => { + let ((name, pos), _, _, args, def_value) = x.as_mut(); + // First search in script-defined functions (can override built-in) - if state.fn_lib.iter().find(|(name, len)| *name == x.0 && *len == x.3.len()).is_some() { + 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(); return Expr::FnCall(x); } - let mut arg_values: Vec<_> = x.3.iter().map(Expr::get_constant_value).collect(); + let mut arg_values: Vec<_> = args.iter().map(Expr::get_constant_value).collect(); let mut call_args: Vec<_> = 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 - let arg_for_type_of = if x.0 == KEYWORD_TYPE_OF && call_args.len() == 1 { + let arg_for_type_of = if name == KEYWORD_TYPE_OF && call_args.len() == 1 { state.engine.map_type_name(call_args[0].type_name()) } else { "" }; - call_fn(&state.engine.packages, &state.engine.base_package, &x.0, &mut call_args, x.5).ok() + call_fn(&state.engine.packages, &state.engine.base_package, name, &mut call_args, *pos).ok() .and_then(|result| result.or_else(|| { if !arg_for_type_of.is_empty() { @@ -564,9 +563,9 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { Some(arg_for_type_of.to_string().into()) } else { // Otherwise use the default value, if any - x.4.clone().map(|v| *v) + def_value.clone() } - }).and_then(|result| map_dynamic_to_expr(result, x.5)) + }).and_then(|result| map_dynamic_to_expr(result, *pos)) .map(|expr| { state.set_dirty(); expr @@ -585,11 +584,12 @@ fn optimize_expr<'a>(expr: Expr, state: &mut State<'a>) -> Expr { } // constant-name - Expr::Variable(x) if x.1.is_none() && state.contains_constant(&x.0) => { + Expr::Variable(x) if x.1.is_none() && state.contains_constant(&(x.0).0) => { + let (name, pos) = x.0; state.set_dirty(); // Replace constant with value - state.find_constant(&x.0).expect("should find constant in scope!").clone().set_position(x.4) + state.find_constant(&name).expect("should find constant in scope!").clone().set_position(pos) } // All other expressions - skip @@ -643,9 +643,10 @@ fn optimize<'a>( .enumerate() .map(|(i, stmt)| { match &stmt { - Stmt::Const(x) => { + Stmt::Const(v) => { // Load constants - state.push_constant(&x.0, x.1.clone()); + let ((name, _), expr) = v.as_ref(); + state.push_constant(&name, expr.clone()); stmt // Keep it in the global scope } _ => { @@ -718,12 +719,16 @@ pub fn optimize_into_ast( // {} -> Noop fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { // { return val; } -> val - Stmt::ReturnWithVal(x) if x.0.is_some() && x.1 == ReturnType::Return => { - Stmt::Expr(Box::new(x.0.unwrap())) + Stmt::ReturnWithVal(x) + if x.1.is_some() && (x.0).0 == ReturnType::Return => + { + Stmt::Expr(Box::new(x.1.unwrap())) } // { return; } -> () - Stmt::ReturnWithVal(x) if x.0.is_none() && x.1 == ReturnType::Return => { - Stmt::Expr(Box::new(Expr::Unit(x.2))) + Stmt::ReturnWithVal(x) + if x.1.is_none() && (x.0).0 == ReturnType::Return => + { + Stmt::Expr(Box::new(Expr::Unit((x.0).1))) } // All others stmt => stmt, diff --git a/src/parser.rs b/src/parser.rs index 7a88abfc..8041dcef 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -280,9 +280,9 @@ pub enum Stmt { /// for id in expr { stmt } For(Box<(String, Expr, Stmt)>), /// let id = expr - Let(Box<(String, Option, Position)>), + Let(Box<((String, Position), Option)>), /// const id = expr - Const(Box<(String, Expr, Position)>), + Const(Box<((String, Position), Expr)>), /// { stmt; ... } Block(Box<(Vec, Position)>), /// { stmt } @@ -292,11 +292,11 @@ pub enum Stmt { /// break Break(Position), /// return/throw - ReturnWithVal(Box<(Option, ReturnType, Position)>), + ReturnWithVal(Box<((ReturnType, Position), Option)>), /// import expr as module - Import(Box<(Expr, String, Position)>), + Import(Box<(Expr, (String, Position))>), /// expr id as name, ... - Export(Box)>>), + Export(Box)>>), } impl Stmt { @@ -304,17 +304,17 @@ impl Stmt { pub fn position(&self) -> Position { match self { Stmt::Noop(pos) | Stmt::Continue(pos) | Stmt::Break(pos) => *pos, - Stmt::Let(x) => x.2, - Stmt::Const(x) => x.2, - Stmt::ReturnWithVal(x) => x.2, + Stmt::Let(x) => (x.0).1, + Stmt::Const(x) => (x.0).1, + Stmt::ReturnWithVal(x) => (x.0).1, Stmt::Block(x) => x.1, Stmt::IfThenElse(x) => x.0.position(), Stmt::Expr(x) => x.position(), Stmt::While(x) => x.1.position(), Stmt::Loop(x) => x.position(), Stmt::For(x) => x.2.position(), - Stmt::Import(x) => x.2, - Stmt::Export(x) => x.get(0).unwrap().1, + Stmt::Import(x) => (x.1).1, + Stmt::Export(x) => (x.get(0).unwrap().0).1, } } @@ -379,23 +379,22 @@ pub enum Expr { CharConstant(Box<(char, Position)>), /// String constant. StringConstant(Box<(String, Position)>), - /// Variable access - (variable name, optional modules, hash, optional index, position) - Variable(Box<(String, MRef, u64, Option, Position)>), + /// Variable access - ((variable name, position), optional modules, hash, optional index) + Variable(Box<((String, Position), MRef, u64, Option)>), /// Property access. Property(Box<(String, Position)>), /// { stmt } Stmt(Box<(Stmt, Position)>), - /// func(expr, ... ) - (function name, optional modules, hash, arguments, optional default value, position) + /// func(expr, ... ) - ((function name, position), optional modules, hash, arguments, optional default value) /// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls /// and the function names are predictable, so no need to allocate a new `String`. FnCall( Box<( - Cow<'static, str>, + (Cow<'static, str>, Position), MRef, u64, Vec, - Option>, - Position, + Option, )>, ), /// expr = expr @@ -407,7 +406,7 @@ pub enum Expr { /// [ expr, ... ] Array(Box<(Vec, Position)>), /// #{ name:expr, ... } - Map(Box<(Vec<(String, Expr, Position)>, Position)>), + Map(Box<(Vec<((String, Position), Expr)>, Position)>), /// lhs in rhs In(Box<(Expr, Expr, Position)>), /// lhs && rhs @@ -445,10 +444,10 @@ impl Expr { ))), #[cfg(not(feature = "no_object"))] - Self::Map(x) if x.0.iter().all(|(_, v, _)| v.is_constant()) => { + Self::Map(x) if x.0.iter().all(|(_, v)| v.is_constant()) => { Dynamic(Union::Map(Box::new( x.0.iter() - .map(|(k, v, _)| (k.clone(), v.get_constant_value())) + .map(|((k, _), v)| (k.clone(), v.get_constant_value())) .collect::>(), ))) } @@ -493,8 +492,8 @@ impl Expr { Self::Map(x) => x.1, Self::Property(x) => x.1, Self::Stmt(x) => x.1, - Self::Variable(x) => x.4, - Self::FnCall(x) => x.5, + Self::Variable(x) => (x.0).1, + Self::FnCall(x) => (x.0).1, Self::And(x) | Self::Or(x) | Self::In(x) => x.2, @@ -515,10 +514,10 @@ impl Expr { Self::StringConstant(x) => x.1 = new_pos, Self::Array(x) => x.1 = new_pos, Self::Map(x) => x.1 = new_pos, - Self::Variable(x) => x.4 = new_pos, + Self::Variable(x) => (x.0).1 = new_pos, Self::Property(x) => x.1 = new_pos, Self::Stmt(x) => x.1 = new_pos, - Self::FnCall(x) => x.5 = new_pos, + Self::FnCall(x) => (x.0).1 = new_pos, Self::And(x) => x.2 = new_pos, Self::Or(x) => x.2 = new_pos, Self::In(x) => x.2 = new_pos, @@ -541,7 +540,8 @@ impl Expr { Self::Array(x) => x.0.iter().all(Self::is_pure), Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => { - x.0.is_pure() && x.1.is_pure() + let (lhs, rhs, _) = x.as_ref(); + lhs.is_pure() && rhs.is_pure() } Self::Stmt(x) => x.0.is_pure(), @@ -569,7 +569,7 @@ impl Expr { Self::Array(x) => x.0.iter().all(Self::is_constant), // An map literal is constant if all items are constant - Self::Map(x) => x.0.iter().map(|(_, expr, _)| expr).all(Self::is_constant), + Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_constant), // Check in expression Self::In(x) => match (&x.0, &x.1) { @@ -626,7 +626,10 @@ impl Expr { /// Convert a `Variable` into a `Property`. All other variants are untouched. pub(crate) fn into_property(self) -> Self { match self { - Self::Variable(x) if x.1.is_none() => Self::Property(Box::new((x.0.clone(), x.4))), + Self::Variable(x) if x.1.is_none() => { + let (name, pos) = x.0; + Self::Property(Box::new((name.clone(), pos))) + } _ => self, } } @@ -713,34 +716,31 @@ fn parse_call_expr<'a>( eat_token(input, Token::RightParen); #[cfg(not(feature = "no_module"))] - let hash1 = { + let hash_fn_def = { if let Some(modules) = modules.as_mut() { 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, // i.e. qualifiers + function name + no parameters. - let hash1 = calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()); // 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. - - hash1 + calc_fn_hash(modules.iter().map(|(m, _)| m.as_str()), &id, empty()) } else { calc_fn_hash(empty(), &id, empty()) } }; // Qualifiers (none) + function name + no parameters. #[cfg(feature = "no_module")] - let hash1 = calc_fn_hash(empty(), &id, empty()); + let hash_fn_def = calc_fn_hash(empty(), &id, empty()); return Ok(Expr::FnCall(Box::new(( - id.into(), + (id.into(), begin), modules, - hash1, + hash_fn_def, args, None, - begin, )))); } // id... @@ -756,38 +756,36 @@ fn parse_call_expr<'a>( eat_token(input, Token::RightParen); #[cfg(not(feature = "no_module"))] - let hash1 = { + let hash_fn_def = { if let Some(modules) = modules.as_mut() { 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, // i.e. qualifiers + function name + dummy parameter types (one for each parameter). - let hash1 = calc_fn_hash( - modules.iter().map(|(m, _)| m.as_str()), - &id, - repeat(EMPTY_TYPE_ID()).take(args.len()), - ); // 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. - - hash1 + calc_fn_hash( + modules.iter().map(|(m, _)| m.as_str()), + &id, + repeat(EMPTY_TYPE_ID()).take(args.len()), + ) } else { calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())) } }; // Qualifiers (none) + function name + dummy parameter types (one for each parameter). #[cfg(feature = "no_module")] - let hash1 = calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())); + let hash_fn_def = + calc_fn_hash(empty(), &id, repeat(EMPTY_TYPE_ID()).take(args.len())); return Ok(Expr::FnCall(Box::new(( - id.into(), + (id.into(), begin), modules, - hash1, + hash_fn_def, args, None, - begin, )))); } // id(...args, @@ -1094,7 +1092,7 @@ fn parse_map_literal<'a>( let expr = parse_expr(input, stack, allow_stmt_expr)?; - map.push((name, expr, pos)); + map.push(((name, pos), expr)); match input.peek().unwrap() { (Token::Comma, _) => { @@ -1127,11 +1125,11 @@ fn parse_map_literal<'a>( // Check for duplicating properties map.iter() .enumerate() - .try_for_each(|(i, (k1, _, _))| { + .try_for_each(|(i, ((k1, _), _))| { map.iter() .skip(i + 1) - .find(|(k2, _, _)| k2 == k1) - .map_or_else(|| Ok(()), |(k2, _, pos)| Err((k2, *pos))) + .find(|((k2, _), _)| k2 == k1) + .map_or_else(|| Ok(()), |((k2, pos), _)| Err((k2, *pos))) }) .map_err(|(key, pos)| PERR::DuplicatedProperty(key.to_string()).into_err(pos))?; @@ -1163,7 +1161,7 @@ fn parse_primary<'a>( Token::StringConst(s) => Expr::StringConstant(Box::new((s, pos))), Token::Identifier(s) => { let index = stack.find(&s); - Expr::Variable(Box::new((s, None, 0, index, pos))) + Expr::Variable(Box::new(((s, pos), None, 0, index))) } Token::LeftParen => parse_paren_expr(input, stack, pos, allow_stmt_expr)?, #[cfg(not(feature = "no_index"))] @@ -1191,24 +1189,27 @@ fn parse_primary<'a>( root_expr = match (root_expr, token) { // Function call (Expr::Variable(x), Token::LeftParen) => { - parse_call_expr(input, stack, x.0, x.1, x.4, allow_stmt_expr)? + let ((name, pos), modules, _, _) = *x; + parse_call_expr(input, stack, name, modules, pos, allow_stmt_expr)? } (Expr::Property(x), Token::LeftParen) => { - parse_call_expr(input, stack, x.0, None, x.1, allow_stmt_expr)? + let (name, pos) = *x; + parse_call_expr(input, stack, name, None, pos, allow_stmt_expr)? } // module access #[cfg(not(feature = "no_module"))] - (Expr::Variable(mut x), Token::DoubleColon) => match input.next().unwrap() { + (Expr::Variable(x), Token::DoubleColon) => match input.next().unwrap() { (Token::Identifier(id2), pos2) => { - if let Some(ref mut modules) = x.1 { - modules.push((x.0, x.4)); + let ((name, pos), mut modules, _, index) = *x; + if let Some(ref mut modules) = modules { + modules.push((name, pos)); } else { let mut m: ModuleRef = Default::default(); - m.push((x.0, x.4)); - x.1 = Some(Box::new(m)); + m.push((name, pos)); + modules = Some(Box::new(m)); } - Expr::Variable(Box::new((id2, x.1, 0, x.3, pos2))) + Expr::Variable(Box::new(((id2, pos2), modules, 0, index))) } (_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)), }, @@ -1226,10 +1227,11 @@ fn parse_primary<'a>( // Cache the hash key for module-qualified variables #[cfg(not(feature = "no_module"))] Expr::Variable(x) if x.1.is_some() => { - let modules = x.1.as_mut().unwrap(); + let ((name, _), modules, hash, _) = x.as_mut(); + let modules = modules.as_mut().unwrap(); // Qualifiers + variable name - x.2 = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), &x.0, empty()); + *hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, empty()); modules.set_index(stack.find_module(&modules.get(0).0)); } _ => (), @@ -1292,12 +1294,11 @@ fn parse_unary<'a>( let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); Ok(Expr::FnCall(Box::new(( - op.into(), + (op.into(), pos), None, hash, vec![e], None, - pos, )))) } } @@ -1310,16 +1311,17 @@ 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 op = "!"; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(2)); Ok(Expr::FnCall(Box::new(( - op.into(), + (op.into(), pos), None, hash, - vec![parse_primary(input, stack, allow_stmt_expr)?], - Some(Box::new(false.into())), // NOT operator, when operating on invalid operand, defaults to false - pos, + expr, + Some(false.into()), // NOT operator, when operating on invalid operand, defaults to false )))) } // @@ -1338,11 +1340,12 @@ fn make_assignment_stmt<'a>( match &lhs { Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { - match stack[(stack.len() - x.3.unwrap().get())].1 { + let ((name, name_pos), _, _, index) = x.as_ref(); + match stack[(stack.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { - Err(PERR::AssignmentToConstant(x.0.to_string()).into_err(x.4)) + Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos)) } ScopeEntryType::Module => unreachable!(), } @@ -1350,11 +1353,12 @@ fn make_assignment_stmt<'a>( Expr::Index(x) | Expr::Dot(x) => match &x.0 { Expr::Variable(x) if x.3.is_none() => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), Expr::Variable(x) => { - match stack[(stack.len() - x.3.unwrap().get())].1 { + let ((name, name_pos), _, _, index) = x.as_ref(); + match stack[(stack.len() - index.unwrap().get())].1 { ScopeEntryType::Normal => Ok(Expr::Assignment(Box::new((lhs, rhs, pos)))), // Constant values cannot be assigned to ScopeEntryType::Constant => { - Err(PERR::AssignmentToConstant(x.0.to_string()).into_err(x.4)) + Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos)) } ScopeEntryType::Module => unreachable!(), } @@ -1403,7 +1407,7 @@ fn parse_op_assignment_stmt<'a>( // lhs op= rhs -> lhs = op(lhs, rhs) let args = vec![lhs_copy, rhs]; let hash = calc_fn_hash(empty(), op, repeat(EMPTY_TYPE_ID()).take(args.len())); - let rhs_expr = Expr::FnCall(Box::new((op.into(), None, hash, args, None, pos))); + let rhs_expr = Expr::FnCall(Box::new(((op.into(), pos), None, hash, args, None))); make_assignment_stmt(stack, lhs, rhs_expr, pos) } @@ -1423,15 +1427,13 @@ fn make_dot_expr( // lhs.id (lhs, Expr::Variable(x)) if x.1.is_none() => { let lhs = if is_index { lhs.into_property() } else { lhs }; - Expr::Dot(Box::new(( - lhs, - Expr::Property(Box::new((x.0, x.4))), - op_pos, - ))) + let rhs = Expr::Property(Box::new(x.0)); + Expr::Dot(Box::new((lhs, rhs, op_pos))) } (lhs, Expr::Property(x)) => { let lhs = if is_index { lhs.into_property() } else { lhs }; - Expr::Dot(Box::new((lhs, Expr::Property(x), op_pos))) + let rhs = Expr::Property(x); + Expr::Dot(Box::new((lhs, rhs, op_pos))) } // lhs.module::id - syntax error (_, Expr::Variable(x)) if x.1.is_some() => { @@ -1441,17 +1443,31 @@ fn make_dot_expr( return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1)); } // lhs.dot_lhs.dot_rhs - (lhs, Expr::Dot(x)) => Expr::Dot(Box::new(( - lhs, - Expr::Dot(Box::new((x.0.into_property(), x.1.into_property(), x.2))), - op_pos, - ))), + (lhs, Expr::Dot(x)) => { + let (dot_lhs, dot_rhs, pos) = *x; + Expr::Dot(Box::new(( + lhs, + Expr::Dot(Box::new(( + dot_lhs.into_property(), + dot_rhs.into_property(), + pos, + ))), + op_pos, + ))) + } // lhs.idx_lhs[idx_rhs] - (lhs, Expr::Index(x)) => Expr::Dot(Box::new(( - lhs, - Expr::Index(Box::new((x.0.into_property(), x.1.into_property(), x.2))), - op_pos, - ))), + (lhs, Expr::Index(x)) => { + let (idx_lhs, idx_rhs, pos) = *x; + Expr::Dot(Box::new(( + lhs, + Expr::Index(Box::new(( + idx_lhs.into_property(), + idx_rhs.into_property(), + pos, + ))), + op_pos, + ))) + } // lhs.rhs (lhs, rhs) => Expr::Dot(Box::new((lhs, rhs.into_property(), op_pos))), }) @@ -1664,32 +1680,32 @@ fn parse_binary_op<'a>( rhs }; - let cmp_default = Some(Box::new(false.into())); + 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]; current_lhs = match op_token { - Token::Plus => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::Minus => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::Multiply => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::Divide => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + 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))), + Token::Divide => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), - Token::LeftShift => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::RightShift => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::Modulo => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::PowerOf => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::LeftShift => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::RightShift => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::Modulo => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::PowerOf => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), // Comparison operators default to false when passed invalid operands - Token::EqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), - Token::NotEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), - Token::LessThan => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::EqualsTo => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), + Token::NotEqualsTo => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), + Token::LessThan => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), Token::LessThanEqualsTo => { - Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))) + Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))) } - Token::GreaterThan => Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))), + Token::GreaterThan => Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))), Token::GreaterThanEqualsTo => { - Expr::FnCall(Box::new((op, None, hash, args, cmp_default, pos))) + Expr::FnCall(Box::new(((op, pos), None, hash, args, cmp_def))) } Token::Or => { @@ -1702,9 +1718,9 @@ fn parse_binary_op<'a>( let current_lhs = args.pop().unwrap(); Expr::And(Box::new((current_lhs, rhs, pos))) } - Token::Ampersand => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::Pipe => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), - Token::XOr => Expr::FnCall(Box::new((op, None, hash, args, None, pos))), + Token::Ampersand => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::Pipe => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), + Token::XOr => Expr::FnCall(Box::new(((op, pos), None, hash, args, None))), Token::In => { let rhs = args.pop().unwrap(); @@ -1916,12 +1932,12 @@ fn parse_let<'a>( // let name = expr ScopeEntryType::Normal => { stack.push((name.clone(), ScopeEntryType::Normal)); - Ok(Stmt::Let(Box::new((name, Some(init_value), pos)))) + 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)); - Ok(Stmt::Const(Box::new((name, init_value, pos)))) + Ok(Stmt::Const(Box::new(((name, pos), init_value)))) } // const name = expr - error ScopeEntryType::Constant => { @@ -1935,11 +1951,11 @@ fn parse_let<'a>( match var_type { ScopeEntryType::Normal => { stack.push((name.clone(), ScopeEntryType::Normal)); - Ok(Stmt::Let(Box::new((name, None, pos)))) + Ok(Stmt::Let(Box::new(((name, pos), None)))) } ScopeEntryType::Constant => { stack.push((name.clone(), ScopeEntryType::Constant)); - Ok(Stmt::Const(Box::new((name, Expr::Unit(pos), pos)))) + Ok(Stmt::Const(Box::new(((name, pos), Expr::Unit(pos))))) } // Variable cannot be a module ScopeEntryType::Module => unreachable!(), @@ -1978,7 +1994,7 @@ fn parse_import<'a>( }; stack.push((name.clone(), ScopeEntryType::Module)); - Ok(Stmt::Import(Box::new((expr, name, pos)))) + Ok(Stmt::Import(Box::new((expr, (name, pos))))) } /// Parse an export statement. @@ -2005,7 +2021,7 @@ fn parse_export<'a>(input: &mut Peekable>) -> Result { @@ -2026,14 +2042,14 @@ fn parse_export<'a>(input: &mut Peekable>) -> Result( match input.peek().unwrap() { // `return`/`throw` at - (Token::EOF, pos) => Ok(Stmt::ReturnWithVal(Box::new((None, return_type, *pos)))), + (Token::EOF, pos) => Ok(Stmt::ReturnWithVal(Box::new(((return_type, *pos), None)))), // `return;` or `throw;` (Token::SemiColon, _) => { - Ok(Stmt::ReturnWithVal(Box::new((None, return_type, pos)))) + Ok(Stmt::ReturnWithVal(Box::new(((return_type, pos), None)))) } // `return` or `throw` with expression (_, _) => { @@ -2176,9 +2192,8 @@ fn parse_stmt<'a>( let pos = expr.position(); Ok(Stmt::ReturnWithVal(Box::new(( + (return_type, pos), Some(expr), - return_type, - pos, )))) } } @@ -2448,13 +2463,14 @@ pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option { Union::Map(map) => { let items: Vec<_> = map .into_iter() - .map(|(k, v)| (k, map_dynamic_to_expr(v, pos), pos)) + .map(|(k, v)| ((k, pos), map_dynamic_to_expr(v, pos))) .collect(); - if items.iter().all(|(_, expr, _)| expr.is_some()) { + + if items.iter().all(|(_, expr)| expr.is_some()) { Some(Expr::Map(Box::new(( items .into_iter() - .map(|(k, expr, pos)| (k, expr.unwrap(), pos)) + .map(|((k, pos), expr)| ((k, pos), expr.unwrap())) .collect(), pos, )))) diff --git a/tests/modules.rs b/tests/modules.rs index 1fc4d895..52f6818e 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -18,12 +18,7 @@ fn test_module_sub_module() -> Result<(), Box> { let mut sub_module2 = Module::new(); sub_module2.set_var("answer", 41 as INT); - let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1), false); - let hash_hidden = sub_module2.set_fn_0( - "hidden", - || Err("shouldn't see me!".into()) as Result<(), Box>, - true, - ); + let hash_inc = sub_module2.set_fn_1("inc", |x: INT| Ok(x + 1)); sub_module.set_sub_module("universe", sub_module2); module.set_sub_module("life", sub_module); @@ -36,7 +31,6 @@ fn test_module_sub_module() -> Result<(), Box> { assert!(m2.contains_var("answer")); assert!(m2.contains_fn(hash_inc)); - assert!(m2.contains_fn(hash_hidden)); assert_eq!(m2.get_var_value::("answer").unwrap(), 41); @@ -59,11 +53,6 @@ fn test_module_sub_module() -> Result<(), Box> { )?, 42 ); - assert!(matches!( - *engine.eval_expression_with_scope::<()>(&mut scope, "question::life::universe::hidden()") - .expect_err("should error"), - EvalAltResult::ErrorFunctionNotFound(fn_name, _) if fn_name == "hidden" - )); Ok(()) }