Skip to content

Commit 35441b7

Browse files
committed
Array.prototype.reduce() fixes.
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 7c2f3b7 commit 35441b7

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,10 +1281,9 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
12811281
ecma_number_t *num_p = ecma_alloc_number ();
12821282
ecma_object_t *func_object_p;
12831283

1284-
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
1285-
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
1286-
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
1287-
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
1284+
JERRY_ASSERT (ecma_is_value_object (arg1));
1285+
func_object_p = ecma_get_object_from_value (arg1);
1286+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
12881287

12891288
/* 5 */
12901289
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
@@ -1299,23 +1298,23 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
12991298
/* 7a */
13001299
if (!ecma_is_value_undefined (arg2))
13011300
{
1302-
accumulator = ecma_copy_completion_value (arg2);
1301+
accumulator = ecma_copy_value (arg2, true);
13031302
}
13041303
else
13051304
{
13061305
/* 8a */
1307-
bool kPresent = false;
1306+
bool k_present = false;
13081307
/* 8b */
1309-
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
1308+
while (!k_present && index < len && ecma_is_completion_value_empty (ret_value))
13101309
{
13111310
/* 8b-i */
13121311
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
13131312

13141313
/* 8b-ii-iii */
1315-
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
1314+
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
13161315
{
13171316
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
1318-
accumulator = ecma_copy_completion_value (current_value);
1317+
accumulator = ecma_copy_value (current_value, true);
13191318
ECMA_FINALIZE (current_value);
13201319
}
13211320
/* 8b-iv */
@@ -1324,13 +1323,12 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
13241323
ecma_deref_ecma_string (index_str_p);
13251324
}
13261325
/* 8c */
1327-
if (!kPresent)
1326+
if (!k_present)
13281327
{
13291328
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
13301329
}
13311330
}
13321331
/* 9 */
1333-
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
13341332
ecma_value_t current_index;
13351333

13361334
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
@@ -1345,15 +1343,17 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
13451343
/* 9c-ii */
13461344
*num_p = ecma_uint32_to_number (index);
13471345
current_index = ecma_make_number_value (num_p);
1348-
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
1349-
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
1346+
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};
13501347

13511348
ECMA_TRY_CATCH (call_value,
1352-
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
1349+
ecma_op_function_call (func_object_p,
1350+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
1351+
call_args,
1352+
4),
13531353
ret_value);
13541354

1355-
ecma_free_completion_value (accumulator);
1356-
accumulator = ecma_copy_completion_value (call_value);
1355+
ecma_free_value (accumulator, true);
1356+
accumulator = ecma_copy_value (call_value, true);
13571357

13581358
ECMA_FINALIZE (call_value);
13591359
ECMA_FINALIZE (current_value);
@@ -1364,14 +1364,12 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
13641364

13651365
if (ecma_is_completion_value_empty (ret_value))
13661366
{
1367-
ret_value = ecma_copy_completion_value (accumulator);
1367+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
13681368
}
13691369

1370-
ecma_free_value (undefined_value, false);
13711370
}
13721371

1373-
ecma_free_completion_value (accumulator);
1374-
ecma_free_completion_value (to_object_comp);
1372+
ecma_free_value (accumulator, true);
13751373
ecma_dealloc_number (num_p);
13761374
}
13771375

tests/jerry/array_prototype_reduce.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var func = function(a, b) {
2121
try {
2222
[0].reduce(new Object());
2323
assert(false);
24-
} catch(e) {
24+
}
25+
catch(e) {
2526
assert(e instanceof TypeError);
2627
}
2728

@@ -54,3 +55,9 @@ assert ([0, 1].reduce(func, 3.2) === 4.2);
5455
assert ([0, "x", 1].reduce(func) === "0x1");
5556

5657
assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1");
58+
59+
var long_array = [0, 1];
60+
assert (long_array.reduce(func,10) === 11);
61+
62+
long_array[10000] = 1;
63+
assert (long_array.reduce(func,10) === 12);

0 commit comments

Comments
 (0)