Remove duplicated functions lookup for script function call.

This commit is contained in:
Stephen Chung 2021-02-11 12:13:30 +08:00
parent 645e1fe583
commit d49581356d

View File

@ -577,7 +577,7 @@ impl Engine {
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s. // Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
let arg_types = args.iter().map(|a| a.type_id()); let arg_types = args.iter().map(|a| a.type_id());
let hash_fn = calc_native_fn_hash(empty(), fn_name, arg_types); let hash_fn = calc_native_fn_hash(empty(), fn_name, arg_types).unwrap();
match fn_name { match fn_name {
// type_of // type_of
@ -587,7 +587,7 @@ impl Engine {
Some(mods), Some(mods),
Some(state), Some(state),
lib, lib,
hash_fn, Some(hash_fn),
hash_script, hash_script,
pub_only, pub_only,
) => ) =>
@ -606,7 +606,7 @@ impl Engine {
Some(mods), Some(mods),
Some(state), Some(state),
lib, lib,
hash_fn, Some(hash_fn),
hash_script, hash_script,
pub_only, pub_only,
) => ) =>
@ -622,14 +622,11 @@ impl Engine {
.into() .into()
} }
// Script-like function found
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
_ if hash_script.is_some() _ if hash_script.is_some() => {
&& self.has_override(Some(mods), Some(state), lib, None, hash_script, pub_only) =>
{
let hash_script = hash_script.unwrap(); let hash_script = hash_script.unwrap();
// Check if function access already in the cache // Check if script function access already in the cache
let (func, source) = state let (func, source) = state
.fn_resolution_cache_mut() .fn_resolution_cache_mut()
.entry(hash_script) .entry(hash_script)
@ -649,9 +646,11 @@ impl Engine {
//.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.id_raw().clone())))) //.or_else(|| mods.iter().find_map(|(_, m)| m.get_qualified_fn(hash_script).map(|f| (f, m.id_raw().clone()))))
}) })
.as_ref() .as_ref()
.map(|(f, s)| (f.clone(), s.clone())) .map(|(f, s)| (Some(f.clone()), s.clone()))
.unwrap(); .unwrap_or((None, None));
if let Some(func) = func {
// Script function call
assert!(func.is_script()); assert!(func.is_script());
let func = func.get_fn_def(); let func = func.get_fn_def();
@ -708,8 +707,9 @@ impl Engine {
let level = _level + 1; let level = _level + 1;
let result = self let result = self.call_script_fn(
.call_script_fn(scope, mods, state, lib, &mut None, func, args, pos, level); scope, mods, state, lib, &mut None, func, args, pos, level,
);
// Restore the original source // Restore the original source
state.source = orig_source; state.source = orig_source;
@ -721,20 +721,17 @@ impl Engine {
}; };
Ok((result, false)) Ok((result, false))
} else {
// Native function call
self.call_native_fn(
mods, state, lib, fn_name, hash_fn, args, is_ref, pub_only, pos, def_val,
)
}
} }
// Normal native function call // Native function call
_ => self.call_native_fn( _ => self.call_native_fn(
mods, mods, state, lib, fn_name, hash_fn, args, is_ref, pub_only, pos, def_val,
state,
lib,
fn_name,
hash_fn.unwrap(),
args,
is_ref,
pub_only,
pos,
def_val,
), ),
} }
} }