regenerator-runtime.js (24583B)
1 /** 2 * Copyright (c) 2014-present, Facebook, Inc. 3 * 4 * This source code is licensed under the MIT license found in the 5 * LICENSE file in the root directory of this source tree. 6 */ 7 8 var runtime = (function (exports) { 9 "use strict"; 10 11 var Op = Object.prototype; 12 var hasOwn = Op.hasOwnProperty; 13 var undefined; // More compressible than void 0. 14 var $Symbol = typeof Symbol === "function" ? Symbol : {}; 15 var iteratorSymbol = $Symbol.iterator || "@@iterator"; 16 var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator"; 17 var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; 18 19 function define(obj, key, value) { 20 Object.defineProperty(obj, key, { 21 value: value, 22 enumerable: true, 23 configurable: true, 24 writable: true 25 }); 26 return obj[key]; 27 } 28 try { 29 // IE 8 has a broken Object.defineProperty that only works on DOM objects. 30 define({}, ""); 31 } catch (err) { 32 define = function(obj, key, value) { 33 return obj[key] = value; 34 }; 35 } 36 37 function wrap(innerFn, outerFn, self, tryLocsList) { 38 // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator. 39 var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator; 40 var generator = Object.create(protoGenerator.prototype); 41 var context = new Context(tryLocsList || []); 42 43 // The ._invoke method unifies the implementations of the .next, 44 // .throw, and .return methods. 45 generator._invoke = makeInvokeMethod(innerFn, self, context); 46 47 return generator; 48 } 49 exports.wrap = wrap; 50 51 // Try/catch helper to minimize deoptimizations. Returns a completion 52 // record like context.tryEntries[i].completion. This interface could 53 // have been (and was previously) designed to take a closure to be 54 // invoked without arguments, but in all the cases we care about we 55 // already have an existing method we want to call, so there's no need 56 // to create a new function object. We can even get away with assuming 57 // the method takes exactly one argument, since that happens to be true 58 // in every case, so we don't have to touch the arguments object. The 59 // only additional allocation required is the completion record, which 60 // has a stable shape and so hopefully should be cheap to allocate. 61 function tryCatch(fn, obj, arg) { 62 try { 63 return { type: "normal", arg: fn.call(obj, arg) }; 64 } catch (err) { 65 return { type: "throw", arg: err }; 66 } 67 } 68 69 var GenStateSuspendedStart = "suspendedStart"; 70 var GenStateSuspendedYield = "suspendedYield"; 71 var GenStateExecuting = "executing"; 72 var GenStateCompleted = "completed"; 73 74 // Returning this object from the innerFn has the same effect as 75 // breaking out of the dispatch switch statement. 76 var ContinueSentinel = {}; 77 78 // Dummy constructor functions that we use as the .constructor and 79 // .constructor.prototype properties for functions that return Generator 80 // objects. For full spec compliance, you may wish to configure your 81 // minifier not to mangle the names of these two functions. 82 function Generator() {} 83 function GeneratorFunction() {} 84 function GeneratorFunctionPrototype() {} 85 86 // This is a polyfill for %IteratorPrototype% for environments that 87 // don't natively support it. 88 var IteratorPrototype = {}; 89 IteratorPrototype[iteratorSymbol] = function () { 90 return this; 91 }; 92 93 var getProto = Object.getPrototypeOf; 94 var NativeIteratorPrototype = getProto && getProto(getProto(values([]))); 95 if (NativeIteratorPrototype && 96 NativeIteratorPrototype !== Op && 97 hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) { 98 // This environment has a native %IteratorPrototype%; use it instead 99 // of the polyfill. 100 IteratorPrototype = NativeIteratorPrototype; 101 } 102 103 var Gp = GeneratorFunctionPrototype.prototype = 104 Generator.prototype = Object.create(IteratorPrototype); 105 GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype; 106 GeneratorFunctionPrototype.constructor = GeneratorFunction; 107 GeneratorFunction.displayName = define( 108 GeneratorFunctionPrototype, 109 toStringTagSymbol, 110 "GeneratorFunction" 111 ); 112 113 // Helper for defining the .next, .throw, and .return methods of the 114 // Iterator interface in terms of a single ._invoke method. 115 function defineIteratorMethods(prototype) { 116 ["next", "throw", "return"].forEach(function(method) { 117 define(prototype, method, function(arg) { 118 return this._invoke(method, arg); 119 }); 120 }); 121 } 122 123 exports.isGeneratorFunction = function(genFun) { 124 var ctor = typeof genFun === "function" && genFun.constructor; 125 return ctor 126 ? ctor === GeneratorFunction || 127 // For the native GeneratorFunction constructor, the best we can 128 // do is to check its .name property. 129 (ctor.displayName || ctor.name) === "GeneratorFunction" 130 : false; 131 }; 132 133 exports.mark = function(genFun) { 134 if (Object.setPrototypeOf) { 135 Object.setPrototypeOf(genFun, GeneratorFunctionPrototype); 136 } else { 137 genFun.__proto__ = GeneratorFunctionPrototype; 138 define(genFun, toStringTagSymbol, "GeneratorFunction"); 139 } 140 genFun.prototype = Object.create(Gp); 141 return genFun; 142 }; 143 144 // Within the body of any async function, `await x` is transformed to 145 // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test 146 // `hasOwn.call(value, "__await")` to determine if the yielded value is 147 // meant to be awaited. 148 exports.awrap = function(arg) { 149 return { __await: arg }; 150 }; 151 152 function AsyncIterator(generator, PromiseImpl) { 153 function invoke(method, arg, resolve, reject) { 154 var record = tryCatch(generator[method], generator, arg); 155 if (record.type === "throw") { 156 reject(record.arg); 157 } else { 158 var result = record.arg; 159 var value = result.value; 160 if (value && 161 typeof value === "object" && 162 hasOwn.call(value, "__await")) { 163 return PromiseImpl.resolve(value.__await).then(function(value) { 164 invoke("next", value, resolve, reject); 165 }, function(err) { 166 invoke("throw", err, resolve, reject); 167 }); 168 } 169 170 return PromiseImpl.resolve(value).then(function(unwrapped) { 171 // When a yielded Promise is resolved, its final value becomes 172 // the .value of the Promise<{value,done}> result for the 173 // current iteration. 174 result.value = unwrapped; 175 resolve(result); 176 }, function(error) { 177 // If a rejected Promise was yielded, throw the rejection back 178 // into the async generator function so it can be handled there. 179 return invoke("throw", error, resolve, reject); 180 }); 181 } 182 } 183 184 var previousPromise; 185 186 function enqueue(method, arg) { 187 function callInvokeWithMethodAndArg() { 188 return new PromiseImpl(function(resolve, reject) { 189 invoke(method, arg, resolve, reject); 190 }); 191 } 192 193 return previousPromise = 194 // If enqueue has been called before, then we want to wait until 195 // all previous Promises have been resolved before calling invoke, 196 // so that results are always delivered in the correct order. If 197 // enqueue has not been called before, then it is important to 198 // call invoke immediately, without waiting on a callback to fire, 199 // so that the async generator function has the opportunity to do 200 // any necessary setup in a predictable way. This predictability 201 // is why the Promise constructor synchronously invokes its 202 // executor callback, and why async functions synchronously 203 // execute code before the first await. Since we implement simple 204 // async functions in terms of async generators, it is especially 205 // important to get this right, even though it requires care. 206 previousPromise ? previousPromise.then( 207 callInvokeWithMethodAndArg, 208 // Avoid propagating failures to Promises returned by later 209 // invocations of the iterator. 210 callInvokeWithMethodAndArg 211 ) : callInvokeWithMethodAndArg(); 212 } 213 214 // Define the unified helper method that is used to implement .next, 215 // .throw, and .return (see defineIteratorMethods). 216 this._invoke = enqueue; 217 } 218 219 defineIteratorMethods(AsyncIterator.prototype); 220 AsyncIterator.prototype[asyncIteratorSymbol] = function () { 221 return this; 222 }; 223 exports.AsyncIterator = AsyncIterator; 224 225 // Note that simple async functions are implemented on top of 226 // AsyncIterator objects; they just return a Promise for the value of 227 // the final result produced by the iterator. 228 exports.async = function(innerFn, outerFn, self, tryLocsList, PromiseImpl) { 229 if (PromiseImpl === void 0) PromiseImpl = Promise; 230 231 var iter = new AsyncIterator( 232 wrap(innerFn, outerFn, self, tryLocsList), 233 PromiseImpl 234 ); 235 236 return exports.isGeneratorFunction(outerFn) 237 ? iter // If outerFn is a generator, return the full iterator. 238 : iter.next().then(function(result) { 239 return result.done ? result.value : iter.next(); 240 }); 241 }; 242 243 function makeInvokeMethod(innerFn, self, context) { 244 var state = GenStateSuspendedStart; 245 246 return function invoke(method, arg) { 247 if (state === GenStateExecuting) { 248 throw new Error("Generator is already running"); 249 } 250 251 if (state === GenStateCompleted) { 252 if (method === "throw") { 253 throw arg; 254 } 255 256 // Be forgiving, per 25.3.3.3.3 of the spec: 257 // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume 258 return doneResult(); 259 } 260 261 context.method = method; 262 context.arg = arg; 263 264 while (true) { 265 var delegate = context.delegate; 266 if (delegate) { 267 var delegateResult = maybeInvokeDelegate(delegate, context); 268 if (delegateResult) { 269 if (delegateResult === ContinueSentinel) continue; 270 return delegateResult; 271 } 272 } 273 274 if (context.method === "next") { 275 // Setting context._sent for legacy support of Babel's 276 // function.sent implementation. 277 context.sent = context._sent = context.arg; 278 279 } else if (context.method === "throw") { 280 if (state === GenStateSuspendedStart) { 281 state = GenStateCompleted; 282 throw context.arg; 283 } 284 285 context.dispatchException(context.arg); 286 287 } else if (context.method === "return") { 288 context.abrupt("return", context.arg); 289 } 290 291 state = GenStateExecuting; 292 293 var record = tryCatch(innerFn, self, context); 294 if (record.type === "normal") { 295 // If an exception is thrown from innerFn, we leave state === 296 // GenStateExecuting and loop back for another invocation. 297 state = context.done 298 ? GenStateCompleted 299 : GenStateSuspendedYield; 300 301 if (record.arg === ContinueSentinel) { 302 continue; 303 } 304 305 return { 306 value: record.arg, 307 done: context.done 308 }; 309 310 } else if (record.type === "throw") { 311 state = GenStateCompleted; 312 // Dispatch the exception by looping back around to the 313 // context.dispatchException(context.arg) call above. 314 context.method = "throw"; 315 context.arg = record.arg; 316 } 317 } 318 }; 319 } 320 321 // Call delegate.iterator[context.method](context.arg) and handle the 322 // result, either by returning a { value, done } result from the 323 // delegate iterator, or by modifying context.method and context.arg, 324 // setting context.delegate to null, and returning the ContinueSentinel. 325 function maybeInvokeDelegate(delegate, context) { 326 var method = delegate.iterator[context.method]; 327 if (method === undefined) { 328 // A .throw or .return when the delegate iterator has no .throw 329 // method always terminates the yield* loop. 330 context.delegate = null; 331 332 if (context.method === "throw") { 333 // Note: ["return"] must be used for ES3 parsing compatibility. 334 if (delegate.iterator["return"]) { 335 // If the delegate iterator has a return method, give it a 336 // chance to clean up. 337 context.method = "return"; 338 context.arg = undefined; 339 maybeInvokeDelegate(delegate, context); 340 341 if (context.method === "throw") { 342 // If maybeInvokeDelegate(context) changed context.method from 343 // "return" to "throw", let that override the TypeError below. 344 return ContinueSentinel; 345 } 346 } 347 348 context.method = "throw"; 349 context.arg = new TypeError( 350 "The iterator does not provide a 'throw' method"); 351 } 352 353 return ContinueSentinel; 354 } 355 356 var record = tryCatch(method, delegate.iterator, context.arg); 357 358 if (record.type === "throw") { 359 context.method = "throw"; 360 context.arg = record.arg; 361 context.delegate = null; 362 return ContinueSentinel; 363 } 364 365 var info = record.arg; 366 367 if (! info) { 368 context.method = "throw"; 369 context.arg = new TypeError("iterator result is not an object"); 370 context.delegate = null; 371 return ContinueSentinel; 372 } 373 374 if (info.done) { 375 // Assign the result of the finished delegate to the temporary 376 // variable specified by delegate.resultName (see delegateYield). 377 context[delegate.resultName] = info.value; 378 379 // Resume execution at the desired location (see delegateYield). 380 context.next = delegate.nextLoc; 381 382 // If context.method was "throw" but the delegate handled the 383 // exception, let the outer generator proceed normally. If 384 // context.method was "next", forget context.arg since it has been 385 // "consumed" by the delegate iterator. If context.method was 386 // "return", allow the original .return call to continue in the 387 // outer generator. 388 if (context.method !== "return") { 389 context.method = "next"; 390 context.arg = undefined; 391 } 392 393 } else { 394 // Re-yield the result returned by the delegate method. 395 return info; 396 } 397 398 // The delegate iterator is finished, so forget it and continue with 399 // the outer generator. 400 context.delegate = null; 401 return ContinueSentinel; 402 } 403 404 // Define Generator.prototype.{next,throw,return} in terms of the 405 // unified ._invoke helper method. 406 defineIteratorMethods(Gp); 407 408 define(Gp, toStringTagSymbol, "Generator"); 409 410 // A Generator should always return itself as the iterator object when the 411 // @@iterator function is called on it. Some browsers' implementations of the 412 // iterator prototype chain incorrectly implement this, causing the Generator 413 // object to not be returned from this call. This ensures that doesn't happen. 414 // See https://github.com/facebook/regenerator/issues/274 for more details. 415 Gp[iteratorSymbol] = function() { 416 return this; 417 }; 418 419 Gp.toString = function() { 420 return "[object Generator]"; 421 }; 422 423 function pushTryEntry(locs) { 424 var entry = { tryLoc: locs[0] }; 425 426 if (1 in locs) { 427 entry.catchLoc = locs[1]; 428 } 429 430 if (2 in locs) { 431 entry.finallyLoc = locs[2]; 432 entry.afterLoc = locs[3]; 433 } 434 435 this.tryEntries.push(entry); 436 } 437 438 function resetTryEntry(entry) { 439 var record = entry.completion || {}; 440 record.type = "normal"; 441 delete record.arg; 442 entry.completion = record; 443 } 444 445 function Context(tryLocsList) { 446 // The root entry object (effectively a try statement without a catch 447 // or a finally block) gives us a place to store values thrown from 448 // locations where there is no enclosing try statement. 449 this.tryEntries = [{ tryLoc: "root" }]; 450 tryLocsList.forEach(pushTryEntry, this); 451 this.reset(true); 452 } 453 454 exports.keys = function(object) { 455 var keys = []; 456 for (var key in object) { 457 keys.push(key); 458 } 459 keys.reverse(); 460 461 // Rather than returning an object with a next method, we keep 462 // things simple and return the next function itself. 463 return function next() { 464 while (keys.length) { 465 var key = keys.pop(); 466 if (key in object) { 467 next.value = key; 468 next.done = false; 469 return next; 470 } 471 } 472 473 // To avoid creating an additional object, we just hang the .value 474 // and .done properties off the next function object itself. This 475 // also ensures that the minifier will not anonymize the function. 476 next.done = true; 477 return next; 478 }; 479 }; 480 481 function values(iterable) { 482 if (iterable) { 483 var iteratorMethod = iterable[iteratorSymbol]; 484 if (iteratorMethod) { 485 return iteratorMethod.call(iterable); 486 } 487 488 if (typeof iterable.next === "function") { 489 return iterable; 490 } 491 492 if (!isNaN(iterable.length)) { 493 var i = -1, next = function next() { 494 while (++i < iterable.length) { 495 if (hasOwn.call(iterable, i)) { 496 next.value = iterable[i]; 497 next.done = false; 498 return next; 499 } 500 } 501 502 next.value = undefined; 503 next.done = true; 504 505 return next; 506 }; 507 508 return next.next = next; 509 } 510 } 511 512 // Return an iterator with no values. 513 return { next: doneResult }; 514 } 515 exports.values = values; 516 517 function doneResult() { 518 return { value: undefined, done: true }; 519 } 520 521 Context.prototype = { 522 constructor: Context, 523 524 reset: function(skipTempReset) { 525 this.prev = 0; 526 this.next = 0; 527 // Resetting context._sent for legacy support of Babel's 528 // function.sent implementation. 529 this.sent = this._sent = undefined; 530 this.done = false; 531 this.delegate = null; 532 533 this.method = "next"; 534 this.arg = undefined; 535 536 this.tryEntries.forEach(resetTryEntry); 537 538 if (!skipTempReset) { 539 for (var name in this) { 540 // Not sure about the optimal order of these conditions: 541 if (name.charAt(0) === "t" && 542 hasOwn.call(this, name) && 543 !isNaN(+name.slice(1))) { 544 this[name] = undefined; 545 } 546 } 547 } 548 }, 549 550 stop: function() { 551 this.done = true; 552 553 var rootEntry = this.tryEntries[0]; 554 var rootRecord = rootEntry.completion; 555 if (rootRecord.type === "throw") { 556 throw rootRecord.arg; 557 } 558 559 return this.rval; 560 }, 561 562 dispatchException: function(exception) { 563 if (this.done) { 564 throw exception; 565 } 566 567 var context = this; 568 function handle(loc, caught) { 569 record.type = "throw"; 570 record.arg = exception; 571 context.next = loc; 572 573 if (caught) { 574 // If the dispatched exception was caught by a catch block, 575 // then let that catch block handle the exception normally. 576 context.method = "next"; 577 context.arg = undefined; 578 } 579 580 return !! caught; 581 } 582 583 for (var i = this.tryEntries.length - 1; i >= 0; --i) { 584 var entry = this.tryEntries[i]; 585 var record = entry.completion; 586 587 if (entry.tryLoc === "root") { 588 // Exception thrown outside of any try block that could handle 589 // it, so set the completion value of the entire function to 590 // throw the exception. 591 return handle("end"); 592 } 593 594 if (entry.tryLoc <= this.prev) { 595 var hasCatch = hasOwn.call(entry, "catchLoc"); 596 var hasFinally = hasOwn.call(entry, "finallyLoc"); 597 598 if (hasCatch && hasFinally) { 599 if (this.prev < entry.catchLoc) { 600 return handle(entry.catchLoc, true); 601 } else if (this.prev < entry.finallyLoc) { 602 return handle(entry.finallyLoc); 603 } 604 605 } else if (hasCatch) { 606 if (this.prev < entry.catchLoc) { 607 return handle(entry.catchLoc, true); 608 } 609 610 } else if (hasFinally) { 611 if (this.prev < entry.finallyLoc) { 612 return handle(entry.finallyLoc); 613 } 614 615 } else { 616 throw new Error("try statement without catch or finally"); 617 } 618 } 619 } 620 }, 621 622 abrupt: function(type, arg) { 623 for (var i = this.tryEntries.length - 1; i >= 0; --i) { 624 var entry = this.tryEntries[i]; 625 if (entry.tryLoc <= this.prev && 626 hasOwn.call(entry, "finallyLoc") && 627 this.prev < entry.finallyLoc) { 628 var finallyEntry = entry; 629 break; 630 } 631 } 632 633 if (finallyEntry && 634 (type === "break" || 635 type === "continue") && 636 finallyEntry.tryLoc <= arg && 637 arg <= finallyEntry.finallyLoc) { 638 // Ignore the finally entry if control is not jumping to a 639 // location outside the try/catch block. 640 finallyEntry = null; 641 } 642 643 var record = finallyEntry ? finallyEntry.completion : {}; 644 record.type = type; 645 record.arg = arg; 646 647 if (finallyEntry) { 648 this.method = "next"; 649 this.next = finallyEntry.finallyLoc; 650 return ContinueSentinel; 651 } 652 653 return this.complete(record); 654 }, 655 656 complete: function(record, afterLoc) { 657 if (record.type === "throw") { 658 throw record.arg; 659 } 660 661 if (record.type === "break" || 662 record.type === "continue") { 663 this.next = record.arg; 664 } else if (record.type === "return") { 665 this.rval = this.arg = record.arg; 666 this.method = "return"; 667 this.next = "end"; 668 } else if (record.type === "normal" && afterLoc) { 669 this.next = afterLoc; 670 } 671 672 return ContinueSentinel; 673 }, 674 675 finish: function(finallyLoc) { 676 for (var i = this.tryEntries.length - 1; i >= 0; --i) { 677 var entry = this.tryEntries[i]; 678 if (entry.finallyLoc === finallyLoc) { 679 this.complete(entry.completion, entry.afterLoc); 680 resetTryEntry(entry); 681 return ContinueSentinel; 682 } 683 } 684 }, 685 686 "catch": function(tryLoc) { 687 for (var i = this.tryEntries.length - 1; i >= 0; --i) { 688 var entry = this.tryEntries[i]; 689 if (entry.tryLoc === tryLoc) { 690 var record = entry.completion; 691 if (record.type === "throw") { 692 var thrown = record.arg; 693 resetTryEntry(entry); 694 } 695 return thrown; 696 } 697 } 698 699 // The context.catch method must only be called with a location 700 // argument that corresponds to a known catch block. 701 throw new Error("illegal catch attempt"); 702 }, 703 704 delegateYield: function(iterable, resultName, nextLoc) { 705 this.delegate = { 706 iterator: values(iterable), 707 resultName: resultName, 708 nextLoc: nextLoc 709 }; 710 711 if (this.method === "next") { 712 // Deliberately forget the last sent value so that we don't 713 // accidentally pass it on to the delegate. 714 this.arg = undefined; 715 } 716 717 return ContinueSentinel; 718 } 719 }; 720 721 // Regardless of whether this script is executing as a CommonJS module 722 // or not, return the runtime object so that we can declare the variable 723 // regeneratorRuntime in the outer scope, which allows this module to be 724 // injected easily by `bin/regenerator --include-runtime script.js`. 725 return exports; 726 727 }( 728 // If this script is executing as a CommonJS module, use module.exports 729 // as the regeneratorRuntime namespace. Otherwise create a new empty 730 // object. Either way, the resulting object will be used to initialize 731 // the regeneratorRuntime variable at the top of this file. 732 typeof module === "object" ? module.exports : {} 733 )); 734 735 try { 736 regeneratorRuntime = runtime; 737 } catch (accidentalStrictMode) { 738 // This module should not be running in strict mode, so the above 739 // assignment should always work unless something is misconfigured. Just 740 // in case runtime.js accidentally runs in strict mode, we can escape 741 // strict mode using a global Function call. This could conceivably fail 742 // if a Content Security Policy forbids using Function, but in that case 743 // the proper solution is to fix the accidental strict mode problem. If 744 // you've misconfigured your bundler to force strict mode and applied a 745 // CSP to forbid Function, and you're not willing to fix either of those 746 // problems, please detail your unique predicament in a GitHub issue. 747 Function("r", "regeneratorRuntime = r")(runtime); 748 }