Skip to content

Commit

Permalink
[JSC] Rename Array#flatten to flat
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=186012

Reviewed by Saam Barati.

JSTests:

* stress/array-flatten.js:
(shouldThrow):
* test262/expectations.yaml:

Source/JavaScriptCore:

Rename Array#flatten to Array#flat. This rename is done in TC39 since flatten
conflicts with the mootools' function name.

* builtins/ArrayPrototype.js:
(globalPrivate.flatIntoArray):
(flat):
(globalPrivate.flatIntoArrayWithCallback):
(flatMap):
(globalPrivate.flattenIntoArray): Deleted.
(flatten): Deleted.
(globalPrivate.flattenIntoArrayWithCallback): Deleted.
* runtime/ArrayPrototype.cpp:
(JSC::ArrayPrototype::finishCreation):

LayoutTests:

* inspector/model/remote-object-get-properties-expected.txt:
* js/Object-getOwnPropertyNames-expected.txt:
* js/script-tests/Object-getOwnPropertyNames.js:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@232226 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
[email protected] committed May 26, 2018
1 parent ebf7dc7 commit 5124407
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 77 deletions.
11 changes: 11 additions & 0 deletions JSTests/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2018-05-26 Yusuke Suzuki <[email protected]>

[JSC] Rename Array#flatten to flat
https://bugs.webkit.org/show_bug.cgi?id=186012

Reviewed by Saam Barati.

* stress/array-flatten.js:
(shouldThrow):
* test262/expectations.yaml:

2018-05-23 Yusuke Suzuki <[email protected]>

Upgrade test262 to May 24 version
Expand Down
64 changes: 32 additions & 32 deletions JSTests/stress/array-flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,75 +34,75 @@ function shouldThrow(func, errorMessage) {
throw new Error(`bad error: ${String(error)}`);
}

shouldBe([].flatten.length, 0);
shouldBe([].flatten.name, `flatten`);
shouldBe([].flat.length, 0);
shouldBe([].flat.name, `flat`);

shouldBeArray([].flatten(), []);
shouldBeArray([0, 1, 2, 3, , 4].flatten(), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flatten(), []);
shouldBeArray([].flat(), []);
shouldBeArray([0, 1, 2, 3, , 4].flat(), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flat(), []);

shouldBeArray([].flatten(0), []);
shouldBeArray([0, 1, 2, 3, , 4].flatten(0), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flatten(0), []);
shouldBeArray([].flat(0), []);
shouldBeArray([0, 1, 2, 3, , 4].flat(0), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flat(0), []);

shouldBeArray([].flatten(-1), []);
shouldBeArray([0, 1, 2, 3, , 4].flatten(-1), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flatten(-1), []);
shouldBeArray([].flat(-1), []);
shouldBeArray([0, 1, 2, 3, , 4].flat(-1), [0, 1, 2, 3, 4]);
shouldBeArray([,,,,,].flat(-1), []);

shouldBeArray([[],[]].flatten(), []);
shouldBeArray([[0],[1]].flatten(), [0,1]);
shouldBeArray([[0],[],1].flatten(), [0,1]);
shouldBeArray([[0],[[]],1].flatten(), [0,[],1]);
shouldBeArray([[0],[[]],1].flatten(1), [0,[],1]);
shouldBeArray([[0],[[]],1].flatten(2), [0,1]);
shouldBeArray([[],[]].flat(), []);
shouldBeArray([[0],[1]].flat(), [0,1]);
shouldBeArray([[0],[],1].flat(), [0,1]);
shouldBeArray([[0],[[]],1].flat(), [0,[],1]);
shouldBeArray([[0],[[]],1].flat(1), [0,[],1]);
shouldBeArray([[0],[[]],1].flat(2), [0,1]);

shouldBeArray([[],[]].flatten(0), [[],[]]);
shouldBeArray([[0],[1]].flatten(0), [[0],[1]]);
shouldBeArray([[0],[],1].flatten(0), [[0],[],1]);
shouldBeArray([[0],[[]],1].flatten(0), [[0],[[]],1]);
shouldBeArray([[],[]].flat(0), [[],[]]);
shouldBeArray([[0],[1]].flat(0), [[0],[1]]);
shouldBeArray([[0],[],1].flat(0), [[0],[],1]);
shouldBeArray([[0],[[]],1].flat(0), [[0],[[]],1]);

shouldBeArray([[[[[[[[[[[[[[[[[[[[[42]]]]]]]]]]]]]]]]]]]]].flatten(Infinity), [42]);
shouldBeArray([[[[[[[[[[[[[[[[[[[[[42]]]]]]]]]]]]]]]]]]]]].flat(Infinity), [42]);

var array = [];
shouldBe(array.flatten() !== array, true);
shouldBe(array.flat() !== array, true);

class DerivedArray extends Array { }
shouldBe((new DerivedArray).flatten() instanceof DerivedArray, true);
var flatten = [].flatten;
shouldBe((new DerivedArray).flat() instanceof DerivedArray, true);
var flat = [].flat;
var realm = createGlobalObject();
shouldBe(flatten.call({}) instanceof Array, true);
shouldBe(flatten.call(new realm.Array) instanceof Array, true);
shouldBe(flat.call({}) instanceof Array, true);
shouldBe(flat.call(new realm.Array) instanceof Array, true);
var array2 = new realm.Array;
array2.constructor = 0;

shouldThrow(() => {
flatten.call(array2);
flat.call(array2);
}, `TypeError: 0 is not a constructor`);

var array2 = new realm.Array;
array2.constructor = undefined;
shouldBe(flatten.call(array2) instanceof Array, true);
shouldBe(flat.call(array2) instanceof Array, true);

var array2 = new realm.Array;
array2.constructor = {
get [Symbol.species]() {
return null;
}
};
shouldBe(flatten.call(array2) instanceof Array, true);
shouldBe(flat.call(array2) instanceof Array, true);

var array2 = new realm.Array;
array2.constructor = {
get [Symbol.species]() {
return undefined;
}
};
shouldBe(flatten.call(array2) instanceof Array, true);
shouldBe(flat.call(array2) instanceof Array, true);

var array2 = new realm.Array;
array2.constructor = {
get [Symbol.species]() {
return DerivedArray;
}
};
shouldBe(flatten.call(array2) instanceof DerivedArray, true);
shouldBe(flat.call(array2) instanceof DerivedArray, true);
30 changes: 0 additions & 30 deletions JSTests/test262/expectations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -633,36 +633,6 @@ test/built-ins/Array/prototype/concat/is-concat-spreadable-proxy.js:
test/built-ins/Array/prototype/filter/target-array-with-non-writable-property.js:
default: 'TypeError: Attempted to assign to readonly property.'
strict mode: 'TypeError: Attempted to assign to readonly property.'
test/built-ins/Array/prototype/flat/array-like-objects.js:
default: "TypeError: undefined is not an object (evaluating '[].flat.call')"
strict mode: "TypeError: undefined is not an object (evaluating '[].flat.call')"
test/built-ins/Array/prototype/flat/bound-function-call.js:
default: "TypeError: undefined is not an object (evaluating '[].flat.bind')"
strict mode: "TypeError: undefined is not an object (evaluating '[].flat.bind')"
test/built-ins/Array/prototype/flat/empty-array-elements.js:
default: "TypeError: [].flat is not a function. (In '[].flat()', '[].flat' is undefined)"
strict mode: "TypeError: [].flat is not a function. (In '[].flat()', '[].flat' is undefined)"
test/built-ins/Array/prototype/flat/empty-object-elements.js:
default: "TypeError: [a].flat is not a function. (In '[a].flat()', '[a].flat' is undefined)"
strict mode: "TypeError: [a].flat is not a function. (In '[a].flat()', '[a].flat' is undefined)"
test/built-ins/Array/prototype/flat/length.js:
default: "TypeError: undefined is not an object (evaluating 'Array.prototype.flat.length')"
strict mode: "TypeError: undefined is not an object (evaluating 'Array.prototype.flat.length')"
test/built-ins/Array/prototype/flat/name.js:
default: "TypeError: undefined is not an object (evaluating 'Array.prototype.flat.name')"
strict mode: "TypeError: undefined is not an object (evaluating 'Array.prototype.flat.name')"
test/built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js:
default: "TypeError: a.flat is not a function. (In 'a.flat(depthNum)', 'a.flat' is undefined)"
strict mode: "TypeError: a.flat is not a function. (In 'a.flat(depthNum)', 'a.flat' is undefined)"
test/built-ins/Array/prototype/flat/null-undefined-elements.js:
default: "TypeError: [1, null, void 0].flat is not a function. (In '[1, null, void 0].flat()', '[1, null, void 0].flat' is undefined)"
strict mode: "TypeError: [1, null, void 0].flat is not a function. (In '[1, null, void 0].flat()', '[1, null, void 0].flat' is undefined)"
test/built-ins/Array/prototype/flat/positive-infinity.js:
default: "TypeError: a.flat is not a function. (In 'a.flat(Number.POSITIVE_INFINITY)', 'a.flat' is undefined)"
strict mode: "TypeError: a.flat is not a function. (In 'a.flat(Number.POSITIVE_INFINITY)', 'a.flat' is undefined)"
test/built-ins/Array/prototype/flat/prop-desc.js:
default: 'Test262Error: `typeof Array.prototype.flat` is `function` Expected SameValue(«undefined», «function») to be true'
strict mode: 'Test262Error: `typeof Array.prototype.flat` is `function` Expected SameValue(«undefined», «function») to be true'
test/built-ins/Array/prototype/indexOf/15.4.4.14-3-28.js:
default: 'Test262Error: Array.prototype.indexOf.call(obj, targetObj) Expected SameValue(«-1», «0») to be true'
strict mode: 'Test262Error: Array.prototype.indexOf.call(obj, targetObj) Expected SameValue(«-1», «0») to be true'
Expand Down
11 changes: 11 additions & 0 deletions LayoutTests/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2018-05-26 Yusuke Suzuki <[email protected]>

[JSC] Rename Array#flatten to flat
https://bugs.webkit.org/show_bug.cgi?id=186012

Reviewed by Saam Barati.

* inspector/model/remote-object-get-properties-expected.txt:
* js/Object-getOwnPropertyNames-expected.txt:
* js/script-tests/Object-getOwnPropertyNames.js:

2018-05-25 Youenn Fablet <[email protected]>

Migrate From-Origin to Cross-Origin-Resource-Policy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ ALL PROPERTIES:
indexOf
lastIndexOf
filter
flat
flatMap
flatten
reduce
reduceRight
map
Expand Down Expand Up @@ -127,8 +127,8 @@ OWN PROPERTIES:
indexOf
lastIndexOf
filter
flat
flatMap
flatten
reduce
reduceRight
map
Expand Down Expand Up @@ -165,8 +165,8 @@ DISPLAYABLE PROPERTIES:
indexOf
lastIndexOf
filter
flat
flatMap
flatten
reduce
reduceRight
map
Expand Down Expand Up @@ -203,8 +203,8 @@ ALL PROPERTIES:
indexOf
lastIndexOf
filter
flat
flatMap
flatten
reduce
reduceRight
map
Expand Down
2 changes: 1 addition & 1 deletion LayoutTests/js/Object-getOwnPropertyNames-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ PASS getSortedOwnPropertyNames(Object.prototype) is ['__defineGetter__', '__defi
PASS getSortedOwnPropertyNames(Function) is ['length', 'name', 'prototype']
PASS getSortedOwnPropertyNames(Function.prototype) is ['apply', 'arguments', 'bind', 'call', 'caller', 'constructor', 'length', 'name', 'toString']
PASS getSortedOwnPropertyNames(Array) is ['from', 'isArray', 'length', 'name', 'of', 'prototype']
PASS getSortedOwnPropertyNames(Array.prototype) is ['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flatMap', 'flatten', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']
PASS getSortedOwnPropertyNames(Array.prototype) is ['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flat', 'flatMap', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']
PASS getSortedOwnPropertyNames(String) is ['fromCharCode', 'fromCodePoint', 'length', 'name', 'prototype', 'raw']
PASS getSortedOwnPropertyNames(String.prototype) is ['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimEnd', 'trimLeft', 'trimRight', 'trimStart', 'valueOf']
PASS getSortedOwnPropertyNames(Boolean) is ['length', 'name', 'prototype']
Expand Down
2 changes: 1 addition & 1 deletion LayoutTests/js/script-tests/Object-getOwnPropertyNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var expectedPropertyNamesSet = {
"Function": "['length', 'name', 'prototype']",
"Function.prototype": "['apply', 'arguments', 'bind', 'call', 'caller', 'constructor', 'length', 'name', 'toString']",
"Array": "['from', 'isArray', 'length', 'name', 'of', 'prototype']",
"Array.prototype": "['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flatMap', 'flatten', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']",
"Array.prototype": "['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flat', 'flatMap', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']",
"String": "['fromCharCode', 'fromCodePoint', 'length', 'name', 'prototype', 'raw']",
"String.prototype": "['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimEnd', 'trimLeft', 'trimRight', 'trimStart', 'valueOf']",
"Boolean": "['length', 'name', 'prototype']",
Expand Down
21 changes: 21 additions & 0 deletions Source/JavaScriptCore/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
2018-05-26 Yusuke Suzuki <[email protected]>

[JSC] Rename Array#flatten to flat
https://bugs.webkit.org/show_bug.cgi?id=186012

Reviewed by Saam Barati.

Rename Array#flatten to Array#flat. This rename is done in TC39 since flatten
conflicts with the mootools' function name.

* builtins/ArrayPrototype.js:
(globalPrivate.flatIntoArray):
(flat):
(globalPrivate.flatIntoArrayWithCallback):
(flatMap):
(globalPrivate.flattenIntoArray): Deleted.
(flatten): Deleted.
(globalPrivate.flattenIntoArrayWithCallback): Deleted.
* runtime/ArrayPrototype.cpp:
(JSC::ArrayPrototype::finishCreation):

2018-05-25 Mark Lam <[email protected]>

for-in loops should preserve and restore the TDZ stack for each of its internal loops.
Expand Down
16 changes: 8 additions & 8 deletions Source/JavaScriptCore/builtins/ArrayPrototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,15 +767,15 @@ function arraySpeciesCreate(array, length)
}

@globalPrivate
function flattenIntoArray(target, source, sourceLength, targetIndex, depth)
function flatIntoArray(target, source, sourceLength, targetIndex, depth)
{
"use strict";

for (var sourceIndex = 0; sourceIndex < sourceLength; ++sourceIndex) {
if (sourceIndex in source) {
var element = source[sourceIndex];
if (depth > 0 && @isArray(element))
targetIndex = @flattenIntoArray(target, element, @toLength(element.length), targetIndex, depth - 1);
targetIndex = @flatIntoArray(target, element, @toLength(element.length), targetIndex, depth - 1);
else {
if (targetIndex >= @MAX_SAFE_INTEGER)
@throwTypeError("flatten array exceeds 2**53 - 1");
Expand All @@ -787,11 +787,11 @@ function flattenIntoArray(target, source, sourceLength, targetIndex, depth)
return targetIndex;
}

function flatten()
function flat()
{
"use strict";

var array = @toObject(this, "Array.prototype.flatten requires that |this| not be null or undefined");
var array = @toObject(this, "Array.prototype.flat requires that |this| not be null or undefined");
var length = @toLength(array.length);

var depthNum = 1;
Expand All @@ -801,20 +801,20 @@ function flatten()

var result = @arraySpeciesCreate(array, 0);

@flattenIntoArray(result, array, length, 0, depthNum);
@flatIntoArray(result, array, length, 0, depthNum);
return result;
}

@globalPrivate
function flattenIntoArrayWithCallback(target, source, sourceLength, targetIndex, callback, thisArg)
function flatIntoArrayWithCallback(target, source, sourceLength, targetIndex, callback, thisArg)
{
"use strict";

for (var sourceIndex = 0; sourceIndex < sourceLength; ++sourceIndex) {
if (sourceIndex in source) {
var element = callback.@call(thisArg, source[sourceIndex], sourceIndex, source);
if (@isArray(element))
targetIndex = @flattenIntoArray(target, element, @toLength(element.length), targetIndex, 0);
targetIndex = @flatIntoArray(target, element, @toLength(element.length), targetIndex, 0);
else {
if (targetIndex >= @MAX_SAFE_INTEGER)
@throwTypeError("flatten array exceeds 2**53 - 1");
Expand All @@ -840,5 +840,5 @@ function flatMap(callback)

var result = @arraySpeciesCreate(array, 0);

return @flattenIntoArrayWithCallback(result, array, length, 0, callback, thisArg);
return @flatIntoArrayWithCallback(result, array, length, 0, callback, thisArg);
}
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/runtime/ArrayPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void ArrayPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION("indexOf", arrayProtoFuncIndexOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ArrayIndexOfIntrinsic);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("lastIndexOf", arrayProtoFuncLastIndexOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("filter", arrayPrototypeFilterCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("flat", arrayPrototypeFlatCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("flatMap", arrayPrototypeFlatMapCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("flatten", arrayPrototypeFlattenCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("reduce", arrayPrototypeReduceCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("reduceRight", arrayPrototypeReduceRightCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("map", arrayPrototypeMapCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
Expand Down

0 comments on commit 5124407

Please sign in to comment.