Skip to content
Permalink
Browse files
[JSC] Clean up ArraySpeciesCreate
https://bugs.webkit.org/show_bug.cgi?id=182434

Reviewed by Saam Barati.

We have duplicate code in filter, map, concatSlowPath.
This patch creates a new global private function @arraySpeciesCreate,
and use it.

* builtins/ArrayPrototype.js:
(globalPrivate.arraySpeciesCreate):
(filter):
(map):
(globalPrivate.concatSlowPath):

Canonical link: https://commits.webkit.org/198230@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@228012 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Constellation committed Feb 2, 2018
1 parent ad83002 commit 6d9e0014d0238e657a384cc29985e7912e822770
Showing with 43 additions and 57 deletions.
  1. +17 −0 Source/JavaScriptCore/ChangeLog
  2. +26 −57 Source/JavaScriptCore/builtins/ArrayPrototype.js
@@ -1,3 +1,20 @@
2018-02-02 Yusuke Suzuki <utatane.tea@gmail.com>

[JSC] Clean up ArraySpeciesCreate
https://bugs.webkit.org/show_bug.cgi?id=182434

Reviewed by Saam Barati.

We have duplicate code in filter, map, concatSlowPath.
This patch creates a new global private function @arraySpeciesCreate,
and use it.

* builtins/ArrayPrototype.js:
(globalPrivate.arraySpeciesCreate):
(filter):
(map):
(globalPrivate.concatSlowPath):

2018-02-01 Mark Lam <mark.lam@apple.com>

Fix broken bounds check in FTL's compileGetMyArgumentByVal().
@@ -164,38 +164,44 @@ function forEach(callback /*, thisArg */)
}
}

function filter(callback /*, thisArg */)
@globalPrivate
function arraySpeciesCreate(array, length)
{
"use strict";

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

if (typeof callback !== "function")
@throwTypeError("Array.prototype.filter callback must be a function");

var thisArg = @argument(1);

// Do 9.4.2.3 ArraySpeciesCreate
var result;
var constructor;
var arrayConstructorInRealm = @Array;
if (@isArray(array)) {
constructor = array.constructor;
// We have this check so that if some array from a different global object
// calls this map they don't get an array with the Array.prototype of the
// other global object.
if (@isArrayConstructor(constructor) && @Array !== constructor)
if (@isArrayConstructor(constructor) && arrayConstructorInRealm !== constructor)
constructor = @undefined;
if (@isObject(constructor)) {
else if (@isObject(constructor)) {
constructor = constructor.@speciesSymbol;
if (constructor === null)
constructor = @undefined;
}
}
if (constructor === @Array || constructor === @undefined)
result = @newArrayWithSize(0);
else
result = new constructor(0);
if (constructor === arrayConstructorInRealm || constructor === @undefined)
return @newArrayWithSize(length);
return new constructor(length);
}

function filter(callback /*, thisArg */)
{
"use strict";

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

if (typeof callback !== "function")
@throwTypeError("Array.prototype.filter callback must be a function");

var thisArg = @argument(1);

var result = @arraySpeciesCreate(array, 0);

var nextIndex = 0;
for (var i = 0; i < length; i++) {
@@ -222,26 +228,7 @@ function map(callback /*, thisArg */)

var thisArg = @argument(1);

// Do 9.4.2.3 ArraySpeciesCreate
var result;
var constructor;
if (@isArray(array)) {
constructor = array.constructor;
// We have this check so that if some array from a different global object
// calls this map they don't get an array with the Array.prototype of the
// other global object.
if (@isArrayConstructor(constructor) && @Array !== constructor)
constructor = @undefined;
if (@isObject(constructor)) {
constructor = constructor.@speciesSymbol;
if (constructor === null)
constructor = @undefined;
}
}
if (constructor === @Array || constructor === @undefined)
result = @newArrayWithSize(length);
else
result = new constructor(length);
var result = @arraySpeciesCreate(array, length);

for (var i = 0; i < length; i++) {
if (!(i in array))
@@ -617,28 +604,10 @@ function concatSlowPath()

var currentElement = @toObject(this, "Array.prototype.concat requires that |this| not be null or undefined");

var constructor;
if (@isArray(currentElement)) {
constructor = currentElement.constructor;
// We have this check so that if some array from a different global object
// calls this map they don't get an array with the Array.prototype of the
// other global object.
if (@isArrayConstructor(constructor) && @Array !== constructor)
constructor = @undefined;
else if (@isObject(constructor)) {
constructor = constructor.@speciesSymbol;
if (constructor === null)
constructor = @Array;
}
}
var result = @arraySpeciesCreate(currentElement, 0);
var resultIsArray = @isJSArray(result);

var argCount = arguments.length;
var result;
if (constructor === @Array || constructor === @undefined)
result = @newArrayWithSize(0);
else
result = new constructor(0);
var resultIsArray = @isJSArray(result);

var resultIndex = 0;
var argIndex = 0;

0 comments on commit 6d9e001

Please sign in to comment.