Skip to content

Commit 3a1653e

Browse files
committed
Implement Array.prototype.reduceRight()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 8b28cac commit 3a1653e

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

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

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,150 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
21062106
return ret_value;
21072107
} /* ecma_builtin_array_prototype_object_splice */
21082108

2109+
/**
2110+
* The Array.prototype object's 'reduceRight' routine
2111+
*
2112+
* See also:
2113+
* ECMA-262 v5, 15.4.4.22
2114+
*
2115+
* @return completion value
2116+
* Returned value must be freed with ecma_free_completion_value.
2117+
*/
2118+
static ecma_completion_value_t
2119+
ecma_builtin_array_prototype_object_reduce_right (ecma_value_t this_arg, /**< this argument */
2120+
ecma_value_t arg1, /**< callbackfn */
2121+
ecma_value_t arg2) /**< initialValue */
2122+
{
2123+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
2124+
2125+
/* 1 */
2126+
ECMA_TRY_CATCH (obj_this,
2127+
ecma_op_to_object (this_arg),
2128+
ret_value);
2129+
2130+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
2131+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
2132+
2133+
/* 2 */
2134+
ECMA_TRY_CATCH (len_value,
2135+
ecma_op_object_get (obj_p, magic_string_length_p),
2136+
ret_value);
2137+
2138+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
2139+
2140+
/* 3 */
2141+
uint32_t len = ecma_number_to_uint32 (len_number);
2142+
2143+
/* 4 */
2144+
if (!ecma_op_is_callable (arg1))
2145+
{
2146+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2147+
}
2148+
else
2149+
{
2150+
ecma_number_t *num_p = ecma_alloc_number ();
2151+
ecma_object_t *func_object_p;
2152+
2153+
JERRY_ASSERT (ecma_is_value_object (arg1));
2154+
func_object_p = ecma_get_object_from_value (arg1);
2155+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
2156+
2157+
/* 5 */
2158+
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
2159+
{
2160+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2161+
}
2162+
else
2163+
{
2164+
/* 6 */
2165+
uint32_t index = len - 1;
2166+
bool zero_reached = !len;
2167+
2168+
/* 7a */
2169+
if (!ecma_is_value_undefined (arg2))
2170+
{
2171+
accumulator = ecma_copy_value (arg2, true);
2172+
}
2173+
else
2174+
{
2175+
/* 8a */
2176+
bool k_present = false;
2177+
/* 8b */
2178+
while (!k_present && !zero_reached && ecma_is_completion_value_empty (ret_value))
2179+
{
2180+
/* 8b-i */
2181+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2182+
2183+
/* 8b-ii-iii */
2184+
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2185+
{
2186+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2187+
accumulator = ecma_copy_value (current_value, true);
2188+
ECMA_FINALIZE (current_value);
2189+
}
2190+
/* 8b-iv */
2191+
index ? --index : zero_reached = true;
2192+
2193+
ecma_deref_ecma_string (index_str_p);
2194+
}
2195+
/* 8c */
2196+
if (!k_present)
2197+
{
2198+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2199+
}
2200+
}
2201+
/* 9 */
2202+
ecma_value_t current_index;
2203+
2204+
for (; !zero_reached && ecma_is_completion_value_empty (ret_value); index ? --index : zero_reached = true)
2205+
{
2206+
/* 9a */
2207+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2208+
/* 9b */
2209+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
2210+
{
2211+
/* 9c-i */
2212+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2213+
/* 9c-ii */
2214+
*num_p = ecma_uint32_to_number (index);
2215+
current_index = ecma_make_number_value (num_p);
2216+
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};
2217+
2218+
ECMA_TRY_CATCH (call_value,
2219+
ecma_op_function_call (func_object_p,
2220+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
2221+
call_args,
2222+
4),
2223+
ret_value);
2224+
2225+
ecma_free_value (accumulator, true);
2226+
accumulator = ecma_copy_value (call_value, true);
2227+
2228+
ECMA_FINALIZE (call_value);
2229+
ECMA_FINALIZE (current_value);
2230+
}
2231+
ecma_deref_ecma_string (index_str_p);
2232+
/* 9d in for loop */
2233+
}
2234+
2235+
if (ecma_is_completion_value_empty (ret_value))
2236+
{
2237+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
2238+
}
2239+
}
2240+
2241+
ecma_free_value (accumulator, true);
2242+
ecma_dealloc_number (num_p);
2243+
}
2244+
2245+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
2246+
ECMA_FINALIZE (len_value);
2247+
ecma_deref_ecma_string (magic_string_length_p);
2248+
ECMA_FINALIZE (obj_this);
2249+
2250+
return ret_value;
2251+
} /* ecma_builtin_array_prototype_object_reduce */
2252+
21092253
/**
21102254
* @}
21112255
* @}

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
@@ -75,6 +75,7 @@ ROUTINE (ECMA_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1)
7575
ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2, 2)
7676
ROUTINE (ECMA_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2)
7777
ROUTINE (ECMA_MAGIC_STRING_FILTER, ecma_builtin_array_prototype_object_filter, 2, 1)
78+
ROUTINE (ECMA_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_reduce_right, 2, 1)
7879

7980
#undef OBJECT_ID
8081
#undef SIMPLE_VALUE
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// 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");
56+
57+
var long_array = [0, 1];
58+
assert (long_array.reduceRight(func,10) === 11);
59+
60+
long_array[10000] = 1;
61+
assert (long_array.reduceRight(func,10) === 12);

0 commit comments

Comments
 (0)