Map actual 0 hash to 42.
This commit is contained in:
parent
8506640073
commit
363085efc3
@ -20,6 +20,7 @@ use crate::stdlib::{
|
||||
string::ToString,
|
||||
vec::Vec,
|
||||
};
|
||||
use crate::utils::combine_hashes;
|
||||
use crate::{
|
||||
calc_native_fn_hash, calc_script_fn_hash, Dynamic, Engine, EvalAltResult, FnPtr,
|
||||
ImmutableString, Module, ParseErrorType, Position, Scope, StaticVec, INT,
|
||||
@ -1177,9 +1178,8 @@ impl Engine {
|
||||
// and the actual list of argument `TypeId`'.s
|
||||
let hash_fn_args =
|
||||
calc_native_fn_hash(empty(), "", args.iter().map(|a| a.type_id())).unwrap();
|
||||
// 3) The final hash is the XOR of the two hashes.
|
||||
let hash_qualified_fn =
|
||||
NonZeroU64::new(hash_script.get() ^ hash_fn_args.get()).unwrap();
|
||||
// 3) The two hashes are combined.
|
||||
let hash_qualified_fn = combine_hashes(hash_script, hash_fn_args);
|
||||
|
||||
module.get_qualified_fn(hash_qualified_fn)
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use crate::stdlib::{
|
||||
vec::Vec,
|
||||
};
|
||||
use crate::token::Token;
|
||||
use crate::utils::StraightHasherBuilder;
|
||||
use crate::utils::{combine_hashes, StraightHasherBuilder};
|
||||
use crate::{
|
||||
Dynamic, EvalAltResult, ImmutableString, NativeCallContext, Position, Shared, StaticVec,
|
||||
};
|
||||
@ -1823,10 +1823,9 @@ impl Module {
|
||||
param_types.iter().cloned(),
|
||||
)
|
||||
.unwrap();
|
||||
// 3) The final hash is the XOR of the two hashes.
|
||||
// 3) The two hashes are combined.
|
||||
let hash_qualified_fn =
|
||||
NonZeroU64::new(hash_qualified_script.get() ^ hash_fn_args.get())
|
||||
.unwrap();
|
||||
combine_hashes(hash_qualified_script, hash_fn_args);
|
||||
|
||||
functions.insert(hash_qualified_fn, func.clone());
|
||||
} else if cfg!(not(feature = "no_function")) {
|
||||
|
@ -2797,17 +2797,14 @@ fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Po
|
||||
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
externals.iter().for_each(|x| {
|
||||
args.push(Expr::Variable(Box::new((
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
x.clone().into(),
|
||||
))));
|
||||
let expr = Expr::Variable(Box::new((None, None, None, x.clone().into())));
|
||||
args.push(expr);
|
||||
});
|
||||
|
||||
#[cfg(feature = "no_closure")]
|
||||
externals.into_iter().for_each(|x| {
|
||||
args.push(Expr::Variable(Box::new((None, None, 0, x.clone().into()))));
|
||||
let expr = Expr::Variable(Box::new((None, None, None, x.clone().into())));
|
||||
args.push(expr);
|
||||
});
|
||||
|
||||
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
|
||||
|
43
src/utils.rs
43
src/utils.rs
@ -22,18 +22,23 @@ use crate::Shared;
|
||||
///
|
||||
/// Panics when hashing any data type other than a [`NonZeroU64`].
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct StraightHasher(NonZeroU64);
|
||||
pub struct StraightHasher(u64);
|
||||
|
||||
impl Hasher for StraightHasher {
|
||||
#[inline(always)]
|
||||
fn finish(&self) -> u64 {
|
||||
self.0.get()
|
||||
self.0
|
||||
}
|
||||
#[inline(always)]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
let mut key = [0_u8; 8];
|
||||
key.copy_from_slice(&bytes[..8]); // Panics if fewer than 8 bytes
|
||||
self.0 = NonZeroU64::new(u64::from_le_bytes(key)).unwrap();
|
||||
self.0 = u64::from_le_bytes(key);
|
||||
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
if self.0 == 0 {
|
||||
self.0 = 42;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,10 +51,20 @@ impl BuildHasher for StraightHasherBuilder {
|
||||
|
||||
#[inline(always)]
|
||||
fn build_hasher(&self) -> Self::Hasher {
|
||||
StraightHasher(NonZeroU64::new(1).unwrap())
|
||||
StraightHasher(1)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an instance of the default hasher.
|
||||
pub fn get_hasher() -> impl Hasher {
|
||||
#[cfg(feature = "no_std")]
|
||||
let s: ahash::AHasher = Default::default();
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
let s = crate::stdlib::collections::hash_map::DefaultHasher::new();
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
/// _(INTERNALS)_ Calculate a [`NonZeroU64`] hash key from a namespace-qualified function name and parameter types.
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
@ -87,16 +102,6 @@ pub fn calc_script_fn_hash<'a>(
|
||||
calc_fn_hash(modules, fn_name, Some(num), empty())
|
||||
}
|
||||
|
||||
/// Create an instance of the default hasher.
|
||||
pub fn get_hasher() -> impl Hasher {
|
||||
#[cfg(feature = "no_std")]
|
||||
let s: ahash::AHasher = Default::default();
|
||||
#[cfg(not(feature = "no_std"))]
|
||||
let s = crate::stdlib::collections::hash_map::DefaultHasher::new();
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
/// Calculate a [`NonZeroU64`] hash key from a namespace-qualified function name and parameter types.
|
||||
///
|
||||
/// Module names are passed in via `&str` references from an iterator.
|
||||
@ -123,7 +128,15 @@ fn calc_fn_hash<'a>(
|
||||
} else {
|
||||
params.for_each(|t| t.hash(s));
|
||||
}
|
||||
NonZeroU64::new(s.finish())
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
NonZeroU64::new(s.finish()).or_else(|| NonZeroU64::new(42))
|
||||
}
|
||||
|
||||
/// Combine two [`NonZeroU64`] hashes by taking the XOR of them.
|
||||
#[inline(always)]
|
||||
pub(crate) fn combine_hashes(a: NonZeroU64, b: NonZeroU64) -> NonZeroU64 {
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
NonZeroU64::new(a.get() ^ b.get()).unwrap_or_else(|| NonZeroU64::new(42).unwrap())
|
||||
}
|
||||
|
||||
/// The system immutable string type.
|
||||
|
Loading…
Reference in New Issue
Block a user