|
14 | 14 | ArrayEntries: { className: "Array", methodName: "entries", argumentsCount: 0, forceInline: true /*optional*/ },
|
15 | 15 | ArrayIndexOf: { className: "Array", methodName: "indexOf", argumentsCount: 1, forceInline: true /*optional*/ },
|
16 | 16 | ArrayFilter: { className: "Array", methodName: "filter", argumentsCount: 1, forceInline: true /*optional*/ },
|
| 17 | + ArrayFlat: { className: "Array", methodName: "flat", argumentsCount: 0, forceInline: true /*optional*/ }, |
| 18 | + ArrayFlatMap: { className: "Array", methodName: "flatMap", argumentsCount: 1, forceInline: true /*optional*/ }, |
17 | 19 | };
|
18 | 20 |
|
19 | 21 | var setPrototype = platform.builtInSetPrototype;
|
|
36 | 38 | __chakraLibrary.ArrayIterator.prototype = CreateObject(iteratorPrototype);
|
37 | 39 | __chakraLibrary.raiseNeedObjectOfType = platform.raiseNeedObjectOfType;
|
38 | 40 | __chakraLibrary.raiseThis_NullOrUndefined = platform.raiseThis_NullOrUndefined;
|
| 41 | + __chakraLibrary.raiseLengthIsTooBig = platform.raiseLengthIsTooBig; |
39 | 42 | __chakraLibrary.raiseFunctionArgument_NeedFunction = platform.raiseFunctionArgument_NeedFunction;
|
40 | 43 | __chakraLibrary.callInstanceFunc = platform.builtInCallInstanceFunction;
|
41 | 44 | __chakraLibrary.functionBind = platform.builtInJavascriptFunctionEntryBind;
|
|
200 | 203 | len = __chakraLibrary.GetLength(o);
|
201 | 204 | }
|
202 | 205 |
|
203 |
| - if (typeof callbackfn != "function") { |
| 206 | + if (typeof callbackfn !== "function") { |
204 | 207 | __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.filter");
|
205 | 208 | }
|
206 | 209 |
|
|
238 | 241 |
|
239 | 242 | return a;
|
240 | 243 | });
|
241 |
| - |
| 244 | + |
| 245 | + platform.registerChakraLibraryFunction("FlattenIntoArray", function(target, source, sourceLen, start, depth) |
| 246 | + { |
| 247 | + // this is FlattenIntoArray from the flat/flatMap proposal BUT with no mapperFunction |
| 248 | + // a seperate function has been made to handle the case where there is a mapperFunction |
| 249 | + "use strict"; |
| 250 | + //1. Let targetIndex be start. |
| 251 | + let targetIndex = start; |
| 252 | + //2. Let sourceIndex be 0. |
| 253 | + let sourceIndex = 0; |
| 254 | + //3. Repeat, while sourceIndex < sourceLen |
| 255 | + let element; |
| 256 | + while (sourceIndex < sourceLen) { |
| 257 | + // a. Let P be ! ToString(sourceIndex). |
| 258 | + // b. Let exists be ? HasProperty(source, P). |
| 259 | + if (sourceIndex in source) { |
| 260 | + // c. If exists is true, then |
| 261 | + // i. Let element be ? Get(source, P). |
| 262 | + element = source[sourceIndex]; |
| 263 | + // ii. If mapperFunction is present - skipped see separate function |
| 264 | + // iii. Let shouldFlatten be false. |
| 265 | + // iv. If depth > 0, then |
| 266 | + // 1. Set shouldFlatten to ? IsArray(element). |
| 267 | + if (depth > 0 && __chakraLibrary.isArray(element)) { |
| 268 | + // v. If shouldFlatten is true, then |
| 269 | + // 1. Let elementLen be ? ToLength(? Get(element, "length")). |
| 270 | + // 2. Set targetIndex to ? FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1). |
| 271 | + targetIndex = __chakraLibrary.FlattenIntoArray(target, element, __chakraLibrary.toLength(element.length), targetIndex, depth - 1); |
| 272 | + } else { |
| 273 | + // vi. Else, |
| 274 | + // 1. If targetIndex >= 2^53-1, throw a TypeError exception. |
| 275 | + if (targetIndex >= 9007199254740991 /* 2^53-1 */) { |
| 276 | + __chakraLibrary.raiseLengthIsTooBig("Array.prototype.flat"); |
| 277 | + } |
| 278 | + // 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). |
| 279 | + __chakraLibrary.arrayCreateDataPropertyOrThrow(target, targetIndex, element); |
| 280 | + // 3. Increase targetIndex by 1. |
| 281 | + ++targetIndex; |
| 282 | + } |
| 283 | + } |
| 284 | + // d. Increase sourceIndex by 1. |
| 285 | + ++sourceIndex; |
| 286 | + } |
| 287 | + //4. Return targetIndex. |
| 288 | + return targetIndex; |
| 289 | + }); |
| 290 | + |
| 291 | + platform.registerChakraLibraryFunction("FlattenIntoArrayMapped", function(target, source, sourceLen, start, mapperFunction) { |
| 292 | + "use strict"; |
| 293 | + // this is FlattenIntoArray from the flat/flatMap proposal BUT with: |
| 294 | + // depth = 1 and the presence of a mapperFunction guaranteed |
| 295 | + // both these conditions are always met when this is called from flatMap |
| 296 | + // Additionally this is slightly refactored rather than taking a thisArg |
| 297 | + // the calling function binds the thisArg if it's required |
| 298 | + //1. Let targetIndex be start. |
| 299 | + let targetIndex = start; |
| 300 | + //2. Let sourceIndex be 0. |
| 301 | + let sourceIndex = 0; |
| 302 | + //3. Repeat, while sourceIndex < sourceLen |
| 303 | + |
| 304 | + let element, innerLength, innerIndex; |
| 305 | + while (sourceIndex < sourceLen) { |
| 306 | + // a. Let P be ! ToString(sourceIndex). |
| 307 | + // b. Let exists be ? HasProperty(source, P). |
| 308 | + if (sourceIndex in source) { |
| 309 | + // c. If exists is true, then |
| 310 | + // i. Let element be ? Get(source, P). |
| 311 | + // ii. If mapperFunction is present, then |
| 312 | + // 1. Assert: thisArg is present. |
| 313 | + // 2. Set element to ? Call(mapperFunction, thisArg , element, sourceIndex, source). |
| 314 | + element = mapperFunction(source[sourceIndex], sourceIndex, source); |
| 315 | + // iii. Let shouldFlatten be false. |
| 316 | + // iv. If depth > 0, then |
| 317 | + // 1. Set shouldFlatten to ? IsArray(element). |
| 318 | + // v. If shouldFlatten is true, then |
| 319 | + // 1. Let elementLen be ? ToLength(? Get(element, "length")). |
| 320 | + // 2. Set targetIndex to ? FlattenIntoArray(target, element, elementLen, targetIndex, depth - 1). |
| 321 | + if (__chakraLibrary.isArray(element)) { |
| 322 | + // instead of calling FlattenIntoArray use a simple loop here - as depth is always 0 |
| 323 | + innerLength = __chakraLibrary.toLength(element.length); |
| 324 | + innerIndex = 0; |
| 325 | + while (innerIndex < innerLength) { |
| 326 | + if (innerIndex in element) { |
| 327 | + // 1. If targetIndex >= 2^53-1, throw a TypeError exception. |
| 328 | + if (targetIndex >= 9007199254740991 /* 2^53-1 */) { |
| 329 | + __chakraLibrary.raiseLengthIsTooBig("Array.prototype.flatMap"); |
| 330 | + } |
| 331 | + // 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). |
| 332 | + __chakraLibrary.arrayCreateDataPropertyOrThrow(target, targetIndex, element[innerIndex]); |
| 333 | + // 3. Increase targetIndex by 1. |
| 334 | + ++targetIndex; |
| 335 | + } |
| 336 | + ++innerIndex; |
| 337 | + } |
| 338 | + } else { |
| 339 | + // vi. Else, |
| 340 | + // 1. If targetIndex >= 2^53-1, throw a TypeError exception. |
| 341 | + if (targetIndex >= 9007199254740991 /* 2^53-1 */) { |
| 342 | + __chakraLibrary.raiseLengthIsTooBig("Array.prototype.flatMap"); |
| 343 | + } |
| 344 | + // 2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element). |
| 345 | + __chakraLibrary.arrayCreateDataPropertyOrThrow(target, targetIndex, element); |
| 346 | + // 3. Increase targetIndex by 1. |
| 347 | + ++targetIndex; |
| 348 | + } |
| 349 | + } |
| 350 | + // d. Increase sourceIndex by 1. |
| 351 | + ++sourceIndex; |
| 352 | + } |
| 353 | + //4. Return targetIndex. |
| 354 | + return targetIndex; |
| 355 | + }); |
| 356 | + |
| 357 | + platform.registerFunction(FunctionsEnum.ArrayFlat, function (depth) { |
| 358 | + "use strict"; |
| 359 | + //1. Let O be ? ToObject(this value). |
| 360 | + //2. Let sourceLen be ? ToLength(? Get(O, "length")). |
| 361 | + let o, sourceLen; |
| 362 | + |
| 363 | + if (__chakraLibrary.isArray(this)) { |
| 364 | + o = this; |
| 365 | + sourceLen = o.length; |
| 366 | + } else { |
| 367 | + if (this === null || this === undefined) { |
| 368 | + __chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.flat"); |
| 369 | + } |
| 370 | + o = __chakraLibrary.Object(this); |
| 371 | + sourceLen = __chakraLibrary.GetLength(o); |
| 372 | + } |
| 373 | + //3. Let depthNum be 1. |
| 374 | + //4. If depth is not undefined, then |
| 375 | + //5. Set depthNum to ? ToInteger(depth). |
| 376 | + const depthNum = depth !== undefined ? __chakraLibrary.toInteger(depth) : 1; |
| 377 | + //6. Let A be ? ArraySpeciesCreate(O, 0). |
| 378 | + const A = __chakraLibrary.arraySpeciesCreate(o, 0); |
| 379 | + //7. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum). |
| 380 | + __chakraLibrary.FlattenIntoArray(A, o, sourceLen, 0, depthNum); |
| 381 | + //8. Return A. |
| 382 | + return A; |
| 383 | + }); |
| 384 | + |
| 385 | + platform.registerFunction(FunctionsEnum.ArrayFlatMap, function (mapperFunction, thisArg) { |
| 386 | + "use strict"; |
| 387 | + //1. Let O be ? ToObject(this value). |
| 388 | + //2. Let sourceLen be ? ToLength(? Get(O, "length")). |
| 389 | + let o, sourceLen; |
| 390 | + if (__chakraLibrary.isArray(this)) { |
| 391 | + o = this; |
| 392 | + sourceLen = o.length; |
| 393 | + } else { |
| 394 | + if (this === null || this === undefined) { |
| 395 | + __chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.flatMap"); |
| 396 | + } |
| 397 | + o = __chakraLibrary.Object(this); |
| 398 | + sourceLen = __chakraLibrary.GetLength(o); |
| 399 | + } |
| 400 | + //3. If IsCallable(mapperFunction) is false throw a TypeError exception |
| 401 | + if (typeof mapperFunction !== "function") { |
| 402 | + __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.flatMap"); |
| 403 | + } |
| 404 | + //4. If thisArg is present, let T be thisArg; else let T be undefined |
| 405 | + //5. Let A be ? ArraySpeciesCreate(O, 0). |
| 406 | + const A = __chakraLibrary.arraySpeciesCreate(o, 0); |
| 407 | + //6. Perform ? FlattenIntoArray(A, O, sourceLen, 0, depthNum). |
| 408 | + if (thisArg === undefined) { |
| 409 | + __chakraLibrary.FlattenIntoArrayMapped(A, o, sourceLen, 0, mapperFunction); |
| 410 | + } else { |
| 411 | + const func = __chakraLibrary.callInstanceFunc(__chakraLibrary.functionBind, mapperFunction, thisArg); |
| 412 | + __chakraLibrary.FlattenIntoArrayMapped(A, o, sourceLen, 0, func); |
| 413 | + } |
| 414 | + //7. Return A. |
| 415 | + return A; |
| 416 | + }); |
| 417 | + |
242 | 418 | });
|
0 commit comments