-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ES2019 Array.prototype.flatMap (#1372)
* feat: implement Array.prototype.flatMap * tests: update test262.properties
- Loading branch information
Showing
5 changed files
with
274 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright 2018 the V8 project authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
// Taken from https://github.com/v8/v8/blob/main/test/mjsunit/harmony/array-flat-map.js and changed due to Rhino errors | ||
// TypeError: redeclaration of const input | ||
|
||
// Flags: --allow-natives-syntax | ||
load("testsrc/assert.js"); | ||
|
||
let input; | ||
let result; | ||
|
||
assertEquals(Array.prototype.flatMap.length, 1); | ||
assertEquals(Array.prototype.flatMap.name, 'flatMap'); | ||
|
||
assertEquals( | ||
[1, 2, 3, 4].flatMap((element) => [element, element ** 2]), | ||
[1, 1, 2, 4, 3, 9, 4, 16] | ||
); | ||
assertEquals( | ||
[1, 2, 3, 4].flatMap((element) => [[element, element ** 2]]), | ||
[[1, 1], [2, 4], [3, 9], [4, 16]] | ||
); | ||
|
||
{ | ||
const elements = new Set([ | ||
-Infinity, | ||
-1, | ||
-0, | ||
+0, | ||
+1, | ||
Infinity, | ||
null, | ||
undefined, | ||
true, | ||
false, | ||
'', | ||
'foo', | ||
/./, | ||
[], | ||
{}, | ||
Object.create(null), | ||
//new Proxy({}, {}), | ||
Symbol(), | ||
x => x ** 2, | ||
String | ||
]); | ||
|
||
for (let value of elements) { | ||
assertEquals( | ||
[value].flatMap((element) => [element, element]), | ||
[value, value] | ||
); | ||
} | ||
} | ||
|
||
{ | ||
const array = [42]; | ||
assertEquals( | ||
[array].flatMap((element) => [element, element]), | ||
[array, array] | ||
); | ||
} | ||
|
||
{ | ||
const nonCallables = new Set([ | ||
-Infinity, | ||
-1, | ||
-0, | ||
+0, | ||
+1, | ||
Infinity, | ||
null, | ||
undefined, | ||
true, | ||
false, | ||
'', | ||
'foo', | ||
/./, | ||
[], | ||
{}, | ||
Object.create(null), | ||
//new Proxy({}, {}), | ||
Symbol(), | ||
]); | ||
for (let nonCallable of nonCallables) { | ||
assertThrows(() => { | ||
[].flatMap(nonCallable); | ||
}, TypeError); | ||
} | ||
} | ||
|
||
{ | ||
const object = { | ||
foo: 42, | ||
get length() { | ||
object.foo = 0; | ||
} | ||
}; | ||
result = [object].flatMap((element) => [element, element]); | ||
//%HeapObjectVerify(result); | ||
assertEquals(result, [object, object]); | ||
assertEquals(result[0].foo, 42); | ||
} | ||
|
||
assertThrows(() => { | ||
Array.prototype.flatMap.call(null, (element) => element); | ||
}, TypeError); | ||
assertThrows(() => { | ||
Array.prototype.flatMap.call(undefined, (element) => element); | ||
}, TypeError); | ||
|
||
assertEquals( | ||
Array.prototype.flatMap.call( | ||
{ | ||
length: 1, | ||
0: 'a', | ||
1: 'b', | ||
}, | ||
(element) => element | ||
), | ||
['a'] | ||
); | ||
assertEquals( | ||
Array.prototype.flatMap.call( | ||
{ | ||
length: 2, | ||
0: 'a', | ||
1: 'b', | ||
}, | ||
(element) => element | ||
), | ||
['a', 'b'] | ||
); | ||
|
||
{ | ||
result = [1, 2, 3].flatMap(function() { | ||
return [this]; | ||
}, 'abc'); | ||
assertEquals(true, result[0] == 'abc'); | ||
assertEquals(true, result[1] == 'abc'); | ||
assertEquals(true, result[2] == 'abc'); | ||
} | ||
|
||
{ | ||
input = { 0: 'a', 1: 'b', 2: 'c', length: 'wat' }; | ||
assertEquals(Array.prototype.flatMap.call(input, x => [x]), []); | ||
} | ||
|
||
{ | ||
let count = 0; | ||
input = { | ||
get length() { ++count; return 0; } | ||
}; | ||
result = Array.prototype.flatMap.call(input, x => [x]); | ||
assertEquals(count, 1); | ||
} | ||
|
||
{ | ||
const descriptor = Object.getOwnPropertyDescriptor( | ||
Array.prototype, | ||
'flatMap' | ||
); | ||
assertEquals(descriptor.value, Array.prototype.flatMap); | ||
assertEquals(descriptor.writable, true); | ||
assertEquals(descriptor.enumerable, false); | ||
assertEquals(descriptor.configurable, true); | ||
} | ||
|
||
"success"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
testsrc/org/mozilla/javascript/tests/es2019/ArrayFlatMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript.tests.es2019; | ||
|
||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.drivers.LanguageVersion; | ||
import org.mozilla.javascript.drivers.RhinoTest; | ||
import org.mozilla.javascript.drivers.ScriptTestsBase; | ||
|
||
@RhinoTest("testsrc/jstests/es2019/array-flat-map.js") | ||
@LanguageVersion(Context.VERSION_ES6) | ||
public class ArrayFlatMapTest extends ScriptTestsBase {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters