Fix function calls.

This commit is contained in:
Stephen Chung 2020-09-25 19:07:24 +08:00
parent 599fe846cb
commit f406fc0ac0
3 changed files with 69 additions and 47 deletions

View File

@ -221,6 +221,7 @@ impl Engine {
let func = self
.global_module
.get_fn(hash_fn, pub_only)
.or_else(|| lib.get_fn(hash_fn, pub_only))
.or_else(|| self.packages.get_fn(hash_fn, pub_only));
if let Some(func) = func {
@ -439,9 +440,9 @@ impl Engine {
// First check script-defined functions
lib.contains_fn(hash_script, pub_only)
//|| lib.contains_fn(hash_fn, pub_only)
|| lib.contains_fn(hash_fn, pub_only)
// Then check registered functions
//|| self.global_module.contains_fn(hash_script, pub_only)
|| 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)
@ -522,17 +523,22 @@ impl Engine {
.into()
}
// Normal script function call
// Script-like function found
#[cfg(not(feature = "no_function"))]
_ if lib.contains_fn(hash_script, pub_only)
_ if self.global_module.contains_fn(hash_script, pub_only)
|| lib.contains_fn(hash_script, pub_only)
|| self.packages.contains_fn(hash_script, pub_only) =>
{
// Get scripted function
let func = lib
// Get function
let func = self
.global_module
.get_fn(hash_script, pub_only)
.or_else(|| lib.get_fn(hash_script, pub_only))
.or_else(|| self.packages.get_fn(hash_script, pub_only))
.unwrap()
.get_fn_def();
.unwrap();
if func.is_script() {
let func = func.get_fn_def();
let scope = &mut Scope::new();
let mods = &mut Imports::new();
@ -574,6 +580,19 @@ impl Engine {
};
Ok((result, false))
} else {
// If it is a native function, redirect it
self.call_native_fn(
state,
lib,
fn_name,
hash_script,
args,
is_ref,
pub_only,
def_val,
)
}
}
// Normal native function call

View File

@ -262,7 +262,7 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
engine.set_module_resolver(Some(resolver1));
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, true, &engine)?;
let mut resolver2 = StaticModuleResolver::new();
resolver2.insert("testing", module);

View File

@ -34,12 +34,15 @@ fn test_packages() -> Result<(), Box<EvalAltResult>> {
#[test]
fn test_packages_with_script() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let ast = engine.compile("fn foo(x) { x + 1 }")?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
let ast = engine.compile("fn foo(x) { x + 1 } fn bar(x) { foo(x) + 1 }")?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, false, &engine)?;
engine.load_package(module);
assert_eq!(engine.eval::<INT>("foo(41)")?, 42);
let module = Module::eval_ast_as_new(Scope::new(), &ast, true, &engine)?;
engine.load_package(module);
assert_eq!(engine.eval::<INT>("bar(40)")?, 42);
Ok(())
}