runtime.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. * Copyright (c) 2014, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  7. * additional grant of patent rights can be found in the PATENTS file in
  8. * the same directory.
  9. */
  10. !(function(global) {
  11. "use strict";
  12. var hasOwn = Object.prototype.hasOwnProperty;
  13. var undefined; // More compressible than void 0.
  14. var iteratorSymbol =
  15. typeof Symbol === "function" && Symbol.iterator || "@@iterator";
  16. var inModule = typeof module === "object";
  17. var runtime = global.regeneratorRuntime;
  18. if (runtime) {
  19. if (inModule) {
  20. // If regeneratorRuntime is defined globally and we're in a module,
  21. // make the exports object identical to regeneratorRuntime.
  22. module.exports = runtime;
  23. }
  24. // Don't bother evaluating the rest of this file if the runtime was
  25. // already defined globally.
  26. return;
  27. }
  28. // Define the runtime globally (as expected by generated code) as either
  29. // module.exports (if we're in a module) or a new, empty object.
  30. runtime = global.regeneratorRuntime = inModule ? module.exports : {};
  31. function wrap(innerFn, outerFn, self, tryLocsList) {
  32. return new Generator(innerFn, outerFn, self || null, tryLocsList || []);
  33. }
  34. runtime.wrap = wrap;
  35. // Try/catch helper to minimize deoptimizations. Returns a completion
  36. // record like context.tryEntries[i].completion. This interface could
  37. // have been (and was previously) designed to take a closure to be
  38. // invoked without arguments, but in all the cases we care about we
  39. // already have an existing method we want to call, so there's no need
  40. // to create a new function object. We can even get away with assuming
  41. // the method takes exactly one argument, since that happens to be true
  42. // in every case, so we don't have to touch the arguments object. The
  43. // only additional allocation required is the completion record, which
  44. // has a stable shape and so hopefully should be cheap to allocate.
  45. function tryCatch(fn, obj, arg) {
  46. try {
  47. return { type: "normal", arg: fn.call(obj, arg) };
  48. } catch (err) {
  49. return { type: "throw", arg: err };
  50. }
  51. }
  52. var GenStateSuspendedStart = "suspendedStart";
  53. var GenStateSuspendedYield = "suspendedYield";
  54. var GenStateExecuting = "executing";
  55. var GenStateCompleted = "completed";
  56. // Returning this object from the innerFn has the same effect as
  57. // breaking out of the dispatch switch statement.
  58. var ContinueSentinel = {};
  59. // Dummy constructor functions that we use as the .constructor and
  60. // .constructor.prototype properties for functions that return Generator
  61. // objects. For full spec compliance, you may wish to configure your
  62. // minifier not to mangle the names of these two functions.
  63. function GeneratorFunction() {}
  64. function GeneratorFunctionPrototype() {}
  65. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype;
  66. GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
  67. GeneratorFunctionPrototype.constructor = GeneratorFunction;
  68. GeneratorFunction.displayName = "GeneratorFunction";
  69. runtime.isGeneratorFunction = function(genFun) {
  70. var ctor = typeof genFun === "function" && genFun.constructor;
  71. return ctor
  72. ? ctor === GeneratorFunction ||
  73. // For the native GeneratorFunction constructor, the best we can
  74. // do is to check its .name property.
  75. (ctor.displayName || ctor.name) === "GeneratorFunction"
  76. : false;
  77. };
  78. runtime.mark = function(genFun) {
  79. genFun.__proto__ = GeneratorFunctionPrototype;
  80. genFun.prototype = Object.create(Gp);
  81. return genFun;
  82. };
  83. runtime.async = function(innerFn, outerFn, self, tryLocsList) {
  84. return new Promise(function(resolve, reject) {
  85. var generator = wrap(innerFn, outerFn, self, tryLocsList);
  86. var callNext = step.bind(generator.next);
  87. var callThrow = step.bind(generator["throw"]);
  88. function step(arg) {
  89. var record = tryCatch(this, null, arg);
  90. if (record.type === "throw") {
  91. reject(record.arg);
  92. return;
  93. }
  94. var info = record.arg;
  95. if (info.done) {
  96. resolve(info.value);
  97. } else {
  98. Promise.resolve(info.value).then(callNext, callThrow);
  99. }
  100. }
  101. callNext();
  102. });
  103. };
  104. function Generator(innerFn, outerFn, self, tryLocsList) {
  105. var generator = outerFn ? Object.create(outerFn.prototype) : this;
  106. var context = new Context(tryLocsList);
  107. var state = GenStateSuspendedStart;
  108. function invoke(method, arg) {
  109. if (state === GenStateExecuting) {
  110. throw new Error("Generator is already running");
  111. }
  112. if (state === GenStateCompleted) {
  113. // Be forgiving, per 25.3.3.3.3 of the spec:
  114. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  115. return doneResult();
  116. }
  117. while (true) {
  118. var delegate = context.delegate;
  119. if (delegate) {
  120. var record = tryCatch(
  121. delegate.iterator[method],
  122. delegate.iterator,
  123. arg
  124. );
  125. if (record.type === "throw") {
  126. context.delegate = null;
  127. // Like returning generator.throw(uncaught), but without the
  128. // overhead of an extra function call.
  129. method = "throw";
  130. arg = record.arg;
  131. continue;
  132. }
  133. // Delegate generator ran and handled its own exceptions so
  134. // regardless of what the method was, we continue as if it is
  135. // "next" with an undefined arg.
  136. method = "next";
  137. arg = undefined;
  138. var info = record.arg;
  139. if (info.done) {
  140. context[delegate.resultName] = info.value;
  141. context.next = delegate.nextLoc;
  142. } else {
  143. state = GenStateSuspendedYield;
  144. return info;
  145. }
  146. context.delegate = null;
  147. }
  148. if (method === "next") {
  149. if (state === GenStateSuspendedStart &&
  150. typeof arg !== "undefined") {
  151. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
  152. throw new TypeError(
  153. "attempt to send " + JSON.stringify(arg) + " to newborn generator"
  154. );
  155. }
  156. if (state === GenStateSuspendedYield) {
  157. context.sent = arg;
  158. } else {
  159. delete context.sent;
  160. }
  161. } else if (method === "throw") {
  162. if (state === GenStateSuspendedStart) {
  163. state = GenStateCompleted;
  164. throw arg;
  165. }
  166. if (context.dispatchException(arg)) {
  167. // If the dispatched exception was caught by a catch block,
  168. // then let that catch block handle the exception normally.
  169. method = "next";
  170. arg = undefined;
  171. }
  172. } else if (method === "return") {
  173. context.abrupt("return", arg);
  174. }
  175. state = GenStateExecuting;
  176. var record = tryCatch(innerFn, self, context);
  177. if (record.type === "normal") {
  178. // If an exception is thrown from innerFn, we leave state ===
  179. // GenStateExecuting and loop back for another invocation.
  180. state = context.done
  181. ? GenStateCompleted
  182. : GenStateSuspendedYield;
  183. var info = {
  184. value: record.arg,
  185. done: context.done
  186. };
  187. if (record.arg === ContinueSentinel) {
  188. if (context.delegate && method === "next") {
  189. // Deliberately forget the last sent value so that we don't
  190. // accidentally pass it on to the delegate.
  191. arg = undefined;
  192. }
  193. } else {
  194. return info;
  195. }
  196. } else if (record.type === "throw") {
  197. state = GenStateCompleted;
  198. if (method === "next") {
  199. context.dispatchException(record.arg);
  200. } else {
  201. arg = record.arg;
  202. }
  203. }
  204. }
  205. }
  206. generator.next = invoke.bind(generator, "next");
  207. generator["throw"] = invoke.bind(generator, "throw");
  208. generator["return"] = invoke.bind(generator, "return");
  209. return generator;
  210. }
  211. Gp[iteratorSymbol] = function() {
  212. return this;
  213. };
  214. Gp.toString = function() {
  215. return "[object Generator]";
  216. };
  217. function pushTryEntry(locs) {
  218. var entry = { tryLoc: locs[0] };
  219. if (1 in locs) {
  220. entry.catchLoc = locs[1];
  221. }
  222. if (2 in locs) {
  223. entry.finallyLoc = locs[2];
  224. entry.afterLoc = locs[3];
  225. }
  226. this.tryEntries.push(entry);
  227. }
  228. function resetTryEntry(entry) {
  229. var record = entry.completion || {};
  230. record.type = "normal";
  231. delete record.arg;
  232. entry.completion = record;
  233. }
  234. function Context(tryLocsList) {
  235. // The root entry object (effectively a try statement without a catch
  236. // or a finally block) gives us a place to store values thrown from
  237. // locations where there is no enclosing try statement.
  238. this.tryEntries = [{ tryLoc: "root" }];
  239. tryLocsList.forEach(pushTryEntry, this);
  240. this.reset();
  241. }
  242. runtime.keys = function(object) {
  243. var keys = [];
  244. for (var key in object) {
  245. keys.push(key);
  246. }
  247. keys.reverse();
  248. // Rather than returning an object with a next method, we keep
  249. // things simple and return the next function itself.
  250. return function next() {
  251. while (keys.length) {
  252. var key = keys.pop();
  253. if (key in object) {
  254. next.value = key;
  255. next.done = false;
  256. return next;
  257. }
  258. }
  259. // To avoid creating an additional object, we just hang the .value
  260. // and .done properties off the next function object itself. This
  261. // also ensures that the minifier will not anonymize the function.
  262. next.done = true;
  263. return next;
  264. };
  265. };
  266. function values(iterable) {
  267. if (iterable) {
  268. var iteratorMethod = iterable[iteratorSymbol];
  269. if (iteratorMethod) {
  270. return iteratorMethod.call(iterable);
  271. }
  272. if (typeof iterable.next === "function") {
  273. return iterable;
  274. }
  275. if (!isNaN(iterable.length)) {
  276. var i = -1, next = function next() {
  277. while (++i < iterable.length) {
  278. if (hasOwn.call(iterable, i)) {
  279. next.value = iterable[i];
  280. next.done = false;
  281. return next;
  282. }
  283. }
  284. next.value = undefined;
  285. next.done = true;
  286. return next;
  287. };
  288. return next.next = next;
  289. }
  290. }
  291. // Return an iterator with no values.
  292. return { next: doneResult };
  293. }
  294. runtime.values = values;
  295. function doneResult() {
  296. return { value: undefined, done: true };
  297. }
  298. Context.prototype = {
  299. constructor: Context,
  300. reset: function() {
  301. this.prev = 0;
  302. this.next = 0;
  303. this.sent = undefined;
  304. this.done = false;
  305. this.delegate = null;
  306. this.tryEntries.forEach(resetTryEntry);
  307. // Pre-initialize at least 20 temporary variables to enable hidden
  308. // class optimizations for simple generators.
  309. for (var tempIndex = 0, tempName;
  310. hasOwn.call(this, tempName = "t" + tempIndex) || tempIndex < 20;
  311. ++tempIndex) {
  312. this[tempName] = null;
  313. }
  314. },
  315. stop: function() {
  316. this.done = true;
  317. var rootEntry = this.tryEntries[0];
  318. var rootRecord = rootEntry.completion;
  319. if (rootRecord.type === "throw") {
  320. throw rootRecord.arg;
  321. }
  322. return this.rval;
  323. },
  324. dispatchException: function(exception) {
  325. if (this.done) {
  326. throw exception;
  327. }
  328. var context = this;
  329. function handle(loc, caught) {
  330. record.type = "throw";
  331. record.arg = exception;
  332. context.next = loc;
  333. return !!caught;
  334. }
  335. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  336. var entry = this.tryEntries[i];
  337. var record = entry.completion;
  338. if (entry.tryLoc === "root") {
  339. // Exception thrown outside of any try block that could handle
  340. // it, so set the completion value of the entire function to
  341. // throw the exception.
  342. return handle("end");
  343. }
  344. if (entry.tryLoc <= this.prev) {
  345. var hasCatch = hasOwn.call(entry, "catchLoc");
  346. var hasFinally = hasOwn.call(entry, "finallyLoc");
  347. if (hasCatch && hasFinally) {
  348. if (this.prev < entry.catchLoc) {
  349. return handle(entry.catchLoc, true);
  350. } else if (this.prev < entry.finallyLoc) {
  351. return handle(entry.finallyLoc);
  352. }
  353. } else if (hasCatch) {
  354. if (this.prev < entry.catchLoc) {
  355. return handle(entry.catchLoc, true);
  356. }
  357. } else if (hasFinally) {
  358. if (this.prev < entry.finallyLoc) {
  359. return handle(entry.finallyLoc);
  360. }
  361. } else {
  362. throw new Error("try statement without catch or finally");
  363. }
  364. }
  365. }
  366. },
  367. _findFinallyEntry: function(finallyLoc) {
  368. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  369. var entry = this.tryEntries[i];
  370. if (entry.tryLoc <= this.prev &&
  371. hasOwn.call(entry, "finallyLoc") && (
  372. entry.finallyLoc === finallyLoc ||
  373. this.prev < entry.finallyLoc)) {
  374. return entry;
  375. }
  376. }
  377. },
  378. abrupt: function(type, arg) {
  379. var entry = this._findFinallyEntry();
  380. var record = entry ? entry.completion : {};
  381. record.type = type;
  382. record.arg = arg;
  383. if (entry) {
  384. this.next = entry.finallyLoc;
  385. } else {
  386. this.complete(record);
  387. }
  388. return ContinueSentinel;
  389. },
  390. complete: function(record, afterLoc) {
  391. if (record.type === "throw") {
  392. throw record.arg;
  393. }
  394. if (record.type === "break" ||
  395. record.type === "continue") {
  396. this.next = record.arg;
  397. } else if (record.type === "return") {
  398. this.rval = record.arg;
  399. this.next = "end";
  400. } else if (record.type === "normal" && afterLoc) {
  401. this.next = afterLoc;
  402. }
  403. return ContinueSentinel;
  404. },
  405. finish: function(finallyLoc) {
  406. var entry = this._findFinallyEntry(finallyLoc);
  407. return this.complete(entry.completion, entry.afterLoc);
  408. },
  409. "catch": function(tryLoc) {
  410. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  411. var entry = this.tryEntries[i];
  412. if (entry.tryLoc === tryLoc) {
  413. var record = entry.completion;
  414. if (record.type === "throw") {
  415. var thrown = record.arg;
  416. resetTryEntry(entry);
  417. }
  418. return thrown;
  419. }
  420. }
  421. // The context.catch method must only be called with a location
  422. // argument that corresponds to a known catch block.
  423. throw new Error("illegal catch attempt");
  424. },
  425. delegateYield: function(iterable, resultName, nextLoc) {
  426. this.delegate = {
  427. iterator: values(iterable),
  428. resultName: resultName,
  429. nextLoc: nextLoc
  430. };
  431. return ContinueSentinel;
  432. }
  433. };
  434. })(
  435. // Among the various tricks for obtaining a reference to the global
  436. // object, this seems to be the most reliable technique that does not
  437. // use indirect eval (which violates Content Security Policy).
  438. typeof global === "object" ? global :
  439. typeof window === "object" ? window : this
  440. );