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:
committed by
Zoltan Herczeg
parent
c6a2fd5d3d
commit
4f0b075f85
@@ -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 */
|
||||
|
||||
|
||||
@@ -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) \
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var arrow_is_available = false;
|
||||
|
||||
try {
|
||||
assert (eval ("(f => 5) ()") === 5);
|
||||
arrow_is_available = true;
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
var array1 = [5, 12, 0, 8, 130, 44];
|
||||
|
||||
function bigger_than_10 (element) {
|
||||
return element > 10;
|
||||
}
|
||||
|
||||
assert (array1.find (bigger_than_10) === 12);
|
||||
|
||||
function less_than_0 (element) {
|
||||
if (element == 0) {
|
||||
throw new Error ("zero");
|
||||
}
|
||||
return element < 0;
|
||||
}
|
||||
|
||||
try {
|
||||
array1.find (less_than_0);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof Error);
|
||||
assert (e.message === "zero");
|
||||
}
|
||||
|
||||
var inventory = [
|
||||
{name: 'apples', quantity: 2},
|
||||
{name: 'bananas', quantity: 0},
|
||||
{name: 'cherries', quantity: 5}
|
||||
];
|
||||
|
||||
function isCherries (fruit) {
|
||||
return fruit.name === 'cherries';
|
||||
}
|
||||
|
||||
assert (JSON.stringify (inventory.find (isCherries)) === '{"name":"cherries","quantity":5}');
|
||||
|
||||
if (arrow_is_available) {
|
||||
assert (eval ("inventory.find (fruit => fruit.name === 'bananas')") === inventory[1]);
|
||||
}
|
||||
|
||||
/* Test the callback function arguments */
|
||||
var src_array = [4, 6, 8, 12];
|
||||
var array_index = 0;
|
||||
|
||||
function isPrime (element, index, array) {
|
||||
assert (array_index++ === index);
|
||||
assert (array === src_array)
|
||||
assert (element === array[index]);
|
||||
|
||||
var start = 2;
|
||||
while (start <= Math.sqrt (element)) {
|
||||
if (element % start++ < 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return element > 1;
|
||||
}
|
||||
|
||||
assert (src_array.find (isPrime) === undefined);
|
||||
|
||||
src_array = [4, 5, 8, 12];
|
||||
array_index = 0;
|
||||
assert (src_array.find (isPrime) === 5);
|
||||
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty (obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.find = Array.prototype.find;
|
||||
|
||||
try {
|
||||
obj.find ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
var data = { 0: 1, 2: -3, 3: "string" }
|
||||
assert (Array.prototype.find.call (data, function (e) { return e < 5; }) === undefined);
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty (obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.find = Array.prototype.find
|
||||
|
||||
try {
|
||||
obj.find (function () { return undefined; });
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument is not a callback
|
||||
var array = [1, 2, 3];
|
||||
|
||||
try {
|
||||
array.find (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument does not exist
|
||||
try {
|
||||
array.find ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the there are more than 2 arguments
|
||||
assert (array.find (function (e) { return e < 2 }, {}, 8, 4, 5, 6, 6) === 1);
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var arrow_is_available = false;
|
||||
|
||||
try {
|
||||
assert (eval ("(f => 5) ()") === 5);
|
||||
arrow_is_available = true;
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
var typedArrayTypes = [Int8Array,
|
||||
Uint8Array,
|
||||
Uint8ClampedArray,
|
||||
Int16Array,
|
||||
Uint16Array,
|
||||
Int32Array,
|
||||
Uint32Array,
|
||||
Float32Array,
|
||||
Float64Array];
|
||||
|
||||
|
||||
typedArrayTypes.forEach (function (TypedArray) {
|
||||
|
||||
var array1 = new TypedArray ([5, 12, 0, 8, 120, 44]);
|
||||
|
||||
function bigger_than_10 (element) {
|
||||
return element > 10;
|
||||
}
|
||||
|
||||
assert (array1.find (bigger_than_10) === 12);
|
||||
|
||||
function less_than_0 (element) {
|
||||
if (element == 0) {
|
||||
throw new Error ("zero");
|
||||
}
|
||||
return element < 0;
|
||||
}
|
||||
|
||||
try {
|
||||
array1.find (less_than_0);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof Error);
|
||||
assert (e.message === "zero");
|
||||
}
|
||||
|
||||
if (arrow_is_available) {
|
||||
assert (eval ("array1.find (e => e > 100)") === 120);
|
||||
}
|
||||
|
||||
/* Test the callback function arguments */
|
||||
var src_array = new TypedArray ([4, 6, 8, 12]);
|
||||
var array_index = 0;
|
||||
|
||||
function isPrime (element, index, array) {
|
||||
assert (array_index++ === index);
|
||||
assert (array === src_array)
|
||||
assert (element === array[index]);
|
||||
|
||||
var start = 2;
|
||||
while (start <= Math.sqrt (element)) {
|
||||
if (element % start++ < 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return element > 1;
|
||||
}
|
||||
|
||||
assert (src_array.find (isPrime) === undefined);
|
||||
|
||||
src_array = new TypedArray ([4, 5, 8, 12]);
|
||||
array_index = 0;
|
||||
assert (src_array.find (isPrime) === 5);
|
||||
|
||||
// Checking behavior when the given object is not %TypedArray%
|
||||
try {
|
||||
TypedArray.prototype.find.call (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument is not a callback
|
||||
var array = new TypedArray ([1, 2, 3]);
|
||||
|
||||
try {
|
||||
array.find (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument is not exists
|
||||
try {
|
||||
array.find ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the there are more than 2 arguments
|
||||
assert (array.find (function (e) { return e < 2 }, {}, 8, 4, 5, 6, 6) === 1);
|
||||
})
|
||||
Reference in New Issue
Block a user