From 43fdf3f9628aa46ebb8fb4bde37a6c7e63d57eb3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 27 Apr 2020 20:43:55 +0800 Subject: [PATCH] FunctionsLib always exist. --- src/api.rs | 7 +++--- src/engine.rs | 59 ++++++++++++++++++++++----------------------------- src/scope.rs | 5 +---- 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/src/api.rs b/src/api.rs index 64f28b42..b0bb779c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -804,7 +804,7 @@ impl Engine { ast.0 .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, Some(ast.1.as_ref()), stmt, 0) + self.eval_stmt(scope, ast.1.as_ref(), stmt, 0) }) .or_else(|err| match *err { EvalAltResult::Return(out, _) => Ok(out), @@ -867,7 +867,7 @@ impl Engine { ast.0 .iter() .try_fold(().into(), |_, stmt| { - self.eval_stmt(scope, Some(ast.1.as_ref()), stmt, 0) + self.eval_stmt(scope, ast.1.as_ref(), stmt, 0) }) .map_or_else( |err| match *err { @@ -930,8 +930,7 @@ 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), Some(&fn_lib), fn_def, &mut args, pos, 0)?; + let result = self.call_fn_from_lib(Some(scope), fn_lib, 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 0ff8351c..b2c8e731 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -436,21 +436,14 @@ fn default_print(s: &str) { /// Search for a variable within the scope, returning its value and index inside the Scope fn search_scope<'a>( scope: &'a mut Scope, - id: &str, + name: &str, begin: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { let ScopeSource { typ, index, .. } = scope - .get(id) - .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin)))?; + .get(name) + .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(name.into(), begin)))?; - Ok(( - scope.get_mut(ScopeSource { - name: id, - typ, - index, - }), - typ, - )) + Ok((scope.get_mut(ScopeSource { name, typ, index }), typ)) } impl Engine { @@ -512,7 +505,7 @@ impl Engine { pub(crate) fn call_fn_raw( &self, scope: Option<&mut Scope>, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, fn_name: &str, args: &mut FnCallArgs, def_val: Option<&Dynamic>, @@ -525,10 +518,10 @@ impl Engine { } #[cfg(feature = "no_function")] - const fn_lib: Option<&FunctionsLib> = None; + const fn_lib: &FunctionsLib = None; // First search in script-defined functions (can override built-in) - if let Some(fn_def) = fn_lib.and_then(|lib| lib.get_function(fn_name, args.len())) { + 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); } @@ -606,7 +599,7 @@ impl Engine { pub(crate) fn call_fn_from_lib( &self, scope: Option<&mut Scope>, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, fn_def: &FnDef, args: &mut FnCallArgs, pos: Position, @@ -666,7 +659,7 @@ impl Engine { } // Has a system function an override? - fn has_override(&self, fn_lib: Option<&FunctionsLib>, name: &str) -> bool { + fn has_override(&self, fn_lib: &FunctionsLib, name: &str) -> bool { let hash = calc_fn_hash(name, once(TypeId::of::())); // First check registered functions @@ -674,13 +667,13 @@ impl Engine { // Then check packages || self.packages.iter().any(|p| p.functions.contains_key(&hash)) // Then check script-defined functions - || fn_lib.map_or(false, |lib| lib.has_function(name, 1)) + || fn_lib.has_function(name, 1) } // Perform an actual function call, taking care of special functions fn exec_fn_call( &self, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, fn_name: &str, args: &mut [&mut Dynamic], def_val: Option<&Dynamic>, @@ -709,7 +702,7 @@ impl Engine { fn eval_script_expr( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, script: &Dynamic, pos: Position, ) -> Result> { @@ -732,15 +725,13 @@ impl Engine { ))); } - if let Some(lib) = fn_lib { - #[cfg(feature = "sync")] - { - ast.1 = Arc::new(lib.clone()); - } - #[cfg(not(feature = "sync"))] - { - ast.1 = Rc::new(lib.clone()); - } + #[cfg(feature = "sync")] + { + ast.1 = Arc::new(fn_lib.clone()); + } + #[cfg(not(feature = "sync"))] + { + ast.1 = Rc::new(fn_lib.clone()); } // Evaluate the AST @@ -751,7 +742,7 @@ impl Engine { /// Chain-evaluate a dot/index chain. fn eval_dot_index_chain_helper( &self, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, mut target: Target, rhs: &Expr, idx_values: &mut StaticVec, @@ -896,7 +887,7 @@ impl Engine { fn eval_dot_index_chain( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, dot_lhs: &Expr, dot_rhs: &Expr, is_index: bool, @@ -969,7 +960,7 @@ impl Engine { fn eval_indexed_chain( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, expr: &Expr, idx_values: &mut StaticVec, size: usize, @@ -1083,7 +1074,7 @@ impl Engine { fn eval_in_expr( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, lhs: &Expr, rhs: &Expr, level: usize, @@ -1136,7 +1127,7 @@ impl Engine { fn eval_expr( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, expr: &Expr, level: usize, ) -> Result> { @@ -1325,7 +1316,7 @@ impl Engine { pub(crate) fn eval_stmt( &self, scope: &mut Scope, - fn_lib: Option<&FunctionsLib>, + fn_lib: &FunctionsLib, stmt: &Stmt, level: usize, ) -> Result> { diff --git a/src/scope.rs b/src/scope.rs index 5dd23e73..f34905e6 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -392,10 +392,7 @@ impl Default for Scope<'_> { } } -impl<'a, K> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> -where - K: Into>, -{ +impl<'a, K: Into>> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> { fn extend>(&mut self, iter: T) { self.0 .extend(iter.into_iter().map(|(name, typ, value)| Entry {