|
16 | 16 | ArrayFilter: { className: "Array", methodName: "filter", argumentsCount: 1, forceInline: true /*optional*/ },
|
17 | 17 | ArrayFlat: { className: "Array", methodName: "flat", argumentsCount: 0, forceInline: true /*optional*/ },
|
18 | 18 | ArrayFlatMap: { className: "Array", methodName: "flatMap", argumentsCount: 1, forceInline: true /*optional*/ },
|
| 19 | + ArrayForEach: { className: "Array", methodName: "forEach", argumentsCount: 1, forceInline: true /*optional*/ }, |
19 | 20 | };
|
20 | 21 |
|
21 | 22 | var setPrototype = platform.builtInSetPrototype;
|
|
185 | 186 | return -1;
|
186 | 187 | });
|
187 | 188 |
|
188 |
| - platform.registerFunction(FunctionsEnum.ArrayFilter, function (callbackfn, thisArg) { |
189 |
| - // ECMAScript 2017 #sec-array.prototype.filter |
| 189 | + platform.registerChakraLibraryFunction("CheckArrayAndGetLen", function (obj, builtInFunc) { |
190 | 190 | "use strict";
|
191 | 191 |
|
192 |
| - if (this === null || this === undefined) { |
193 |
| - __chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.filter"); |
| 192 | + if (__chakraLibrary.isArray(obj)) { |
| 193 | + return { o: obj, len: obj.length } |
194 | 194 | }
|
195 |
| - |
196 |
| - let o; |
197 |
| - let len |
198 |
| - if (__chakraLibrary.isArray(this)) { |
199 |
| - o = this; |
200 |
| - len = o.length; |
201 |
| - } else { |
202 |
| - o = __chakraLibrary.Object(this); |
203 |
| - len = __chakraLibrary.GetLength(o); |
| 195 | + else { |
| 196 | + if (this === null || this === undefined) { |
| 197 | + __chakraLibrary.raiseThis_NullOrUndefined(builtInFunc); |
| 198 | + } |
| 199 | + return { o: __chakraLibrary.Object(obj), len: __chakraLibrary.GetLength(obj) } |
204 | 200 | }
|
| 201 | + }); |
| 202 | + |
| 203 | + platform.registerFunction(FunctionsEnum.ArrayFilter, function (callbackfn, thisArg) { |
| 204 | + // ECMAScript 2017 #sec-array.prototype.filter |
| 205 | + "use strict"; |
| 206 | + |
| 207 | + let objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.filter"); |
| 208 | + let o = objInfo.o; |
| 209 | + let len = objInfo.len; |
205 | 210 |
|
206 | 211 | if (typeof callbackfn !== "function") {
|
207 | 212 | __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.filter");
|
|
358 | 363 | "use strict";
|
359 | 364 | //1. Let O be ? ToObject(this value).
|
360 | 365 | //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 |
| - } |
| 366 | + let objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.flat"); |
| 367 | + let o = objInfo.o; |
| 368 | + let sourceLen = objInfo.len; |
373 | 369 | //3. Let depthNum be 1.
|
374 | 370 | //4. If depth is not undefined, then
|
375 | 371 | //5. Set depthNum to ? ToInteger(depth).
|
|
386 | 382 | "use strict";
|
387 | 383 | //1. Let O be ? ToObject(this value).
|
388 | 384 | //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 |
| - } |
| 385 | + |
| 386 | + let objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.flatMap"); |
| 387 | + let o = objInfo.o; |
| 388 | + let sourceLen = objInfo.len; |
| 389 | + |
400 | 390 | //3. If IsCallable(mapperFunction) is false throw a TypeError exception
|
401 | 391 | if (typeof mapperFunction !== "function") {
|
402 | 392 | __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.flatMap");
|
|
415 | 405 | return A;
|
416 | 406 | });
|
417 | 407 |
|
| 408 | + platform.registerFunction(FunctionsEnum.ArrayForEach, function (callbackfn, thisArg) { |
| 409 | + // ECMAScript 2017 #sec-array.prototype.foreach |
| 410 | + "use strict"; |
| 411 | + |
| 412 | + let objInfo = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.forEach"); |
| 413 | + let o = objInfo.o; |
| 414 | + let len = objInfo.len; |
| 415 | + |
| 416 | + if (typeof callbackfn !== "function") { |
| 417 | + __chakraLibrary.raiseFunctionArgument_NeedFunction("Array.prototype.forEach"); |
| 418 | + } |
| 419 | + |
| 420 | + let k = 0; |
| 421 | + |
| 422 | + if (thisArg === undefined) { |
| 423 | + while (k < len) { |
| 424 | + if (k in o) { |
| 425 | + let kValue = o[k]; |
| 426 | + callbackfn(kValue, k, o); |
| 427 | + } |
| 428 | + k++; |
| 429 | + } |
| 430 | + } else { |
| 431 | + let boundCallback = __chakraLibrary.callInstanceFunc(__chakraLibrary.functionBind, callbackfn, thisArg); |
| 432 | + while (k < len) { |
| 433 | + if (k in o) { |
| 434 | + let kValue = o[k]; |
| 435 | + boundCallback(kValue, k, o); |
| 436 | + } |
| 437 | + k++; |
| 438 | + } |
| 439 | + } |
| 440 | + |
| 441 | + return undefined; |
| 442 | + }); |
418 | 443 | });
|
0 commit comments