UNPKG

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