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
8 changed files with 405 additions and 39 deletions
@@ -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();
}
}})
@@ -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);
-35
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>