-
Notifications
You must be signed in to change notification settings - Fork 864
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.flat (#1313)
* feat: implement Array.prototype.flat * tests: add (slightly modified) harmony test * feat: throw TypeError if array is too big * fix: remove constructor assigned Array.flat * fix: just remove the comment about es2019 By other code looks like the Rhino version context is just ES6. * tests: update test262 --------- Co-authored-by: RBRi <rbri@rbri.de>
- Loading branch information
Showing
5 changed files
with
156 additions
and
4 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,71 @@ | ||
// 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.js and changed due to Rhino errors | ||
// TypeError: redeclaration of const input | ||
load("testsrc/assert.js"); | ||
|
||
assertEquals(Array.prototype.flat.length, 0); | ||
assertEquals(Array.prototype.flat.name, 'flat'); | ||
|
||
let input; | ||
|
||
{ | ||
input = [1, [2], [[3]]]; | ||
|
||
assertEquals(input.flat(), [1, 2, [3]]); | ||
assertEquals(input.flat(1), [1, 2, [3]]); | ||
assertEquals(input.flat(true), [1, 2, [3]]); | ||
assertEquals(input.flat(undefined), [1, 2, [3]]); | ||
|
||
assertEquals(input.flat(-Infinity), [1, [2], [[3]]]); | ||
assertEquals(input.flat(-1), [1, [2], [[3]]]); | ||
assertEquals(input.flat(-0), [1, [2], [[3]]]); | ||
assertEquals(input.flat(0), [1, [2], [[3]]]); | ||
assertEquals(input.flat(false), [1, [2], [[3]]]); | ||
assertEquals(input.flat(null), [1, [2], [[3]]]); | ||
assertEquals(input.flat(''), [1, [2], [[3]]]); | ||
assertEquals(input.flat('foo'), [1, [2], [[3]]]); | ||
assertEquals(input.flat(/./), [1, [2], [[3]]]); | ||
assertEquals(input.flat([]), [1, [2], [[3]]]); | ||
assertEquals(input.flat({}), [1, [2], [[3]]]); | ||
//assertEquals( | ||
// input.flat(new Proxy({}, {})), [1, [2], [[3]]]); | ||
assertEquals(input.flat((x) => x), [1, [2], [[3]]]); | ||
assertEquals( | ||
input.flat(String), [1, [2], [[3]]]); | ||
|
||
assertEquals(input.flat(2), [1, 2, 3]); | ||
assertEquals(input.flat(Infinity), [1, 2, 3]); | ||
|
||
assertThrows(() => { input.flat(Symbol()); }, TypeError); | ||
assertThrows(() => { input.flat(Object.create(null)); }, TypeError); | ||
} | ||
|
||
{ | ||
input = { 0: 'a', 1: 'b', 2: 'c', length: 'wat' }; | ||
assertEquals(Array.prototype.flat.call(input, Infinity), []); | ||
} | ||
|
||
{ | ||
let count = 0; | ||
input = { | ||
get length() { ++count; return 0; } | ||
}; | ||
const result = Array.prototype.flat.call(input, Infinity); | ||
assertEquals(count, 1); | ||
} | ||
|
||
{ | ||
const descriptor = Object.getOwnPropertyDescriptor( | ||
Array.prototype, | ||
'flat' | ||
); | ||
assertEquals(descriptor.value, Array.prototype.flat); | ||
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/harmony/ArrayFlatTest.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.harmony; | ||
|
||
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/harmony/array-flat.js") | ||
@LanguageVersion(Context.VERSION_ES6) | ||
public class ArrayFlatTest 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