Skip to content

Commit 3e58541

Browse files
committed
Implement Array.prototype.reduceRight()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent 5c012a1 commit 3e58541

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

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

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,151 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
25972597
return ret_value;
25982598
} /* ecma_builtin_array_prototype_object_reduce */
25992599

2600+
/**
2601+
* The Array.prototype object's 'reduceRight' routine
2602+
*
2603+
* See also:
2604+
* ECMA-262 v5, 15.4.4.22
2605+
*
2606+
* @return completion value
2607+
* Returned value must be freed with ecma_free_completion_value.
2608+
*/
2609+
static ecma_completion_value_t
2610+
ecma_builtin_array_prototype_object_reduce_right (ecma_value_t this_arg, /**< this argument */
2611+
ecma_value_t arg1, /**< callbackfn */
2612+
ecma_value_t arg2) /**< initialValue */
2613+
{
2614+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
2615+
2616+
/* 1 */
2617+
ECMA_TRY_CATCH (obj_this,
2618+
ecma_op_to_object (this_arg),
2619+
ret_value);
2620+
2621+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
2622+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
2623+
2624+
/* 2 */
2625+
ECMA_TRY_CATCH (len_value,
2626+
ecma_op_object_get (obj_p, magic_string_length_p),
2627+
ret_value);
2628+
2629+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
2630+
2631+
/* 3 */
2632+
uint32_t len = ecma_number_to_uint32 (len_number);
2633+
2634+
/* 4 */
2635+
if (!ecma_op_is_callable (arg1))
2636+
{
2637+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2638+
}
2639+
else
2640+
{
2641+
ecma_object_t *func_object_p;
2642+
2643+
JERRY_ASSERT (ecma_is_value_object (arg1));
2644+
func_object_p = ecma_get_object_from_value (arg1);
2645+
2646+
/* 5 */
2647+
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
2648+
{
2649+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2650+
}
2651+
else
2652+
{
2653+
ecma_number_t *num_p = ecma_alloc_number ();
2654+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
2655+
2656+
/* 6 */
2657+
uint32_t index = len - 1;
2658+
bool zero_reached = !len;
2659+
2660+
/* 7a */
2661+
if (!ecma_is_value_undefined (arg2))
2662+
{
2663+
accumulator = ecma_copy_value (arg2, true);
2664+
}
2665+
else
2666+
{
2667+
/* 8a */
2668+
bool k_present = false;
2669+
/* 8b */
2670+
while (!k_present && !zero_reached && ecma_is_completion_value_empty (ret_value))
2671+
{
2672+
/* 8b-i */
2673+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2674+
2675+
/* 8b-ii-iii */
2676+
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2677+
{
2678+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2679+
accumulator = ecma_copy_value (current_value, true);
2680+
ECMA_FINALIZE (current_value);
2681+
}
2682+
/* 8b-iv */
2683+
index ? --index : zero_reached = true;
2684+
2685+
ecma_deref_ecma_string (index_str_p);
2686+
}
2687+
/* 8c */
2688+
if (!k_present)
2689+
{
2690+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2691+
}
2692+
}
2693+
/* 9 */
2694+
ecma_value_t current_index;
2695+
2696+
for (; !zero_reached && ecma_is_completion_value_empty (ret_value); index ? --index : zero_reached = true)
2697+
{
2698+
/* 9a */
2699+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2700+
/* 9b */
2701+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
2702+
{
2703+
/* 9c-i */
2704+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2705+
/* 9c-ii */
2706+
*num_p = ecma_uint32_to_number (index);
2707+
current_index = ecma_make_number_value (num_p);
2708+
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};
2709+
2710+
ECMA_TRY_CATCH (call_value,
2711+
ecma_op_function_call (func_object_p,
2712+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
2713+
call_args,
2714+
4),
2715+
ret_value);
2716+
2717+
ecma_free_value (accumulator, true);
2718+
accumulator = ecma_copy_value (call_value, true);
2719+
2720+
ECMA_FINALIZE (call_value);
2721+
ECMA_FINALIZE (current_value);
2722+
}
2723+
ecma_deref_ecma_string (index_str_p);
2724+
/* 9d in for loop */
2725+
}
2726+
2727+
if (ecma_is_completion_value_empty (ret_value))
2728+
{
2729+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
2730+
}
2731+
2732+
ecma_free_value (accumulator, true);
2733+
ecma_dealloc_number (num_p);
2734+
}
2735+
}
2736+
2737+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
2738+
ECMA_FINALIZE (len_value);
2739+
ecma_deref_ecma_string (magic_string_length_p);
2740+
ECMA_FINALIZE (obj_this);
2741+
2742+
return ret_value;
2743+
} /* ecma_builtin_array_prototype_object_reduce */
2744+
26002745
/**
26012746
* @}
26022747
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2,
7777
ROUTINE (ECMA_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2)
7878
ROUTINE (ECMA_MAGIC_STRING_FILTER, ecma_builtin_array_prototype_object_filter, 2, 1)
7979
ROUTINE (ECMA_MAGIC_STRING_REDUCE, ecma_builtin_array_prototype_object_reduce, 2, 1)
80+
ROUTINE (ECMA_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_reduce_right, 2, 1)
8081

8182
#undef OBJECT_ID
8283
#undef SIMPLE_VALUE
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
assert(false);
32+
} catch(e) {
33+
assert(e instanceof TypeError);
34+
}
35+
36+
try {
37+
var arg2;
38+
[].reduceRight(func, arg2);
39+
assert(false);
40+
} catch(e) {
41+
assert(e instanceof TypeError);
42+
}
43+
44+
// various checks
45+
assert([].reduceRight(func, 1) === 1);
46+
47+
assert([0].reduceRight(func) === 0);
48+
49+
assert([0, 1].reduceRight(func) === 1);
50+
51+
assert([0, 1].reduceRight(func, 1) === 2);
52+
53+
assert([0, 1, 2, 3].reduceRight(func, 1) === 7);
54+
55+
assert (["A","B"].reduceRight(func) === "BA");
56+
57+
assert (["A","B"].reduceRight(func, "Init:") === "Init:BA");
58+
59+
assert ([0, 1].reduceRight(func, 3.2) === 4.2);
60+
61+
assert ([0, "x", 1].reduceRight(func) === "1x0");
62+
63+
assert ([0, "x", 1].reduceRight(func, 3.2) === "4.2x0");
64+
65+
var long_array = [0, 1];
66+
assert (long_array.reduceRight(func,10) === 11);
67+
68+
long_array[10000] = 1;
69+
assert (long_array.reduceRight(func,10) === 12);

0 commit comments

Comments
 (0)