regenerator-runtime.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. export default (function(module) {
  2. /**
  3. * Copyright (c) 2014, Facebook, Inc.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under the BSD-style license found in the
  7. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  8. * additional grant of patent rights can be found in the PATENTS file in
  9. * the same directory.
  10. */
  11. !(function(global) {
  12. "use strict";
  13. var Op = Object.prototype;
  14. var hasOwn = Op.hasOwnProperty;
  15. var undefined; // More compressible than void 0.
  16. var $Symbol = typeof Symbol === "function" ? Symbol : {};
  17. var iteratorSymbol = $Symbol.iterator || "@@iterator";
  18. var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  19. var inModule = typeof module === "object";
  20. var runtime = global.regeneratorRuntime;
  21. if (runtime) {
  22. if (inModule) {
  23. // If regeneratorRuntime is defined globally and we're in a module,
  24. // make the exports object identical to regeneratorRuntime.
  25. module.exports = runtime;
  26. }
  27. // Don't bother evaluating the rest of this file if the runtime was
  28. // already defined globally.
  29. return;
  30. }
  31. // Define the runtime globally (as expected by generated code) as either
  32. // module.exports (if we're in a module) or a new, empty object.
  33. runtime = global.regeneratorRuntime = inModule ? module.exports : {};
  34. function wrap(innerFn, outerFn, self, tryLocsList) {
  35. // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
  36. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
  37. var generator = Object.create(protoGenerator.prototype);
  38. var context = new Context(tryLocsList || []);
  39. // The ._invoke method unifies the implementations of the .next,
  40. // .throw, and .return methods.
  41. generator._invoke = makeInvokeMethod(innerFn, self, context);
  42. return generator;
  43. }
  44. runtime.wrap = wrap;
  45. // Try/catch helper to minimize deoptimizations. Returns a completion
  46. // record like context.tryEntries[i].completion. This interface could
  47. // have been (and was previously) designed to take a closure to be
  48. // invoked without arguments, but in all the cases we care about we
  49. // already have an existing method we want to call, so there's no need
  50. // to create a new function object. We can even get away with assuming
  51. // the method takes exactly one argument, since that happens to be true
  52. // in every case, so we don't have to touch the arguments object. The
  53. // only additional allocation required is the completion record, which
  54. // has a stable shape and so hopefully should be cheap to allocate.
  55. function tryCatch(fn, obj, arg) {
  56. try {
  57. return { type: "normal", arg: fn.call(obj, arg) };
  58. } catch (err) {
  59. return { type: "throw", arg: err };
  60. }
  61. }
  62. var GenStateSuspendedStart = "suspendedStart";
  63. var GenStateSuspendedYield = "suspendedYield";
  64. var GenStateExecuting = "executing";
  65. var GenStateCompleted = "completed";
  66. // Returning this object from the innerFn has the same effect as
  67. // breaking out of the dispatch switch statement.
  68. var ContinueSentinel = {};
  69. // Dummy constructor functions that we use as the .constructor and
  70. // .constructor.prototype properties for functions that return Generator
  71. // objects. For full spec compliance, you may wish to configure your
  72. // minifier not to mangle the names of these two functions.
  73. function Generator() {}
  74. function GeneratorFunction() {}
  75. function GeneratorFunctionPrototype() {}
  76. // This is a polyfill for %IteratorPrototype% for environments that
  77. // don't natively support it.
  78. var IteratorPrototype = {};
  79. IteratorPrototype[iteratorSymbol] = function () {
  80. return this;
  81. };
  82. var getProto = Object.getPrototypeOf;
  83. var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  84. if (NativeIteratorPrototype &&
  85. NativeIteratorPrototype !== Op &&
  86. hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
  87. // This environment has a native %IteratorPrototype%; use it instead
  88. // of the polyfill.
  89. IteratorPrototype = NativeIteratorPrototype;
  90. }
  91. var Gp = GeneratorFunctionPrototype.prototype =
  92. Generator.prototype = Object.create(IteratorPrototype);
  93. GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
  94. GeneratorFunctionPrototype.constructor = GeneratorFunction;
  95. GeneratorFunctionPrototype[toStringTagSymbol] =
  96. GeneratorFunction.displayName = "GeneratorFunction";
  97. // Helper for defining the .next, .throw, and .return methods of the
  98. // Iterator interface in terms of a single ._invoke method.
  99. function defineIteratorMethods(prototype) {
  100. ["next", "throw", "return"].forEach(function(method) {
  101. prototype[method] = function(arg) {
  102. return this._invoke(method, arg);
  103. };
  104. });
  105. }
  106. runtime.isGeneratorFunction = function(genFun) {
  107. var ctor = typeof genFun === "function" && genFun.constructor;
  108. return ctor
  109. ? ctor === GeneratorFunction ||
  110. // For the native GeneratorFunction constructor, the best we can
  111. // do is to check its .name property.
  112. (ctor.displayName || ctor.name) === "GeneratorFunction"
  113. : false;
  114. };
  115. runtime.mark = function(genFun) {
  116. if (Object.setPrototypeOf) {
  117. Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
  118. } else {
  119. genFun.__proto__ = GeneratorFunctionPrototype;
  120. if (!(toStringTagSymbol in genFun)) {
  121. genFun[toStringTagSymbol] = "GeneratorFunction";
  122. }
  123. }
  124. genFun.prototype = Object.create(Gp);
  125. return genFun;
  126. };
  127. // Within the body of any async function, `await x` is transformed to
  128. // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
  129. // `hasOwn.call(value, "__await")` to determine if the yielded value is
  130. // meant to be awaited.
  131. runtime.awrap = function(arg) {
  132. return { __await: arg };
  133. };
  134. function AsyncIterator(generator) {
  135. function invoke(method, arg, resolve, reject) {
  136. var record = tryCatch(generator[method], generator, arg);
  137. if (record.type === "throw") {
  138. reject(record.arg);
  139. } else {
  140. var result = record.arg;
  141. var value = result.value;
  142. if (value &&
  143. typeof value === "object" &&
  144. hasOwn.call(value, "__await")) {
  145. return Promise.resolve(value.__await).then(function(value) {
  146. invoke("next", value, resolve, reject);
  147. }, function(err) {
  148. invoke("throw", err, resolve, reject);
  149. });
  150. }
  151. return Promise.resolve(value).then(function(unwrapped) {
  152. // When a yielded Promise is resolved, its final value becomes
  153. // the .value of the Promise<{value,done}> result for the
  154. // current iteration. If the Promise is rejected, however, the
  155. // result for this iteration will be rejected with the same
  156. // reason. Note that rejections of yielded Promises are not
  157. // thrown back into the generator function, as is the case
  158. // when an awaited Promise is rejected. This difference in
  159. // behavior between yield and await is important, because it
  160. // allows the consumer to decide what to do with the yielded
  161. // rejection (swallow it and continue, manually .throw it back
  162. // into the generator, abandon iteration, whatever). With
  163. // await, by contrast, there is no opportunity to examine the
  164. // rejection reason outside the generator function, so the
  165. // only option is to throw it from the await expression, and
  166. // let the generator function handle the exception.
  167. result.value = unwrapped;
  168. resolve(result);
  169. }, reject);
  170. }
  171. }
  172. if (typeof process === "object" && process.domain) {
  173. invoke = process.domain.bind(invoke);
  174. }
  175. var previousPromise;
  176. function enqueue(method, arg) {
  177. function callInvokeWithMethodAndArg() {
  178. return new Promise(function(resolve, reject) {
  179. invoke(method, arg, resolve, reject);
  180. });
  181. }
  182. return previousPromise =
  183. // If enqueue has been called before, then we want to wait until
  184. // all previous Promises have been resolved before calling invoke,
  185. // so that results are always delivered in the correct order. If
  186. // enqueue has not been called before, then it is important to
  187. // call invoke immediately, without waiting on a callback to fire,
  188. // so that the async generator function has the opportunity to do
  189. // any necessary setup in a predictable way. This predictability
  190. // is why the Promise constructor synchronously invokes its
  191. // executor callback, and why async functions synchronously
  192. // execute code before the first await. Since we implement simple
  193. // async functions in terms of async generators, it is especially
  194. // important to get this right, even though it requires care.
  195. previousPromise ? previousPromise.then(
  196. callInvokeWithMethodAndArg,
  197. // Avoid propagating failures to Promises returned by later
  198. // invocations of the iterator.
  199. callInvokeWithMethodAndArg
  200. ) : callInvokeWithMethodAndArg();
  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. defineIteratorMethods(AsyncIterator.prototype);
  207. runtime.AsyncIterator = AsyncIterator;
  208. // Note that simple async functions are implemented on top of
  209. // AsyncIterator objects; they just return a Promise for the value of
  210. // the final result produced by the iterator.
  211. runtime.async = function(innerFn, outerFn, self, tryLocsList) {
  212. var iter = new AsyncIterator(
  213. wrap(innerFn, outerFn, self, tryLocsList)
  214. );
  215. return runtime.isGeneratorFunction(outerFn)
  216. ? iter // If outerFn is a generator, return the full iterator.
  217. : iter.next().then(function(result) {
  218. return result.done ? result.value : iter.next();
  219. });
  220. };
  221. function makeInvokeMethod(innerFn, self, context) {
  222. var state = GenStateSuspendedStart;
  223. return function invoke(method, arg) {
  224. if (state === GenStateExecuting) {
  225. throw new Error("Generator is already running");
  226. }
  227. if (state === GenStateCompleted) {
  228. if (method === "throw") {
  229. throw arg;
  230. }
  231. // Be forgiving, per 25.3.3.3.3 of the spec:
  232. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  233. return doneResult();
  234. }
  235. while (true) {
  236. var delegate = context.delegate;
  237. if (delegate) {
  238. if (method === "return" ||
  239. (method === "throw" && delegate.iterator[method] === undefined)) {
  240. // A return or throw (when the delegate iterator has no throw
  241. // method) always terminates the yield* loop.
  242. context.delegate = null;
  243. // If the delegate iterator has a return method, give it a
  244. // chance to clean up.
  245. var returnMethod = delegate.iterator["return"];
  246. if (returnMethod) {
  247. var record = tryCatch(returnMethod, delegate.iterator, arg);
  248. if (record.type === "throw") {
  249. // If the return method threw an exception, let that
  250. // exception prevail over the original return or throw.
  251. method = "throw";
  252. arg = record.arg;
  253. continue;
  254. }
  255. }
  256. if (method === "return") {
  257. // Continue with the outer return, now that the delegate
  258. // iterator has been terminated.
  259. continue;
  260. }
  261. }
  262. var record = tryCatch(
  263. delegate.iterator[method],
  264. delegate.iterator,
  265. arg
  266. );
  267. if (record.type === "throw") {
  268. context.delegate = null;
  269. // Like returning generator.throw(uncaught), but without the
  270. // overhead of an extra function call.
  271. method = "throw";
  272. arg = record.arg;
  273. continue;
  274. }
  275. // Delegate generator ran and handled its own exceptions so
  276. // regardless of what the method was, we continue as if it is
  277. // "next" with an undefined arg.
  278. method = "next";
  279. arg = undefined;
  280. var info = record.arg;
  281. if (info.done) {
  282. context[delegate.resultName] = info.value;
  283. context.next = delegate.nextLoc;
  284. } else {
  285. state = GenStateSuspendedYield;
  286. return info;
  287. }
  288. context.delegate = null;
  289. }
  290. if (method === "next") {
  291. // Setting context._sent for legacy support of Babel's
  292. // function.sent implementation.
  293. context.sent = context._sent = arg;
  294. } else if (method === "throw") {
  295. if (state === GenStateSuspendedStart) {
  296. state = GenStateCompleted;
  297. throw arg;
  298. }
  299. if (context.dispatchException(arg)) {
  300. // If the dispatched exception was caught by a catch block,
  301. // then let that catch block handle the exception normally.
  302. method = "next";
  303. arg = undefined;
  304. }
  305. } else if (method === "return") {
  306. context.abrupt("return", arg);
  307. }
  308. state = GenStateExecuting;
  309. var record = tryCatch(innerFn, self, context);
  310. if (record.type === "normal") {
  311. // If an exception is thrown from innerFn, we leave state ===
  312. // GenStateExecuting and loop back for another invocation.
  313. state = context.done
  314. ? GenStateCompleted
  315. : GenStateSuspendedYield;
  316. var info = {
  317. value: record.arg,
  318. done: context.done
  319. };
  320. if (record.arg === ContinueSentinel) {
  321. if (context.delegate && method === "next") {
  322. // Deliberately forget the last sent value so that we don't
  323. // accidentally pass it on to the delegate.
  324. arg = undefined;
  325. }
  326. } else {
  327. return info;
  328. }
  329. } else if (record.type === "throw") {
  330. state = GenStateCompleted;
  331. // Dispatch the exception by looping back around to the
  332. // context.dispatchException(arg) call above.
  333. method = "throw";
  334. arg = record.arg;
  335. }
  336. }
  337. };
  338. }
  339. // Define Generator.prototype.{next,throw,return} in terms of the
  340. // unified ._invoke helper method.
  341. defineIteratorMethods(Gp);
  342. Gp[toStringTagSymbol] = "Generator";
  343. Gp.toString = function() {
  344. return "[object Generator]";
  345. };
  346. function pushTryEntry(locs) {
  347. var entry = { tryLoc: locs[0] };
  348. if (1 in locs) {
  349. entry.catchLoc = locs[1];
  350. }
  351. if (2 in locs) {
  352. entry.finallyLoc = locs[2];
  353. entry.afterLoc = locs[3];
  354. }
  355. this.tryEntries.push(entry);
  356. }
  357. function resetTryEntry(entry) {
  358. var record = entry.completion || {};
  359. record.type = "normal";
  360. delete record.arg;
  361. entry.completion = record;
  362. }
  363. function Context(tryLocsList) {
  364. // The root entry object (effectively a try statement without a catch
  365. // or a finally block) gives us a place to store values thrown from
  366. // locations where there is no enclosing try statement.
  367. this.tryEntries = [{ tryLoc: "root" }];
  368. tryLocsList.forEach(pushTryEntry, this);
  369. this.reset(true);
  370. }
  371. runtime.keys = function(object) {
  372. var keys = [];
  373. for (var key in object) {
  374. keys.push(key);
  375. }
  376. keys.reverse();
  377. // Rather than returning an object with a next method, we keep
  378. // things simple and return the next function itself.
  379. return function next() {
  380. while (keys.length) {
  381. var key = keys.pop();
  382. if (key in object) {
  383. next.value = key;
  384. next.done = false;
  385. return next;
  386. }
  387. }
  388. // To avoid creating an additional object, we just hang the .value
  389. // and .done properties off the next function object itself. This
  390. // also ensures that the minifier will not anonymize the function.
  391. next.done = true;
  392. return next;
  393. };
  394. };
  395. function values(iterable) {
  396. if (iterable) {
  397. var iteratorMethod = iterable[iteratorSymbol];
  398. if (iteratorMethod) {
  399. return iteratorMethod.call(iterable);
  400. }
  401. if (typeof iterable.next === "function") {
  402. return iterable;
  403. }
  404. if (!isNaN(iterable.length)) {
  405. var i = -1, next = function next() {
  406. while (++i < iterable.length) {
  407. if (hasOwn.call(iterable, i)) {
  408. next.value = iterable[i];
  409. next.done = false;
  410. return next;
  411. }
  412. }
  413. next.value = undefined;
  414. next.done = true;
  415. return next;
  416. };
  417. return next.next = next;
  418. }
  419. }
  420. // Return an iterator with no values.
  421. return { next: doneResult };
  422. }
  423. runtime.values = values;
  424. function doneResult() {
  425. return { value: undefined, done: true };
  426. }
  427. Context.prototype = {
  428. constructor: Context,
  429. reset: function(skipTempReset) {
  430. this.prev = 0;
  431. this.next = 0;
  432. // Resetting context._sent for legacy support of Babel's
  433. // function.sent implementation.
  434. this.sent = this._sent = undefined;
  435. this.done = false;
  436. this.delegate = null;
  437. this.tryEntries.forEach(resetTryEntry);
  438. if (!skipTempReset) {
  439. for (var name in this) {
  440. // Not sure about the optimal order of these conditions:
  441. if (name.charAt(0) === "t" &&
  442. hasOwn.call(this, name) &&
  443. !isNaN(+name.slice(1))) {
  444. this[name] = undefined;
  445. }
  446. }
  447. }
  448. },
  449. stop: function() {
  450. this.done = true;
  451. var rootEntry = this.tryEntries[0];
  452. var rootRecord = rootEntry.completion;
  453. if (rootRecord.type === "throw") {
  454. throw rootRecord.arg;
  455. }
  456. return this.rval;
  457. },
  458. dispatchException: function(exception) {
  459. if (this.done) {
  460. throw exception;
  461. }
  462. var context = this;
  463. function handle(loc, caught) {
  464. record.type = "throw";
  465. record.arg = exception;
  466. context.next = loc;
  467. return !!caught;
  468. }
  469. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  470. var entry = this.tryEntries[i];
  471. var record = entry.completion;
  472. if (entry.tryLoc === "root") {
  473. // Exception thrown outside of any try block that could handle
  474. // it, so set the completion value of the entire function to
  475. // throw the exception.
  476. return handle("end");
  477. }
  478. if (entry.tryLoc <= this.prev) {
  479. var hasCatch = hasOwn.call(entry, "catchLoc");
  480. var hasFinally = hasOwn.call(entry, "finallyLoc");
  481. if (hasCatch && hasFinally) {
  482. if (this.prev < entry.catchLoc) {
  483. return handle(entry.catchLoc, true);
  484. } else if (this.prev < entry.finallyLoc) {
  485. return handle(entry.finallyLoc);
  486. }
  487. } else if (hasCatch) {
  488. if (this.prev < entry.catchLoc) {
  489. return handle(entry.catchLoc, true);
  490. }
  491. } else if (hasFinally) {
  492. if (this.prev < entry.finallyLoc) {
  493. return handle(entry.finallyLoc);
  494. }
  495. } else {
  496. throw new Error("try statement without catch or finally");
  497. }
  498. }
  499. }
  500. },
  501. abrupt: function(type, arg) {
  502. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  503. var entry = this.tryEntries[i];
  504. if (entry.tryLoc <= this.prev &&
  505. hasOwn.call(entry, "finallyLoc") &&
  506. this.prev < entry.finallyLoc) {
  507. var finallyEntry = entry;
  508. break;
  509. }
  510. }
  511. if (finallyEntry &&
  512. (type === "break" ||
  513. type === "continue") &&
  514. finallyEntry.tryLoc <= arg &&
  515. arg <= finallyEntry.finallyLoc) {
  516. // Ignore the finally entry if control is not jumping to a
  517. // location outside the try/catch block.
  518. finallyEntry = null;
  519. }
  520. var record = finallyEntry ? finallyEntry.completion : {};
  521. record.type = type;
  522. record.arg = arg;
  523. if (finallyEntry) {
  524. this.next = finallyEntry.finallyLoc;
  525. } else {
  526. this.complete(record);
  527. }
  528. return ContinueSentinel;
  529. },
  530. complete: function(record, afterLoc) {
  531. if (record.type === "throw") {
  532. throw record.arg;
  533. }
  534. if (record.type === "break" ||
  535. record.type === "continue") {
  536. this.next = record.arg;
  537. } else if (record.type === "return") {
  538. this.rval = record.arg;
  539. this.next = "end";
  540. } else if (record.type === "normal" && afterLoc) {
  541. this.next = afterLoc;
  542. }
  543. },
  544. finish: function(finallyLoc) {
  545. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  546. var entry = this.tryEntries[i];
  547. if (entry.finallyLoc === finallyLoc) {
  548. this.complete(entry.completion, entry.afterLoc);
  549. resetTryEntry(entry);
  550. return ContinueSentinel;
  551. }
  552. }
  553. },
  554. "catch": function(tryLoc) {
  555. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  556. var entry = this.tryEntries[i];
  557. if (entry.tryLoc === tryLoc) {
  558. var record = entry.completion;
  559. if (record.type === "throw") {
  560. var thrown = record.arg;
  561. resetTryEntry(entry);
  562. }
  563. return thrown;
  564. }
  565. }
  566. // The context.catch method must only be called with a location
  567. // argument that corresponds to a known catch block.
  568. throw new Error("illegal catch attempt");
  569. },
  570. delegateYield: function(iterable, resultName, nextLoc) {
  571. this.delegate = {
  572. iterator: values(iterable),
  573. resultName: resultName,
  574. nextLoc: nextLoc
  575. };
  576. return ContinueSentinel;
  577. }
  578. };
  579. })(
  580. // Among the various tricks for obtaining a reference to the global
  581. // object, this seems to be the most reliable technique that does not
  582. // use indirect eval (which violates Content Security Policy).
  583. typeof global === "object" ? global :
  584. typeof window === "object" ? window :
  585. typeof self === "object" ? self : this
  586. );
  587. return module.exports; })({exports:{}});