Implement Array.prototype.includes (#3991)

The algorithm is based on ECMA-262 v11, 22.1.3.13

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam 2020-07-15 19:21:24 +02:00 committed by GitHub
parent c8d15ddbf7
commit 604cfaced4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 181 additions and 1 deletions

View File

@ -76,6 +76,7 @@ enum
ECMA_ARRAY_PROTOTYPE_SYMBOL_ITERATOR,
ECMA_ARRAY_PROTOTYPE_FILL,
ECMA_ARRAY_PROTOTYPE_COPY_WITHIN,
ECMA_ARRAY_PROTOTYPE_INCLUDES,
};
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-array-prototype.inc.h"
@ -2579,6 +2580,90 @@ ecma_builtin_array_prototype_object_copy_within (const ecma_value_t args[], /**<
return ecma_copy_value (ecma_make_object_value (obj_p));
} /* ecma_builtin_array_prototype_object_copy_within */
/**
* The Array.prototype object's 'includes' routine
*
* See also:
* ECMA-262 v11, 22.1.3.13
*
* @return ECMA_VALUE_ERROR -if the operation fails
* ECMA_VALUE_{TRUE/FALSE} - depends on whether the search element is in the array or not
*/
static ecma_value_t
ecma_builtin_array_prototype_includes (const ecma_value_t args[], /**< arguments list */
ecma_length_t args_number, /**< number of arguments */
ecma_object_t *obj_p, /**< array object */
uint32_t len) /**< array object's length */
{
/* 3. */
if (len == 0)
{
return ECMA_VALUE_FALSE;
}
uint32_t from_index = 0;
/* 4-7. */
if (args_number > 1)
{
if (ECMA_IS_VALUE_ERROR (ecma_builtin_helper_array_index_normalize (args[1], len, &from_index)))
{
return ECMA_VALUE_ERROR;
}
}
/* Fast array path */
if (ecma_op_object_is_fast_array (obj_p))
{
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
if (ext_obj_p->u.array.u.hole_count < ECMA_FAST_ARRAY_HOLE_ONE)
{
if (obj_p->u1.property_list_cp != JMEM_CP_NULL)
{
len = JERRY_MIN (ext_obj_p->u.array.length, len);
ecma_value_t *buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, obj_p->u1.property_list_cp);
while (from_index < len)
{
if (ecma_op_same_value_zero (buffer_p[from_index], args[0]))
{
return ECMA_VALUE_TRUE;
}
from_index++;
}
}
return ECMA_VALUE_FALSE;
}
}
/* 8. */
while (from_index < len)
{
ecma_value_t element = ecma_op_object_get_by_uint32_index (obj_p, from_index);
if (ECMA_IS_VALUE_ERROR (element))
{
return element;
}
if (ecma_op_same_value_zero (element, args[0]))
{
ecma_free_value (element);
return ECMA_VALUE_TRUE;
}
ecma_free_value (element);
from_index++;
}
/* 9. */
return ECMA_VALUE_FALSE;
} /* ecma_builtin_array_prototype_includes */
#endif /* ENABLED (JERRY_ESNEXT) */
/**
@ -2798,6 +2883,14 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
length);
break;
}
case ECMA_ARRAY_PROTOTYPE_INCLUDES:
{
ret_value = ecma_builtin_array_prototype_includes (arguments_list_p,
arguments_number,
obj_p,
length);
break;
}
#endif /* ENABLED (JERRY_ESNEXT) */
default:
{

View File

@ -76,6 +76,7 @@ ROUTINE (LIT_MAGIC_STRING_FILL, ECMA_ARRAY_PROTOTYPE_FILL, 3, 1)
ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ECMA_ARRAY_PROTOTYPE_COPY_WITHIN, NON_FIXED, 2)
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ECMA_ARRAY_PROTOTYPE_ENTRIES, 0, 0)
ROUTINE (LIT_MAGIC_STRING_KEYS, ECMA_ARRAY_PROTOTYPE_KEYS, 0, 0)
ROUTINE (LIT_MAGIC_STRING_INCLUDES, ECMA_ARRAY_PROTOTYPE_INCLUDES, NON_FIXED, 1)
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_VALUES, LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
INTRINSIC_PROPERTY (LIT_GLOBAL_SYMBOL_ITERATOR, LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES,

View File

@ -546,7 +546,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_MONTH_UL, "getMonth")
#if ENABLED (JERRY_BUILTIN_DATAVIEW)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UINT8_UL, "getUint8")
#endif
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT)
#if ENABLED (JERRY_BUILTIN_ARRAY) && ENABLED (JERRY_ESNEXT) \
|| ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_INCLUDES, "includes")
#endif
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_IS_FINITE, "isFinite")

View File

@ -0,0 +1,85 @@
// 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.
assert(Array.prototype.includes.length === 1);
assert(Array.prototype.includes.name === "includes");
var num_arr = [1, 2, 3,,4, 5];
var str_arr = ['foo', 'bar', 'baz', NaN, 'foo'];
var obj = {};
var obj_arr = [1, obj, 2];
var empry_arr = [];
assert(num_arr.includes(3) === true);
assert(num_arr.includes(3, 4) === false);
assert(num_arr.includes(3, -5) === true);
assert(num_arr.includes(undefined) === true);
assert(num_arr.includes(3, Infinity) === false);
assert(num_arr.includes(3, -0) === true);
assert(str_arr.includes(NaN) === true);
assert(str_arr.includes('foo', 4) === true);
assert(str_arr.includes('f') === false);
assert(obj_arr.includes(obj) === true);
assert(obj_arr.includes({}) === false);
assert(empry_arr.includes() === false);
assert(empry_arr.includes(3) === false);
assert([undefined].includes() === true);
Object.defineProperty(num_arr, "1", { get: function() {throw 42}});
try {
num_arr.includes(4);
assert(false);
} catch (e) {
assert(e === 42);
}
var sym = Symbol('foo');
try {
num_arr.includes(3, sym);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// Remove the buffer
var array = [1, 2, 3, 4, 5];
var found = array.includes(4, {
valueOf: function() {
array.length = 0;
}
})
assert(found === false);
// Extend the buffer
var array = [1, 2, 3];
var found = array.includes(2, {
valueOf: function() {
array.length = 5;
}
})
assert(found === true);
// Reduce the buffer
var array = [1, 2, 3, 4, 5, 6, 7];
var found = array.includes(6, {
valueOf: function() {
array.length = 5;
}
})
assert(found === false);