Skip to content

Commit 89e5444

Browse files
lvidacsgalpeter
authored andcommitted
Array.prototype.reduce() fixes.
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 95adf0e commit 89e5444

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
@@ -2497,10 +2497,9 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
24972497
ecma_number_t *num_p = ecma_alloc_number ();
24982498
ecma_object_t *func_object_p;
24992499

2500-
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
2501-
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
2502-
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
2503-
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
2500+
JERRY_ASSERT (ecma_is_value_object (arg1));
2501+
func_object_p = ecma_get_object_from_value (arg1);
2502+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
25042503

25052504
/* 5 */
25062505
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
@@ -2515,23 +2514,23 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
25152514
/* 7a */
25162515
if (!ecma_is_value_undefined (arg2))
25172516
{
2518-
accumulator = ecma_copy_completion_value (arg2);
2517+
accumulator = ecma_copy_value (arg2, true);
25192518
}
25202519
else
25212520
{
25222521
/* 8a */
2523-
bool kPresent = false;
2522+
bool k_present = false;
25242523
/* 8b */
2525-
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
2524+
while (!k_present && index < len && ecma_is_completion_value_empty (ret_value))
25262525
{
25272526
/* 8b-i */
25282527
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
25292528

25302529
/* 8b-ii-iii */
2531-
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2530+
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
25322531
{
25332532
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2534-
accumulator = ecma_copy_completion_value (current_value);
2533+
accumulator = ecma_copy_value (current_value, true);
25352534
ECMA_FINALIZE (current_value);
25362535
}
25372536
/* 8b-iv */
@@ -2540,13 +2539,12 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
25402539
ecma_deref_ecma_string (index_str_p);
25412540
}
25422541
/* 8c */
2543-
if (!kPresent)
2542+
if (!k_present)
25442543
{
25452544
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
25462545
}
25472546
}
25482547
/* 9 */
2549-
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
25502548
ecma_value_t current_index;
25512549

25522550
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
@@ -2561,15 +2559,17 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
25612559
/* 9c-ii */
25622560
*num_p = ecma_uint32_to_number (index);
25632561
current_index = ecma_make_number_value (num_p);
2564-
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
2565-
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
2562+
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};
25662563

25672564
ECMA_TRY_CATCH (call_value,
2568-
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
2565+
ecma_op_function_call (func_object_p,
2566+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
2567+
call_args,
2568+
4),
25692569
ret_value);
25702570

2571-
ecma_free_completion_value (accumulator);
2572-
accumulator = ecma_copy_completion_value (call_value);
2571+
ecma_free_value (accumulator, true);
2572+
accumulator = ecma_copy_value (call_value, true);
25732573

25742574
ECMA_FINALIZE (call_value);
25752575
ECMA_FINALIZE (current_value);
@@ -2580,14 +2580,12 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
25802580

25812581
if (ecma_is_completion_value_empty (ret_value))
25822582
{
2583-
ret_value = ecma_copy_completion_value (accumulator);
2583+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
25842584
}
25852585

2586-
ecma_free_value (undefined_value, false);
25872586
}
25882587

2589-
ecma_free_completion_value (accumulator);
2590-
ecma_free_completion_value (to_object_comp);
2588+
ecma_free_value (accumulator, true);
25912589
ecma_dealloc_number (num_p);
25922590
}
25932591

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)