-
Notifications
You must be signed in to change notification settings - Fork 859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ES2019 Array.prototype.flatMap #1372
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want the case to be the same as other method names.
rhino/src/org/mozilla/javascript/NativeArray.java
Lines 430 to 431 in 90d3b44