Fix bug with calling scripted function.

This commit is contained in:
Stephen Chung 2020-10-16 21:16:06 +08:00
parent e62df6cbed
commit f995d09d8b
4 changed files with 46 additions and 17 deletions

View File

@ -13,7 +13,6 @@ Breaking changes
* `AST::iter_functions` now returns an iterator instead of taking a closure. * `AST::iter_functions` now returns an iterator instead of taking a closure.
* `Module::get_script_function_by_signature` renamed to `Module::get_script_fn` and returns `&<Shared<ScriptFnDef>>`. * `Module::get_script_function_by_signature` renamed to `Module::get_script_fn` and returns `&<Shared<ScriptFnDef>>`.
* `Module::num_fn`, `Module::num_var` and `Module::num_iter` are removed and merged into `Module::count`. * `Module::num_fn`, `Module::num_var` and `Module::num_iter` are removed and merged into `Module::count`.
* `Module::set_iter` is renamed to `Module::set_iter_raw`.
* The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`. * The `merge_namespaces` parameter to `Module::eval_ast_as_new` is removed and now defaults to `true`.
* `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant. * `GlobalFileModuleResolver` is removed because its performance gain over the `FileModuleResolver` is no longer very significant.
* The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch` * The following `EvalAltResult` variants are removed and merged into `EvalAltResult::ErrorMismatchDataType`: `ErrorCharMismatch`, `ErrorNumericIndexExpr`, `ErrorStringIndexExpr`, `ErrorImportExpr`, `ErrorLogicGuard`, `ErrorBooleanArgMismatch`

View File

@ -1652,9 +1652,7 @@ impl Engine {
ensure_no_data_race(name, args, false)?; ensure_no_data_race(name, args, false)?;
} }
self.call_script_fn( self.call_script_fn(scope, &mut mods, &mut state, lib, this_ptr, fn_def, args, 0)
scope, &mut mods, &mut state, lib, this_ptr, name, fn_def, args, 0,
)
} }
/// Optimize the `AST` with constants defined in an external Scope. /// Optimize the `AST` with constants defined in an external Scope.

View File

@ -347,7 +347,6 @@ impl Engine {
state: &mut State, state: &mut State,
lib: &Module, lib: &Module,
this_ptr: &mut Option<&mut Dynamic>, this_ptr: &mut Option<&mut Dynamic>,
fn_name: &str,
fn_def: &ScriptFnDef, fn_def: &ScriptFnDef,
args: &mut FnCallArgs, args: &mut FnCallArgs,
level: usize, level: usize,
@ -386,7 +385,7 @@ impl Engine {
let mut lib_merged; let mut lib_merged;
let unified_lib = if let Some(ref env_lib) = fn_def.lib { let unified_lib = if let Some(ref env_lib) = fn_def.lib {
if !lib.is_empty() { if lib.is_empty() {
// In the special case of the main script not defining any function // In the special case of the main script not defining any function
env_lib env_lib
} else { } else {
@ -408,14 +407,18 @@ impl Engine {
EvalAltResult::Return(x, _) => Ok(x), EvalAltResult::Return(x, _) => Ok(x),
EvalAltResult::ErrorInFunctionCall(name, err, _) => { EvalAltResult::ErrorInFunctionCall(name, err, _) => {
EvalAltResult::ErrorInFunctionCall( EvalAltResult::ErrorInFunctionCall(
format!("{} > {}", fn_name, name), format!("{} > {}", fn_def.name, name),
err, err,
Position::none(), Position::none(),
) )
.into() .into()
} }
_ => EvalAltResult::ErrorInFunctionCall(fn_name.to_string(), err, Position::none()) _ => EvalAltResult::ErrorInFunctionCall(
.into(), fn_def.name.to_string(),
err,
Position::none(),
)
.into(),
}); });
// Remove all local variables // Remove all local variables
@ -594,7 +597,6 @@ impl Engine {
state, state,
lib, lib,
&mut Some(*first), &mut Some(*first),
fn_name,
func, func,
rest, rest,
_level, _level,
@ -605,9 +607,8 @@ impl Engine {
let mut backup: ArgBackup = Default::default(); let mut backup: ArgBackup = Default::default();
backup.change_first_arg_to_copy(is_ref, args); backup.change_first_arg_to_copy(is_ref, args);
let result = self.call_script_fn( let result = self
scope, mods, state, lib, &mut None, fn_name, func, args, _level, .call_script_fn(scope, mods, state, lib, &mut None, func, args, _level);
);
// Restore the original reference // Restore the original reference
backup.restore_first_arg(args); backup.restore_first_arg(args);
@ -1195,14 +1196,12 @@ impl Engine {
} }
let args = args.as_mut(); let args = args.as_mut();
let func = f.get_fn_def(); let fn_def = f.get_fn_def();
let new_scope = &mut Scope::new(); let new_scope = &mut Scope::new();
let mods = &mut Imports::new(); let mods = &mut Imports::new();
self.call_script_fn( self.call_script_fn(new_scope, mods, state, lib, &mut None, fn_def, args, level)
new_scope, mods, state, lib, &mut None, name, func, args, level,
)
} }
Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut()), Some(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut()),
Some(f) if f.is_native() => { Some(f) if f.is_native() => {

View File

@ -399,3 +399,36 @@ fn test_module_ast_namespace() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[cfg(not(feature = "no_function"))]
#[test]
fn test_module_ast_namespace2() -> Result<(), Box<EvalAltResult>> {
use rhai::{Engine, Module, Scope};
const MODULE_TEXT: &str = r#"
fn run_function(function) {
function.call()
}
"#;
const SCRIPT: &str = r#"
import "test_module" as test;
fn foo() {
print("foo");
}
test::run_function(Fn("foo"));
"#;
let mut engine = Engine::new();
let module_ast = engine.compile(MODULE_TEXT)?;
let module = Module::eval_ast_as_new(Scope::new(), &module_ast, &engine)?;
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test_module", module);
engine.set_module_resolver(Some(static_modules));
engine.consume(SCRIPT)?;
Ok(())
}