diff --git a/RELEASES.md b/RELEASES.md index 89b05c36..d42f41e2 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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 ------------ diff --git a/src/fn_call.rs b/src/fn_call.rs index c7300f15..494fe1a0 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -974,6 +974,7 @@ impl Engine { ) -> Result> { 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::>()?; + // 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!( diff --git a/tests/modules.rs b/tests/modules.rs index 3b00ab17..06f5e37e 100644 --- a/tests/modules.rs +++ b/tests/modules.rs @@ -94,6 +94,31 @@ fn test_module_resolver() -> Result<(), Box> { 42 ); + assert_eq!( + engine.eval::( + r#" + import "hello" as h1; + import "hello" as h2; + let x = 42; + h1::sum(x, -10, 3, 7) + "# + )?, + 42 + ); + + assert_eq!( + engine.eval::( + r#" + import "hello" as h1; + import "hello" as h2; + let x = 42; + h1::sum(x, 0, 0, 0); + x + "# + )?, + 42 + ); + assert_eq!( engine.eval::( r#"