Fix array bug.

This commit is contained in:
Stephen Chung 2022-12-26 22:05:29 +08:00
parent c5cb2d5e0f
commit 07f522e6d7
2 changed files with 18 additions and 6 deletions

View File

@ -421,9 +421,12 @@ impl FnPtr {
) -> RhaiResult { ) -> RhaiResult {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
if let Some(arity) = self.fn_def().map(|f| f.params.len()) { if let Some(arity) = self.fn_def().map(|f| f.params.len()) {
if arity == N + self.curry().len() {
return self.call_raw(ctx, this_ptr, args);
}
if let Some(move_to_args) = move_this_ptr_to_args { if let Some(move_to_args) = move_this_ptr_to_args {
if this_ptr.is_some() { if this_ptr.is_some() {
if arity == N + 1 { if arity == N + 1 + self.curry().len() {
let mut args2 = FnArgsVec::with_capacity(args.len() + 1); let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
if move_to_args == 0 { if move_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone()); args2.push(this_ptr.as_mut().unwrap().clone());
@ -434,7 +437,7 @@ impl FnPtr {
} }
return self.call_raw(ctx, None, args2); return self.call_raw(ctx, None, args2);
} }
if arity == N + E + 1 { if arity == N + E + 1 + self.curry().len() {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1); let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
if move_to_args == 0 { if move_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone()); args2.push(this_ptr.as_mut().unwrap().clone());
@ -449,10 +452,7 @@ impl FnPtr {
} }
} }
} }
if arity == N { if arity == N + E + self.curry().len() {
return self.call_raw(ctx, this_ptr, args);
}
if arity == N + E {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len()); let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
args2.extend(args); args2.extend(args);
args2.extend(extras); args2.extend(extras);

View File

@ -303,6 +303,18 @@ fn test_arrays_map_reduce() -> Result<(), Box<EvalAltResult>> {
engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|| this += 41); x[0]")?, engine.eval::<INT>("let x = [1, 2, 3]; x.for_each(|| this += 41); x[0]")?,
42 42
); );
assert_eq!(
engine.eval::<INT>(
"
let x = [1, 2, 3];
let sum = 0;
let factor = 2;
x.for_each(|| sum += this * factor);
sum
"
)?,
12
);
assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42); assert_eq!(engine.eval::<INT>("([1].map(|x| x + 41))[0]")?, 42);
assert_eq!( assert_eq!(
engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?, engine.eval::<INT>("let c = 40; let y = 1; [1].map(|x, i| c + x + y + i)[0]")?,