Change hash to hashes.

This commit is contained in:
Stephen Chung 2021-04-20 22:26:08 +08:00
parent 61d06183ea
commit 8f1bff2ded
7 changed files with 60 additions and 60 deletions

View File

@ -1369,14 +1369,14 @@ impl OpAssignment {
///
/// This type is volatile and may change.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Default)]
pub struct FnCallHash {
pub struct FnCallHashes {
/// Pre-calculated hash for a script-defined function ([`None`] if native functions only).
pub script: Option<u64>,
/// Pre-calculated hash for a native Rust function with no parameter types.
pub native: u64,
}
impl fmt::Debug for FnCallHash {
impl fmt::Debug for FnCallHashes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(script) = self.script {
if script == self.native {
@ -1390,7 +1390,7 @@ impl fmt::Debug for FnCallHash {
}
}
impl FnCallHash {
impl FnCallHashes {
/// Create a [`FnCallHash`] with only the native Rust hash.
#[inline(always)]
pub fn from_native(hash: u64) -> Self {
@ -1444,8 +1444,8 @@ impl FnCallHash {
/// This type is volatile and may change.
#[derive(Clone, Default, Hash)]
pub struct FnCallExpr {
/// Pre-calculated hash.
pub hash: FnCallHash,
/// Pre-calculated hashes.
pub hashes: FnCallHashes,
/// Does this function call capture the parent scope?
pub capture: bool,
/// List of function call argument expressions.
@ -1713,7 +1713,7 @@ impl fmt::Debug for Expr {
ff.field("namespace", ns);
}
ff.field("name", &x.name)
.field("hash", &x.hash)
.field("hash", &x.hashes)
.field("args", &x.args);
if !x.constant_args.is_empty() {
ff.field("constant_args", &x.constant_args);

View File

@ -35,7 +35,7 @@ use crate::{calc_fn_hash, Array};
use crate::Map;
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
use crate::ast::FnCallHash;
use crate::ast::FnCallHashes;
pub type Precedence = NonZeroU8;
@ -1186,7 +1186,7 @@ impl Engine {
let val_type_name = target.type_name();
let ((_, val_pos), _) = new_val;
let hash_set = FnCallHash::from_native(calc_fn_hash(
let hash_set = FnCallHashes::from_native(calc_fn_hash(
std::iter::empty(),
FN_IDX_SET,
3,
@ -1228,10 +1228,10 @@ impl Engine {
match rhs {
// xxx.fn_name(arg_expr_list)
Expr::FnCall(x, pos) if x.namespace.is_none() && new_val.is_none() => {
let FnCallExpr { name, hash, .. } = x.as_ref();
let FnCallExpr { name, hashes, .. } = x.as_ref();
let mut args = idx_val.as_fn_call_args();
self.make_method_call(
mods, state, lib, name, *hash, target, &mut args, *pos, level,
mods, state, lib, name, *hashes, target, &mut args, *pos, level,
)
}
// xxx.fn_name(...) = ???
@ -1272,7 +1272,7 @@ impl Engine {
let ((mut new_val, new_pos), (op_info, op_pos)) = new_val.unwrap();
if op_info.is_some() {
let hash = FnCallHash::from_native(*hash_get);
let hash = FnCallHashes::from_native(*hash_get);
let mut args = [target.as_mut()];
let (mut orig_val, _) = self.exec_fn_call(
mods, state, lib, getter, hash, &mut args, is_ref, true, *pos,
@ -1285,7 +1285,7 @@ impl Engine {
new_val = orig_val;
}
let hash = FnCallHash::from_native(*hash_set);
let hash = FnCallHashes::from_native(*hash_set);
let mut args = [target.as_mut(), &mut new_val];
self.exec_fn_call(
mods, state, lib, setter, hash, &mut args, is_ref, true, *pos, None,
@ -1296,7 +1296,7 @@ impl Engine {
// xxx.id
Expr::Property(x) => {
let ((getter, hash_get), _, Ident { pos, .. }) = x.as_ref();
let hash = FnCallHash::from_native(*hash_get);
let hash = FnCallHashes::from_native(*hash_get);
let mut args = [target.as_mut()];
self.exec_fn_call(
mods, state, lib, getter, hash, &mut args, is_ref, true, *pos, None,
@ -1317,10 +1317,10 @@ impl Engine {
}
// {xxx:map}.fn_name(arg_expr_list)[expr] | {xxx:map}.fn_name(arg_expr_list).expr
Expr::FnCall(x, pos) if x.namespace.is_none() => {
let FnCallExpr { name, hash, .. } = x.as_ref();
let FnCallExpr { name, hashes, .. } = x.as_ref();
let mut args = idx_val.as_fn_call_args();
let (val, _) = self.make_method_call(
mods, state, lib, name, *hash, target, &mut args, *pos, level,
mods, state, lib, name, *hashes, target, &mut args, *pos, level,
)?;
val.into()
}
@ -1345,8 +1345,8 @@ impl Engine {
Expr::Property(p) => {
let ((getter, hash_get), (setter, hash_set), Ident { pos, .. }) =
p.as_ref();
let hash_get = FnCallHash::from_native(*hash_get);
let hash_set = FnCallHash::from_native(*hash_set);
let hash_get = FnCallHashes::from_native(*hash_get);
let hash_set = FnCallHashes::from_native(*hash_set);
let arg_values = &mut [target.as_mut(), &mut Default::default()];
let args = &mut arg_values[..1];
let (mut val, updated) = self.exec_fn_call(
@ -1395,10 +1395,10 @@ impl Engine {
}
// xxx.fn_name(arg_expr_list)[expr] | xxx.fn_name(arg_expr_list).expr
Expr::FnCall(f, pos) if f.namespace.is_none() => {
let FnCallExpr { name, hash, .. } = f.as_ref();
let FnCallExpr { name, hashes, .. } = f.as_ref();
let mut args = idx_val.as_fn_call_args();
let (mut val, _) = self.make_method_call(
mods, state, lib, name, *hash, target, &mut args, *pos, level,
mods, state, lib, name, *hashes, target, &mut args, *pos, level,
)?;
let val = &mut val;
let target = &mut val.into();
@ -1704,7 +1704,7 @@ impl Engine {
let type_name = target.type_name();
let args = &mut [target, &mut _idx];
let hash_get =
FnCallHash::from_native(calc_fn_hash(std::iter::empty(), FN_IDX_GET, 2));
FnCallHashes::from_native(calc_fn_hash(std::iter::empty(), FN_IDX_GET, 2));
self.exec_fn_call(
_mods, state, _lib, FN_IDX_GET, hash_get, args, _is_ref, true, idx_pos, None,
_level,
@ -1833,13 +1833,13 @@ impl Engine {
let FnCallExpr {
name,
capture,
hash,
hashes,
args,
constant_args: c_args,
..
} = x.as_ref();
self.make_function_call(
scope, mods, state, lib, this_ptr, name, args, c_args, *hash, *pos, *capture,
scope, mods, state, lib, this_ptr, name, args, c_args, *hashes, *pos, *capture,
level,
)
}
@ -1849,13 +1849,13 @@ impl Engine {
let FnCallExpr {
name,
namespace,
hash,
hashes,
args,
constant_args: c_args,
..
} = x.as_ref();
let namespace = namespace.as_ref();
let hash = hash.native_hash();
let hash = hashes.native_hash();
self.make_qualified_function_call(
scope, mods, state, lib, this_ptr, namespace, name, args, c_args, hash, *pos,
level,

View File

@ -1,6 +1,6 @@
//! Implement function-calling mechanism for [`Engine`].
use crate::ast::FnCallHash;
use crate::ast::FnCallHashes;
use crate::engine::{
FnResolutionCacheEntry, 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,
@ -623,7 +623,7 @@ impl Engine {
state: &mut State,
lib: &[&Module],
fn_name: &str,
hash: FnCallHash,
hash: FnCallHashes,
args: &mut FnCallArgs,
is_ref: bool,
_is_method: bool,
@ -893,7 +893,7 @@ impl Engine {
state: &mut State,
lib: &[&Module],
fn_name: &str,
mut hash: FnCallHash,
mut hash: FnCallHashes,
target: &mut crate::engine::Target,
(call_args, call_arg_positions): &mut (StaticVec<Dynamic>, StaticVec<Position>),
pos: Position,
@ -913,7 +913,7 @@ impl Engine {
let fn_name = fn_ptr.fn_name();
let args_len = call_args.len() + fn_ptr.curry().len();
// Recalculate hashes
let new_hash = FnCallHash::from_script(calc_fn_hash(empty(), fn_name, args_len));
let new_hash = FnCallHashes::from_script(calc_fn_hash(empty(), fn_name, args_len));
// Arguments are passed as-is, adding the curried arguments
let mut curry = fn_ptr.curry().iter().cloned().collect::<StaticVec<_>>();
let mut args = curry
@ -948,7 +948,7 @@ impl Engine {
let fn_name = fn_ptr.fn_name();
let args_len = call_args.len() + fn_ptr.curry().len();
// Recalculate hash
let new_hash = FnCallHash::from_script_and_native(
let new_hash = FnCallHashes::from_script_and_native(
calc_fn_hash(empty(), fn_name, args_len),
calc_fn_hash(empty(), fn_name, args_len + 1),
);
@ -1022,7 +1022,7 @@ impl Engine {
call_arg_positions.insert(i, Position::NONE);
});
// Recalculate the hash based on the new function name and new arguments
hash = FnCallHash::from_script_and_native(
hash = FnCallHashes::from_script_and_native(
calc_fn_hash(empty(), fn_name, call_args.len()),
calc_fn_hash(empty(), fn_name, call_args.len() + 1),
);
@ -1060,7 +1060,7 @@ impl Engine {
fn_name: &str,
args_expr: &[Expr],
constant_args: &[(Dynamic, Position)],
mut hash: FnCallHash,
mut hashes: FnCallHashes,
pos: Position,
capture_scope: bool,
level: usize,
@ -1108,10 +1108,10 @@ impl Engine {
// Recalculate hash
let args_len = total_args + curry.len();
hash = if !hash.is_native_only() {
FnCallHash::from_script(calc_fn_hash(empty(), name, args_len))
hashes = if !hashes.is_native_only() {
FnCallHashes::from_script(calc_fn_hash(empty(), name, args_len))
} else {
FnCallHash::from_native(calc_fn_hash(empty(), name, args_len))
FnCallHashes::from_native(calc_fn_hash(empty(), name, args_len))
};
}
// Handle Fn()
@ -1345,7 +1345,7 @@ impl Engine {
}
self.exec_fn_call(
mods, state, lib, name, hash, &mut args, is_ref, false, pos, capture, level,
mods, state, lib, name, hashes, &mut args, is_ref, false, pos, capture, level,
)
.map(|(v, _)| v)
}

View File

@ -1,6 +1,6 @@
//! Module defining interfaces to native-Rust functions.
use crate::ast::{FnAccess, FnCallHash};
use crate::ast::{FnAccess, FnCallHashes};
use crate::engine::Imports;
use crate::plugin::PluginFunction;
use crate::token::is_valid_identifier;
@ -191,12 +191,12 @@ impl<'a> NativeCallContext<'a> {
let fn_name = fn_name.as_ref();
let hash = if is_method {
FnCallHash::from_script_and_native(
FnCallHashes::from_script_and_native(
calc_fn_hash(empty(), fn_name, args.len() - 1),
calc_fn_hash(empty(), fn_name, args.len()),
)
} else {
FnCallHash::from_script(calc_fn_hash(empty(), fn_name, args.len()))
FnCallHashes::from_script(calc_fn_hash(empty(), fn_name, args.len()))
};
self.engine()

View File

@ -220,7 +220,7 @@ pub use token::{
#[cfg(feature = "internals")]
#[deprecated = "this type is volatile and may change"]
pub use ast::{
ASTNode, BinaryExpr, CustomExpr, Expr, FloatWrapper, FnCallExpr, FnCallHash, Ident,
ASTNode, BinaryExpr, CustomExpr, Expr, FloatWrapper, FnCallExpr, FnCallHashes, Ident,
OpAssignment, ReturnType, ScriptFnDef, Stmt, StmtBlock,
};

View File

@ -893,7 +893,7 @@ fn optimize_expr(expr: &mut Expr, state: &mut State) {
let arg_types: StaticVec<_> = arg_values.iter().map(Dynamic::type_id).collect();
// Search for overloaded operators (can override built-in).
if !has_native_fn(state, x.hash.native_hash(), arg_types.as_ref()) {
if !has_native_fn(state, x.hashes.native_hash(), arg_types.as_ref()) {
if let Some(result) = get_builtin_binary_op_fn(x.name.as_ref(), &arg_values[0], &arg_values[1])
.and_then(|f| {
let ctx = (state.engine, x.name.as_ref(), state.lib).into();

View File

@ -1,7 +1,7 @@
//! Main module defining the lexer and parser.
use crate::ast::{
BinaryExpr, CustomExpr, Expr, FnCallExpr, FnCallHash, Ident, OpAssignment, ReturnType,
BinaryExpr, CustomExpr, Expr, FnCallExpr, FnCallHashes, Ident, OpAssignment, ReturnType,
ScriptFnDef, Stmt, StmtBlock,
};
use crate::dynamic::{AccessMode, Union};
@ -342,10 +342,10 @@ fn parse_fn_call(
calc_fn_hash(empty(), &id, 0)
};
let hash = if is_valid_identifier(id.chars()) {
FnCallHash::from_script(hash)
let hashes = if is_valid_identifier(id.chars()) {
FnCallHashes::from_script(hash)
} else {
FnCallHash::from_native(hash)
FnCallHashes::from_native(hash)
};
return Ok(Expr::FnCall(
@ -353,7 +353,7 @@ fn parse_fn_call(
name: state.get_identifier(id),
capture,
namespace,
hash,
hashes,
args,
..Default::default()
}),
@ -387,10 +387,10 @@ fn parse_fn_call(
calc_fn_hash(empty(), &id, args.len())
};
let hash = if is_valid_identifier(id.chars()) {
FnCallHash::from_script(hash)
let hashes = if is_valid_identifier(id.chars()) {
FnCallHashes::from_script(hash)
} else {
FnCallHash::from_native(hash)
FnCallHashes::from_native(hash)
};
return Ok(Expr::FnCall(
@ -398,7 +398,7 @@ fn parse_fn_call(
name: state.get_identifier(id),
capture,
namespace,
hash,
hashes,
args,
..Default::default()
}),
@ -1348,7 +1348,7 @@ fn parse_unary(
Ok(Expr::FnCall(
Box::new(FnCallExpr {
name: state.get_identifier("-"),
hash: FnCallHash::from_native(calc_fn_hash(empty(), "-", 1)),
hashes: FnCallHashes::from_native(calc_fn_hash(empty(), "-", 1)),
args,
..Default::default()
}),
@ -1374,7 +1374,7 @@ fn parse_unary(
Ok(Expr::FnCall(
Box::new(FnCallExpr {
name: state.get_identifier("+"),
hash: FnCallHash::from_native(calc_fn_hash(empty(), "+", 1)),
hashes: FnCallHashes::from_native(calc_fn_hash(empty(), "+", 1)),
args,
..Default::default()
}),
@ -1393,7 +1393,7 @@ fn parse_unary(
Ok(Expr::FnCall(
Box::new(FnCallExpr {
name: state.get_identifier("!"),
hash: FnCallHash::from_native(calc_fn_hash(empty(), "!", 1)),
hashes: FnCallHashes::from_native(calc_fn_hash(empty(), "!", 1)),
args,
..Default::default()
}),
@ -1595,7 +1595,7 @@ fn make_dot_expr(
}
Expr::FnCall(mut func, func_pos) => {
// Recalculate hash
func.hash = FnCallHash::from_script_and_native(
func.hashes = FnCallHashes::from_script_and_native(
calc_fn_hash(empty(), &func.name, func.num_args()),
calc_fn_hash(empty(), &func.name, func.num_args() + 1),
);
@ -1651,7 +1651,7 @@ fn make_dot_expr(
// lhs.func(...)
(lhs, Expr::FnCall(mut func, func_pos)) => {
// Recalculate hash
func.hash = FnCallHash::from_script_and_native(
func.hashes = FnCallHashes::from_script_and_native(
calc_fn_hash(empty(), &func.name, func.num_args()),
calc_fn_hash(empty(), &func.name, func.num_args() + 1),
);
@ -1739,7 +1739,7 @@ fn parse_binary_op(
let op_base = FnCallExpr {
name: state.get_identifier(op.as_ref()),
hash: FnCallHash::from_native(hash),
hashes: FnCallHashes::from_native(hash),
capture: false,
..Default::default()
};
@ -1804,7 +1804,7 @@ fn parse_binary_op(
let hash = calc_fn_hash(empty(), OP_CONTAINS, 2);
Expr::FnCall(
Box::new(FnCallExpr {
hash: FnCallHash::from_script(hash),
hashes: FnCallHashes::from_script(hash),
args,
name: state.get_identifier(OP_CONTAINS),
..op_base
@ -1824,10 +1824,10 @@ fn parse_binary_op(
Expr::FnCall(
Box::new(FnCallExpr {
hash: if is_valid_identifier(s.chars()) {
FnCallHash::from_script(hash)
hashes: if is_valid_identifier(s.chars()) {
FnCallHashes::from_script(hash)
} else {
FnCallHash::from_native(hash)
FnCallHashes::from_native(hash)
},
args,
..op_base
@ -2894,7 +2894,7 @@ fn make_curry_from_externals(
let expr = Expr::FnCall(
Box::new(FnCallExpr {
name: state.get_identifier(crate::engine::KEYWORD_FN_PTR_CURRY),
hash: FnCallHash::from_native(calc_fn_hash(
hashes: FnCallHashes::from_native(calc_fn_hash(
empty(),
crate::engine::KEYWORD_FN_PTR_CURRY,
num_externals + 1,