Skip to content

Commit 8230b66

Browse files
committed
Implement Array.prototype.map()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 6b8e34a commit 8230b66

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,115 @@ 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 */
2563+
24552564
/**
24562565
* @}
24572566
* @}

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

8081
#undef OBJECT_ID
8182
#undef SIMPLE_VALUE

0 commit comments

Comments
 (0)