Implement Array.flat and Array.flatMap (#3925)

JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu
This commit is contained in:
kisbg 2020-08-12 16:51:04 +02:00 committed by GitHub
parent 0bb4626ddb
commit cd34bfa4c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 405 additions and 39 deletions

View File

@ -41,6 +41,18 @@ SIMPLE_VALUE (LIT_MAGIC_STRING_FIND_INDEX,
ECMA_VALUE_TRUE,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE)
SIMPLE_VALUE (LIT_MAGIC_STRING_FLAT,
ECMA_VALUE_TRUE,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE)
SIMPLE_VALUE (LIT_MAGIC_STRING_FLATMAP,
ECMA_VALUE_TRUE,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE)
SIMPLE_VALUE (LIT_MAGIC_STRING_INCLUDES,
ECMA_VALUE_TRUE,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE)
SIMPLE_VALUE (LIT_MAGIC_STRING_KEYS,
ECMA_VALUE_TRUE,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE)

View File

@ -79,6 +79,8 @@ enum
ECMA_ARRAY_PROTOTYPE_FILL,
ECMA_ARRAY_PROTOTYPE_COPY_WITHIN,
ECMA_ARRAY_PROTOTYPE_INCLUDES,
ECMA_ARRAY_PROTOTYPE_FLAT,
ECMA_ARRAY_PROTOTYPE_FLATMAP,
};
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-array-prototype.inc.h"
@ -2687,6 +2689,222 @@ ecma_builtin_array_prototype_includes (const ecma_value_t args[], /**< arguments
/* 9. */
return ECMA_VALUE_FALSE;
} /* ecma_builtin_array_prototype_includes */
/**
* Abstract operation: FlattenIntoArray
*
* See also:
* ECMA-262 v10, 22.1.3.10.1
*
* @return ECMA_VALUE_ERROR -if the operation fails
* ecma value which contains target_index
*/
static ecma_value_t
ecma_builtin_array_flatten_into_array (ecma_value_t target, /**< target will contains source's elements */
ecma_object_t *source, /**< source object */
ecma_length_t source_len, /**< source object length */
ecma_length_t start, /**< remaining recursion depth */
ecma_number_t depth, /**< start index offset */
ecma_value_t mapped_value, /**< mapped value */
ecma_value_t thisArg) /**< this arg */
{
/* 7. */
ecma_length_t target_index = start;
/* 9. */
for (ecma_length_t source_index = 0; source_index < source_len; source_index++)
{
/* a. */
ecma_value_t element = ecma_op_object_find_by_index (source, source_index);
if (ECMA_IS_VALUE_ERROR (element))
{
return element;
}
if (!ecma_is_value_found (element))
{
continue;
}
/* b-c. */
if (!ecma_is_value_undefined (mapped_value))
{
/* i-ii. */
ecma_value_t source_val = ecma_make_length_value (source_index);
ecma_value_t args[] = {element, source_val, ecma_make_object_value (source)};
ecma_value_t temp_element = ecma_op_function_call (ecma_get_object_from_value (mapped_value), thisArg, args, 3);
ecma_free_value (element);
ecma_free_value (source_val);
if (ECMA_IS_VALUE_ERROR (temp_element))
{
return temp_element;
}
element = temp_element;
}
/* iv-v. */
if (depth > 0)
{
ecma_value_t is_array = ecma_is_value_array (element);
if (ECMA_IS_VALUE_ERROR (is_array))
{
ecma_free_value (element);
return is_array;
}
if (ecma_is_value_true (is_array))
{
ecma_object_t *element_obj = ecma_get_object_from_value (element);
ecma_length_t element_len;
ecma_value_t len_value = ecma_op_object_get_length (element_obj, &element_len);
if (ECMA_IS_VALUE_ERROR (len_value))
{
ecma_deref_object (element_obj);
return len_value;
}
ecma_value_t target_index_val = ecma_builtin_array_flatten_into_array (target,
element_obj,
element_len,
target_index,
depth - 1,
ECMA_VALUE_UNDEFINED,
ECMA_VALUE_UNDEFINED);
ecma_deref_object (element_obj);
if (ECMA_IS_VALUE_ERROR (target_index_val))
{
return target_index_val;
}
target_index = (ecma_length_t) ecma_get_number_from_value (target_index_val);
continue;
}
}
/* vi. */
const uint8_t flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_IS_THROW;
ecma_value_t element_temp = ecma_builtin_helper_def_prop_by_index (ecma_get_object_from_value (target),
target_index,
element,
flags);
ecma_free_value (element);
if (ECMA_IS_VALUE_ERROR (element_temp))
{
return element_temp;
}
target_index++;
}
/* 10. */
return ecma_make_length_value (target_index);
} /* ecma_builtin_array_flatten_into_array */
/**
* The Array.prototype object's 'flat' routine
*
* See also:
* ECMA-262 v10, 22.1.3.10
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_array_prototype_object_flat (const ecma_value_t args[], /**< arguments list */
uint32_t args_number, /**< number of arguments */
ecma_object_t *obj_p, /**< array object */
ecma_length_t len) /**< array object's length */
{
/* 3. */
ecma_number_t depth_num = 1;
/* 4. */
if (args_number > 0 && ECMA_IS_VALUE_ERROR (ecma_op_to_integer (args[0], &depth_num)))
{
return ECMA_VALUE_ERROR;
}
/* 5. */
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
{
return new_array;
}
/* 6. */
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (new_array,
obj_p,
len,
0,
depth_num,
ECMA_VALUE_UNDEFINED,
ECMA_VALUE_UNDEFINED);
if (ECMA_IS_VALUE_ERROR (flatten_val))
{
ecma_free_value (new_array);
return flatten_val;
}
/* 7. */
return new_array;
} /* ecma_builtin_array_prototype_object_flat */
/**
* The Array.prototype object's 'flatMap' routine
*
* See also:
* ECMA-262 v10, 22.1.3.11
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_array_prototype_object_flat_map (ecma_value_t callback, /**< callbackFn */
ecma_value_t this_arg, /**< thisArg */
ecma_object_t *obj_p, /**< array object */
ecma_length_t len) /**< array object's length */
{
if (!ecma_op_is_callable (callback))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
}
/* 4. */
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
if (ECMA_IS_VALUE_ERROR (new_array))
{
return new_array;
}
/* 5. */
ecma_value_t flatten_val = ecma_builtin_array_flatten_into_array (new_array,
obj_p,
len,
0,
1,
callback,
this_arg);
if (ECMA_IS_VALUE_ERROR (flatten_val))
{
ecma_free_value (new_array);
return flatten_val;
}
/* 6. */
return new_array;
} /* ecma_builtin_array_prototype_object_flat_map */
#endif /* ENABLED (JERRY_ESNEXT) */
/**
@ -2916,6 +3134,22 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
length);
break;
}
case ECMA_ARRAY_PROTOTYPE_FLAT:
{
ret_value = ecma_builtin_array_prototype_object_flat (arguments_list_p,
arguments_number,
obj_p,
length);
break;
}
case ECMA_ARRAY_PROTOTYPE_FLATMAP:
{
ret_value = ecma_builtin_array_prototype_object_flat_map (routine_arg_1,
routine_arg_2,
obj_p,
length);
break;
}
#endif /* ENABLED (JERRY_ESNEXT) */
default:
{

View File

@ -79,6 +79,8 @@ ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ECMA_ARRAY_PROTOTYPE_COPY_WITHIN, NON_FIX
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)
ROUTINE (LIT_MAGIC_STRING_FLAT, ECMA_ARRAY_PROTOTYPE_FLAT, 1, 0)
ROUTINE (LIT_MAGIC_STRING_FLATMAP, ECMA_ARRAY_PROTOTYPE_FLATMAP, 2, 1)
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_TO_STRING_UL, LIT_MAGIC_STRING_TO_STRING_UL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_VALUES, LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES,

View File

@ -156,6 +156,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FILL, "fill")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FIND, "find")
#endif
#if ENABLED (JERRY_ESNEXT)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FLAT, "flat")
#endif
#if ENABLED (JERRY_BUILTIN_ARRAY) && ENABLED (JERRY_ESNEXT) \
|| ENABLED (JERRY_BUILTIN_TYPEDARRAY)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
@ -465,6 +468,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENTRIES, "entries")
#if ENABLED (JERRY_BUILTIN_PROMISE)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FINALLY, "finally")
#endif
#if ENABLED (JERRY_ESNEXT)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FLATMAP, "flatMap")
#endif
#if ENABLED (JERRY_BUILTIN_ARRAY) \
|| ENABLED (JERRY_BUILTIN_MAP) \
|| ENABLED (JERRY_BUILTIN_SET) \
@ -558,9 +564,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_ARRAY) && ENABLED (JERRY_ESNEXT) \
|| ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT) \
|| ENABLED (JERRY_BUILTIN_TYPEDARRAY)
#if ENABLED (JERRY_BUILTIN_TYPEDARRAY) \
|| 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

@ -75,6 +75,7 @@ LIT_MAGIC_STRING_EVAL = "eval"
LIT_MAGIC_STRING_EXEC = "exec"
LIT_MAGIC_STRING_FILL = "fill"
LIT_MAGIC_STRING_FIND = "find"
LIT_MAGIC_STRING_FLAT = "flat"
LIT_MAGIC_STRING_FROM = "from"
LIT_MAGIC_STRING_IMUL = "imul"
LIT_MAGIC_STRING_JOIN = "join"
@ -185,6 +186,7 @@ LIT_MAGIC_STRING_BOOLEAN = "boolean"
LIT_MAGIC_STRING_COMPILE = "compile"
LIT_MAGIC_STRING_DEFAULT = "default"
LIT_MAGIC_STRING_DOTALL = "dotAll"
LIT_MAGIC_STRING_FLATMAP = "flatMap"
LIT_MAGIC_STRING_FOR_EACH_UL = "forEach"
LIT_MAGIC_STRING_GET_DATE_UL = "getDate"
LIT_MAGIC_STRING_GET_INT8_UL = "getInt8"

View File

@ -0,0 +1,146 @@
// 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.
// helper function - simple implementation
Array.prototype.equals = function (array) {
if (this.length != array.length)
return false;
for (var i = 0; i < this.length; i++) {
if (this[i] instanceof Array && array[i] instanceof Array) {
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
return false;
}
}
return true;
}
// various checks on flat prototype
assert ([1, 4, 9].flat(2).equals([1, 4, 9]))
assert ([[4,9]].flat(0).equals([[4,9]]))
assert ([[4,9]].flat(1).equals([4,9]))
assert ([1, 4, [9,3,[2,4,[3,4]]]].flat(5).equals([1, 4, 9, 3, 2, 4, 3, 4]));
var array1 = [1,3,4]
var array2 = [array1,array1]
assert(array2.flat(1).equals([1,3,4,1,3,4]))
assert(array2.flat(0).equals([[1,3,4],[1,3,4]]))
var array3 = [array2]
assert(array3.flat(0).equals([[[1,3,4],[1,3,4]]]))
assert(array3.flat(1).equals([[1,3,4],[1,3,4]]))
assert(array3.flat(2).equals([1,3,4,1,3,4]))
var array4 = []
assert(array4.flat(10).equals([]))
var array5 = [null, array4]
assert(array5.flat(10).equals([null]))
// various checks on flatMap Prototype
assert([1, 2, 3, 4].flatMap(x => [x, x * 2]).equals([1,2,2,4,3,6,4,8]))
assert([1, 2, 3, 4].flatMap(x => [x, x * x]).equals([1,1,2,4,3,9,4,16]))
assert(array1.flatMap(x => [x, x * 2]).equals([1,2,3,6,4,8]))
assert(array4.flatMap(x => [x, x * 2]).equals([]))
function check_flat (map, depth)
{
try {
map.flat (depth)
assert (false)
} catch (e) {
assert (e instanceof TypeError);
}
}
function check_flat_map (map, mapper)
{
try {
map.flatMap (mapper)
assert (false)
} catch (e) {
assert (e instanceof TypeError);
}
}
// invalid depth
check_flat ([1,2], Symbol())
// constructor is null
var a = new Array();
a.constructor = null;
check_flat (a,1)
// callback is not object
check_flat_map ([1,2], null)
// constructor is null
check_flat_map (a,x => [x, x * x])
// "0" get is a TypeError
var array_2 = [1,2]
Object.defineProperty (array_2, '0', { 'get' : function () { throw new TypeError (); } });
check_flat (array_2, 1)
check_flat_map (array_2,x => [x, x * x])
// "0" is not Array and throw error
var revocable = Proxy.revocable ({}, {});
var proxy = revocable.proxy;
revocable.revoke();
var array_3 = [proxy,2]
check_flat (array_3, 1)
// second call of FlattenIntoArray return with a error
var array_4_1 = [1,2]
Object.defineProperty (array_4_1, '0', { 'get' : function () { throw new TypeError (); } });
var array_4 = [array_4_1,1,2]
check_flat (array_4, 1)
// unable to add new property
var array_5 = [array_2,1,2]
check_flat (array_2, 1)
var A = function(_length) {
Object.defineProperty(this, "0", {
writable: true,
configurable: false,
});
};
var arr = [1];
arr.constructor = {};
arr.constructor[Symbol.species] = A;
check_flat_map (arr, A)
// element value is not found
var array_6 = []
array_6.length = 2
array_6.flat()
// mapped function is error
var array_7 = [1,2]
var f = function () {
throw new TypeError()
}
check_flat_map (array_7, f)
var obj = new Proxy ([], { get(t, p, r) {
if (p === 'length') {
throw new TypeError();
}
}})

View File

@ -13,5 +13,5 @@
// limitations under the License.
var expected = '{"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"keys":true,"values":true}';
var expected = '{"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"flat":true,"flatMap":true,"includes":true,"keys":true,"values":true}';
assert(JSON.stringify(Array.prototype[Symbol.unscopables]) === expected);

View File

@ -151,47 +151,12 @@
<test id="built-ins/Array/proto-from-ctor-realm-one.js"><reason></reason></test>
<test id="built-ins/Array/proto-from-ctor-realm-two.js"><reason></reason></test>
<test id="built-ins/Array/proto-from-ctor-realm-zero.js"><reason></reason></test>
<test id="built-ins/Array/prototype/Symbol.unscopables/value.js"><reason></reason></test>
<test id="built-ins/Array/prototype/concat/create-proto-from-ctor-realm-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/concat/create-proto-from-ctor-realm-non-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/copyWithin/coerced-values-start-change-start.js"><reason></reason></test>
<test id="built-ins/Array/prototype/copyWithin/coerced-values-start-change-target.js"><reason></reason></test>
<test id="built-ins/Array/prototype/filter/create-proto-from-ctor-realm-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/filter/create-proto-from-ctor-realm-non-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/array-like-objects.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/bound-function-call.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/empty-array-elements.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/empty-object-elements.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/length.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/name.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/non-object-ctor-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/null-undefined-elements.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/null-undefined-input-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/positive-infinity.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/prop-desc.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/proxy-access-count.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/symbol-object-create-null-depth-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flat/target-array-with-non-writable-property.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/array-like-objects-nested.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/array-like-objects-poisoned-length.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/array-like-objects-typedarrays.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/array-like-objects.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/bound-function-argument.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/depth-always-one.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/length.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/name.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/non-callable-argument-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/prop-desc.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/proxy-access-count.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/target-array-with-non-writable-property.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-ctor-non-object.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-ctor-object-species-bad-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor-poisoned-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-ctor-object-species-custom-ctor.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-ctor-object-species.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/this-value-null-undefined-throws.js"><reason></reason></test>
<test id="built-ins/Array/prototype/flatMap/thisArg-argument.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/create-proto-from-ctor-realm-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/create-proto-from-ctor-realm-non-array.js"><reason></reason></test>
<test id="built-ins/Array/prototype/slice/create-proto-from-ctor-realm-array.js"><reason></reason></test>