Changeset 228012 in webkit
- Timestamp:
- Feb 2, 2018 9:12:23 AM (6 years ago)
- Location:
- trunk/Source/JavaScriptCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/ChangeLog
r227998 r228012 1 2018-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 1 18 2018-02-01 Mark Lam <mark.lam@apple.com> 2 19 -
trunk/Source/JavaScriptCore/builtins/ArrayPrototype.js
r224280 r228012 165 165 } 166 166 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 168 function arraySpeciesCreate(array, length) 169 { 170 "use strict"; 171 181 172 var constructor; 173 var arrayConstructorInRealm = @Array; 182 174 if (@isArray(array)) { 183 175 constructor = array.constructor; … … 185 177 // calls this map they don't get an array with the Array.prototype of the 186 178 // other global object. 187 if (@isArrayConstructor(constructor) && @Array!== constructor)179 if (@isArrayConstructor(constructor) && arrayConstructorInRealm !== constructor) 188 180 constructor = @undefined; 189 if (@isObject(constructor)) {181 else if (@isObject(constructor)) { 190 182 constructor = constructor.@speciesSymbol; 191 183 if (constructor === null) … … 193 185 } 194 186 } 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 192 function 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); 199 205 200 206 var nextIndex = 0; … … 223 229 var thisArg = @argument(1); 224 230 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); 245 232 246 233 for (var i = 0; i < length; i++) { … … 618 605 var currentElement = @toObject(this, "Array.prototype.concat requires that |this| not be null or undefined"); 619 606 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); 634 609 635 610 var argCount = arguments.length; 636 var result;637 if (constructor === @Array || constructor === @undefined)638 result = @newArrayWithSize(0);639 else640 result = new constructor(0);641 var resultIsArray = @isJSArray(result);642 611 643 612 var resultIndex = 0;
Note: See TracChangeset
for help on using the changeset viewer.