Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: HalidOdat <halidodat@gmail.com>
  • Loading branch information
benjaminflin and HalidOdat authored Jul 9, 2020
1 parent 264878e commit 2bbb231
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,18 +973,16 @@ impl Array {
args: &[Value],
interpreter: &mut Interpreter,
) -> ResultValue {
if args.is_empty() {
return Err(Value::from(
"missing callback when calling function Array.prototype.reduce",
));
}
let callback = args.get(0).cloned().unwrap_or_else(Value::undefined);
let callback = match args.get(0) {
Some(value) if value.is_funtion() => value,
_ => return interperter.throw_type_error("missing callback when calling function Array.prototype.reduce"),
};
let initial_value = args.get(1).cloned().unwrap_or_else(Value::undefined);
let mut length = i32::from(&this.get_field("length"));
if length == 0 && initial_value.is_undefined() {
return Err(Value::from(
return interpreter.throw_type_error(
"Array contains no elements and initial value is not provided",
));
);
}

let mut k = 0;
Expand All @@ -998,9 +996,9 @@ impl Array {
k += 1;
}
if !k_present {
return Err(Value::from(
return interpreter.throw_type_error(
"Array contains no elements and initial value is not provided",
));
);
}
let result = this.get_field(k.to_string());
k += 1;
Expand All @@ -1016,10 +1014,7 @@ impl Array {
Value::integer(k),
this.clone(),
];
match interpreter.call(&callback, &Value::undefined(), &arguments) {
Ok(value) => accumulator = value,
Err(e) => return Err(e),
}
accumulator = interpreter.call(&callback, &Value::undefined(), &arguments)?;
// reduce may change the length of the array
length = min(length, i32::from(&this.get_field("length")));
}
Expand Down

0 comments on commit 2bbb231

Please sign in to comment.