Skip to content

Commit 9e0709e

Browse files
ossy-szegedrerobika
authored andcommitted
Fix Array.prototype.reduce and reduceRight (#3487)
Don't throw TypeError when object length is 0 and initialValue is undefined. Additionally the error message should be more precise and match other engines. Fixes #3463 JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác oszi@inf.u-szeged.hu
1 parent 40b38f7 commit 9e0709e

4 files changed

Lines changed: 8 additions & 12 deletions

File tree

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,9 +2069,9 @@ ecma_builtin_array_reduce_from (ecma_value_t callbackfn, /**< routine's 1st argu
20692069
}
20702070

20712071
/* 5. */
2072-
if (len == 0 && ecma_is_value_undefined (initial_value))
2072+
if (len == 0 && args_number == 1)
20732073
{
2074-
return ecma_raise_type_error (ECMA_ERR_MSG ("Initial value cannot be undefined."));
2074+
return ecma_raise_type_error (ECMA_ERR_MSG ("Reduce of empty array with no initial value."));
20752075
}
20762076

20772077
JERRY_ASSERT (ecma_is_value_object (callbackfn));

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ECMA_ARRAY_PROTOTYPE_FOR_EACH, 2, 1)
6767
ROUTINE (LIT_MAGIC_STRING_MAP, ECMA_ARRAY_PROTOTYPE_MAP, 2, 1)
6868
ROUTINE (LIT_MAGIC_STRING_FILTER, ECMA_ARRAY_PROTOTYPE_FILTER, 2, 1)
6969
/* Note these 2 routines must be in this order */
70-
ROUTINE (LIT_MAGIC_STRING_REDUCE, ECMA_ARRAY_PROTOTYPE_REDUCE, NON_FIXED, 1)
71-
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ECMA_ARRAY_PROTOTYPE_REDUCE_RIGHT, NON_FIXED, 1)
70+
ROUTINE (LIT_MAGIC_STRING_REDUCE, ECMA_ARRAY_PROTOTYPE_REDUCE, 2, 1)
71+
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ECMA_ARRAY_PROTOTYPE_REDUCE_RIGHT, 2, 1)
7272
#if ENABLED (JERRY_ES2015)
7373
ROUTINE (LIT_MAGIC_STRING_FIND, ECMA_ARRAY_PROTOTYPE_FIND, 2, 1)
7474
ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ECMA_ARRAY_PROTOTYPE_FIND_INDEX, 2, 1)

tests/jerry/array-prototype-reduce-right.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ try {
3434
assert(e instanceof TypeError);
3535
}
3636

37-
try {
38-
var arg2;
39-
[].reduceRight(func, arg2);
40-
assert(false);
41-
} catch(e) {
42-
assert(e instanceof TypeError);
43-
}
44-
4537
try {
4638
var a = new Array();
4739
a.length = 10;
@@ -54,6 +46,8 @@ try {
5446
// various checks
5547
assert([].reduceRight(func, 1) === 1);
5648

49+
assert([].reduceRight(func, undefined) === undefined);
50+
5751
assert([0].reduceRight(func) === 0);
5852

5953
assert([0, 1].reduceRight(func) === 1);

tests/jerry/array-prototype-reduce.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ catch(e) {
3939
// various checks
4040
assert ([].reduce(func, 1) === 1);
4141

42+
assert ([].reduce(func, undefined) === undefined);
43+
4244
assert ([0].reduce(func) === 0);
4345

4446
assert ([0, 1].reduce(func) === 1);

0 commit comments

Comments
 (0)