Skip to content

Commit 0960607

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

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
@@ -2106,6 +2106,151 @@ 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_object_t *func_object_p;
2151+
2152+
JERRY_ASSERT (ecma_is_value_object (arg1));
2153+
func_object_p = ecma_get_object_from_value (arg1);
2154+
2155+
/* 5 */
2156+
if (len_number == ECMA_NUMBER_ZERO && ecma_is_value_undefined (arg2))
2157+
{
2158+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2159+
}
2160+
else
2161+
{
2162+
ecma_number_t *num_p = ecma_alloc_number ();
2163+
ecma_value_t accumulator = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
2164+
2165+
/* 6 */
2166+
uint32_t index = len - 1;
2167+
bool zero_reached = !len;
2168+
2169+
/* 7a */
2170+
if (!ecma_is_value_undefined (arg2))
2171+
{
2172+
accumulator = ecma_copy_value (arg2, true);
2173+
}
2174+
else
2175+
{
2176+
/* 8a */
2177+
bool k_present = false;
2178+
/* 8b */
2179+
while (!k_present && !zero_reached && ecma_is_completion_value_empty (ret_value))
2180+
{
2181+
/* 8b-i */
2182+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2183+
2184+
/* 8b-ii-iii */
2185+
if ((k_present = (ecma_op_object_get_property (obj_p, index_str_p) != NULL)))
2186+
{
2187+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2188+
accumulator = ecma_copy_value (current_value, true);
2189+
ECMA_FINALIZE (current_value);
2190+
}
2191+
/* 8b-iv */
2192+
index ? --index : zero_reached = true;
2193+
2194+
ecma_deref_ecma_string (index_str_p);
2195+
}
2196+
/* 8c */
2197+
if (!k_present)
2198+
{
2199+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2200+
}
2201+
}
2202+
/* 9 */
2203+
ecma_value_t current_index;
2204+
2205+
for (; !zero_reached && ecma_is_completion_value_empty (ret_value); index ? --index : zero_reached = true)
2206+
{
2207+
/* 9a */
2208+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2209+
/* 9b */
2210+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
2211+
{
2212+
/* 9c-i */
2213+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2214+
/* 9c-ii */
2215+
*num_p = ecma_uint32_to_number (index);
2216+
current_index = ecma_make_number_value (num_p);
2217+
ecma_value_t call_args[] = {accumulator, current_value, current_index, obj_this};
2218+
2219+
ECMA_TRY_CATCH (call_value,
2220+
ecma_op_function_call (func_object_p,
2221+
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
2222+
call_args,
2223+
4),
2224+
ret_value);
2225+
2226+
ecma_free_value (accumulator, true);
2227+
accumulator = ecma_copy_value (call_value, true);
2228+
2229+
ECMA_FINALIZE (call_value);
2230+
ECMA_FINALIZE (current_value);
2231+
}
2232+
ecma_deref_ecma_string (index_str_p);
2233+
/* 9d in for loop */
2234+
}
2235+
2236+
if (ecma_is_completion_value_empty (ret_value))
2237+
{
2238+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (accumulator, true));
2239+
}
2240+
2241+
ecma_free_value (accumulator, true);
2242+
ecma_dealloc_number (num_p);
2243+
}
2244+
}
2245+
2246+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
2247+
ECMA_FINALIZE (len_value);
2248+
ecma_deref_ecma_string (magic_string_length_p);
2249+
ECMA_FINALIZE (obj_this);
2250+
2251+
return ret_value;
2252+
} /* ecma_builtin_array_prototype_object_reduce */
2253+
21092254
/**
21102255
* @}
21112256
* @}

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: 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)