Skip to content

Commit 0921f85

Browse files
committed
Array.prototype.map()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 5c012a1 commit 0921f85

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,114 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
24522452
return ret_value;
24532453
} /* ecma_builtin_array_prototype_object_splice */
24542454

2455+
/**
2456+
* The Array.prototype object's 'map' routine
2457+
*
2458+
* See also:
2459+
* ECMA-262 v5, 15.4.4.19
2460+
*
2461+
* @return completion value
2462+
* Returned value must be freed with ecma_free_completion_value.
2463+
*/
2464+
static ecma_completion_value_t
2465+
ecma_builtin_array_prototype_object_map (ecma_value_t this_arg, /**< this argument */
2466+
ecma_value_t arg1, /**< callbackfn */
2467+
ecma_value_t arg2) /**< thisArg */
2468+
{
2469+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
2470+
2471+
/* 1 */
2472+
ECMA_TRY_CATCH (obj_this,
2473+
ecma_op_to_object (this_arg),
2474+
ret_value);
2475+
2476+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
2477+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
2478+
2479+
/* 2 */
2480+
ECMA_TRY_CATCH (len_value,
2481+
ecma_op_object_get (obj_p, magic_string_length_p),
2482+
ret_value);
2483+
2484+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
2485+
2486+
/* 3 */
2487+
uint32_t len = ecma_number_to_uint32 (len_number);
2488+
2489+
/* 4 */
2490+
if (!ecma_op_is_callable (arg1))
2491+
{
2492+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
2493+
}
2494+
else
2495+
{
2496+
ecma_number_t *num_p = ecma_alloc_number ();
2497+
ecma_object_t *func_object_p;
2498+
JERRY_ASSERT (ecma_is_value_object (arg1));
2499+
func_object_p = ecma_get_object_from_value (arg1);
2500+
2501+
/* 5: arg2 is simply used as T */
2502+
2503+
/* 6 */
2504+
ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
2505+
JERRY_ASSERT (ecma_is_completion_value_normal (new_array));
2506+
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
2507+
2508+
/* 7-8 */
2509+
ecma_value_t current_index;
2510+
2511+
for (uint32_t index = 0; index < len && ecma_is_completion_value_empty (ret_value); ++index)
2512+
{
2513+
/* 8a */
2514+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
2515+
/* 8b */
2516+
if (ecma_op_object_get_property (obj_p, index_str_p) != NULL)
2517+
{
2518+
/* 8c-i */
2519+
ECMA_TRY_CATCH (current_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
2520+
/* 8c-ii */
2521+
*num_p = ecma_uint32_to_number (index);
2522+
current_index = ecma_make_number_value (num_p);
2523+
ecma_value_t call_args[] = {current_value, current_index, obj_this};
2524+
2525+
ECMA_TRY_CATCH (mapped_value, ecma_op_function_call (func_object_p, arg2, call_args, 3), ret_value);
2526+
2527+
/* 8c-iii
2528+
* By definition we should use [[DefineOwnProperty]] here, but since [[Put]] will create the
2529+
* same property that we need, we can use it for simplicity. No need for a try-catch block
2530+
* since it is called with is_throw = false.
2531+
* ecma_op_to_boolean always returns a simple value, so no need to free.
2532+
*/
2533+
ecma_completion_value_t put_comp_value = ecma_op_object_put (new_array_p, index_str_p, mapped_value, false);
2534+
JERRY_ASSERT (ecma_is_completion_value_normal_true (put_comp_value));
2535+
ecma_free_completion_value (put_comp_value);
2536+
2537+
ECMA_FINALIZE (mapped_value);
2538+
ECMA_FINALIZE (current_value);
2539+
}
2540+
2541+
ecma_deref_ecma_string (index_str_p);
2542+
}
2543+
2544+
if (ecma_is_completion_value_empty (ret_value))
2545+
{
2546+
ret_value = new_array;
2547+
}
2548+
else
2549+
{
2550+
ecma_free_completion_value (new_array);
2551+
}
2552+
2553+
ecma_dealloc_number (num_p);
2554+
}
2555+
2556+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
2557+
ECMA_FINALIZE (len_value);
2558+
ecma_deref_ecma_string (magic_string_length_p);
2559+
ECMA_FINALIZE (obj_this);
2560+
2561+
return ret_value;
2562+
} /* ecma_builtin_array_prototype_object_map */
24552563

24562564
/**
24572565
* The Array.prototype object's 'reduce' routine

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
@@ -76,6 +76,7 @@ ROUTINE (ECMA_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1)
7676
ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2, 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)
79+
ROUTINE (ECMA_MAGIC_STRING_MAP, ecma_builtin_array_prototype_object_map, 2, 1)
7980
ROUTINE (ECMA_MAGIC_STRING_REDUCE, ecma_builtin_array_prototype_object_reduce, 2, 1)
8081

8182
#undef OBJECT_ID

tests/jerry/array_prototype_map.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// helper function - simple implementation
17+
Array.prototype.equals = function (array) {
18+
if (this.length != array.length)
19+
return false;
20+
21+
for (var i = 0; i < this.length; i++) {
22+
if (this[i] instanceof Array && array[i] instanceof Array) {
23+
if (!this[i].equals(array[i]))
24+
return false;
25+
}
26+
else if (this[i] != array[i]) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
// check function type
35+
try {
36+
[0].map(new Object());
37+
assert(false);
38+
} catch(e) {
39+
assert(e instanceof TypeError);
40+
}
41+
42+
// various checks
43+
assert ([1, 4, 9].map(Math.sqrt).equals([1, 2, 3]));
44+
45+
assert (isNaN([1, 4, "X"].map(Number)[2]));
46+
47+
var func = function(val, idx) {
48+
return val + idx;
49+
};
50+
51+
assert ([1, 4, 9].map(func).equals([1,5,11]));
52+
53+
assert ([1, "X", 10].map(func).equals([1, "X1", 12]));
54+
55+
var long_array = [0, 1];
56+
assert (long_array.map(func).equals([0,2]));
57+
58+
long_array[100] = 1;
59+
assert (long_array.map(func).equals([0,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101]));
60+
61+
// check behavior when unable to get length
62+
var obj = {};
63+
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
64+
obj.map = Array.prototype.map;
65+
66+
try {
67+
obj.map(func);
68+
assert(false);
69+
} catch (e) {
70+
assert(e.message === "foo");
71+
assert(e instanceof ReferenceError);
72+
}
73+
74+
// check behavior when unable to get element
75+
var obj = {}
76+
obj.length = 1;
77+
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
78+
obj.map = Array.prototype.map
79+
80+
try {
81+
obj.map(func);
82+
assert(false);
83+
} catch (e) {
84+
assert(e.message === "foo");
85+
assert(e instanceof ReferenceError);
86+
}
87+
88+
// check thisArg
89+
var thisArg = {add: 10};
90+
var func2 = function(value) {
91+
return this.add + value;
92+
}
93+
assert([1,2].map(func2, thisArg).equals([11, 12]));
94+
95+
// check passed Object
96+
var array_example = [1,2];
97+
Object.defineProperty(array_example, 'const', { 'get' : function () {return "CT";} });
98+
var func3 = function(value, idx, thisobj) {
99+
return value * idx + thisobj.const;
100+
}
101+
assert(array_example.map(func3).equals(["0CT", "2CT"]));

0 commit comments

Comments
 (0)