Skip to content

Commit 95adf0e

Browse files
lvidacsgalpeter
authored andcommitted
Array.prototype.reduce() fixes.
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent f22f3c5 commit 95adf0e

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

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

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,93 +2494,98 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
24942494
}
24952495
else
24962496
{
2497-
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
24982497
ecma_number_t *num_p = ecma_alloc_number ();
24992498
ecma_object_t *func_object_p;
2500-
ecma_value_t current_index;
25012499

25022500
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
25032501
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
25042502
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
2503+
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
25052504

25062505
/* 5 */
2507-
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg1))
2506+
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
25082507
{
25092508
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
25102509
}
2511-
2512-
/* 6 */
2513-
uint32_t index = 0;
2514-
2515-
/* 7a */
2516-
if (!ecma_is_value_undefined (arg2))
2517-
{
2518-
accumulator = ecma_copy_completion_value (arg2);
2519-
}
25202510
else
25212511
{
2522-
/* 8a */
2523-
bool kPresent = false;
2524-
/* 8b */
2525-
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
2512+
/* 6 */
2513+
uint32_t index = 0;
2514+
2515+
/* 7a */
2516+
if (!ecma_is_value_undefined (arg2))
25262517
{
2527-
/* 8b-i */
2528-
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2518+
accumulator = ecma_copy_completion_value (arg2);
2519+
}
2520+
else
2521+
{
2522+
/* 8a */
2523+
bool kPresent = false;
2524+
/* 8b */
2525+
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
2526+
{
2527+
/* 8b-i */
2528+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2529+
2530+
/* 8b-ii-iii */
2531+
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2532+
{
2533+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2534+
accumulator = ecma_copy_completion_value (current_value);
2535+
ECMA_FINALIZE (current_value);
2536+
}
2537+
/* 8b-iv */
2538+
index++;
2539+
2540+
ecma_deref_ecma_string (index_str_p);
2541+
}
2542+
/* 8c */
2543+
if (!kPresent)
2544+
{
2545+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2546+
}
2547+
}
2548+
/* 9 */
2549+
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
2550+
ecma_value_t current_index;
25292551

2530-
/* 8b-ii-iii */
2531-
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2552+
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
2553+
{
2554+
/* 9a */
2555+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2556+
/* 9b */
2557+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
25322558
{
2559+
/* 9c-i */
25332560
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2534-
accumulator = ecma_copy_completion_value (current_value);
2561+
/* 9c-ii */
2562+
*num_p = ecma_uint32_to_number (index);
2563+
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};
2566+
2567+
ECMA_TRY_CATCH (call_value,
2568+
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
2569+
ret_value);
2570+
2571+
ecma_free_completion_value (accumulator);
2572+
accumulator = ecma_copy_completion_value (call_value);
2573+
2574+
ECMA_FINALIZE (call_value);
25352575
ECMA_FINALIZE (current_value);
25362576
}
2537-
/* 8b-iv */
2538-
index++;
2539-
25402577
ecma_deref_ecma_string (index_str_p);
2578+
/* 9d in for loop */
25412579
}
2542-
/* 8c */
2543-
if (!kPresent)
2580+
2581+
if (ecma_is_completion_value_empty (ret_value))
25442582
{
2545-
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2583+
ret_value = ecma_copy_completion_value (accumulator);
25462584
}
2547-
}
2548-
/* 9 */
2549-
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
2550-
{
2551-
/* 9a */
2552-
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2553-
/* 9b */
2554-
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
2555-
{
2556-
/* 9c-i */
2557-
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2558-
/* 9c-ii */
2559-
*num_p = ecma_uint32_to_number (index);
2560-
current_index = ecma_make_number_value (num_p);
2561-
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
2562-
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
2563-
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
2564-
2565-
ECMA_TRY_CATCH (call_value,
2566-
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
2567-
ret_value);
2568-
2569-
ecma_free_completion_value (accumulator);
2570-
accumulator = ecma_copy_completion_value (call_value);
25712585

2572-
ECMA_FINALIZE (call_value);
2573-
ecma_free_value (undefined_value, false);
2574-
ECMA_FINALIZE (current_value);
2575-
}
2576-
ecma_deref_ecma_string (index_str_p);
2577-
/* 9d in for loop */
2586+
ecma_free_value (undefined_value, false);
25782587
}
25792588

2580-
if (ecma_is_completion_value_empty (ret_value))
2581-
{
2582-
ret_value = ecma_copy_completion_value (accumulator);
2583-
}
25842589
ecma_free_completion_value (accumulator);
25852590
ecma_free_completion_value (to_object_comp);
25862591
ecma_dealloc_number (num_p);

tests/jerry/array_prototype_reduce.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ try {
2828
// check for init value
2929
try {
3030
[].reduce(func);
31+
assert(false);
3132
}
3233
catch(e) {
3334
assert(e instanceof TypeError);

0 commit comments

Comments
 (0)