From d36e2d22d1f4aa993a1bfb1854e41c49902f8307 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 2 Dec 2021 12:49:46 +0800 Subject: [PATCH] Minor refactor. --- src/custom_syntax.rs | 1 - src/func/call.rs | 7 +++++-- src/optimizer.rs | 7 ++----- tests/fn_ptr.rs | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/custom_syntax.rs b/src/custom_syntax.rs index a2040dbe..03802854 100644 --- a/src/custom_syntax.rs +++ b/src/custom_syntax.rs @@ -335,7 +335,6 @@ impl Engine { /// * `Ok(None)`: parsing complete and there are no more symbols to match. /// * `Ok(Some(symbol))`: the next symbol to match, which can also be `$expr$`, `$ident$` or `$block$`. /// * `Err(ParseError)`: error that is reflected back to the [`Engine`], normally `ParseError(ParseErrorType::BadInput(LexError::ImproperSymbol(message)), Position::NONE)` to indicate a syntax error, but it can be any [`ParseError`]. - /// pub fn register_custom_syntax_raw( &mut self, key: impl Into, diff --git a/src/func/call.rs b/src/func/call.rs index 64bf299d..aab599c2 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -313,7 +313,7 @@ impl Engine { name: impl AsRef, hash: u64, args: &mut FnCallArgs, - is_method_call: bool, + is_ref_mut: bool, is_op_assign: bool, pos: Position, ) -> Result<(Dynamic, bool), Box> { @@ -331,7 +331,8 @@ impl Engine { // Calling pure function but the first argument is a reference? let mut backup: Option = None; - if is_method_call && func.is_pure() && !args.is_empty() { + if is_ref_mut && func.is_pure() && !args.is_empty() { + // Clone the first argument backup = Some(ArgBackup::new()); backup .as_mut() @@ -397,6 +398,8 @@ impl Engine { }); } + // Error handling + match name { // index getter function not found? #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] diff --git a/src/optimizer.rs b/src/optimizer.rs index a25f4d67..bfbbf9aa 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -22,9 +22,6 @@ use std::{ ops::DerefMut, }; -#[cfg(not(feature = "no_closure"))] -use crate::engine::KEYWORD_IS_SHARED; - /// Level of optimization performed. #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] pub enum OptimizationLevel { @@ -986,7 +983,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) { return; } #[cfg(not(feature = "no_closure"))] - KEYWORD_IS_SHARED if arg_values.len() == 1 => { + crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => { state.set_dirty(); *expr = Expr::from_dynamic(Dynamic::FALSE, *pos); return; @@ -1046,7 +1043,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, chaining: bool) { let result = match x.name.as_str() { KEYWORD_TYPE_OF if arg_values.len() == 1 => Some(state.engine.map_type_name(arg_values[0].type_name()).into()), #[cfg(not(feature = "no_closure"))] - KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE), + crate::engine::KEYWORD_IS_SHARED if arg_values.len() == 1 => Some(Dynamic::FALSE), _ => state.call_fn_with_constant_arguments(&x.name, arg_values) }; diff --git a/tests/fn_ptr.rs b/tests/fn_ptr.rs index bf513a08..5b4164a3 100644 --- a/tests/fn_ptr.rs +++ b/tests/fn_ptr.rs @@ -143,7 +143,7 @@ fn test_fn_ptr_make_closure() -> Result<(), Box> { let fn_ptr = engine.eval_ast::(&ast)?; - move |x: INT| -> Result> { fn_ptr.call(&engine, &ast, (x,)) } + move |x: INT| -> Result { fn_ptr.call(&engine, &ast, (x,)) } }; // 'f' captures: the Engine, the AST, and the closure