Implement Array/TypedArray find index (#2893)

JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu
This commit is contained in:
Daniella Barsony
2019-06-24 11:14:06 +02:00
committed by Robert Fancsik
parent 95650993a2
commit 6f7dbbce7e
8 changed files with 335 additions and 16 deletions
@@ -71,6 +71,7 @@ enum
ECMA_ARRAY_PROTOTYPE_REDUCE,
ECMA_ARRAY_PROTOTYPE_REDUCE_RIGHT,
ECMA_ARRAY_PROTOTYPE_FIND,
ECMA_ARRAY_PROTOTYPE_FIND_INDEX,
ECMA_ARRAY_PROTOTYPE_ENTRIES,
ECMA_ARRAY_PROTOTYPE_VALUES,
ECMA_ARRAY_PROTOTYPE_KEYS,
@@ -1995,10 +1996,11 @@ ecma_builtin_array_reduce_from (ecma_value_t callbackfn, /**< routine's 1st argu
#if ENABLED (JERRY_ES2015_BUILTIN)
/**
* The Array.prototype object's 'find' routine
* The Array.prototype object's 'find' and 'findIndex' routine
*
* See also:
* ECMA-262 v6, 22.1.3.8
* ECMA-262 v6, 22.1.3.9
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
@@ -2007,6 +2009,8 @@ static ecma_value_t
ecma_builtin_array_prototype_object_find (ecma_value_t predicate, /**< callback function */
ecma_value_t predicate_this_arg, /**< this argument for
* invoke predicate */
bool is_find, /**< true - find routine
* false - findIndex routine */
ecma_object_t *obj_p, /**< array object */
uint32_t len) /**< array object's length */
{
@@ -2034,7 +2038,7 @@ ecma_builtin_array_prototype_object_find (ecma_value_t predicate, /**< callback
if (ecma_is_value_found (get_value))
{
/* 8.d - 8.e */
uint32_t current_index = ecma_make_uint32_value (index);
ecma_value_t current_index = ecma_make_uint32_value (index);
ecma_value_t call_args[] = { get_value, current_index, ecma_make_object_value (obj_p) };
@@ -2043,7 +2047,7 @@ ecma_builtin_array_prototype_object_find (ecma_value_t predicate, /**< callback
if (ecma_op_to_boolean (call_value))
{
/* 8.f */
ret_value = ecma_copy_value (get_value);
ret_value = is_find ? ecma_copy_value (get_value) : current_index;
}
ECMA_FINALIZE (call_value);
@@ -2057,7 +2061,7 @@ ecma_builtin_array_prototype_object_find (ecma_value_t predicate, /**< callback
if (ecma_is_value_empty (ret_value))
{
/* 9. */
ret_value = ECMA_VALUE_UNDEFINED;
ret_value = is_find ? ECMA_VALUE_UNDEFINED : ecma_make_integer_value (-1);
}
return ret_value;
@@ -2303,9 +2307,11 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
}
#if ENABLED (JERRY_ES2015_BUILTIN)
case ECMA_ARRAY_PROTOTYPE_FIND:
case ECMA_ARRAY_PROTOTYPE_FIND_INDEX:
{
ret_value = ecma_builtin_array_prototype_object_find (routine_arg_1,
routine_arg_2,
builtin_routine_id == ECMA_ARRAY_PROTOTYPE_FIND,
obj_p,
length);
break;
@@ -64,6 +64,7 @@ ROUTINE (LIT_MAGIC_STRING_REDUCE, ECMA_ARRAY_PROTOTYPE_REDUCE, NON_FIXED, 1)
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ECMA_ARRAY_PROTOTYPE_REDUCE_RIGHT, NON_FIXED, 1)
#if ENABLED (JERRY_ES2015_BUILTIN)
ROUTINE (LIT_MAGIC_STRING_FIND, ECMA_ARRAY_PROTOTYPE_FIND, 2, 1)
ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ECMA_ARRAY_PROTOTYPE_FIND_INDEX, 2, 1)
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ECMA_ARRAY_PROTOTYPE_ENTRIES, 0, 0)
@@ -1501,19 +1501,18 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
} /* ecma_builtin_typedarray_prototype_sort */
/**
* The %TypedArray%.prototype object's 'find' routine
*
* See also:
* ECMA-262 v6, 22.2.3.10
* The %TypedArray%.prototype object's 'find' and 'findIndex' routine helper
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_find (ecma_value_t this_arg, /**< this argument */
ecma_value_t predicate, /**< callback function */
ecma_value_t predicate_this_arg) /**< this argument for
* invoke predicate */
ecma_builtin_typedarray_prototype_find_helper (ecma_value_t this_arg, /**< this argument */
ecma_value_t predicate, /**< callback function */
ecma_value_t predicate_this_arg, /**< this argument for
* invoke predicate */
bool is_find) /**< true - find routine
* false - findIndex routine */
{
if (!ecma_is_typedarray (this_arg))
{
@@ -1544,7 +1543,7 @@ ecma_builtin_typedarray_prototype_find (ecma_value_t this_arg, /**< this argumen
ecma_number_t element_num = ecma_get_typedarray_element (typedarray_buffer_p + byte_index, class_id);
ecma_value_t element_value = ecma_make_number_value (element_num);
ecma_value_t call_args[] = { element_value, ecma_make_uint32_value (buffer_index++), this_arg };
ecma_value_t call_args[] = { element_value, ecma_make_uint32_value (buffer_index), this_arg };
ecma_value_t call_value = ecma_op_function_call (func_object_p, predicate_this_arg, call_args, 3);
@@ -1559,15 +1558,51 @@ ecma_builtin_typedarray_prototype_find (ecma_value_t this_arg, /**< this argumen
if (call_result)
{
return element_value;
return is_find ? element_value : ecma_make_uint32_value (buffer_index);
}
buffer_index++;
ecma_free_value (element_value);
}
return ECMA_VALUE_UNDEFINED;
return is_find ? ECMA_VALUE_UNDEFINED : ecma_make_integer_value (-1);
} /* ecma_builtin_typedarray_prototype_find_helper */
/**
* The %TypedArray%.prototype object's 'find' routine
*
* See also:
* ECMA-262 v6, 22.2.3.10
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_find (ecma_value_t this_arg, /**< this argument */
ecma_value_t predicate, /**< callback function */
ecma_value_t predicate_this_arg) /**< this argument for
* invoke predicate */
{
return ecma_builtin_typedarray_prototype_find_helper (this_arg, predicate, predicate_this_arg, true);
} /* ecma_builtin_typedarray_prototype_find */
/**
* The %TypedArray%.prototype object's 'findIndex' routine
*
* See also:
* ECMA-262 v6, 22.2.3.11
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_find_index (ecma_value_t this_arg, /**< this argument */
ecma_value_t predicate, /**< callback function */
ecma_value_t predicate_this_arg) /**< this argument for
* invoke predicate */
{
return ecma_builtin_typedarray_prototype_find_helper (this_arg, predicate, predicate_this_arg, false);
} /* ecma_builtin_typedarray_prototype_find_index */
/**
* @}
* @}
@@ -69,6 +69,7 @@ ROUTINE (LIT_MAGIC_STRING_SUBARRAY, ecma_builtin_typedarray_prototype_subarray,
ROUTINE (LIT_MAGIC_STRING_FILL, ecma_builtin_typedarray_prototype_fill, 3, 1)
ROUTINE (LIT_MAGIC_STRING_SORT, ecma_builtin_typedarray_prototype_sort, 1, 1)
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_typedarray_prototype_find, 2, 1)
ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ecma_builtin_typedarray_prototype_find_index, 2, 1)
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
+4
View File
@@ -477,6 +477,10 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UNDEFINED_UL, "Undefined")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ARGUMENTS, "arguments")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DECODE_URI, "decodeURI")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENCODE_URI, "encodeURI")
#if ENABLED (JERRY_BUILTIN_ARRAY) && ENABLED (JERRY_ES2015_BUILTIN) \
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FIND_INDEX, "findIndex")
#endif
#if ENABLED (JERRY_BUILTIN_DATE)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_DAY_UL, "getUTCDay")
#endif
+1
View File
@@ -211,6 +211,7 @@ LIT_MAGIC_STRING_UNDEFINED_UL = "Undefined"
LIT_MAGIC_STRING_ARGUMENTS = "arguments"
LIT_MAGIC_STRING_DECODE_URI = "decodeURI"
LIT_MAGIC_STRING_ENCODE_URI = "encodeURI"
LIT_MAGIC_STRING_FIND_INDEX = "findIndex"
LIT_MAGIC_STRING_GET_UTC_DAY_UL = "getUTCDay"
LIT_MAGIC_STRING_GET_UINT16_UL = "getUint16"
LIT_MAGIC_STRING_GET_UINT32_UL = "getUint32"