Extract function resolution code.

This commit is contained in:
Stephen Chung 2021-03-01 16:53:03 +08:00
parent b466d58192
commit b7e864bb78
2 changed files with 117 additions and 118 deletions

View File

@ -1,6 +1,5 @@
//! Implement function-calling mechanism for [`Engine`]. //! Implement function-calling mechanism for [`Engine`].
use crate::ast::{Expr, Stmt};
use crate::engine::{ use crate::engine::{
Imports, State, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, 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, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
@ -21,6 +20,10 @@ use crate::stdlib::{
vec::Vec, vec::Vec,
}; };
use crate::utils::combine_hashes; use crate::utils::combine_hashes;
use crate::{
ast::{Expr, Stmt},
fn_native::CallableFunction,
};
use crate::{ use crate::{
calc_native_fn_hash, calc_script_fn_hash, Dynamic, Engine, EvalAltResult, FnPtr, calc_native_fn_hash, calc_script_fn_hash, Dynamic, Engine, EvalAltResult, FnPtr,
ImmutableString, Module, ParseErrorType, Position, Scope, StaticVec, INT, ImmutableString, Module, ParseErrorType, Position, Scope, StaticVec, INT,
@ -178,6 +181,105 @@ impl Engine {
) )
} }
/// Resolve a function call.
///
/// Search order:
/// 1) AST - script functions in the AST
/// 2) Global namespace - functions registered via Engine::register_XXX
/// 3) Global modules - packages
/// 4) Imported modules - functions marked with global namespace
/// 5) Global sub-modules - functions marked with global namespace
#[inline]
fn resolve_function<'s>(
&self,
state: &'s mut State,
lib: &[&Module],
mods: &Imports,
fn_name: &str,
mut hash: NonZeroU64,
args: &mut FnCallArgs,
allow_dynamic: bool,
) -> &'s Option<(CallableFunction, Option<ImmutableString>)> {
fn find_function(
engine: &Engine,
hash: NonZeroU64,
lib: &[&Module],
mods: &Imports,
) -> Option<(CallableFunction, Option<ImmutableString>)> {
lib.iter()
.find_map(|m| {
m.get_fn(hash, false)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
.or_else(|| {
engine
.global_namespace
.get_fn(hash, false)
.cloned()
.map(|f| (f, None))
})
.or_else(|| {
engine.global_modules.iter().find_map(|m| {
m.get_fn(hash, false)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
.or_else(|| {
mods.get_fn(hash)
.map(|(f, source)| (f.clone(), source.cloned()))
})
.or_else(|| {
engine.global_sub_modules.values().find_map(|m| {
m.get_qualified_fn(hash)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
}
&*state
.fn_resolution_cache_mut()
.entry(hash)
.or_insert_with(|| {
let num_args = args.len();
let max_bitmask = if !allow_dynamic {
0
} else {
1usize << args.len().min(MAX_DYNAMIC_PARAMETERS)
};
let mut bitmask = 1usize; // Bitmask of which parameter to replace with `Dynamic`
loop {
match find_function(self, hash, lib, mods) {
// Specific version found
Some(f) => return Some(f),
// Stop when all permutations are exhausted
None if bitmask >= max_bitmask => return None,
// Try all permutations with `Dynamic` wildcards
None => {
hash = calc_native_fn_hash(
empty(),
fn_name,
args.iter().enumerate().map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask != 0 {
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}),
)
.unwrap();
bitmask += 1;
}
}
}
})
}
/// Call a native Rust function registered with the [`Engine`]. /// Call a native Rust function registered with the [`Engine`].
/// ///
/// # WARNING /// # WARNING
@ -202,73 +304,7 @@ impl Engine {
let source = state.source.clone(); let source = state.source.clone();
// Check if function access already in the cache // Check if function access already in the cache
let func = &*state let func = self.resolve_function(state, lib, mods, fn_name, hash_fn, args, true);
.fn_resolution_cache_mut()
.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 should only contain scripts, so technically speaking we don't have to check it
// lib.iter().find_map(|m| m.get_fn(hash, false).map(|f| (f.clone(), m.id_raw().cloned()))).or_else(|| {
// Search order:
// 1) Global namespace - functions registered via Engine::register_XXX
// 2) Global modules - packages
// 3) Imported modules - functions marked with global namespace
// 4) Global sub-modules - functions marked with global namespace
match self
.global_namespace
.get_fn(hash, false)
.cloned()
.map(|f| (f, None))
.or_else(|| {
self.global_modules.iter().find_map(|m| {
m.get_fn(hash, false)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
.or_else(|| {
mods.get_fn(hash)
.map(|(f, source)| (f.clone(), source.cloned()))
})
.or_else(|| {
self.global_sub_modules.values().find_map(|m| {
m.get_qualified_fn(hash)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
}) {
// Specific version found
Some(f) => return Some(f),
// Stop when all permutations are exhausted
_ if bitmask >= max_bitmask => return None,
// Try all permutations with `Dynamic` wildcards
_ => {
hash = calc_native_fn_hash(
empty(),
fn_name,
args.iter().enumerate().map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask != 0 {
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}),
)
.unwrap();
bitmask += 1;
}
}
}
});
if let Some((func, src)) = func { if let Some((func, src)) = func {
assert!(func.is_native()); assert!(func.is_native());
@ -683,43 +719,8 @@ impl Engine {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
_ if hash_script.is_some() => { _ if hash_script.is_some() => {
let hash_script = hash_script.unwrap(); let (func, source) = self
.resolve_function(state, lib, mods, fn_name, hash_script.unwrap(), args, false)
// Check if script function access already in the cache
let (func, source) = state
.fn_resolution_cache_mut()
.entry(hash_script)
.or_insert_with(|| {
// Search order:
// 1) AST - script functions in the AST
// 2) Global modules - packages
// 3) Imported modules - functions marked with global namespace
// 4) Global sub-modules - functions marked with global namespace
lib.iter()
.find_map(|&m| {
m.get_fn(hash_script, false)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
//.or_else(|| self.global_namespace.get_fn(hash_script, false).cloned().map(|f| (f, None)))
.or_else(|| {
self.global_modules.iter().find_map(|m| {
m.get_fn(hash_script, false)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
.or_else(|| {
mods.iter().find_map(|(_, m)| {
m.get_qualified_fn(hash_script)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
.or_else(|| {
self.global_sub_modules.values().find_map(|m| {
m.get_qualified_fn(hash_script)
.map(|f| (f.clone(), m.id_raw().cloned()))
})
})
})
.as_ref() .as_ref()
.map(|(f, s)| (Some(f.clone()), s.clone())) .map(|(f, s)| (Some(f.clone()), s.clone()))
.unwrap_or((None, None)); .unwrap_or((None, None));

View File

@ -1809,19 +1809,17 @@ impl Module {
// Non-private functions defined become module functions // Non-private functions defined become module functions
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
{ ast.lib()
ast.lib() .functions
.functions .values()
.values() .filter(|FuncInfo { access, func, .. }| !access.is_private() && func.is_script())
.filter(|FuncInfo { access, func, .. }| !access.is_private() && func.is_script()) .for_each(|FuncInfo { func, .. }| {
.for_each(|FuncInfo { func, .. }| { // Encapsulate AST environment
// Encapsulate AST environment let mut func = func.get_fn_def().clone();
let mut func = func.get_fn_def().clone(); func.lib = Some(ast.shared_lib());
func.lib = Some(ast.shared_lib()); func.mods = func_mods.clone();
func.mods = func_mods.clone(); module.set_script_fn(func);
module.set_script_fn(func); });
});
}
module.set_id(ast.clone_source()); module.set_id(ast.clone_source());
module.build_index(); module.build_index();