Implement ES2015 {Array, %TypedArray%}.prototype.find routine (#2631)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2018-12-12 10:13:05 +01:00
committed by Zoltan Herczeg
parent c6a2fd5d3d
commit 4f0b075f85
8 changed files with 432 additions and 0 deletions
@@ -2404,6 +2404,106 @@ ecma_builtin_array_prototype_object_reduce_right (ecma_value_t this_arg, /**< th
return ecma_builtin_array_reduce_from (this_arg, args, args_number, false);
} /* ecma_builtin_array_prototype_object_reduce_right */
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
/**
* The Array.prototype object's 'find' routine
*
* See also:
* ECMA-262 v6, 22.1.3.8
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_array_prototype_object_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 */
{
/* 1. */
ecma_value_t obj_this = ecma_op_to_object (this_arg);
/* 2. */
if (ECMA_IS_VALUE_ERROR (obj_this))
{
return obj_this;
}
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
/* 3 - 4. */
ecma_value_t len_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_LENGTH);
if (ECMA_IS_VALUE_ERROR (len_value))
{
ecma_free_value (obj_this);
return len_value;
}
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
uint32_t len = ecma_number_to_uint32 (len_number);
/* 5. */
if (!ecma_op_is_callable (predicate))
{
ecma_free_value (len_value);
ecma_free_value (obj_this);
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
}
/* We already checked that predicate is callable, so it will always be an object. */
JERRY_ASSERT (ecma_is_value_object (predicate));
ecma_object_t *func_object_p = ecma_get_object_from_value (predicate);
/* 7 - 8. */
for (uint32_t index = 0; index < len && ecma_is_value_empty (ret_value); index++)
{
/* 8.a */
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
/* 8.b - 8.c */
ECMA_TRY_CATCH (get_value, ecma_op_object_find (obj_p, index_str_p), ret_value);
if (ecma_is_value_found (get_value))
{
/* 8.d - 8.e */
uint32_t current_index = ecma_make_uint32_value (index);
ecma_value_t call_args[] = { get_value, current_index, obj_this };
ECMA_TRY_CATCH (call_value, ecma_op_function_call (func_object_p, predicate_this_arg, call_args, 3), ret_value);
if (ecma_op_to_boolean (call_value))
{
/* 8.f */
ret_value = ecma_copy_value (get_value);
}
ECMA_FINALIZE (call_value);
}
ECMA_FINALIZE (get_value);
ecma_deref_ecma_string (index_str_p);
}
if (ecma_is_value_empty (ret_value))
{
/* 9. */
ret_value = ECMA_VALUE_UNDEFINED;
}
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
ecma_free_value (len_value);
ecma_free_value (obj_this);
return ret_value;
} /* ecma_builtin_array_prototype_object_find */
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
/**
* @}
* @}
@@ -60,6 +60,9 @@ ROUTINE (LIT_MAGIC_STRING_MAP, ecma_builtin_array_prototype_object_map, 2, 1)
ROUTINE (LIT_MAGIC_STRING_FILTER, ecma_builtin_array_prototype_object_filter, 2, 1)
ROUTINE (LIT_MAGIC_STRING_REDUCE, ecma_builtin_array_prototype_object_reduce, NON_FIXED, 1)
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_reduce_right, NON_FIXED, 1)
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_array_prototype_object_find, 2, 1)
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
#endif /* !CONFIG_DISABLE_ARRAY_BUILTIN */
@@ -1471,6 +1471,74 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
return ret_value;
} /* ecma_builtin_typedarray_prototype_sort */
/**
* 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 */
{
if (!ecma_is_typedarray (this_arg))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
}
if (!ecma_op_is_callable (predicate))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
}
JERRY_ASSERT (ecma_is_value_object (predicate));
ecma_object_t *func_object_p = ecma_get_object_from_value (predicate);
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
uint32_t typedarray_length = ecma_typedarray_get_length (typedarray_p);
lit_magic_string_id_t class_id = ecma_object_get_class_name (typedarray_p);
lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (typedarray_p);
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
uint8_t element_size = (uint8_t) (1 << shift);
uint32_t buffer_index = 0;
uint32_t limit = typedarray_length * element_size;
for (uint32_t byte_index = 0; byte_index < limit; byte_index += element_size)
{
JERRY_ASSERT (buffer_index < typedarray_length);
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_value = ecma_op_function_call (func_object_p, predicate_this_arg, call_args, 3);
if (ECMA_IS_VALUE_ERROR (call_value))
{
ecma_free_value (element_value);
return call_value;
}
bool call_result = ecma_op_to_boolean (call_value);
ecma_free_value (call_value);
if (call_result)
{
return element_value;
}
ecma_free_value (element_value);
}
return ECMA_VALUE_UNDEFINED;
} /* ecma_builtin_typedarray_prototype_find */
/**
* @}
* @}
@@ -61,6 +61,7 @@ ROUTINE (LIT_MAGIC_STRING_SET, ecma_builtin_typedarray_prototype_set, 2, 1)
ROUTINE (LIT_MAGIC_STRING_SUBARRAY, ecma_builtin_typedarray_prototype_subarray, 2, 2)
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)
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
+6
View File
@@ -112,6 +112,12 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
#endif
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FILL, "fill")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) && !defined (CONFIG_DISABLE_ES2015_BUILTIN) \
|| !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FIND, "find")
#endif
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \
+1
View File
@@ -67,6 +67,7 @@ LIT_MAGIC_STRING_CEIL = "ceil"
LIT_MAGIC_STRING_EVAL = "eval"
LIT_MAGIC_STRING_EXEC = "exec"
LIT_MAGIC_STRING_FILL = "fill"
LIT_MAGIC_STRING_FIND = "find"
LIT_MAGIC_STRING_FROM = "from"
LIT_MAGIC_STRING_JOIN = "join"
LIT_MAGIC_STRING_KEYS = "keys"