Allow scripted functions in packages.
This commit is contained in:
@@ -444,7 +444,7 @@ impl Engine {
|
||||
//|| self.global_module.contains_fn(hash_script, pub_only)
|
||||
|| self.global_module.contains_fn(hash_fn, pub_only)
|
||||
// Then check packages
|
||||
//|| self.packages.contains_fn(hash_script, pub_only)
|
||||
|| self.packages.contains_fn(hash_script, pub_only)
|
||||
|| self.packages.contains_fn(hash_fn, pub_only)
|
||||
}
|
||||
|
||||
@@ -477,7 +477,17 @@ impl Engine {
|
||||
|
||||
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
|
||||
let arg_types = args.iter().map(|a| a.type_id());
|
||||
let hash_fn = calc_fn_hash(empty(), fn_name, args.len(), arg_types);
|
||||
let hash_fn = calc_fn_hash(
|
||||
empty(),
|
||||
fn_name,
|
||||
if args.is_empty() {
|
||||
// Distinguish between a script function and a native function with no parameters
|
||||
usize::MAX
|
||||
} else {
|
||||
args.len()
|
||||
},
|
||||
arg_types,
|
||||
);
|
||||
|
||||
match fn_name {
|
||||
// type_of
|
||||
@@ -514,9 +524,15 @@ impl Engine {
|
||||
|
||||
// Normal script function call
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
_ if hash_script > 0 && lib.contains_fn(hash_script, pub_only) => {
|
||||
_ if lib.contains_fn(hash_script, pub_only)
|
||||
|| self.packages.contains_fn(hash_script, pub_only) =>
|
||||
{
|
||||
// Get scripted function
|
||||
let func = lib.get_fn(hash_script, pub_only).unwrap().get_fn_def();
|
||||
let func = lib
|
||||
.get_fn(hash_script, pub_only)
|
||||
.or_else(|| self.packages.get_fn(hash_script, pub_only))
|
||||
.unwrap()
|
||||
.get_fn_def();
|
||||
|
||||
let scope = &mut Scope::new();
|
||||
let mods = &mut Imports::new();
|
||||
@@ -559,6 +575,7 @@ impl Engine {
|
||||
|
||||
Ok((result, false))
|
||||
}
|
||||
|
||||
// Normal native function call
|
||||
_ => self.call_native_fn(
|
||||
state, lib, fn_name, hash_fn, args, is_ref, pub_only, def_val,
|
||||
|
@@ -247,9 +247,13 @@ impl Module {
|
||||
&mut self,
|
||||
hash_var: u64,
|
||||
) -> Result<&mut Dynamic, Box<EvalAltResult>> {
|
||||
self.all_variables.get_mut(&hash_var).ok_or_else(|| {
|
||||
EvalAltResult::ErrorVariableNotFound(String::new(), Position::none()).into()
|
||||
})
|
||||
if hash_var == 0 {
|
||||
Err(EvalAltResult::ErrorVariableNotFound(String::new(), Position::none()).into())
|
||||
} else {
|
||||
self.all_variables.get_mut(&hash_var).ok_or_else(|| {
|
||||
EvalAltResult::ErrorVariableNotFound(String::new(), Position::none()).into()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a script-defined function into the module.
|
||||
@@ -354,7 +358,9 @@ impl Module {
|
||||
/// assert!(module.contains_fn(hash, true));
|
||||
/// ```
|
||||
pub fn contains_fn(&self, hash_fn: u64, public_only: bool) -> bool {
|
||||
if public_only {
|
||||
if hash_fn == 0 {
|
||||
false
|
||||
} else if public_only {
|
||||
self.functions
|
||||
.get(&hash_fn)
|
||||
.map(|(_, access, _, _)| match access {
|
||||
@@ -383,7 +389,14 @@ impl Module {
|
||||
) -> u64 {
|
||||
let name = name.into();
|
||||
|
||||
let hash_fn = calc_fn_hash(empty(), &name, arg_types.len(), arg_types.iter().cloned());
|
||||
let args_len = if arg_types.is_empty() {
|
||||
// Distinguish between a script function and a function with no parameters
|
||||
usize::MAX
|
||||
} else {
|
||||
arg_types.len()
|
||||
};
|
||||
|
||||
let hash_fn = calc_fn_hash(empty(), &name, args_len, arg_types.iter().cloned());
|
||||
|
||||
let params = arg_types.into_iter().cloned().collect();
|
||||
|
||||
@@ -910,13 +923,17 @@ impl Module {
|
||||
/// The `u64` hash is calculated by the function `crate::calc_fn_hash`.
|
||||
/// It is also returned by the `set_fn_XXX` calls.
|
||||
pub(crate) fn get_fn(&self, hash_fn: u64, public_only: bool) -> Option<&Func> {
|
||||
self.functions
|
||||
.get(&hash_fn)
|
||||
.and_then(|(_, access, _, f)| match access {
|
||||
_ if !public_only => Some(f),
|
||||
FnAccess::Public => Some(f),
|
||||
FnAccess::Private => None,
|
||||
})
|
||||
if hash_fn == 0 {
|
||||
None
|
||||
} else {
|
||||
self.functions
|
||||
.get(&hash_fn)
|
||||
.and_then(|(_, access, _, f)| match access {
|
||||
_ if !public_only => Some(f),
|
||||
FnAccess::Public => Some(f),
|
||||
FnAccess::Private => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a modules-qualified function.
|
||||
|
Reference in New Issue
Block a user