Merge pull request #645 from schungx/master

Bug fixes.
This commit is contained in:
Stephen Chung 2022-09-22 00:26:21 +08:00 committed by GitHub
commit 9faab46356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 148 additions and 145 deletions

View File

@ -69,8 +69,8 @@ jobs:
# smoketests for different toolchains # smoketests for different toolchains
- {toolchain: stable, os: windows-latest, experimental: false, flags: ""} - {toolchain: stable, os: windows-latest, experimental: false, flags: ""}
- {toolchain: stable, os: macos-latest, experimental: false, flags: ""} - {toolchain: stable, os: macos-latest, experimental: false, flags: ""}
- {toolchain: beta, os: ubuntu-latest, experimental: false, flags: ""} # data structure size changes - wait for beta to become stable and uncomment
# nightly is a bit volatile #- {toolchain: beta, os: ubuntu-latest, experimental: false, flags: ""}
#- {toolchain: nightly, os: ubuntu-latest, experimental: true, flags: ""} #- {toolchain: nightly, os: ubuntu-latest, experimental: true, flags: ""}
fail-fast: false fail-fast: false
steps: steps:

View File

@ -195,13 +195,14 @@ impl Engine {
/// ///
/// A [`GlobalRuntimeState`] and [`Caches`] need to be passed into the function, which can be /// A [`GlobalRuntimeState`] and [`Caches`] need to be passed into the function, which can be
/// created via [`GlobalRuntimeState::new`] and [`Caches::new`]. /// created via [`GlobalRuntimeState::new`] and [`Caches::new`].
/// This makes repeatedly calling particular functions more efficient as the functions resolution cache ///
/// is kept intact. /// This makes repeatedly calling particular functions more efficient as the functions
/// resolution cache is kept intact.
/// ///
/// # Arguments /// # Arguments
/// ///
/// All the arguments are _consumed_, meaning that they're replaced by `()`. /// All the arguments are _consumed_, meaning that they're replaced by `()`. This is to avoid
/// This is to avoid unnecessarily cloning the arguments. /// unnecessarily cloning the arguments.
/// ///
/// Do not use the arguments after this call. If they are needed afterwards, clone them _before_ /// Do not use the arguments after this call. If they are needed afterwards, clone them _before_
/// calling this function. /// calling this function.
@ -246,6 +247,8 @@ impl Engine {
arg_values: &mut [Dynamic], arg_values: &mut [Dynamic],
) -> RhaiResult { ) -> RhaiResult {
let statements = ast.statements(); let statements = ast.statements();
let lib = &[ast.as_ref()];
let mut this_ptr = this_ptr;
let orig_scope_len = scope.len(); let orig_scope_len = scope.len();
@ -255,28 +258,25 @@ impl Engine {
ast.resolver().cloned(), ast.resolver().cloned(),
); );
let mut result = Ok(Dynamic::UNIT);
if eval_ast && !statements.is_empty() { if eval_ast && !statements.is_empty() {
self.eval_global_statements(scope, global, caches, statements, &[ast.as_ref()], 0)?; result = self.eval_global_statements(scope, global, caches, statements, lib, 0);
if rewind_scope { if rewind_scope {
scope.rewind(orig_scope_len); scope.rewind(orig_scope_len);
} }
} }
let mut this_ptr = this_ptr; result = result.and_then(|_| {
let mut args: StaticVec<_> = arg_values.iter_mut().collect(); let mut args: StaticVec<_> = arg_values.iter_mut().collect();
// Check for data race. // Check for data race.
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
crate::func::call::ensure_no_data_race(name, &args, false)?; crate::func::call::ensure_no_data_race(name, &args, false).map(|_| Dynamic::UNIT)?;
let lib = &[ast.as_ref()]; if let Some(fn_def) = ast.shared_lib().get_script_fn(name, args.len()) {
let fn_def = ast self.call_script_fn(
.shared_lib()
.get_script_fn(name, args.len())
.ok_or_else(|| ERR::ErrorFunctionNotFound(name.into(), Position::NONE))?;
let result = self.call_script_fn(
scope, scope,
global, global,
caches, caches,
@ -287,13 +287,19 @@ impl Engine {
rewind_scope, rewind_scope,
Position::NONE, Position::NONE,
0, 0,
)?; )
} else {
Err(ERR::ErrorFunctionNotFound(name.into(), Position::NONE).into())
}
});
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
global.embedded_module_resolver = orig_embedded_module_resolver; global.embedded_module_resolver = orig_embedded_module_resolver;
} }
let result = result?;
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
if self.debugger.is_some() { if self.debugger.is_some() {
global.debugger.status = crate::eval::DebuggerStatus::Terminate; global.debugger.status = crate::eval::DebuggerStatus::Terminate;

View File

@ -4,7 +4,7 @@
use crate::module::FuncInfo; use crate::module::FuncInfo;
use crate::tokenizer::{is_valid_function_name, Token}; use crate::tokenizer::{is_valid_function_name, Token};
use crate::{Engine, FnAccess, Module, Scope, INT}; use crate::{Engine, FnAccess, FnPtr, Module, Scope, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -545,6 +545,20 @@ fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
let ty = ty.replace(type_name::<crate::FLOAT>(), "float"); let ty = ty.replace(type_name::<crate::FLOAT>(), "float");
#[cfg(not(feature = "no_index"))]
let ty = ty.replace(type_name::<crate::Array>(), "Array");
#[cfg(not(feature = "no_index"))]
let ty = ty.replace(type_name::<crate::Blob>(), "Blob");
#[cfg(not(feature = "no_object"))]
let ty = ty.replace(type_name::<crate::Map>(), "Map");
#[cfg(not(feature = "no_std"))]
let ty = ty.replace(type_name::<crate::Instant>(), "Instant");
let ty = ty.replace(type_name::<FnPtr>(), "FnPtr");
ty.into() ty.into()
} }

View File

@ -666,7 +666,7 @@ impl Expr {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
namespace: super::Namespace::NONE, namespace: super::Namespace::NONE,
name: KEYWORD_FN_PTR.into(), name: KEYWORD_FN_PTR.into(),
hashes: calc_fn_hash(f.fn_name(), 1).into(), hashes: calc_fn_hash(None, f.fn_name(), 1).into(),
args: once(Self::StringConstant(f.fn_name().into(), pos)).collect(), args: once(Self::StringConstant(f.fn_name().into(), pos)).collect(),
capture_parent_scope: false, capture_parent_scope: false,
is_native_operator: false, is_native_operator: false,

View File

@ -74,8 +74,8 @@ impl OpAssignment {
.expect("op-assignment operator") .expect("op-assignment operator")
.literal_syntax(); .literal_syntax();
Self { Self {
hash_op_assign: calc_fn_hash(op.literal_syntax(), 2), hash_op_assign: calc_fn_hash(None, op.literal_syntax(), 2),
hash_op: calc_fn_hash(op_raw, 2), hash_op: calc_fn_hash(None, op_raw, 2),
op_assign: op.literal_syntax(), op_assign: op.literal_syntax(),
op: op_raw, op: op_raw,
pos, pos,

View File

@ -285,8 +285,8 @@ impl GlobalRuntimeState<'_> {
#[must_use] #[must_use]
pub(crate) fn hash_idx_get(&mut self) -> u64 { pub(crate) fn hash_idx_get(&mut self) -> u64 {
if self.fn_hash_indexing == (0, 0) { if self.fn_hash_indexing == (0, 0) {
let n1 = crate::calc_fn_hash(crate::engine::FN_IDX_GET, 2); let n1 = crate::calc_fn_hash(None, crate::engine::FN_IDX_GET, 2);
let n2 = crate::calc_fn_hash(crate::engine::FN_IDX_SET, 3); let n2 = crate::calc_fn_hash(None, crate::engine::FN_IDX_SET, 3);
self.fn_hash_indexing = (n1, n2); self.fn_hash_indexing = (n1, n2);
n1 n1
} else { } else {
@ -298,8 +298,8 @@ impl GlobalRuntimeState<'_> {
#[must_use] #[must_use]
pub(crate) fn hash_idx_set(&mut self) -> u64 { pub(crate) fn hash_idx_set(&mut self) -> u64 {
if self.fn_hash_indexing == (0, 0) { if self.fn_hash_indexing == (0, 0) {
let n1 = crate::calc_fn_hash(crate::engine::FN_IDX_GET, 2); let n1 = crate::calc_fn_hash(None, crate::engine::FN_IDX_GET, 2);
let n2 = crate::calc_fn_hash(crate::engine::FN_IDX_SET, 3); let n2 = crate::calc_fn_hash(None, crate::engine::FN_IDX_SET, 3);
self.fn_hash_indexing = (n1, n2); self.fn_hash_indexing = (n1, n2);
n2 n2
} else { } else {

View File

@ -865,7 +865,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
if global.scope_level == 0 if global.scope_level == 0
&& access == AccessMode::ReadOnly && access == AccessMode::ReadOnly
&& lib.iter().any(|&m| !m.is_empty()) && lib.iter().any(|m| !m.is_empty())
{ {
crate::func::locked_write(global.constants.get_or_insert_with( crate::func::locked_write(global.constants.get_or_insert_with(
|| { || {

View File

@ -198,10 +198,8 @@ impl Engine {
} }
let mut hash = args.as_ref().map_or(hash_base, |args| { let mut hash = args.as_ref().map_or(hash_base, |args| {
combine_hashes( let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id()));
hash_base, combine_hashes(hash_base, hash_params)
calc_fn_params_hash(args.iter().map(|a| a.type_id())),
)
}); });
let cache = caches.fn_resolution_cache_mut(); let cache = caches.fn_resolution_cache_mut();
@ -217,19 +215,21 @@ impl Engine {
loop { loop {
let func = lib let func = lib
.iter() .iter()
.find_map(|&m| m.get_fn(hash).map(|f| (f, m.id()))) .copied()
.or_else(|| { .chain(self.global_modules.iter().map(|m| m.as_ref()))
self.global_modules .find_map(|m| m.get_fn(hash).map(|f| (f, m.id())));
.iter()
.find_map(|m| m.get_fn(hash).map(|f| (f, m.id())))
});
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let func = func.or_else(|| _global.get_qualified_fn(hash)).or_else(|| { let func = if args.is_none() {
// Scripted functions are not exposed globally
func
} else {
func.or_else(|| _global.get_qualified_fn(hash)).or_else(|| {
self.global_sub_modules self.global_sub_modules
.values() .values()
.find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id()))) .find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
}); })
};
if let Some((f, s)) = func { if let Some((f, s)) = func {
// Specific version found // Specific version found
@ -249,7 +249,7 @@ impl Engine {
// Check `Dynamic` parameters for functions with parameters // Check `Dynamic` parameters for functions with parameters
if allow_dynamic && max_bitmask == 0 && num_args > 0 { if allow_dynamic && max_bitmask == 0 && num_args > 0 {
let is_dynamic = lib.iter().any(|&m| m.may_contain_dynamic_fn(hash_base)) let is_dynamic = lib.iter().any(|m| m.may_contain_dynamic_fn(hash_base))
|| self || self
.global_modules .global_modules
.iter() .iter()
@ -611,7 +611,7 @@ impl Engine {
if num_params < 0 || num_params > crate::MAX_USIZE_INT { if num_params < 0 || num_params > crate::MAX_USIZE_INT {
false false
} else { } else {
let hash_script = calc_fn_hash(fn_name.as_str(), num_params as usize); let hash_script = calc_fn_hash(None, fn_name.as_str(), num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script) self.has_script_fn(Some(global), caches, lib, hash_script)
} }
.into(), .into(),
@ -815,7 +815,7 @@ impl Engine {
let fn_name = fn_ptr.fn_name(); let fn_name = fn_ptr.fn_name();
let args_len = call_args.len() + fn_ptr.curry().len(); let args_len = call_args.len() + fn_ptr.curry().len();
// Recalculate hashes // Recalculate hashes
let new_hash = calc_fn_hash(fn_name, args_len).into(); let new_hash = calc_fn_hash(None, fn_name, args_len).into();
// Arguments are passed as-is, adding the curried arguments // Arguments are passed as-is, adding the curried arguments
let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len()); let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len());
curry.extend(fn_ptr.curry().iter().cloned()); curry.extend(fn_ptr.curry().iter().cloned());
@ -857,8 +857,8 @@ impl Engine {
// Recalculate hash // Recalculate hash
let new_hash = FnCallHashes::from_all( let new_hash = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, args_len), calc_fn_hash(None, fn_name, args_len),
calc_fn_hash(fn_name, args_len + 1), calc_fn_hash(None, fn_name, args_len + 1),
); );
// Replace the first argument with the object pointer, adding the curried arguments // Replace the first argument with the object pointer, adding the curried arguments
let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len()); let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len());
@ -944,8 +944,8 @@ impl Engine {
// Recalculate the hash based on the new function name and new arguments // Recalculate the hash based on the new function name and new arguments
hash = FnCallHashes::from_all( hash = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, call_args.len()), calc_fn_hash(None, fn_name, call_args.len()),
calc_fn_hash(fn_name, call_args.len() + 1), calc_fn_hash(None, fn_name, call_args.len() + 1),
); );
} }
} }
@ -1036,9 +1036,9 @@ impl Engine {
// Recalculate hash // Recalculate hash
let args_len = total_args + curry.len(); let args_len = total_args + curry.len();
hashes = if hashes.is_native_only() { hashes = if hashes.is_native_only() {
FnCallHashes::from_native(calc_fn_hash(name, args_len)) FnCallHashes::from_native(calc_fn_hash(None, name, args_len))
} else { } else {
calc_fn_hash(name, args_len).into() calc_fn_hash(None, name, args_len).into()
}; };
} }
// Handle Fn() // Handle Fn()
@ -1110,7 +1110,7 @@ impl Engine {
return Ok(if num_params < 0 || num_params > crate::MAX_USIZE_INT { return Ok(if num_params < 0 || num_params > crate::MAX_USIZE_INT {
false false
} else { } else {
let hash_script = calc_fn_hash(&fn_name, num_params as usize); let hash_script = calc_fn_hash(None, &fn_name, num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script) self.has_script_fn(Some(global), caches, lib, hash_script)
} }
.into()); .into());

View File

@ -91,7 +91,7 @@ pub fn get_hasher() -> ahash::AHasher {
/// The first module name is skipped. Hashing starts from the _second_ module in the chain. /// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_qualified_var_hash<'a>( pub fn calc_var_hash<'a>(
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>, modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
var_name: &str, var_name: &str,
) -> u64 { ) -> u64 {
@ -113,9 +113,11 @@ pub fn calc_qualified_var_hash<'a>(
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name /// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name
/// and the number of parameters, but no parameter types. /// and the number of parameters, but no parameter types.
/// ///
/// Module names are passed in via `&str` references from an iterator. /// Module names making up the namespace are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator. /// Parameter types are passed in via [`TypeId`] values from an iterator.
/// ///
/// If the function is not namespace-qualified, pass [`None`] as the namespace.
///
/// # Zeros /// # Zeros
/// ///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`. /// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
@ -125,15 +127,15 @@ pub fn calc_qualified_var_hash<'a>(
/// The first module name is skipped. Hashing starts from the _second_ module in the chain. /// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_qualified_fn_hash<'a>( pub fn calc_fn_hash<'a>(
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>, namespace: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
fn_name: &str, fn_name: &str,
num: usize, num: usize,
) -> u64 { ) -> u64 {
let s = &mut get_hasher(); let s = &mut get_hasher();
// We always skip the first module // We always skip the first module
let iter = modules.into_iter(); let iter = namespace.into_iter();
let len = iter.len(); let len = iter.len();
iter.skip(1).for_each(|m| m.hash(s)); iter.skip(1).for_each(|m| m.hash(s));
len.hash(s); len.hash(s);
@ -146,20 +148,6 @@ pub fn calc_qualified_fn_hash<'a>(
} }
} }
/// Calculate a non-zero [`u64`] hash key from a non-namespace-qualified function name
/// and the number of parameters, but no parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
#[inline(always)]
#[must_use]
pub fn calc_fn_hash(fn_name: &str, num: usize) -> u64 {
calc_qualified_fn_hash(None, fn_name, num)
}
/// Calculate a non-zero [`u64`] hash key from a list of parameter types. /// Calculate a non-zero [`u64`] hash key from a list of parameter types.
/// ///
/// Parameter types are passed in via [`TypeId`] values from an iterator. /// Parameter types are passed in via [`TypeId`] values from an iterator.

View File

@ -20,8 +20,7 @@ pub use callable_function::CallableFunction;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub use func::Func; pub use func::Func;
pub use hashing::{ pub use hashing::{
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash, calc_fn_hash, calc_fn_params_hash, calc_var_hash, combine_hashes, get_hasher, StraightHashMap,
combine_hashes, get_hasher, StraightHashMap,
}; };
pub use native::{ pub use native::{
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone, locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,

View File

@ -321,11 +321,11 @@ impl<'a> NativeCallContext<'a> {
let hash = if is_method_call { let hash = if is_method_call {
FnCallHashes::from_all( FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, args_len - 1), calc_fn_hash(None, fn_name, args_len - 1),
calc_fn_hash(fn_name, args_len), calc_fn_hash(None, fn_name, args_len),
) )
} else { } else {
calc_fn_hash(fn_name, args_len).into() calc_fn_hash(None, fn_name, args_len).into()
}; };
self.engine() self.engine()

View File

@ -240,7 +240,7 @@ impl Engine {
} }
// First check script-defined functions // First check script-defined functions
let result = lib.iter().any(|&m| m.contains_fn(hash_script)) let result = lib.iter().any(|m| m.contains_fn(hash_script))
// Then check the global namespace and packages // Then check the global namespace and packages
|| self.global_modules.iter().any(|m| m.contains_fn(hash_script)); || self.global_modules.iter().any(|m| m.contains_fn(hash_script));

View File

@ -237,10 +237,7 @@ pub use func::Shared;
/// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag. /// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag.
pub use func::Locked; pub use func::Locked;
pub(crate) use func::{ pub(crate) use func::{calc_fn_hash, calc_fn_params_hash, calc_var_hash, combine_hashes};
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,
combine_hashes,
};
pub use rhai_codegen::*; pub use rhai_codegen::*;

View File

@ -9,8 +9,8 @@ use crate::func::{
}; };
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection}; use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
use crate::{ use crate::{
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, combine_hashes, Dynamic, Identifier, calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Identifier, ImmutableString,
ImmutableString, NativeCallContext, RhaiResultOf, Shared, SmartString, StaticVec, NativeCallContext, RhaiResultOf, Shared, SmartString, StaticVec,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -150,7 +150,7 @@ pub fn calc_native_fn_hash<'a>(
fn_name: &str, fn_name: &str,
params: &[TypeId], params: &[TypeId],
) -> u64 { ) -> u64 {
let hash_script = calc_qualified_fn_hash(modules, fn_name, params.len()); let hash_script = calc_fn_hash(modules, fn_name, params.len());
let hash_params = calc_fn_params_hash(params.iter().copied()); let hash_params = calc_fn_params_hash(params.iter().copied());
combine_hashes(hash_script, hash_params) combine_hashes(hash_script, hash_params)
} }
@ -182,8 +182,8 @@ pub struct Module {
/// Flattened collection of all functions, native Rust and scripted. /// Flattened collection of all functions, native Rust and scripted.
/// including those in sub-modules. /// including those in sub-modules.
all_functions: Option<StraightHashMap<CallableFunction>>, all_functions: Option<StraightHashMap<CallableFunction>>,
/// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters. /// Bloom filter on native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters.
dynamic_functions: BloomFilterU64, dynamic_functions_filter: BloomFilterU64,
/// Iterator functions, keyed by the type producing the iterator. /// Iterator functions, keyed by the type producing the iterator.
type_iterators: Option<BTreeMap<TypeId, Shared<IteratorFn>>>, type_iterators: Option<BTreeMap<TypeId, Shared<IteratorFn>>>,
/// Flattened collection of iterator functions, including those in sub-modules. /// Flattened collection of iterator functions, including those in sub-modules.
@ -300,7 +300,7 @@ impl Module {
all_variables: None, all_variables: None,
functions: StraightHashMap::with_capacity_and_hasher(capacity, Default::default()), functions: StraightHashMap::with_capacity_and_hasher(capacity, Default::default()),
all_functions: None, all_functions: None,
dynamic_functions: BloomFilterU64::new(), dynamic_functions_filter: BloomFilterU64::new(),
type_iterators: None, type_iterators: None,
all_type_iterators: None, all_type_iterators: None,
indexed: true, indexed: true,
@ -442,7 +442,7 @@ impl Module {
self.all_variables = None; self.all_variables = None;
self.functions.clear(); self.functions.clear();
self.all_functions = None; self.all_functions = None;
self.dynamic_functions.clear(); self.dynamic_functions_filter.clear();
self.type_iterators = None; self.type_iterators = None;
self.all_type_iterators = None; self.all_type_iterators = None;
self.indexed = false; self.indexed = false;
@ -662,7 +662,7 @@ impl Module {
let value = Dynamic::from(value); let value = Dynamic::from(value);
if self.indexed { if self.indexed {
let hash_var = crate::calc_qualified_var_hash(Some(""), &ident); let hash_var = crate::calc_var_hash(Some(""), &ident);
self.all_variables self.all_variables
.get_or_insert_with(|| Default::default()) .get_or_insert_with(|| Default::default())
.insert(hash_var, value.clone()); .insert(hash_var, value.clone());
@ -692,7 +692,7 @@ impl Module {
// None + function name + number of arguments. // None + function name + number of arguments.
let num_params = fn_def.params.len(); let num_params = fn_def.params.len();
let hash_script = crate::calc_fn_hash(&fn_def.name, num_params); let hash_script = crate::calc_fn_hash(None, &fn_def.name, num_params);
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
let params_info = fn_def.params.iter().map(Into::into).collect(); let params_info = fn_def.params.iter().map(Into::into).collect();
self.functions.insert( self.functions.insert(
@ -1021,11 +1021,12 @@ impl Module {
}; };
let name = name.as_ref(); let name = name.as_ref();
let hash_fn = calc_native_fn_hash(None, name, &param_types); let hash_script = calc_fn_hash(None, name, param_types.len());
let hash_params = calc_fn_params_hash(param_types.iter().copied());
let hash_fn = combine_hashes(hash_script, hash_params);
if is_dynamic { if is_dynamic {
self.dynamic_functions self.dynamic_functions_filter.mark(hash_script);
.mark(calc_fn_hash(name, param_types.len()));
} }
self.functions.insert( self.functions.insert(
@ -1546,7 +1547,7 @@ impl Module {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool { pub(crate) fn may_contain_dynamic_fn(&self, hash_script: u64) -> bool {
!self.dynamic_functions.is_absent(hash_script) !self.dynamic_functions_filter.is_absent(hash_script)
} }
/// Does the particular namespace-qualified function exist in the [`Module`]? /// Does the particular namespace-qualified function exist in the [`Module`]?
@ -1591,7 +1592,7 @@ impl Module {
None => self.variables = other.variables, None => self.variables = other.variables,
} }
self.functions.extend(other.functions.into_iter()); self.functions.extend(other.functions.into_iter());
self.dynamic_functions += &other.dynamic_functions; self.dynamic_functions_filter += &other.dynamic_functions_filter;
match self.type_iterators { match self.type_iterators {
Some(ref mut m) if other.type_iterators.is_some() => { Some(ref mut m) if other.type_iterators.is_some() => {
m.extend(other.type_iterators.unwrap().into_iter()) m.extend(other.type_iterators.unwrap().into_iter())
@ -1634,7 +1635,7 @@ impl Module {
None => self.variables = other.variables, None => self.variables = other.variables,
} }
self.functions.extend(other.functions.into_iter()); self.functions.extend(other.functions.into_iter());
self.dynamic_functions += &other.dynamic_functions; self.dynamic_functions_filter += &other.dynamic_functions_filter;
match self.type_iterators { match self.type_iterators {
Some(ref mut m) if other.type_iterators.is_some() => { Some(ref mut m) if other.type_iterators.is_some() => {
m.extend(other.type_iterators.unwrap().into_iter()) m.extend(other.type_iterators.unwrap().into_iter())
@ -1684,7 +1685,7 @@ impl Module {
for (&k, v) in &other.functions { for (&k, v) in &other.functions {
self.functions.entry(k).or_insert_with(|| v.clone()); self.functions.entry(k).or_insert_with(|| v.clone());
} }
self.dynamic_functions += &other.dynamic_functions; self.dynamic_functions_filter += &other.dynamic_functions_filter;
if let Some(ref type_iterators) = other.type_iterators { if let Some(ref type_iterators) = other.type_iterators {
let t = self let t = self
.type_iterators .type_iterators
@ -1760,7 +1761,7 @@ impl Module {
}) })
.map(|(&k, v)| (k, v.clone())), .map(|(&k, v)| (k, v.clone())),
); );
self.dynamic_functions += &other.dynamic_functions; self.dynamic_functions_filter += &other.dynamic_functions_filter;
if let Some(ref type_iterators) = other.type_iterators { if let Some(ref type_iterators) = other.type_iterators {
if let Some(ref mut t) = self.type_iterators { if let Some(ref mut t) = self.type_iterators {
@ -1804,7 +1805,7 @@ impl Module {
}) })
.collect(); .collect();
self.dynamic_functions.clear(); self.dynamic_functions_filter.clear();
self.all_functions = None; self.all_functions = None;
self.all_variables = None; self.all_variables = None;
self.all_type_iterators = None; self.all_type_iterators = None;
@ -2126,7 +2127,7 @@ impl Module {
// Index all variables // Index all variables
if let Some(ref v) = module.variables { if let Some(ref v) = module.variables {
for (var_name, value) in v { for (var_name, value) in v {
let hash_var = crate::calc_qualified_var_hash(path.iter().copied(), var_name); let hash_var = crate::calc_var_hash(path.iter().copied(), var_name);
variables.insert(hash_var, value.clone()); variables.insert(hash_var, value.clone());
} }
} }
@ -2135,7 +2136,6 @@ impl Module {
if let Some(ref t) = module.type_iterators { if let Some(ref t) = module.type_iterators {
for (&type_id, func) in t { for (&type_id, func) in t {
type_iterators.insert(type_id, func.clone()); type_iterators.insert(type_id, func.clone());
contains_indexed_global_functions = true;
} }
} }
@ -2160,7 +2160,7 @@ impl Module {
functions.insert(hash_qualified_fn, f.func.clone()); functions.insert(hash_qualified_fn, f.func.clone());
} else if cfg!(not(feature = "no_function")) { } else if cfg!(not(feature = "no_function")) {
let hash_qualified_script = let hash_qualified_script =
crate::calc_qualified_fn_hash(path.iter().copied(), &f.name, f.num_params); crate::calc_fn_hash(path.iter().copied(), &f.name, f.num_params);
functions.insert(hash_qualified_script, f.func.clone()); functions.insert(hash_qualified_script, f.func.clone());
} }
} }
@ -2248,7 +2248,6 @@ impl Module {
self.all_type_iterators self.all_type_iterators
.get_or_insert_with(|| Default::default()) .get_or_insert_with(|| Default::default())
.insert(type_id, func.clone()); .insert(type_id, func.clone());
self.contains_indexed_global_functions = true;
} }
self.type_iterators self.type_iterators
.get_or_insert_with(|| Default::default()) .get_or_insert_with(|| Default::default())

View File

@ -151,7 +151,7 @@ impl<'a> OptimizerState<'a> {
&mut self.caches, &mut self.caches,
lib, lib,
fn_name, fn_name,
calc_fn_hash(fn_name, arg_values.len()), calc_fn_hash(None, fn_name, arg_values.len()),
&mut arg_values.iter_mut().collect::<StaticVec<_>>(), &mut arg_values.iter_mut().collect::<StaticVec<_>>(),
false, false,
false, false,
@ -1229,7 +1229,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
=> { => {
// First search for script-defined functions (can override built-in) // First search for script-defined functions (can override built-in)
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
let has_script_fn = state.lib.iter().any(|&m| m.get_script_fn(&x.name, x.args.len()).is_some()); let has_script_fn = state.lib.iter().copied().any(|m| m.get_script_fn(&x.name, x.args.len()).is_some());
#[cfg(feature = "no_function")] #[cfg(feature = "no_function")]
let has_script_fn = false; let has_script_fn = false;

View File

@ -358,9 +358,9 @@ impl Expr {
Self::Variable(x, .., pos) => { Self::Variable(x, .., pos) => {
let ident = x.3.clone(); let ident = x.3.clone();
let getter = state.get_interned_getter(ident.as_str()); let getter = state.get_interned_getter(ident.as_str());
let hash_get = calc_fn_hash(&getter, 1); let hash_get = calc_fn_hash(None, &getter, 1);
let setter = state.get_interned_setter(ident.as_str()); let setter = state.get_interned_setter(ident.as_str());
let hash_set = calc_fn_hash(&setter, 2); let hash_set = calc_fn_hash(None, &setter, 2);
Self::Property( Self::Property(
Box::new(((getter, hash_get), (setter, hash_set), ident)), Box::new(((getter, hash_get), (setter, hash_set), ident)),
@ -576,7 +576,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let hash = if namespace.is_empty() { let hash = if namespace.is_empty() {
calc_fn_hash(&id, 0) calc_fn_hash(None, &id, 0)
} else { } else {
let root = namespace.root(); let root = namespace.root();
let index = state.find_module(root); let index = state.find_module(root);
@ -600,10 +600,10 @@ impl Engine {
namespace.set_index(index); namespace.set_index(index);
crate::calc_qualified_fn_hash(namespace.iter().map(Ident::as_str), &id, 0) crate::calc_fn_hash(namespace.iter().map(Ident::as_str), &id, 0)
}; };
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
let hash = calc_fn_hash(&id, 0); let hash = calc_fn_hash(None, &id, 0);
let hashes = if is_valid_function_name(&id) { let hashes = if is_valid_function_name(&id) {
hash.into() hash.into()
@ -645,7 +645,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let hash = if namespace.is_empty() { let hash = if namespace.is_empty() {
calc_fn_hash(&id, args.len()) calc_fn_hash(None, &id, args.len())
} else { } else {
let root = namespace.root(); let root = namespace.root();
let index = state.find_module(root); let index = state.find_module(root);
@ -668,14 +668,10 @@ impl Engine {
namespace.set_index(index); namespace.set_index(index);
crate::calc_qualified_fn_hash( crate::calc_fn_hash(namespace.iter().map(Ident::as_str), &id, args.len())
namespace.iter().map(Ident::as_str),
&id,
args.len(),
)
}; };
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
let hash = calc_fn_hash(&id, args.len()); let hash = calc_fn_hash(None, &id, args.len());
let hashes = if is_valid_function_name(&id) { let hashes = if is_valid_function_name(&id) {
hash.into() hash.into()
@ -1470,7 +1466,7 @@ impl Engine {
}, },
)?; )?;
let hash_script = calc_fn_hash(&func.name, func.params.len()); let hash_script = calc_fn_hash(None, &func.name, func.params.len());
lib.insert(hash_script, func.into()); lib.insert(hash_script, func.into());
expr expr
@ -1835,7 +1831,7 @@ impl Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
if let Some((.., namespace, hash, name)) = namespaced_variable { if let Some((.., namespace, hash, name)) = namespaced_variable {
if !namespace.is_empty() { if !namespace.is_empty() {
*hash = crate::calc_qualified_var_hash(namespace.iter().map(Ident::as_str), name); *hash = crate::calc_var_hash(namespace.iter().map(Ident::as_str), name);
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
{ {
@ -1919,7 +1915,7 @@ impl Engine {
Ok(FnCallExpr { Ok(FnCallExpr {
name: state.get_interned_string("-"), name: state.get_interned_string("-"),
hashes: FnCallHashes::from_native(calc_fn_hash("-", 1)), hashes: FnCallHashes::from_native(calc_fn_hash(None, "-", 1)),
args, args,
pos, pos,
is_native_operator: true, is_native_operator: true,
@ -1947,7 +1943,7 @@ impl Engine {
Ok(FnCallExpr { Ok(FnCallExpr {
name: state.get_interned_string("+"), name: state.get_interned_string("+"),
hashes: FnCallHashes::from_native(calc_fn_hash("+", 1)), hashes: FnCallHashes::from_native(calc_fn_hash(None, "+", 1)),
args, args,
pos, pos,
is_native_operator: true, is_native_operator: true,
@ -1966,7 +1962,7 @@ impl Engine {
Ok(FnCallExpr { Ok(FnCallExpr {
name: state.get_interned_string("!"), name: state.get_interned_string("!"),
hashes: FnCallHashes::from_native(calc_fn_hash("!", 1)), hashes: FnCallHashes::from_native(calc_fn_hash(None, "!", 1)),
args, args,
pos, pos,
is_native_operator: true, is_native_operator: true,
@ -2187,8 +2183,8 @@ impl Engine {
// Recalculate hash // Recalculate hash
func.hashes = FnCallHashes::from_all( func.hashes = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
calc_fn_hash(&func.name, func.args.len()), calc_fn_hash(None, &func.name, func.args.len()),
calc_fn_hash(&func.name, func.args.len() + 1), calc_fn_hash(None, &func.name, func.args.len() + 1),
); );
let rhs = Expr::MethodCall(func, func_pos); let rhs = Expr::MethodCall(func, func_pos);
@ -2233,8 +2229,8 @@ impl Engine {
// Recalculate hash // Recalculate hash
func.hashes = FnCallHashes::from_all( func.hashes = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
calc_fn_hash(&func.name, func.args.len()), calc_fn_hash(None, &func.name, func.args.len()),
calc_fn_hash(&func.name, func.args.len() + 1), calc_fn_hash(None, &func.name, func.args.len() + 1),
); );
let new_lhs = BinaryExpr { let new_lhs = BinaryExpr {
@ -2338,7 +2334,7 @@ impl Engine {
settings.ensure_level_within_max_limit(state.max_expr_depth)?; settings.ensure_level_within_max_limit(state.max_expr_depth)?;
let op = op_token.syntax(); let op = op_token.syntax();
let hash = calc_fn_hash(&op, 2); let hash = calc_fn_hash(None, &op, 2);
let op_base = FnCallExpr { let op_base = FnCallExpr {
name: state.get_interned_string(op.as_ref()), name: state.get_interned_string(op.as_ref()),
@ -2412,7 +2408,7 @@ impl Engine {
// Convert into a call to `contains` // Convert into a call to `contains`
FnCallExpr { FnCallExpr {
hashes: calc_fn_hash(OP_CONTAINS, 2).into(), hashes: calc_fn_hash(None, OP_CONTAINS, 2).into(),
args, args,
name: state.get_interned_string(OP_CONTAINS), name: state.get_interned_string(OP_CONTAINS),
..op_base ..op_base
@ -2427,7 +2423,7 @@ impl Engine {
.get(s.as_str()) .get(s.as_str())
.map_or(false, Option::is_some) => .map_or(false, Option::is_some) =>
{ {
let hash = calc_fn_hash(&s, 2); let hash = calc_fn_hash(None, &s, 2);
let pos = args[0].start_position(); let pos = args[0].start_position();
FnCallExpr { FnCallExpr {
@ -3345,7 +3341,7 @@ impl Engine {
let func = func?; let func = func?;
let hash = calc_fn_hash(&func.name, func.params.len()); let hash = calc_fn_hash(None, &func.name, func.params.len());
if !lib.is_empty() && lib.contains_key(&hash) { if !lib.is_empty() && lib.contains_key(&hash) {
return Err(PERR::FnDuplicatedDefinition( return Err(PERR::FnDuplicatedDefinition(
@ -3657,6 +3653,7 @@ impl Engine {
let expr = FnCallExpr { let expr = FnCallExpr {
name: state.get_interned_string(crate::engine::KEYWORD_FN_PTR_CURRY), name: state.get_interned_string(crate::engine::KEYWORD_FN_PTR_CURRY),
hashes: FnCallHashes::from_native(calc_fn_hash( hashes: FnCallHashes::from_native(calc_fn_hash(
None,
crate::engine::KEYWORD_FN_PTR_CURRY, crate::engine::KEYWORD_FN_PTR_CURRY,
num_externals + 1, num_externals + 1,
)), )),

View File

@ -66,7 +66,7 @@ impl Ord for FnMetadata<'_> {
impl<'a> From<&'a FuncInfo> for FnMetadata<'a> { impl<'a> From<&'a FuncInfo> for FnMetadata<'a> {
fn from(info: &'a FuncInfo) -> Self { fn from(info: &'a FuncInfo) -> Self {
let base_hash = calc_fn_hash(&info.name, info.num_params); let base_hash = calc_fn_hash(None, &info.name, info.num_params);
let (typ, full_hash) = if info.func.is_script() { let (typ, full_hash) = if info.func.is_script() {
(FnType::Script, base_hash) (FnType::Script, base_hash)
} else { } else {

View File

@ -7,8 +7,11 @@ use std::{
ops::{Add, AddAssign}, ops::{Add, AddAssign},
}; };
/// Number of bits for a `usize`.
const USIZE_BITS: usize = mem::size_of::<usize>() * 8;
/// Number of `usize` values required for 256 bits. /// Number of `usize` values required for 256 bits.
const SIZE: usize = (256 / 8) / mem::size_of::<usize>(); const SIZE: usize = 256 / USIZE_BITS;
/// A simple bloom filter implementation for `u64` hash values only - i.e. all 64 bits are assumed /// A simple bloom filter implementation for `u64` hash values only - i.e. all 64 bits are assumed
/// to be relatively random. /// to be relatively random.
@ -26,7 +29,7 @@ impl BloomFilterU64 {
#[must_use] #[must_use]
const fn calc_hash(value: u64) -> (usize, usize) { const fn calc_hash(value: u64) -> (usize, usize) {
let hash = (value & 0x00ff) as usize; let hash = (value & 0x00ff) as usize;
(hash / 64, 0x01 << (hash % 64)) (hash / USIZE_BITS, 0x01 << (hash % USIZE_BITS))
} }
/// Create a new [`BloomFilterU64`]. /// Create a new [`BloomFilterU64`].
#[inline(always)] #[inline(always)]