| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
function IteratorIdentity() { |
| 6 |
return this; |
| 7 |
} |
| 8 |
|
| 9 |
/* ECMA262 7.2.7 */ |
| 10 |
function IteratorNext(iteratorRecord, value) { |
| 11 |
// Steps 1-2. |
| 12 |
var result = |
| 13 |
ArgumentsLength() < 2 |
| 14 |
? callContentFunction(iteratorRecord.nextMethod, iteratorRecord.iterator) |
| 15 |
: callContentFunction( |
| 16 |
iteratorRecord.nextMethod, |
| 17 |
iteratorRecord.iterator, |
| 18 |
value |
| 19 |
); |
| 20 |
// Step 3. |
| 21 |
if (!IsObject(result)) { |
| 22 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, result); |
| 23 |
} |
| 24 |
// Step 4. |
| 25 |
return result; |
| 26 |
} |
| 27 |
|
| 28 |
// https://tc39.es/ecma262/#sec-getiterator |
| 29 |
function GetIterator(obj, isAsync, method) { |
| 30 |
// Step 1. If hint is not present, set hint to sync. |
| 31 |
// Step 2. If method is not present, then |
| 32 |
if (!method) { |
| 33 |
// Step 2.a. If hint is async, then |
| 34 |
if (isAsync) { |
| 35 |
// Step 2.a.i. Set method to ? GetMethod(obj, @@asyncIterator). |
| 36 |
method = GetMethod(obj, GetBuiltinSymbol("asyncIterator")); |
| 37 |
|
| 38 |
// Step 2.a.ii. If method is undefined, then |
| 39 |
if (!method) { |
| 40 |
// Step 2.a.ii.1. Let syncMethod be ? GetMethod(obj, @@iterator). |
| 41 |
var syncMethod = GetMethod(obj, GetBuiltinSymbol("iterator")); |
| 42 |
|
| 43 |
// Step 2.a.ii.2. Let syncIteratorRecord be ? GetIterator(obj, sync, syncMethod). |
| 44 |
var syncIteratorRecord = GetIterator(obj, false, syncMethod); |
| 45 |
|
| 46 |
// Step 2.a.ii.2. Return CreateAsyncFromSyncIterator(syncIteratorRecord). |
| 47 |
return CreateAsyncFromSyncIterator(syncIteratorRecord.iterator, syncIteratorRecord.nextMethod); |
| 48 |
} |
| 49 |
} else { |
| 50 |
// Step 2.b. Otherwise, set method to ? GetMethod(obj, @@iterator). |
| 51 |
method = GetMethod(obj, GetBuiltinSymbol("iterator")); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Step 3. Let iterator be ? Call(method, obj). |
| 56 |
var iterator = callContentFunction(method, obj); |
| 57 |
|
| 58 |
// Step 4. If Type(iterator) is not Object, throw a TypeError exception. |
| 59 |
if (!IsObject(iterator)) { |
| 60 |
ThrowTypeError(JSMSG_NOT_ITERABLE, obj === null ? "null" : typeof obj); |
| 61 |
} |
| 62 |
|
| 63 |
// Step 5. Let nextMethod be ? GetV(iterator, "next"). |
| 64 |
var nextMethod = iterator.next; |
| 65 |
|
| 66 |
// Step 6. Let iteratorRecord be the Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]: false }. |
| 67 |
var iteratorRecord = { |
| 68 |
__proto__: null, |
| 69 |
iterator, |
| 70 |
nextMethod, |
| 71 |
done: false, |
| 72 |
}; |
| 73 |
|
| 74 |
// Step 7. Return iteratorRecord. |
| 75 |
return iteratorRecord; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* GetIteratorFlattenable ( obj, stringHandling ) |
| 80 |
* |
| 81 |
* https://tc39.es/proposal-iterator-helpers/#sec-getiteratorflattenable |
| 82 |
*/ |
| 83 |
function GetIteratorFlattenable(obj, rejectStrings) { |
| 84 |
assert(typeof rejectStrings === "boolean", "rejectStrings is a boolean"); |
| 85 |
|
| 86 |
// Step 1. |
| 87 |
if (!IsObject(obj)) { |
| 88 |
// Step 1.a. |
| 89 |
if (rejectStrings || typeof obj !== "string") { |
| 90 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, obj === null ? "null" : typeof obj); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Step 2. |
| 95 |
var method = obj[GetBuiltinSymbol("iterator")]; |
| 96 |
|
| 97 |
// Steps 3-4. |
| 98 |
var iterator; |
| 99 |
if (IsNullOrUndefined(method)) { |
| 100 |
iterator = obj; |
| 101 |
} else { |
| 102 |
iterator = callContentFunction(method, obj); |
| 103 |
} |
| 104 |
|
| 105 |
// Step 5. |
| 106 |
if (!IsObject(iterator)) { |
| 107 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 108 |
} |
| 109 |
|
| 110 |
// Step 6. (Caller must call GetIteratorDirect.) |
| 111 |
return iterator; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Iterator.from ( O ) |
| 116 |
* |
| 117 |
* https://tc39.es/proposal-iterator-helpers/#sec-iterator.from |
| 118 |
*/ |
| 119 |
function IteratorFrom(O) { |
| 120 |
// Step 1. (Inlined call to GetIteratorDirect.) |
| 121 |
var iterator = GetIteratorFlattenable(O, /* rejectStrings= */ false); |
| 122 |
var nextMethod = iterator.next; |
| 123 |
|
| 124 |
// Step 2. |
| 125 |
// |
| 126 |
// Calls |isPrototypeOf| instead of |instanceof| to avoid looking up the |
| 127 |
// `@@hasInstance` property. |
| 128 |
var hasInstance = callFunction( |
| 129 |
std_Object_isPrototypeOf, |
| 130 |
GetBuiltinPrototype("Iterator"), |
| 131 |
iterator |
| 132 |
); |
| 133 |
|
| 134 |
// Step 3. |
| 135 |
if (hasInstance) { |
| 136 |
return iterator; |
| 137 |
} |
| 138 |
|
| 139 |
// Step 4. |
| 140 |
var wrapper = NewWrapForValidIterator(); |
| 141 |
|
| 142 |
// Step 5. |
| 143 |
UnsafeSetReservedSlot( |
| 144 |
wrapper, |
| 145 |
WRAP_FOR_VALID_ITERATOR_ITERATOR_SLOT, |
| 146 |
iterator |
| 147 |
); |
| 148 |
UnsafeSetReservedSlot( |
| 149 |
wrapper, |
| 150 |
WRAP_FOR_VALID_ITERATOR_NEXT_METHOD_SLOT, |
| 151 |
nextMethod |
| 152 |
); |
| 153 |
|
| 154 |
// Step 6. |
| 155 |
return wrapper; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* %WrapForValidIteratorPrototype%.next ( ) |
| 160 |
* |
| 161 |
* https://tc39.es/proposal-iterator-helpers/#sec-wrapforvaliditeratorprototype.next |
| 162 |
*/ |
| 163 |
function WrapForValidIteratorNext() { |
| 164 |
// Steps 1-2. |
| 165 |
var O = this; |
| 166 |
if (!IsObject(O) || (O = GuardToWrapForValidIterator(O)) === null) { |
| 167 |
return callFunction( |
| 168 |
CallWrapForValidIteratorMethodIfWrapped, |
| 169 |
this, |
| 170 |
"WrapForValidIteratorNext" |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
// Step 3. |
| 175 |
var iterator = UnsafeGetReservedSlot(O, WRAP_FOR_VALID_ITERATOR_ITERATOR_SLOT); |
| 176 |
var nextMethod = UnsafeGetReservedSlot(O, WRAP_FOR_VALID_ITERATOR_NEXT_METHOD_SLOT); |
| 177 |
|
| 178 |
// Step 4. |
| 179 |
return callContentFunction(nextMethod, iterator); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* %WrapForValidIteratorPrototype%.return ( ) |
| 184 |
* |
| 185 |
* https://tc39.es/proposal-iterator-helpers/#sec-wrapforvaliditeratorprototype.return |
| 186 |
*/ |
| 187 |
function WrapForValidIteratorReturn() { |
| 188 |
// Steps 1-2. |
| 189 |
var O = this; |
| 190 |
if (!IsObject(O) || (O = GuardToWrapForValidIterator(O)) === null) { |
| 191 |
return callFunction( |
| 192 |
CallWrapForValidIteratorMethodIfWrapped, |
| 193 |
this, |
| 194 |
"WrapForValidIteratorReturn" |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
// Step 3. |
| 199 |
var iterator = UnsafeGetReservedSlot(O, WRAP_FOR_VALID_ITERATOR_ITERATOR_SLOT); |
| 200 |
|
| 201 |
// Step 4. |
| 202 |
assert(IsObject(iterator), "iterator is an object"); |
| 203 |
|
| 204 |
// Step 5. |
| 205 |
var returnMethod = iterator.return; |
| 206 |
|
| 207 |
// Step 6. |
| 208 |
if (IsNullOrUndefined(returnMethod)) { |
| 209 |
return { |
| 210 |
value: undefined, |
| 211 |
done: true, |
| 212 |
}; |
| 213 |
} |
| 214 |
|
| 215 |
// Step 7. |
| 216 |
return callContentFunction(returnMethod, iterator); |
| 217 |
} |
| 218 |
|
| 219 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 220 |
/** |
| 221 |
* Explicit Resource Management Proposal |
| 222 |
* 27.1.2.1 %IteratorPrototype% [ @@dispose ] ( ) |
| 223 |
* https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-%25iteratorprototype%25-%40%40dispose |
| 224 |
*/ |
| 225 |
function IteratorDispose() { |
| 226 |
// Step 1. Let O be the this value. |
| 227 |
var O = this; |
| 228 |
|
| 229 |
// Step 2. Let return be ? GetMethod(O, "return"). |
| 230 |
var returnMethod = GetMethod(O, "return"); |
| 231 |
|
| 232 |
// Step 3. If return is not undefined, then |
| 233 |
if (returnMethod !== undefined) { |
| 234 |
// Step 3.a. Perform ? Call(return, O, « »). |
| 235 |
callContentFunction(returnMethod, O); |
| 236 |
} |
| 237 |
|
| 238 |
// Step 4. Return NormalCompletion(empty). (implicit) |
| 239 |
} |
| 240 |
#endif |
| 241 |
|
| 242 |
/** |
| 243 |
* %IteratorHelperPrototype%.next ( ) |
| 244 |
* |
| 245 |
* https://tc39.es/proposal-iterator-helpers/#sec-%iteratorhelperprototype%.next |
| 246 |
*/ |
| 247 |
function IteratorHelperNext() { |
| 248 |
// Step 1. |
| 249 |
var O = this; |
| 250 |
if (!IsObject(O) || (O = GuardToIteratorHelper(O)) === null) { |
| 251 |
return callFunction( |
| 252 |
CallIteratorHelperMethodIfWrapped, |
| 253 |
this, |
| 254 |
"IteratorHelperNext" |
| 255 |
); |
| 256 |
} |
| 257 |
var generator = UnsafeGetReservedSlot(O, ITERATOR_HELPER_GENERATOR_SLOT); |
| 258 |
return callFunction(GeneratorNext, generator, undefined); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* %IteratorHelperPrototype%.return ( ) |
| 263 |
* |
| 264 |
* https://tc39.es/proposal-iterator-helpers/#sec-%iteratorhelperprototype%.return |
| 265 |
*/ |
| 266 |
function IteratorHelperReturn() { |
| 267 |
// Step 1. |
| 268 |
var O = this; |
| 269 |
|
| 270 |
// Step 2. |
| 271 |
if (!IsObject(O) || (O = GuardToIteratorHelper(O)) === null) { |
| 272 |
return callFunction( |
| 273 |
CallIteratorHelperMethodIfWrapped, |
| 274 |
this, |
| 275 |
"IteratorHelperReturn" |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
// Step 3. (Implicit) |
| 280 |
|
| 281 |
// Steps 4-6. |
| 282 |
var generator = UnsafeGetReservedSlot(O, ITERATOR_HELPER_GENERATOR_SLOT); |
| 283 |
return callFunction(GeneratorReturn, generator, undefined); |
| 284 |
} |
| 285 |
|
| 286 |
// Lazy %Iterator.prototype% methods |
| 287 |
// |
| 288 |
// In order to match the semantics of the built-in generator objects used in |
| 289 |
// the proposal, we use a reserved slot on the IteratorHelper objects to store |
| 290 |
// a regular generator that is called from the %IteratorHelper.prototype% |
| 291 |
// methods. |
| 292 |
// |
| 293 |
// Each of the lazy methods is divided into a prelude and a body, with the |
| 294 |
// eager prelude steps being contained in the corresponding IteratorX method |
| 295 |
// and the lazy body steps inside the IteratorXGenerator generator functions. |
| 296 |
// |
| 297 |
// Each prelude method initializes and returns a new IteratorHelper object. |
| 298 |
// As part of this initialization process, the appropriate generator function |
| 299 |
// is called, followed by GeneratorNext being called on returned generator |
| 300 |
// instance in order to move it to its first yield point. This is done so that |
| 301 |
// if the `return` method is called on the IteratorHelper before `next` has been |
| 302 |
// called, we can catch them in the try and use the finally block to close the |
| 303 |
// underlying iterator. |
| 304 |
|
| 305 |
/** |
| 306 |
* Iterator.prototype.map ( mapper ) |
| 307 |
* |
| 308 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.map |
| 309 |
*/ |
| 310 |
function IteratorMap(mapper) { |
| 311 |
// Step 1. |
| 312 |
var iterator = this; |
| 313 |
|
| 314 |
// Step 2. |
| 315 |
if (!IsObject(iterator)) { |
| 316 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 317 |
} |
| 318 |
|
| 319 |
// Step 3. |
| 320 |
if (!IsCallable(mapper)) { |
| 321 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, mapper)); |
| 322 |
} |
| 323 |
|
| 324 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 325 |
var nextMethod = iterator.next; |
| 326 |
|
| 327 |
// Steps 5-7. |
| 328 |
var result = NewIteratorHelper(); |
| 329 |
var generator = IteratorMapGenerator(iterator, nextMethod, mapper); |
| 330 |
UnsafeSetReservedSlot( |
| 331 |
result, |
| 332 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 333 |
generator |
| 334 |
); |
| 335 |
|
| 336 |
// Stop at the initial yield point. |
| 337 |
callFunction(GeneratorNext, generator); |
| 338 |
|
| 339 |
// Step 8. |
| 340 |
return result; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Iterator.prototype.map ( mapper ) |
| 345 |
* |
| 346 |
* Abstract closure definition. |
| 347 |
* |
| 348 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.map |
| 349 |
*/ |
| 350 |
function* IteratorMapGenerator(iterator, nextMethod, mapper) { |
| 351 |
var isReturnCompletion = true; |
| 352 |
try { |
| 353 |
// Initial yield point to handle closing the iterator before the for-of |
| 354 |
// loop has been entered for the first time. |
| 355 |
yield; |
| 356 |
|
| 357 |
// Not a Return completion when execution continues normally after |yield|. |
| 358 |
isReturnCompletion = false; |
| 359 |
} finally { |
| 360 |
// Call IteratorClose on a Return completion. |
| 361 |
if (isReturnCompletion) { |
| 362 |
IteratorClose(iterator); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
// Step 5.a. |
| 367 |
var counter = 0; |
| 368 |
|
| 369 |
// Step 5.b. |
| 370 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 371 |
// Steps 5.b.i-iii. (Implicit through for-of loop) |
| 372 |
|
| 373 |
// Step 5.b.iv. |
| 374 |
var mapped = callContentFunction(mapper, undefined, value, counter); |
| 375 |
|
| 376 |
// Step 5.b.v. (Implicit through for-of loop) |
| 377 |
|
| 378 |
// Step 5.b.vi. |
| 379 |
yield mapped; |
| 380 |
|
| 381 |
// Step 5.b.vii. (Implicit through for-of loop) |
| 382 |
|
| 383 |
// Step 5.b.viii. |
| 384 |
counter += 1; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Iterator.prototype.filter ( predicate ) |
| 390 |
* |
| 391 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.filter |
| 392 |
*/ |
| 393 |
function IteratorFilter(predicate) { |
| 394 |
// Step 1. |
| 395 |
var iterator = this; |
| 396 |
|
| 397 |
// Step 2. |
| 398 |
if (!IsObject(iterator)) { |
| 399 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 400 |
} |
| 401 |
|
| 402 |
// Step 3. |
| 403 |
if (!IsCallable(predicate)) { |
| 404 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate)); |
| 405 |
} |
| 406 |
|
| 407 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 408 |
var nextMethod = iterator.next; |
| 409 |
|
| 410 |
// Steps 5-7. |
| 411 |
var result = NewIteratorHelper(); |
| 412 |
var generator = IteratorFilterGenerator(iterator, nextMethod, predicate); |
| 413 |
UnsafeSetReservedSlot( |
| 414 |
result, |
| 415 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 416 |
generator |
| 417 |
); |
| 418 |
|
| 419 |
// Stop at the initial yield point. |
| 420 |
callFunction(GeneratorNext, generator); |
| 421 |
|
| 422 |
// Step 8. |
| 423 |
return result; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Iterator.prototype.filter ( predicate ) |
| 428 |
* |
| 429 |
* Abstract closure definition. |
| 430 |
* |
| 431 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.filter |
| 432 |
*/ |
| 433 |
function* IteratorFilterGenerator(iterator, nextMethod, predicate) { |
| 434 |
var isReturnCompletion = true; |
| 435 |
try { |
| 436 |
// Initial yield point to handle closing the iterator before the for-of |
| 437 |
// loop has been entered for the first time. |
| 438 |
yield; |
| 439 |
|
| 440 |
// Not a Return completion when execution continues normally after |yield|. |
| 441 |
isReturnCompletion = false; |
| 442 |
} finally { |
| 443 |
// Call IteratorClose on a Return completion. |
| 444 |
if (isReturnCompletion) { |
| 445 |
IteratorClose(iterator); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
// Step 5.a. |
| 450 |
var counter = 0; |
| 451 |
|
| 452 |
// Step 5.b. |
| 453 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 454 |
// Steps 5.b.i-iii. (Implicit through for-of loop) |
| 455 |
|
| 456 |
// Step 5.b.iv. |
| 457 |
var selected = callContentFunction(predicate, undefined, value, counter); |
| 458 |
|
| 459 |
// Step 5.b.v. (Implicit through for-of loop) |
| 460 |
|
| 461 |
// Step 5.b.vi. |
| 462 |
if (selected) { |
| 463 |
// Step 5.b.vi.1. |
| 464 |
yield value; |
| 465 |
|
| 466 |
// Step 5.b.vi.2. (Implicit through for-of loop) |
| 467 |
} |
| 468 |
|
| 469 |
// Step 5.b.vii. |
| 470 |
counter += 1; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Iterator.prototype.take ( limit ) |
| 476 |
* |
| 477 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.take |
| 478 |
*/ |
| 479 |
function IteratorTake(limit) { |
| 480 |
// Step 1. |
| 481 |
var iterator = this; |
| 482 |
|
| 483 |
// Step 2. |
| 484 |
if (!IsObject(iterator)) { |
| 485 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 486 |
} |
| 487 |
|
| 488 |
// Steps 3-6. |
| 489 |
var integerLimit = std_Math_trunc(limit); |
| 490 |
if (!(integerLimit >= 0)) { |
| 491 |
ThrowRangeError(JSMSG_NEGATIVE_LIMIT); |
| 492 |
} |
| 493 |
|
| 494 |
// Step 7. (Inlined call to GetIteratorDirect.) |
| 495 |
var nextMethod = iterator.next; |
| 496 |
|
| 497 |
// Steps 8-10. |
| 498 |
var result = NewIteratorHelper(); |
| 499 |
var generator = IteratorTakeGenerator(iterator, nextMethod, integerLimit); |
| 500 |
UnsafeSetReservedSlot( |
| 501 |
result, |
| 502 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 503 |
generator |
| 504 |
); |
| 505 |
|
| 506 |
// Stop at the initial yield point. |
| 507 |
callFunction(GeneratorNext, generator); |
| 508 |
|
| 509 |
// Step 11. |
| 510 |
return result; |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Iterator.prototype.take ( limit ) |
| 515 |
* |
| 516 |
* Abstract closure definition. |
| 517 |
* |
| 518 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.take |
| 519 |
*/ |
| 520 |
function* IteratorTakeGenerator(iterator, nextMethod, remaining) { |
| 521 |
var isReturnCompletion = true; |
| 522 |
try { |
| 523 |
// Initial yield point to handle closing the iterator before the for-of |
| 524 |
// loop has been entered for the first time. |
| 525 |
yield; |
| 526 |
|
| 527 |
// Not a Return completion when execution continues normally after |yield|. |
| 528 |
isReturnCompletion = false; |
| 529 |
} finally { |
| 530 |
// Call IteratorClose on a Return completion. |
| 531 |
if (isReturnCompletion) { |
| 532 |
IteratorClose(iterator); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// Step 8.a. (Implicit) |
| 537 |
|
| 538 |
// Step 8.b.i. (Reordered before for-of loop entry) |
| 539 |
if (remaining === 0) { |
| 540 |
IteratorClose(iterator); |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
// Step 8.b. |
| 545 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 546 |
// Steps 8.b.iii-iv. (Implicit through for-of loop) |
| 547 |
|
| 548 |
// Step 8.b.v. |
| 549 |
yield value; |
| 550 |
|
| 551 |
// Step 8.b.vi. (Implicit through for-of loop) |
| 552 |
|
| 553 |
// Steps 8.b.i-ii. (Reordered) |
| 554 |
if (--remaining === 0) { |
| 555 |
// |break| implicitly calls IteratorClose. |
| 556 |
break; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Iterator.prototype.drop ( limit ) |
| 563 |
* |
| 564 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.drop |
| 565 |
*/ |
| 566 |
function IteratorDrop(limit) { |
| 567 |
// Step 1. |
| 568 |
var iterator = this; |
| 569 |
|
| 570 |
// Step 2. |
| 571 |
if (!IsObject(iterator)) { |
| 572 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 573 |
} |
| 574 |
|
| 575 |
// Steps 3-6. |
| 576 |
var integerLimit = std_Math_trunc(limit); |
| 577 |
if (!(integerLimit >= 0)) { |
| 578 |
ThrowRangeError(JSMSG_NEGATIVE_LIMIT); |
| 579 |
} |
| 580 |
|
| 581 |
// Step 7. (Inlined call to GetIteratorDirect.) |
| 582 |
var nextMethod = iterator.next; |
| 583 |
|
| 584 |
// Steps 8-10. |
| 585 |
var result = NewIteratorHelper(); |
| 586 |
var generator = IteratorDropGenerator(iterator, nextMethod, integerLimit); |
| 587 |
UnsafeSetReservedSlot( |
| 588 |
result, |
| 589 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 590 |
generator |
| 591 |
); |
| 592 |
|
| 593 |
// Stop at the initial yield point. |
| 594 |
callFunction(GeneratorNext, generator); |
| 595 |
|
| 596 |
// Step 11. |
| 597 |
return result; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Iterator.prototype.drop ( limit ) |
| 602 |
* |
| 603 |
* Abstract closure definition. |
| 604 |
* |
| 605 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.drop |
| 606 |
*/ |
| 607 |
function* IteratorDropGenerator(iterator, nextMethod, remaining) { |
| 608 |
var isReturnCompletion = true; |
| 609 |
try { |
| 610 |
// Initial yield point to handle closing the iterator before the for-of |
| 611 |
// loop has been entered for the first time. |
| 612 |
yield; |
| 613 |
|
| 614 |
// Not a Return completion when execution continues normally after |yield|. |
| 615 |
isReturnCompletion = false; |
| 616 |
} finally { |
| 617 |
// Call IteratorClose on a Return completion. |
| 618 |
if (isReturnCompletion) { |
| 619 |
IteratorClose(iterator); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
// Step 8.a. (Implicit) |
| 624 |
|
| 625 |
// Steps 8.b-c. |
| 626 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 627 |
// Step 8.b.i. |
| 628 |
if (remaining-- <= 0) { |
| 629 |
// Steps 8.b.ii-iii. (Implicit through for-of loop) |
| 630 |
// Steps 8.c.i-ii. (Implicit through for-of loop) |
| 631 |
|
| 632 |
// Step 8.c.iii. |
| 633 |
yield value; |
| 634 |
|
| 635 |
// Step 8.c.iv. (Implicit through for-of loop) |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Iterator.prototype.flatMap ( mapper ) |
| 642 |
* |
| 643 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.flatmap |
| 644 |
*/ |
| 645 |
function IteratorFlatMap(mapper) { |
| 646 |
// Step 1. |
| 647 |
var iterator = this; |
| 648 |
|
| 649 |
// Step 2. |
| 650 |
if (!IsObject(iterator)) { |
| 651 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 652 |
} |
| 653 |
|
| 654 |
// Step 3. |
| 655 |
if (!IsCallable(mapper)) { |
| 656 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, mapper)); |
| 657 |
} |
| 658 |
|
| 659 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 660 |
var nextMethod = iterator.next; |
| 661 |
|
| 662 |
// Steps 5-7. |
| 663 |
var result = NewIteratorHelper(); |
| 664 |
var generator = IteratorFlatMapGenerator(iterator, nextMethod, mapper); |
| 665 |
UnsafeSetReservedSlot( |
| 666 |
result, |
| 667 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 668 |
generator |
| 669 |
); |
| 670 |
|
| 671 |
// Stop at the initial yield point. |
| 672 |
callFunction(GeneratorNext, generator); |
| 673 |
|
| 674 |
// Step 8. |
| 675 |
return result; |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Iterator.prototype.flatMap ( mapper ) |
| 680 |
* |
| 681 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.flatmap |
| 682 |
*/ |
| 683 |
function* IteratorFlatMapGenerator(iterator, nextMethod, mapper) { |
| 684 |
var isReturnCompletion = true; |
| 685 |
try { |
| 686 |
// Initial yield point to handle closing the iterator before the for-of |
| 687 |
// loop has been entered for the first time. |
| 688 |
yield; |
| 689 |
|
| 690 |
// Not a Return completion when execution continues normally after |yield|. |
| 691 |
isReturnCompletion = false; |
| 692 |
} finally { |
| 693 |
// Call IteratorClose on a Return completion. |
| 694 |
if (isReturnCompletion) { |
| 695 |
IteratorClose(iterator); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// Step 5.a. |
| 700 |
var counter = 0; |
| 701 |
|
| 702 |
// Step 5.b. |
| 703 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 704 |
// Steps 5.b.i-iii. (Implicit through for-of loop) |
| 705 |
|
| 706 |
// Step 5.b.iv. |
| 707 |
var mapped = callContentFunction(mapper, undefined, value, counter); |
| 708 |
|
| 709 |
// Step 5.b.v. (Implicit through for-of loop) |
| 710 |
|
| 711 |
// Steps 5.b.vi. |
| 712 |
var innerIterator = GetIteratorFlattenable(mapped, /* rejectStrings= */ true); |
| 713 |
var innerIteratorNextMethod = innerIterator.next; |
| 714 |
|
| 715 |
// Step 5.b.vii. (Implicit through for-of loop) |
| 716 |
|
| 717 |
// Steps 5.b.viii-ix. |
| 718 |
for (var innerValue of allowContentIterWithNext(innerIterator, innerIteratorNextMethod)) { |
| 719 |
// Steps 5.b.ix.1-3 and 5.b.ix.4.a-b. (Implicit through for-of loop) |
| 720 |
|
| 721 |
// Step 5.b.ix.4.c. |
| 722 |
yield innerValue; |
| 723 |
|
| 724 |
// Step 5.b.ix.4.d. (Implicit through for-of loop) |
| 725 |
} |
| 726 |
|
| 727 |
// Step 5.b.x. |
| 728 |
counter += 1; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Iterator.prototype.reduce ( reducer [ , initialValue ] ) |
| 734 |
* |
| 735 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.reduce |
| 736 |
*/ |
| 737 |
function IteratorReduce(reducer /*, initialValue*/) { |
| 738 |
// Step 1. |
| 739 |
var iterator = this; |
| 740 |
|
| 741 |
// Step 2. |
| 742 |
if (!IsObject(iterator)) { |
| 743 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 744 |
} |
| 745 |
|
| 746 |
// Step 3. |
| 747 |
if (!IsCallable(reducer)) { |
| 748 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, reducer)); |
| 749 |
} |
| 750 |
|
| 751 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 752 |
var nextMethod = iterator.next; |
| 753 |
|
| 754 |
// Steps 5-6. |
| 755 |
var accumulator; |
| 756 |
var counter; |
| 757 |
if (ArgumentsLength() === 1) { |
| 758 |
// Steps 5.a-d. (Moved below.) |
| 759 |
counter = -1; |
| 760 |
} else { |
| 761 |
// Step 6.a. |
| 762 |
accumulator = GetArgument(1); |
| 763 |
|
| 764 |
// Step 6.b. |
| 765 |
counter = 0; |
| 766 |
} |
| 767 |
|
| 768 |
// Step 7. |
| 769 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 770 |
if (counter < 0) { |
| 771 |
// Step 5. (Reordered steps to compute initial accumulator.) |
| 772 |
|
| 773 |
// Step 5.c. |
| 774 |
accumulator = value; |
| 775 |
|
| 776 |
// Step 5.d. |
| 777 |
counter = 1; |
| 778 |
} else { |
| 779 |
// Steps 7.a-c and 7.e. (Implicit through for-of loop) |
| 780 |
|
| 781 |
// Steps 7.d and 7.f-g. |
| 782 |
accumulator = callContentFunction(reducer, undefined, accumulator, value, counter++); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
// Step 5.b. |
| 787 |
if (counter < 0) { |
| 788 |
ThrowTypeError(JSMSG_EMPTY_ITERATOR_REDUCE); |
| 789 |
} |
| 790 |
|
| 791 |
// Step 7.b. |
| 792 |
return accumulator; |
| 793 |
} |
| 794 |
|
| 795 |
/** |
| 796 |
* Iterator.prototype.toArray ( ) |
| 797 |
* |
| 798 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.toarray |
| 799 |
*/ |
| 800 |
function IteratorToArray() { |
| 801 |
// Step 1. |
| 802 |
var iterator = this; |
| 803 |
|
| 804 |
// Step 2. |
| 805 |
if (!IsObject(iterator)) { |
| 806 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 807 |
} |
| 808 |
|
| 809 |
// Step 3. (Inlined call to GetIteratorDirect.) |
| 810 |
var nextMethod = iterator.next; |
| 811 |
|
| 812 |
// Steps 4-5. |
| 813 |
return [...allowContentIterWithNext(iterator, nextMethod)]; |
| 814 |
} |
| 815 |
|
| 816 |
/** |
| 817 |
* Iterator.prototype.forEach ( fn ) |
| 818 |
* |
| 819 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.foreach |
| 820 |
*/ |
| 821 |
function IteratorForEach(fn) { |
| 822 |
// Step 1. |
| 823 |
var iterator = this; |
| 824 |
|
| 825 |
// Step 2. |
| 826 |
if (!IsObject(iterator)) { |
| 827 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 828 |
} |
| 829 |
|
| 830 |
// Step 3. |
| 831 |
if (!IsCallable(fn)) { |
| 832 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, fn)); |
| 833 |
} |
| 834 |
|
| 835 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 836 |
var nextMethod = iterator.next; |
| 837 |
|
| 838 |
// Step 5. |
| 839 |
var counter = 0; |
| 840 |
|
| 841 |
// Step 6. |
| 842 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 843 |
// Steps 6.a-c. (Implicit through for-of loop) |
| 844 |
|
| 845 |
// Steps 6.d and 6.f. |
| 846 |
callContentFunction(fn, undefined, value, counter++); |
| 847 |
|
| 848 |
// Step 6.e. (Implicit through for-of loop) |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
/** |
| 853 |
* Iterator.prototype.some ( predicate ) |
| 854 |
* |
| 855 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.some |
| 856 |
*/ |
| 857 |
function IteratorSome(predicate) { |
| 858 |
// Step 1. |
| 859 |
var iterator = this; |
| 860 |
|
| 861 |
// Step 2. |
| 862 |
if (!IsObject(iterator)) { |
| 863 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 864 |
} |
| 865 |
|
| 866 |
// Step 3. |
| 867 |
if (!IsCallable(predicate)) { |
| 868 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate)); |
| 869 |
} |
| 870 |
|
| 871 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 872 |
var nextMethod = iterator.next; |
| 873 |
|
| 874 |
// Step 5. |
| 875 |
var counter = 0; |
| 876 |
|
| 877 |
// Step 6. |
| 878 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 879 |
// Steps 6.a-c. (Implicit through for-of loop) |
| 880 |
|
| 881 |
// Steps 6.d-g. |
| 882 |
if (callContentFunction(predicate, undefined, value, counter++)) { |
| 883 |
return true; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
// Step 6.b. |
| 888 |
return false; |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* Iterator.prototype.every ( predicate ) |
| 893 |
* |
| 894 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.every |
| 895 |
*/ |
| 896 |
function IteratorEvery(predicate) { |
| 897 |
// Step 1. |
| 898 |
var iterator = this; |
| 899 |
|
| 900 |
// Step 2. |
| 901 |
if (!IsObject(iterator)) { |
| 902 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 903 |
} |
| 904 |
|
| 905 |
// Step 3. |
| 906 |
if (!IsCallable(predicate)) { |
| 907 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate)); |
| 908 |
} |
| 909 |
|
| 910 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 911 |
var nextMethod = iterator.next; |
| 912 |
|
| 913 |
// Step 5. |
| 914 |
var counter = 0; |
| 915 |
|
| 916 |
// Step 6. |
| 917 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 918 |
// Steps 6.a-c. (Implicit through for-of loop) |
| 919 |
|
| 920 |
// Steps 6.d-g. |
| 921 |
if (!callContentFunction(predicate, undefined, value, counter++)) { |
| 922 |
return false; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
// Step 6.b. |
| 927 |
return true; |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* Iterator.prototype.find ( predicate ) |
| 932 |
* |
| 933 |
* https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.find |
| 934 |
*/ |
| 935 |
function IteratorFind(predicate) { |
| 936 |
// Step 1. |
| 937 |
var iterator = this; |
| 938 |
|
| 939 |
// Step 2. |
| 940 |
if (!IsObject(iterator)) { |
| 941 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, iterator === null ? "null" : typeof iterator); |
| 942 |
} |
| 943 |
|
| 944 |
// Step 3. |
| 945 |
if (!IsCallable(predicate)) { |
| 946 |
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate)); |
| 947 |
} |
| 948 |
|
| 949 |
// Step 4. (Inlined call to GetIteratorDirect.) |
| 950 |
var nextMethod = iterator.next; |
| 951 |
|
| 952 |
// Step 5. |
| 953 |
var counter = 0; |
| 954 |
|
| 955 |
// Step 6. |
| 956 |
for (var value of allowContentIterWithNext(iterator, nextMethod)) { |
| 957 |
// Steps 6.a-c. (Implicit through for-of loop) |
| 958 |
|
| 959 |
// Steps 6.d-g. |
| 960 |
if (callContentFunction(predicate, undefined, value, counter++)) { |
| 961 |
return value; |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
#ifdef NIGHTLY_BUILD |
| 967 |
/** |
| 968 |
* Iterator.concat ( ...items ) |
| 969 |
* |
| 970 |
* https://tc39.es/proposal-iterator-sequencing/ |
| 971 |
*/ |
| 972 |
function IteratorConcat() { |
| 973 |
// Step 1. |
| 974 |
// |
| 975 |
// Stored in reversed order to simplify removing processed items. |
| 976 |
var index = ArgumentsLength() * 2; |
| 977 |
var iterables = std_Array(index); |
| 978 |
|
| 979 |
// Step 2. |
| 980 |
for (var i = 0; i < ArgumentsLength(); i++) { |
| 981 |
var item = GetArgument(i); |
| 982 |
|
| 983 |
// Step 2.a. |
| 984 |
if (!IsObject(item)) { |
| 985 |
ThrowTypeError(JSMSG_OBJECT_REQUIRED, typeof item); |
| 986 |
} |
| 987 |
|
| 988 |
// Step 2.b. (Inlined GetMethod) |
| 989 |
var method = item[GetBuiltinSymbol("iterator")]; |
| 990 |
|
| 991 |
// Step 2.c. |
| 992 |
if (!IsCallable(method)) { |
| 993 |
ThrowTypeError(JSMSG_NOT_ITERABLE, ToSource(item)); |
| 994 |
} |
| 995 |
|
| 996 |
// Step 2.d. |
| 997 |
DefineDataProperty(iterables, --index, item); |
| 998 |
DefineDataProperty(iterables, --index, method); |
| 999 |
} |
| 1000 |
assert(index === 0, "all items stored"); |
| 1001 |
|
| 1002 |
// Steps 3-5. |
| 1003 |
var result = NewIteratorHelper(); |
| 1004 |
var generator = IteratorConcatGenerator(iterables); |
| 1005 |
UnsafeSetReservedSlot( |
| 1006 |
result, |
| 1007 |
ITERATOR_HELPER_GENERATOR_SLOT, |
| 1008 |
generator |
| 1009 |
); |
| 1010 |
|
| 1011 |
// Step 6. |
| 1012 |
return result; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** |
| 1016 |
* Iterator.concat ( ...items ) |
| 1017 |
* |
| 1018 |
* https://tc39.es/proposal-iterator-sequencing/ |
| 1019 |
*/ |
| 1020 |
function* IteratorConcatGenerator(iterables) { |
| 1021 |
assert(IsArray(iterables), "iterables is an array"); |
| 1022 |
assert(iterables.length % 2 === 0, "iterables contains pairs (item, method)"); |
| 1023 |
|
| 1024 |
// Step 3.a. |
| 1025 |
for (var i = iterables.length; i > 0;) { |
| 1026 |
var item = iterables[--i]; |
| 1027 |
var method = iterables[--i]; |
| 1028 |
|
| 1029 |
// Remove processed items to avoid keeping them alive. |
| 1030 |
iterables.length -= 2; |
| 1031 |
|
| 1032 |
// Steps 3.a.i-v. |
| 1033 |
for (var innerValue of allowContentIterWith(item, method)) { |
| 1034 |
// Steps 3.a.v.1-3. (Implicit through for-of loop) |
| 1035 |
|
| 1036 |
yield innerValue; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
/** |
| 1042 |
* Iterator.zip (iterables [, options]) |
| 1043 |
* |
| 1044 |
* https://tc39.es/proposal-joint-iteration/#sec-iterator.zip |
| 1045 |
*/ |
| 1046 |
function IteratorZip(predicate) { |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* Iterator.zipKeyed ( iterables [, options] ) |
| 1052 |
* |
| 1053 |
* https://tc39.es/proposal-joint-iteration/#sec-iterator.zipkeyed |
| 1054 |
*/ |
| 1055 |
function IteratorZipKeyed(predicate) { |
| 1056 |
return false; |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
/** |
| 1061 |
* CreateNumericRangeIterator (start, end, optionOrStep, type) |
| 1062 |
* Step 18 |
| 1063 |
* |
| 1064 |
* https://tc39.es/proposal-iterator.range/#sec-create-numeric-range-iterator |
| 1065 |
*/ |
| 1066 |
function IteratorRangeNext() { |
| 1067 |
var obj = this; |
| 1068 |
// Step 18. Let closure be a new Abstract Closure with no parameters |
| 1069 |
// that captures start, end, step, inclusiveEnd, zero, one and performs the following steps when called: |
| 1070 |
|
| 1071 |
if (!IsObject(obj) || (obj = GuardToIteratorRange(obj)) === null) { |
| 1072 |
return callFunction( |
| 1073 |
CallIteratorRangeMethodIfWrapped, |
| 1074 |
this, |
| 1075 |
"IteratorRangeNext" |
| 1076 |
); |
| 1077 |
} |
| 1078 |
|
| 1079 |
// Retrieve values from reserved slots |
| 1080 |
var start = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_START); |
| 1081 |
var end = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_END); |
| 1082 |
var step = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_STEP); |
| 1083 |
var inclusiveEnd = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_INCLUSIVE_END); |
| 1084 |
var zero = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_ZERO); |
| 1085 |
var one = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_ONE); |
| 1086 |
var currentCount = UnsafeGetReservedSlot(obj, ITERATOR_RANGE_SLOT_CURRENT_COUNT); |
| 1087 |
|
| 1088 |
// Step 18.a: If end > start, let ifIncrease be true |
| 1089 |
// Step 18.b: Else let ifIncrease be false |
| 1090 |
var ifIncrease = end > start; |
| 1091 |
|
| 1092 |
// Step 18.c: If step > zero, let ifStepIncrease be true |
| 1093 |
// Step 18.d: Else let ifStepIncrease be false |
| 1094 |
var ifStepIncrease = step > zero; |
| 1095 |
|
| 1096 |
// Step 18.e: If ifIncrease is not ifStepIncrease, return undefined. |
| 1097 |
if (ifIncrease !== ifStepIncrease) { |
| 1098 |
return { value: undefined, done: true }; |
| 1099 |
} |
| 1100 |
|
| 1101 |
// Step 18.f: Let hitsEnd be false |
| 1102 |
var hitsEnd = false; |
| 1103 |
|
| 1104 |
// Step 18.g: Let currentCount be zero (already handled via slots) |
| 1105 |
|
| 1106 |
// Step 18.i.i: Let currentYieldingValue be start + (step × currentCount) |
| 1107 |
var currentYieldingValue = start + (step * currentCount); |
| 1108 |
|
| 1109 |
// Step 18.i.ii: If currentYieldingValue is equal to end, set hitsEnd to true |
| 1110 |
hitsEnd = currentYieldingValue === end && !inclusiveEnd; |
| 1111 |
|
| 1112 |
|
| 1113 |
// Step 18.i.iii: Set currentCount to currentCount + one |
| 1114 |
currentCount = currentCount + one; |
| 1115 |
|
| 1116 |
// Step 18.i.iv: If ifIncrease is true, then |
| 1117 |
if (ifIncrease) { |
| 1118 |
// Step 18.i.iv.1: If inclusiveEnd is true, then |
| 1119 |
if (inclusiveEnd) { |
| 1120 |
// Step 18.i.iv.1.a: If currentYieldingValue > end, return undefined. |
| 1121 |
if (currentYieldingValue > end) { |
| 1122 |
return { value: undefined, done: true }; |
| 1123 |
} |
| 1124 |
} else { |
| 1125 |
// Step 18.i.iv.2: If currentYieldingValue >= end, return undefined |
| 1126 |
if (currentYieldingValue >= end) { |
| 1127 |
return { value: undefined, done: true }; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} else { |
| 1131 |
// Step 18.i.v: Else |
| 1132 |
// Step 18.i.v.1: If inclusiveEnd is true, then |
| 1133 |
if (inclusiveEnd) { |
| 1134 |
//Step 18.i.v.1.a.a. If end > currentYieldingValue, return undefined. |
| 1135 |
if (end > currentYieldingValue) { |
| 1136 |
return { value: undefined, done: true }; |
| 1137 |
} |
| 1138 |
} else { |
| 1139 |
// Step 18.i.v.2: Else |
| 1140 |
if (end >= currentYieldingValue) { |
| 1141 |
// Step 18i.v.2.a: If end >= currentYieldingValue, return undefined |
| 1142 |
return { value: undefined, done: true }; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
// Step 18.i.vi: Yield currentYieldingValue |
| 1148 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_CURRENT_COUNT, currentCount); |
| 1149 |
|
| 1150 |
// Step 18.j: Return undefined if the loop completes |
| 1151 |
if (hitsEnd) { |
| 1152 |
return { value: undefined, done: true }; |
| 1153 |
} |
| 1154 |
|
| 1155 |
// Return the current value |
| 1156 |
return { value: currentYieldingValue, done: false }; |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
/** |
| 1162 |
* CreateNumericRangeIterator (start, end, optionOrStep, type) |
| 1163 |
* |
| 1164 |
* https://tc39.es/proposal-iterator.range/#sec-create-numeric-range-iterator |
| 1165 |
*/ |
| 1166 |
function CreateNumericRangeIterator(start, end, optionOrStep, isNumberRange) { |
| 1167 |
|
| 1168 |
// Step 1: If start is NaN, throw a RangeError exception. |
| 1169 |
if (isNumberRange && Number_isNaN(start)) { |
| 1170 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_INVALID_START_RANGEERR); |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Step 2: If end is NaN, throw a RangeError exception. |
| 1174 |
if (isNumberRange && Number_isNaN(end)) { |
| 1175 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_INVALID_END_RANGEERR); |
| 1176 |
} |
| 1177 |
|
| 1178 |
// Step 3: If type is NUMBER-RANGE, then |
| 1179 |
if (isNumberRange) { |
| 1180 |
// Step 3.a. Assert: start is a Number. |
| 1181 |
assert(typeof start === 'number', "The 'start' argument must be a number"); |
| 1182 |
|
| 1183 |
// Step 3.b. If end is not a Number, throw a TypeError exception. |
| 1184 |
if (typeof end !== 'number') { |
| 1185 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_INVALID_END); |
| 1186 |
} |
| 1187 |
|
| 1188 |
// Step 3.c. Let zero be 0ℤ. |
| 1189 |
var zero = 0; |
| 1190 |
|
| 1191 |
// Step 3.d. Let one be 1ℤ. |
| 1192 |
var one = 1; |
| 1193 |
// 4: Else, |
| 1194 |
} else { |
| 1195 |
// 4.a. Assert: start is a BigInt. |
| 1196 |
assert(typeof start === 'bigint', "The 'start' argument must be a bigint"); |
| 1197 |
|
| 1198 |
// 4.b. If end is not +∞𝔽 or -∞𝔽 and end is not a BigInt, throw a TypeError exception. |
| 1199 |
if (typeof end !== 'bigint' && !(Number_isFinite(end))) { |
| 1200 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_INVALID_END); |
| 1201 |
} |
| 1202 |
|
| 1203 |
// 4.c. Let zero be 0𝔽. |
| 1204 |
var zero = 0n; |
| 1205 |
|
| 1206 |
// 4.d. Let one be 1𝔽. |
| 1207 |
var one = 1n; |
| 1208 |
} |
| 1209 |
// Step 5: If start is +∞ or -∞, throw a RangeError exception. |
| 1210 |
if (typeof start === 'number' && !Number_isFinite(start)) { |
| 1211 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_START_INFINITY); |
| 1212 |
} |
| 1213 |
// Step 6: Let inclusiveEnd be false. |
| 1214 |
var inclusiveEnd = false; |
| 1215 |
|
| 1216 |
// Step 7: If optionOrStep is undefined or null, then |
| 1217 |
// Step 7.a. Let step be undefined. |
| 1218 |
var step; |
| 1219 |
|
| 1220 |
// Step 8: Else if optionOrStep is an Object, then |
| 1221 |
if (optionOrStep !== null && typeof optionOrStep === 'object') { |
| 1222 |
// Step 8.a. Let step be ? Get(optionOrStep, "step"). |
| 1223 |
step = optionOrStep.step; |
| 1224 |
|
| 1225 |
// Step 8.b. Set inclusiveEnd to ToBoolean(? Get(optionOrStep, "inclusive")). |
| 1226 |
inclusiveEnd = ToBoolean(optionOrStep.inclusiveEnd); |
| 1227 |
} |
| 1228 |
// Step 9: Else if type is NUMBER-RANGE and optionOrStep is a Number, then |
| 1229 |
else if (isNumberRange && typeof optionOrStep === 'number') { |
| 1230 |
// Step 9.a. Let step be optionOrStep. |
| 1231 |
step = optionOrStep; |
| 1232 |
} |
| 1233 |
|
| 1234 |
// Step 10: Else if type is BIGINT-RANGE and optionOrStep is a BigInt, then |
| 1235 |
// Step 10.a. Let step be optionOrStep. |
| 1236 |
else if (!isNumberRange && typeof optionOrStep === 'bigint') { |
| 1237 |
step = optionOrStep; |
| 1238 |
} |
| 1239 |
// Step 11: Else, throw a TypeError exception. |
| 1240 |
else if (optionOrStep !== undefined && optionOrStep !== null) { |
| 1241 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_INVALID_STEP); |
| 1242 |
} |
| 1243 |
|
| 1244 |
// Step 12: If step is undefined or null, then |
| 1245 |
if (step === undefined || step === null) { |
| 1246 |
// Step 12.a. If end > start, let step be one. |
| 1247 |
// Step 12.b. Else let step be -one. |
| 1248 |
step = end > start ? one : -one; |
| 1249 |
} |
| 1250 |
|
| 1251 |
// Step 13: If step is NaN, throw a RangeError exception. |
| 1252 |
if (typeof step === "number" && Number_isNaN(step)) { |
| 1253 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_STEP_NAN); |
| 1254 |
} |
| 1255 |
|
| 1256 |
// Step 14: If type is NUMBER-RANGE and step is not a Number, throw a TypeError exception. |
| 1257 |
if (isNumberRange && typeof step !== 'number') { |
| 1258 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_STEP_NOT_NUMBER); |
| 1259 |
} |
| 1260 |
|
| 1261 |
// Step 15: Else if type is BIGINT-RANGE and step is not a BigInt, throw a TypeError exception |
| 1262 |
else if (!isNumberRange && typeof step !== 'bigint') { |
| 1263 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_STEP_NOT_BIGINT); |
| 1264 |
} |
| 1265 |
|
| 1266 |
// Step 16: If step is +∞ or -∞, throw a RangeError exception. |
| 1267 |
if (typeof step === 'number' && !Number_isFinite(step)) { |
| 1268 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_STEP_NOT_FINITE); |
| 1269 |
} |
| 1270 |
|
| 1271 |
// Step 17: If step is zero and start is not end, throw a RangeError exception. |
| 1272 |
if (step === zero && start !== end) { |
| 1273 |
ThrowRangeError(JSMSG_ITERATOR_RANGE_STEP_ZERO); |
| 1274 |
} |
| 1275 |
// Step 19: Return CreateIteratorFromClosure(closure, "%NumericRangeIteratorPrototype%", %NumericRangeIteratorPrototype%). |
| 1276 |
|
| 1277 |
var obj = NewIteratorRange(); |
| 1278 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_START, start); |
| 1279 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_END, end); |
| 1280 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_STEP, step); |
| 1281 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_INCLUSIVE_END, inclusiveEnd); |
| 1282 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_ZERO, zero); |
| 1283 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_ONE, one); |
| 1284 |
UnsafeSetReservedSlot(obj, ITERATOR_RANGE_SLOT_CURRENT_COUNT, zero); |
| 1285 |
|
| 1286 |
return obj; |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
/** |
| 1292 |
* Iterator.range ( start, end, optionOrStep ) |
| 1293 |
* |
| 1294 |
* https://tc39.es/proposal-iterator.range/#sec-iterator.range |
| 1295 |
*/ |
| 1296 |
function IteratorRange(start, end, optionOrStep) { |
| 1297 |
|
| 1298 |
// Step 1. If start is a Number, return ? CreateNumericRangeIterator(start, end, optionOrStep, NUMBER-RANGE) |
| 1299 |
if (typeof start === 'number') { |
| 1300 |
return CreateNumericRangeIterator(start, end, optionOrStep, true); |
| 1301 |
} |
| 1302 |
|
| 1303 |
// Step 2. If start is a BigInt, return ? CreateNumericRangeIterator(start, end, optionOrStep, BIGINT-RANGE) |
| 1304 |
if (typeof start === 'bigint') { |
| 1305 |
return CreateNumericRangeIterator(start, end, optionOrStep, false); |
| 1306 |
} |
| 1307 |
|
| 1308 |
// Step 3. Throw a TypeError exception. |
| 1309 |
ThrowTypeError(JSMSG_ITERATOR_RANGE_INVALID_START); |
| 1310 |
|
| 1311 |
} |
| 1312 |
#endif |