runtime.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. var regeneratorRuntime = (function (exports) {
  8. "use strict";
  9. var Op = Object.prototype;
  10. var hasOwn = Op.hasOwnProperty;
  11. var undefined; // More compressible than void 0.
  12. var $Symbol = typeof Symbol === "function" ? Symbol : {};
  13. var iteratorSymbol = $Symbol.iterator || "@@iterator";
  14. var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
  15. var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  16. function wrap(innerFn, outerFn, self, tryLocsList) {
  17. // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
  18. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
  19. var generator = Object.create(protoGenerator.prototype);
  20. var context = new Context(tryLocsList || []);
  21. // The ._invoke method unifies the implementations of the .next,
  22. // .throw, and .return methods.
  23. generator._invoke = makeInvokeMethod(innerFn, self, context);
  24. return generator;
  25. }
  26. exports.wrap = wrap;
  27. function tryCatch(fn, obj, arg) {
  28. try {
  29. return { type: "normal", arg: fn.call(obj, arg) };
  30. } catch (err) {
  31. return { type: "throw", arg: err };
  32. }
  33. }
  34. var GenStateSuspendedStart = "suspendedStart";
  35. var GenStateSuspendedYield = "suspendedYield";
  36. var GenStateExecuting = "executing";
  37. var GenStateCompleted = "completed";
  38. var ContinueSentinel = {};
  39. function Generator() {}
  40. function GeneratorFunction() {}
  41. function GeneratorFunctionPrototype() {}
  42. // This is a polyfill for %IteratorPrototype% for environments that
  43. // don't natively support it.
  44. var IteratorPrototype = {};
  45. IteratorPrototype[iteratorSymbol] = function () {
  46. return this;
  47. };
  48. var getProto = Object.getPrototypeOf;
  49. var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  50. if (NativeIteratorPrototype &&
  51. NativeIteratorPrototype !== Op &&
  52. hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
  53. // This environment has a native %IteratorPrototype%; use it instead
  54. // of the polyfill.
  55. IteratorPrototype = NativeIteratorPrototype;
  56. }
  57. var Gp = GeneratorFunctionPrototype.prototype =
  58. Generator.prototype = Object.create(IteratorPrototype);
  59. GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
  60. GeneratorFunctionPrototype.constructor = GeneratorFunction;
  61. GeneratorFunctionPrototype[toStringTagSymbol] =
  62. GeneratorFunction.displayName = "GeneratorFunction";
  63. // Helper for defining the .next, .throw, and .return methods of the
  64. // Iterator interface in terms of a single ._invoke method.
  65. function defineIteratorMethods(prototype) {
  66. ["next", "throw", "return"].forEach(function(method) {
  67. prototype[method] = function(arg) {
  68. return this._invoke(method, arg);
  69. };
  70. });
  71. }
  72. exports.isGeneratorFunction = function(genFun) {
  73. var ctor = typeof genFun === "function" && genFun.constructor;
  74. return ctor
  75. ? ctor === GeneratorFunction ||
  76. // For the native GeneratorFunction constructor, the best we can
  77. // do is to check its .name property.
  78. (ctor.displayName || ctor.name) === "GeneratorFunction"
  79. : false;
  80. };
  81. exports.mark = function(genFun) {
  82. if (Object.setPrototypeOf) {
  83. Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
  84. } else {
  85. genFun.__proto__ = GeneratorFunctionPrototype;
  86. if (!(toStringTagSymbol in genFun)) {
  87. genFun[toStringTagSymbol] = "GeneratorFunction";
  88. }
  89. }
  90. genFun.prototype = Object.create(Gp);
  91. return genFun;
  92. };
  93. // Within the body of any async function, `await x` is transformed to
  94. // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
  95. // `hasOwn.call(value, "__await")` to determine if the yielded value is
  96. // meant to be awaited.
  97. exports.awrap = function(arg) {
  98. return { __await: arg };
  99. };
  100. function AsyncIterator(generator) {
  101. function invoke(method, arg, resolve, reject) {
  102. var record = tryCatch(generator[method], generator, arg);
  103. if (record.type === "throw") {
  104. reject(record.arg);
  105. } else {
  106. var result = record.arg;
  107. var value = result.value;
  108. if (value &&
  109. typeof value === "object" &&
  110. hasOwn.call(value, "__await")) {
  111. return Promise.resolve(value.__await).then(function(value) {
  112. invoke("next", value, resolve, reject);
  113. }, function(err) {
  114. invoke("throw", err, resolve, reject);
  115. });
  116. }
  117. return Promise.resolve(value).then(function(unwrapped) {
  118. // When a yielded Promise is resolved, its final value becomes
  119. // the .value of the Promise<{value,done}> result for the
  120. // current iteration.
  121. result.value = unwrapped;
  122. resolve(result);
  123. }, function(error) {
  124. // If a rejected Promise was yielded, throw the rejection back
  125. // into the async generator function so it can be handled there.
  126. return invoke("throw", error, resolve, reject);
  127. });
  128. }
  129. }
  130. var previousPromise;
  131. function enqueue(method, arg) {
  132. function callInvokeWithMethodAndArg() {
  133. return new Promise(function(resolve, reject) {
  134. invoke(method, arg, resolve, reject);
  135. });
  136. }
  137. return previousPromise =
  138. previousPromise ? previousPromise.then(
  139. callInvokeWithMethodAndArg,
  140. callInvokeWithMethodAndArg
  141. ) : callInvokeWithMethodAndArg();
  142. }
  143. this._invoke = enqueue;
  144. }
  145. defineIteratorMethods(AsyncIterator.prototype);
  146. AsyncIterator.prototype[asyncIteratorSymbol] = function () {
  147. return this;
  148. };
  149. exports.AsyncIterator = AsyncIterator;
  150. // Note that simple async functions are implemented on top of
  151. // AsyncIterator objects; they just return a Promise for the value of
  152. // the final result produced by the iterator.
  153. exports.async = function(innerFn, outerFn, self, tryLocsList) {
  154. var iter = new AsyncIterator(
  155. wrap(innerFn, outerFn, self, tryLocsList)
  156. );
  157. return exports.isGeneratorFunction(outerFn)
  158. ? iter // If outerFn is a generator, return the full iterator.
  159. : iter.next().then(function(result) {
  160. return result.done ? result.value : iter.next();
  161. });
  162. };
  163. function makeInvokeMethod(innerFn, self, context) {
  164. var state = GenStateSuspendedStart;
  165. return function invoke(method, arg) {
  166. if (state === GenStateExecuting) {
  167. throw new Error("Generator is already running");
  168. }
  169. if (state === GenStateCompleted) {
  170. if (method === "throw") {
  171. throw arg;
  172. }
  173. // Be forgiving, per 25.3.3.3.3 of the spec:
  174. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  175. return doneResult();
  176. }
  177. context.method = method;
  178. context.arg = arg;
  179. while (true) {
  180. var delegate = context.delegate;
  181. if (delegate) {
  182. var delegateResult = maybeInvokeDelegate(delegate, context);
  183. if (delegateResult) {
  184. if (delegateResult === ContinueSentinel) continue;
  185. return delegateResult;
  186. }
  187. }
  188. if (context.method === "next") {
  189. // Setting context._sent for legacy support of Babel's
  190. // function.sent implementation.
  191. context.sent = context._sent = context.arg;
  192. } else if (context.method === "throw") {
  193. if (state === GenStateSuspendedStart) {
  194. state = GenStateCompleted;
  195. throw context.arg;
  196. }
  197. context.dispatchException(context.arg);
  198. } else if (context.method === "return") {
  199. context.abrupt("return", context.arg);
  200. }
  201. state = GenStateExecuting;
  202. var record = tryCatch(innerFn, self, context);
  203. if (record.type === "normal") {
  204. // If an exception is thrown from innerFn, we leave state ===
  205. // GenStateExecuting and loop back for another invocation.
  206. state = context.done
  207. ? GenStateCompleted
  208. : GenStateSuspendedYield;
  209. if (record.arg === ContinueSentinel) {
  210. continue;
  211. }
  212. return {
  213. value: record.arg,
  214. done: context.done
  215. };
  216. } else if (record.type === "throw") {
  217. state = GenStateCompleted;
  218. context.method = "throw";
  219. context.arg = record.arg;
  220. }
  221. }
  222. };
  223. }
  224. function maybeInvokeDelegate(delegate, context) {
  225. var method = delegate.iterator[context.method];
  226. if (method === undefined) {
  227. context.delegate = null;
  228. if (context.method === "throw") {
  229. if (delegate.iterator.return) {
  230. context.method = "return";
  231. context.arg = undefined;
  232. maybeInvokeDelegate(delegate, context);
  233. if (context.method === "throw") {
  234. return ContinueSentinel;
  235. }
  236. }
  237. context.method = "throw";
  238. context.arg = new TypeError(
  239. "The iterator does not provide a 'throw' method");
  240. }
  241. return ContinueSentinel;
  242. }
  243. var record = tryCatch(method, delegate.iterator, context.arg);
  244. if (record.type === "throw") {
  245. context.method = "throw";
  246. context.arg = record.arg;
  247. context.delegate = null;
  248. return ContinueSentinel;
  249. }
  250. var info = record.arg;
  251. if (! info) {
  252. context.method = "throw";
  253. context.arg = new TypeError("iterator result is not an object");
  254. context.delegate = null;
  255. return ContinueSentinel;
  256. }
  257. if (info.done) {
  258. context[delegate.resultName] = info.value;
  259. context.next = delegate.nextLoc;
  260. if (context.method !== "return") {
  261. context.method = "next";
  262. context.arg = undefined;
  263. }
  264. } else {
  265. // Re-yield the result returned by the delegate method.
  266. return info;
  267. }
  268. // The delegate iterator is finished, so forget it and continue with
  269. // the outer generator.
  270. context.delegate = null;
  271. return ContinueSentinel;
  272. }
  273. // Define Generator.prototype.{next,throw,return} in terms of the
  274. // unified ._invoke helper method.
  275. defineIteratorMethods(Gp);
  276. Gp[toStringTagSymbol] = "Generator";
  277. Gp[iteratorSymbol] = function() {
  278. return this;
  279. };
  280. Gp.toString = function() {
  281. return "[object Generator]";
  282. };
  283. function pushTryEntry(locs) {
  284. var entry = { tryLoc: locs[0] };
  285. if (1 in locs) {
  286. entry.catchLoc = locs[1];
  287. }
  288. if (2 in locs) {
  289. entry.finallyLoc = locs[2];
  290. entry.afterLoc = locs[3];
  291. }
  292. this.tryEntries.push(entry);
  293. }
  294. function resetTryEntry(entry) {
  295. var record = entry.completion || {};
  296. record.type = "normal";
  297. delete record.arg;
  298. entry.completion = record;
  299. }
  300. function Context(tryLocsList) {
  301. this.tryEntries = [{ tryLoc: "root" }];
  302. tryLocsList.forEach(pushTryEntry, this);
  303. this.reset(true);
  304. }
  305. exports.keys = function(object) {
  306. var keys = [];
  307. for (var key in object) {
  308. keys.push(key);
  309. }
  310. keys.reverse();
  311. return function next() {
  312. while (keys.length) {
  313. var key = keys.pop();
  314. if (key in object) {
  315. next.value = key;
  316. next.done = false;
  317. return next;
  318. }
  319. }
  320. next.done = true;
  321. return next;
  322. };
  323. };
  324. function values(iterable) {
  325. if (iterable) {
  326. var iteratorMethod = iterable[iteratorSymbol];
  327. if (iteratorMethod) {
  328. return iteratorMethod.call(iterable);
  329. }
  330. if (typeof iterable.next === "function") {
  331. return iterable;
  332. }
  333. if (!isNaN(iterable.length)) {
  334. var i = -1, next = function next() {
  335. while (++i < iterable.length) {
  336. if (hasOwn.call(iterable, i)) {
  337. next.value = iterable[i];
  338. next.done = false;
  339. return next;
  340. }
  341. }
  342. next.value = undefined;
  343. next.done = true;
  344. return next;
  345. };
  346. return next.next = next;
  347. }
  348. }
  349. // Return an iterator with no values.
  350. return { next: doneResult };
  351. }
  352. exports.values = values;
  353. function doneResult() {
  354. return { value: undefined, done: true };
  355. }
  356. Context.prototype = {
  357. constructor: Context,
  358. reset: function(skipTempReset) {
  359. this.prev = 0;
  360. this.next = 0;
  361. this.sent = this._sent = undefined;
  362. this.done = false;
  363. this.delegate = null;
  364. this.method = "next";
  365. this.arg = undefined;
  366. this.tryEntries.forEach(resetTryEntry);
  367. if (!skipTempReset) {
  368. for (var name in this) {
  369. // Not sure about the optimal order of these conditions:
  370. if (name.charAt(0) === "t" &&
  371. hasOwn.call(this, name) &&
  372. !isNaN(+name.slice(1))) {
  373. this[name] = undefined;
  374. }
  375. }
  376. }
  377. },
  378. stop: function() {
  379. this.done = true;
  380. var rootEntry = this.tryEntries[0];
  381. var rootRecord = rootEntry.completion;
  382. if (rootRecord.type === "throw") {
  383. throw rootRecord.arg;
  384. }
  385. return this.rval;
  386. },
  387. dispatchException: function(exception) {
  388. if (this.done) {
  389. throw exception;
  390. }
  391. var context = this;
  392. function handle(loc, caught) {
  393. record.type = "throw";
  394. record.arg = exception;
  395. context.next = loc;
  396. if (caught) {
  397. context.method = "next";
  398. context.arg = undefined;
  399. }
  400. return !! caught;
  401. }
  402. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  403. var entry = this.tryEntries[i];
  404. var record = entry.completion;
  405. if (entry.tryLoc === "root") {
  406. return handle("end");
  407. }
  408. if (entry.tryLoc <= this.prev) {
  409. var hasCatch = hasOwn.call(entry, "catchLoc");
  410. var hasFinally = hasOwn.call(entry, "finallyLoc");
  411. if (hasCatch && hasFinally) {
  412. if (this.prev < entry.catchLoc) {
  413. return handle(entry.catchLoc, true);
  414. } else if (this.prev < entry.finallyLoc) {
  415. return handle(entry.finallyLoc);
  416. }
  417. } else if (hasCatch) {
  418. if (this.prev < entry.catchLoc) {
  419. return handle(entry.catchLoc, true);
  420. }
  421. } else if (hasFinally) {
  422. if (this.prev < entry.finallyLoc) {
  423. return handle(entry.finallyLoc);
  424. }
  425. } else {
  426. throw new Error("try statement without catch or finally");
  427. }
  428. }
  429. }
  430. },
  431. abrupt: function(type, arg) {
  432. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  433. var entry = this.tryEntries[i];
  434. if (entry.tryLoc <= this.prev &&
  435. hasOwn.call(entry, "finallyLoc") &&
  436. this.prev < entry.finallyLoc) {
  437. var finallyEntry = entry;
  438. break;
  439. }
  440. }
  441. if (finallyEntry &&
  442. (type === "break" ||
  443. type === "continue") &&
  444. finallyEntry.tryLoc <= arg &&
  445. arg <= finallyEntry.finallyLoc) {
  446. finallyEntry = null;
  447. }
  448. var record = finallyEntry ? finallyEntry.completion : {};
  449. record.type = type;
  450. record.arg = arg;
  451. if (finallyEntry) {
  452. this.method = "next";
  453. this.next = finallyEntry.finallyLoc;
  454. return ContinueSentinel;
  455. }
  456. return this.complete(record);
  457. },
  458. complete: function(record, afterLoc) {
  459. if (record.type === "throw") {
  460. throw record.arg;
  461. }
  462. if (record.type === "break" ||
  463. record.type === "continue") {
  464. this.next = record.arg;
  465. } else if (record.type === "return") {
  466. this.rval = this.arg = record.arg;
  467. this.method = "return";
  468. this.next = "end";
  469. } else if (record.type === "normal" && afterLoc) {
  470. this.next = afterLoc;
  471. }
  472. return ContinueSentinel;
  473. },
  474. finish: function(finallyLoc) {
  475. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  476. var entry = this.tryEntries[i];
  477. if (entry.finallyLoc === finallyLoc) {
  478. this.complete(entry.completion, entry.afterLoc);
  479. resetTryEntry(entry);
  480. return ContinueSentinel;
  481. }
  482. }
  483. },
  484. "catch": function(tryLoc) {
  485. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  486. var entry = this.tryEntries[i];
  487. if (entry.tryLoc === tryLoc) {
  488. var record = entry.completion;
  489. if (record.type === "throw") {
  490. var thrown = record.arg;
  491. resetTryEntry(entry);
  492. }
  493. return thrown;
  494. }
  495. }
  496. throw new Error("illegal catch attempt");
  497. },
  498. delegateYield: function(iterable, resultName, nextLoc) {
  499. this.delegate = {
  500. iterator: values(iterable),
  501. resultName: resultName,
  502. nextLoc: nextLoc
  503. };
  504. if (this.method === "next") {
  505. // Deliberately forget the last sent value so that we don't
  506. // accidentally pass it on to the delegate.
  507. this.arg = undefined;
  508. }
  509. return ContinueSentinel;
  510. }
  511. };
  512. return exports;
  513. }(
  514. typeof module === "object" ? module.exports : {}
  515. ));