From baaa0461bf8d89a43b9823f58f4eeab2551abf22 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 24 Feb 2021 22:40:18 +0800 Subject: [PATCH] Limit Dynamic parameters to 16. --- src/engine.rs | 12 ++++++------ src/fn_call.rs | 24 ++++++++---------------- src/optimize.rs | 1 - 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 1cafc84b..07740bad 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -193,6 +193,8 @@ pub const MAX_EXPR_DEPTH: usize = 64; #[cfg(not(debug_assertions))] pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32; +pub const MAX_DYNAMIC_PARAMETERS: usize = 16; + pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_DEBUG: &str = "debug"; pub const KEYWORD_TYPE_OF: &str = "type_of"; @@ -1679,9 +1681,7 @@ impl Engine { let pos = rhs.position(); if self - .call_native_fn( - mods, state, lib, OP_EQUALS, hash_fn, args, false, false, pos, - )? + .call_native_fn(mods, state, lib, OP_EQUALS, hash_fn, args, false, pos)? .0 .as_bool() .unwrap_or(false) @@ -1961,7 +1961,7 @@ impl Engine { let hash_fn = calc_native_fn_hash(empty(), op, args.iter().map(|a| a.type_id())).unwrap(); - match self.call_native_fn(mods, state, lib, op, hash_fn, args, true, false, op_pos) { + match self.call_native_fn(mods, state, lib, op, hash_fn, args, true, op_pos) { Ok(_) => (), Err(err) if matches!(err.as_ref(), EvalAltResult::ErrorFunctionNotFound(f, _) if f.starts_with(op)) => { @@ -1971,8 +1971,8 @@ impl Engine { calc_native_fn_hash(empty(), op, args.iter().map(|a| a.type_id())).unwrap(); // Run function - let (value, _) = self - .call_native_fn(mods, state, lib, op, hash_fn, args, true, false, op_pos)?; + let (value, _) = + self.call_native_fn(mods, state, lib, op, hash_fn, args, true, op_pos)?; *args[0] = value.flatten(); } diff --git a/src/fn_call.rs b/src/fn_call.rs index edb27f13..9b730c7f 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -4,6 +4,7 @@ use crate::ast::{Expr, Stmt}; use crate::engine::{ search_imports, Imports, State, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF, + MAX_DYNAMIC_PARAMETERS, }; use crate::fn_native::FnCallArgs; use crate::module::NamespaceRef; @@ -172,7 +173,6 @@ impl Engine { hash_fn: NonZeroU64, args: &mut FnCallArgs, is_ref: bool, - pub_only: bool, pos: Position, ) -> Result<(Dynamic, bool), Box> { self.inc_operations(state, pos)?; @@ -185,14 +185,15 @@ impl Engine { .entry(hash_fn) .or_insert_with(|| { let num_args = args.len(); + let max_bitmask = 1usize << args.len().min(MAX_DYNAMIC_PARAMETERS); let mut hash = hash_fn; let mut bitmask = 1usize; // Bitmask of which parameter to replace with `Dynamic` loop { - //lib.get_fn(hash, pub_only).or_else(|| + //lib.get_fn(hash, false).or_else(|| match self .global_namespace - .get_fn(hash, pub_only) + .get_fn(hash, false) .cloned() .map(|f| (f, None)) .or_else(|| { @@ -208,8 +209,8 @@ impl Engine { // Specific version found Some(f) => return Some(f), - // No parameters - _ if num_args == 0 => return None, + // Stop when all permutations are exhausted + _ if bitmask >= max_bitmask => return None, // Try all permutations with `Dynamic` wildcards _ => { @@ -228,11 +229,6 @@ impl Engine { ) .unwrap(); - // Stop when all permutations are exhausted - if hash == hash_fn { - return None; - } - bitmask += 1; } } @@ -764,16 +760,12 @@ impl Engine { Ok((result, false)) } else { // Native function call - self.call_native_fn( - mods, state, lib, fn_name, hash_fn, args, is_ref, pub_only, pos, - ) + self.call_native_fn(mods, state, lib, fn_name, hash_fn, args, is_ref, pos) } } // Native function call - _ => self.call_native_fn( - mods, state, lib, fn_name, hash_fn, args, is_ref, pub_only, pos, - ), + _ => self.call_native_fn(mods, state, lib, fn_name, hash_fn, args, is_ref, pos), } } diff --git a/src/optimize.rs b/src/optimize.rs index e6fb62d2..64b6140d 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -149,7 +149,6 @@ fn call_fn_with_constant_arguments( hash_fn.unwrap(), arg_values.iter_mut().collect::>().as_mut(), false, - true, Position::NONE, ) .ok()