Skip to content

Commit 7c2f3b7

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

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
@@ -1278,93 +1278,98 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
12781278
}
12791279
else
12801280
{
1281-
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
12821281
ecma_number_t *num_p = ecma_alloc_number ();
12831282
ecma_object_t *func_object_p;
1284-
ecma_value_t current_index;
12851283

12861284
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
12871285
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
12881286
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
1287+
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
12891288

12901289
/* 5 */
1291-
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg1))
1290+
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
12921291
{
12931292
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
12941293
}
1295-
1296-
/* 6 */
1297-
uint32_t index = 0;
1298-
1299-
/* 7a */
1300-
if (!ecma_is_value_undefined (arg2))
1301-
{
1302-
accumulator = ecma_copy_completion_value (arg2);
1303-
}
13041294
else
13051295
{
1306-
/* 8a */
1307-
bool kPresent = false;
1308-
/* 8b */
1309-
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
1296+
/* 6 */
1297+
uint32_t index = 0;
1298+
1299+
/* 7a */
1300+
if (!ecma_is_value_undefined (arg2))
13101301
{
1311-
/* 8b-i */
1312-
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
1302+
accumulator = ecma_copy_completion_value (arg2);
1303+
}
1304+
else
1305+
{
1306+
/* 8a */
1307+
bool kPresent = false;
1308+
/* 8b */
1309+
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
1310+
{
1311+
/* 8b-i */
1312+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
1313+
1314+
/* 8b-ii-iii */
1315+
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
1316+
{
1317+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
1318+
accumulator = ecma_copy_completion_value (current_value);
1319+
ECMA_FINALIZE (current_value);
1320+
}
1321+
/* 8b-iv */
1322+
index++;
1323+
1324+
ecma_deref_ecma_string (index_str_p);
1325+
}
1326+
/* 8c */
1327+
if (!kPresent)
1328+
{
1329+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
1330+
}
1331+
}
1332+
/* 9 */
1333+
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
1334+
ecma_value_t current_index;
13131335

1314-
/* 8b-ii-iii */
1315-
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
1336+
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
1337+
{
1338+
/* 9a */
1339+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
1340+
/* 9b */
1341+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
13161342
{
1343+
/* 9c-i */
13171344
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
1318-
accumulator = ecma_copy_completion_value (current_value);
1345+
/* 9c-ii */
1346+
*num_p = ecma_uint32_to_number (index);
1347+
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};
1350+
1351+
ECMA_TRY_CATCH (call_value,
1352+
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
1353+
ret_value);
1354+
1355+
ecma_free_completion_value (accumulator);
1356+
accumulator = ecma_copy_completion_value (call_value);
1357+
1358+
ECMA_FINALIZE (call_value);
13191359
ECMA_FINALIZE (current_value);
13201360
}
1321-
/* 8b-iv */
1322-
index++;
1323-
13241361
ecma_deref_ecma_string (index_str_p);
1362+
/* 9d in for loop */
13251363
}
1326-
/* 8c */
1327-
if (!kPresent)
1364+
1365+
if (ecma_is_completion_value_empty (ret_value))
13281366
{
1329-
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
1367+
ret_value = ecma_copy_completion_value (accumulator);
13301368
}
1331-
}
1332-
/* 9 */
1333-
for (; index < len && ecma_is_completion_value_empty (ret_value); ++index)
1334-
{
1335-
/* 9a */
1336-
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
1337-
/* 9b */
1338-
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
1339-
{
1340-
/* 9c-i */
1341-
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
1342-
/* 9c-ii */
1343-
*num_p = ecma_uint32_to_number (index);
1344-
current_index = ecma_make_number_value (num_p);
1345-
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
1346-
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
1347-
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
1348-
1349-
ECMA_TRY_CATCH (call_value,
1350-
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
1351-
ret_value);
13521369

1353-
ecma_free_completion_value (accumulator);
1354-
accumulator = ecma_copy_completion_value (call_value);
1355-
1356-
ECMA_FINALIZE (call_value);
1357-
ecma_free_value (undefined_value, false);
1358-
ECMA_FINALIZE (current_value);
1359-
}
1360-
ecma_deref_ecma_string (index_str_p);
1361-
/* 9d in for loop */
1370+
ecma_free_value (undefined_value, false);
13621371
}
13631372

1364-
if (ecma_is_completion_value_empty (ret_value))
1365-
{
1366-
ret_value = ecma_copy_completion_value (accumulator);
1367-
}
13681373
ecma_free_completion_value (accumulator);
13691374
ecma_free_completion_value (to_object_comp);
13701375
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)