Changeset 228012 in webkit


Ignore:
Timestamp:
Feb 2, 2018 9:12:23 AM (6 years ago)
Author:
Yusuke Suzuki
Message:

[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):

Location:
trunk/Source/JavaScriptCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r227998 r228012  
     12018-02-02  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        [JSC] Clean up ArraySpeciesCreate
     4        https://bugs.webkit.org/show_bug.cgi?id=182434
     5
     6        Reviewed by Saam Barati.
     7
     8        We have duplicate code in filter, map, concatSlowPath.
     9        This patch creates a new global private function @arraySpeciesCreate,
     10        and use it.
     11
     12        * builtins/ArrayPrototype.js:
     13        (globalPrivate.arraySpeciesCreate):
     14        (filter):
     15        (map):
     16        (globalPrivate.concatSlowPath):
     17
    1182018-02-01  Mark Lam  <mark.lam@apple.com>
    219
  • trunk/Source/JavaScriptCore/builtins/ArrayPrototype.js

    r224280 r228012  
    165165}
    166166
    167 function filter(callback /*, thisArg */)
    168 {
    169     "use strict";
    170 
    171     var array = @toObject(this, "Array.prototype.filter requires that |this| not be null or undefined");
    172     var length = @toLength(array.length);
    173 
    174     if (typeof callback !== "function")
    175         @throwTypeError("Array.prototype.filter callback must be a function");
    176    
    177     var thisArg = @argument(1);
    178 
    179     // Do 9.4.2.3 ArraySpeciesCreate
    180     var result;
     167@globalPrivate
     168function arraySpeciesCreate(array, length)
     169{
     170    "use strict";
     171
    181172    var constructor;
     173    var arrayConstructorInRealm = @Array;
    182174    if (@isArray(array)) {
    183175        constructor = array.constructor;
     
    185177        // calls this map they don't get an array with the Array.prototype of the
    186178        // other global object.
    187         if (@isArrayConstructor(constructor) && @Array !== constructor)
     179        if (@isArrayConstructor(constructor) && arrayConstructorInRealm !== constructor)
    188180            constructor = @undefined;
    189         if (@isObject(constructor)) {
     181        else if (@isObject(constructor)) {
    190182            constructor = constructor.@speciesSymbol;
    191183            if (constructor === null)
     
    193185        }
    194186    }
    195     if (constructor === @Array || constructor === @undefined)
    196         result = @newArrayWithSize(0);
    197     else
    198         result = new constructor(0);
     187    if (constructor === arrayConstructorInRealm || constructor === @undefined)
     188        return @newArrayWithSize(length);
     189    return new constructor(length);
     190}
     191
     192function filter(callback /*, thisArg */)
     193{
     194    "use strict";
     195
     196    var array = @toObject(this, "Array.prototype.filter requires that |this| not be null or undefined");
     197    var length = @toLength(array.length);
     198
     199    if (typeof callback !== "function")
     200        @throwTypeError("Array.prototype.filter callback must be a function");
     201   
     202    var thisArg = @argument(1);
     203
     204    var result = @arraySpeciesCreate(array, 0);
    199205
    200206    var nextIndex = 0;
     
    223229    var thisArg = @argument(1);
    224230
    225     // Do 9.4.2.3 ArraySpeciesCreate
    226     var result;
    227     var constructor;
    228     if (@isArray(array)) {
    229         constructor = array.constructor;
    230         // We have this check so that if some array from a different global object
    231         // calls this map they don't get an array with the Array.prototype of the
    232         // other global object.
    233         if (@isArrayConstructor(constructor) && @Array !== constructor)
    234             constructor = @undefined;
    235         if (@isObject(constructor)) {
    236             constructor = constructor.@speciesSymbol;
    237             if (constructor === null)
    238                 constructor = @undefined;
    239         }
    240     }
    241     if (constructor === @Array || constructor === @undefined)
    242         result = @newArrayWithSize(length);
    243     else
    244         result = new constructor(length);
     231    var result = @arraySpeciesCreate(array, length);
    245232
    246233    for (var i = 0; i < length; i++) {
     
    618605    var currentElement = @toObject(this, "Array.prototype.concat requires that |this| not be null or undefined");
    619606
    620     var constructor;
    621     if (@isArray(currentElement)) {
    622         constructor = currentElement.constructor;
    623         // We have this check so that if some array from a different global object
    624         // calls this map they don't get an array with the Array.prototype of the
    625         // other global object.
    626         if (@isArrayConstructor(constructor) && @Array !== constructor)
    627             constructor = @undefined;
    628         else if (@isObject(constructor)) {
    629             constructor = constructor.@speciesSymbol;
    630             if (constructor === null)
    631                 constructor = @Array;
    632         }
    633     }
     607    var result = @arraySpeciesCreate(currentElement, 0);
     608    var resultIsArray = @isJSArray(result);
    634609
    635610    var argCount = arguments.length;
    636     var result;
    637     if (constructor === @Array || constructor === @undefined)
    638         result = @newArrayWithSize(0);
    639     else
    640         result = new constructor(0);
    641     var resultIsArray = @isJSArray(result);
    642611
    643612    var resultIndex = 0;
Note: See TracChangeset for help on using the changeset viewer.