Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Array.prototype.reduce() fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Vidacs <[email protected]>
lvidacs committed May 29, 2015

Verified

This commit was signed with the committer’s verified signature.
jrjohnson Jon Johnson
1 parent 7c2f3b7 commit e45bc7c
Showing 2 changed files with 26 additions and 21 deletions.
38 changes: 18 additions & 20 deletions jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp
Original file line number Diff line number Diff line change
@@ -1281,10 +1281,9 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
ecma_number_t *num_p = ecma_alloc_number ();
ecma_object_t *func_object_p;

ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
JERRY_ASSERT (ecma_is_value_object (arg1));
func_object_p = ecma_get_object_from_value (arg1);
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);

/* 5 */
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
/* 7a */
if (!ecma_is_value_undefined (arg2))
{
accumulator = ecma_copy_completion_value (arg2);
accumulator = ecma_copy_value (arg2, true);
}
else
{
/* 8a */
bool kPresent = false;
bool k_present = false;
/* 8b */
while (!kPresent && index < len && ecma_is_completion_value_empty (ret_value))
while (!k_present && index < len && ecma_is_completion_value_empty (ret_value))
{
/* 8b-i */
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);

/* 8b-ii-iii */
if ((kPresent = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
{
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
accumulator = ecma_copy_completion_value (current_value);
accumulator = ecma_copy_value (current_value, true);
ECMA_FINALIZE (current_value);
}
/* 8b-iv */
@@ -1324,13 +1323,12 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
ecma_deref_ecma_string (index_str_p);
}
/* 8c */
if (!kPresent)
if (!k_present)
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
}
}
/* 9 */
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
ecma_value_t current_index;

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
/* 9c-ii */
*num_p = ecma_uint32_to_number (index);
current_index = ecma_make_number_value (num_p);
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};

ECMA_TRY_CATCH (call_value,
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
ecma_op_function_call (func_object_p,
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
call_args,
4),
ret_value);

ecma_free_completion_value (accumulator);
accumulator = ecma_copy_completion_value (call_value);
ecma_free_value (accumulator, true);
accumulator = ecma_copy_value (call_value, true);

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

if (ecma_is_completion_value_empty (ret_value))
{
ret_value = ecma_copy_completion_value (accumulator);
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
}

ecma_free_value (undefined_value, false);
}

ecma_free_completion_value (accumulator);
ecma_free_completion_value (to_object_comp);
ecma_free_value (accumulator, true);
ecma_dealloc_number (num_p);
}

9 changes: 8 additions & 1 deletion tests/jerry/array_prototype_reduce.js
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@ var func = function(a, b) {
try {
[0].reduce(new Object());
assert(false);
} catch(e) {
}
catch(e) {
assert(e instanceof TypeError);
}

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

assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1");

var long_array = [0, 1];
assert (long_array.reduce(func,10) === 11);

long_array[10000] = 1;
assert (long_array.reduce(func,10) === 12);

0 comments on commit e45bc7c

Please sign in to comment.