Skip to content

Commit 503d889

Browse files
committed
Array.prototype.reduceRight() tests and fixes.
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 85352fb commit 503d889

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,14 +1476,14 @@ ecma_builtin_array_prototype_object_reduce_right (ecma_value_t this_arg, /**< th
14761476
}
14771477
else
14781478
{
1479-
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
14801479
ecma_number_t *num_p = ecma_alloc_number ();
14811480
ecma_object_t *func_object_p;
14821481
ecma_value_t current_index;
14831482

14841483
ecma_completion_value_t to_object_comp = ecma_op_to_object (arg1);
14851484
JERRY_ASSERT (ecma_is_completion_value_normal (to_object_comp));
14861485
func_object_p = ecma_get_object_from_completion_value (to_object_comp);
1486+
ecma_completion_value_t accumulator = ecma_make_empty_completion_value ();
14871487

14881488
/* 5 */
14891489
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg1))
@@ -1543,13 +1543,17 @@ ecma_builtin_array_prototype_object_reduce_right (ecma_value_t this_arg, /**< th
15431543
current_index = ecma_make_number_value (num_p);
15441544
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
15451545
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
1546+
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
15461547

15471548
ECMA_TRY_CATCH (call_value,
1548-
ecma_op_function_call (func_object_p, ECMA_SIMPLE_VALUE_UNDEFINED, call_args, 4),
1549+
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
15491550
ret_value);
1551+
1552+
ecma_free_completion_value (accumulator);
15501553
accumulator = ecma_copy_completion_value (call_value);
15511554

15521555
ECMA_FINALIZE (call_value);
1556+
ecma_free_value (undefined_value, false);
15531557
ECMA_FINALIZE (current_value);
15541558
}
15551559
ecma_deref_ecma_string (index_str_p);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var func = function(a, b) {
17+
return a + b;
18+
}
19+
20+
// check function type
21+
try {
22+
[0].reduceRight(new Object());
23+
assert(false);
24+
} catch(e) {
25+
assert(e instanceof TypeError);
26+
}
27+
28+
// check for init value
29+
try {
30+
[].reduceRight(func);
31+
}
32+
catch(e) {
33+
assert(e instanceof TypeError);
34+
}
35+
36+
// various checks
37+
assert([].reduceRight(func, 1) === 1);
38+
39+
assert([0].reduceRight(func) === 0);
40+
41+
assert([0, 1].reduceRight(func) === 1);
42+
43+
assert([0, 1].reduceRight(func, 1) === 2);
44+
45+
assert([0, 1, 2, 3].reduceRight(func, 1) === 7);
46+
47+
assert (["A","B"].reduceRight(func) === "BA");
48+
49+
assert (["A","B"].reduceRight(func, "Init:") === "Init:BA");
50+
51+
assert ([0, 1].reduceRight(func, 3.2) === 4.2);
52+
53+
assert ([0, "x", 1].reduceRight(func) === "1x0");
54+
55+
assert ([0, "x", 1].reduceRight(func, 3.2) === "4.2x0");

0 commit comments

Comments
 (0)