From 2bbb231c8edc16f9ae185f792cadd5997b0c92bd Mon Sep 17 00:00:00 2001 From: benjaminflin Date: Wed, 8 Jul 2020 22:06:05 -0700 Subject: [PATCH] Apply suggestions from code review Co-authored-by: HalidOdat --- boa/src/builtins/array/mod.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 04c344a7842..cb4c74e4919 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -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; @@ -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; @@ -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"))); }