Fix bug that consumes first argument in module-qualified call.

This commit is contained in:
Stephen Chung 2020-08-20 16:26:10 +08:00
parent 6a3e123306
commit ac6d519d28
3 changed files with 66 additions and 4 deletions

View File

@ -4,6 +4,12 @@ Rhai Release Notes
Version 0.19.0
==============
Bug fixes
---------
* Fixes bug that prevents calling functions in closures.
* Fixes bug that erroneously consumes the first argument to a module-qualified function call.
New features
------------

View File

@ -974,6 +974,7 @@ impl Engine {
) -> Result<Dynamic, Box<EvalAltResult>> {
let modules = modules.as_ref().unwrap();
let mut arg_values: StaticVec<_>;
let mut first_arg_value = None;
let mut args: StaticVec<_>;
if args_expr.is_empty() {
@ -988,17 +989,28 @@ impl Engine {
Expr::Variable(x) if x.1.is_none() => {
arg_values = args_expr
.iter()
.skip(1)
.map(|expr| self.eval_expr(scope, mods, state, lib, this_ptr, expr, level))
.enumerate()
.map(|(i, expr)| {
// Skip the first argument
if i == 0 {
Ok(Default::default())
} else {
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)
}
})
.collect::<Result<_, _>>()?;
// Get target reference to first argument
let (target, _, _, pos) =
search_scope_only(scope, state, this_ptr, args_expr.get(0).unwrap())?;
self.inc_operations(state)
.map_err(|err| err.new_position(pos))?;
args = once(target).chain(arg_values.iter_mut()).collect();
let (first, rest) = arg_values.split_first_mut().unwrap();
first_arg_value = Some(first);
args = once(target).chain(rest.iter_mut()).collect();
}
// func(..., ...) or func(mod::x, ...)
_ => {
@ -1037,6 +1049,13 @@ impl Engine {
match func {
#[cfg(not(feature = "no_function"))]
Some(f) if f.is_script() => {
// Clone first argument
if let Some(first) = first_arg_value {
let first_val = args[0].clone();
args[0] = first;
*args[0] = first_val;
}
let args = args.as_mut();
let func = f.get_fn_def();
@ -1055,7 +1074,19 @@ impl Engine {
self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level)
}
Some(f) => f.get_native_fn()(self, lib, args.as_mut()),
Some(f) if f.is_native() => {
if !f.is_method() {
// Clone first argument
if let Some(first) = first_arg_value {
let first_val = args[0].clone();
args[0] = first;
*args[0] = first_val;
}
}
f.get_native_fn()(self, lib, args.as_mut())
}
Some(_) => unreachable!(),
None if def_val.is_some() => Ok(def_val.unwrap().into()),
None => EvalAltResult::ErrorFunctionNotFound(
format!(

View File

@ -94,6 +94,31 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
42
);
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h1;
import "hello" as h2;
let x = 42;
h1::sum(x, -10, 3, 7)
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"
import "hello" as h1;
import "hello" as h2;
let x = 42;
h1::sum(x, 0, 0, 0);
x
"#
)?,
42
);
assert_eq!(
engine.eval::<INT>(
r#"