task.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. /* eslint-disable spaced-comment */
  7. /* globals StopIteration */
  8. /**
  9. * This module implements a subset of "Task.js" <http://taskjs.org/>.
  10. * It is a copy of toolkit/modules/Task.jsm. Please try not to
  11. * diverge the API here.
  12. *
  13. * Paraphrasing from the Task.js site, tasks make sequential, asynchronous
  14. * operations simple, using the power of JavaScript's "yield" operator.
  15. *
  16. * Tasks are built upon generator functions and promises, documented here:
  17. *
  18. * <https://developer.mozilla.org/en/JavaScript/Guide/Iterators_and_Generators>
  19. * <http://wiki.commonjs.org/wiki/Promises/A>
  20. *
  21. * The "Task.spawn" function takes a generator function and starts running it as
  22. * a task. Every time the task yields a promise, it waits until the promise is
  23. * fulfilled. "Task.spawn" returns a promise that is resolved when the task
  24. * completes successfully, or is rejected if an exception occurs.
  25. *
  26. * -----------------------------------------------------------------------------
  27. *
  28. * const {Task} = require("devtools/shared/task");
  29. *
  30. * Task.spawn(function* () {
  31. *
  32. * // This is our task. Let's create a promise object, wait on it and capture
  33. * // its resolution value.
  34. * let myPromise = getPromiseResolvedOnTimeoutWithValue(1000, "Value");
  35. * let result = yield myPromise;
  36. *
  37. * // This part is executed only after the promise above is fulfilled (after
  38. * // one second, in this imaginary example). We can easily loop while
  39. * // calling asynchronous functions, and wait multiple times.
  40. * for (let i = 0; i < 3; i++) {
  41. * result += yield getPromiseResolvedOnTimeoutWithValue(50, "!");
  42. * }
  43. *
  44. * return "Resolution result for the task: " + result;
  45. * }).then(function (result) {
  46. *
  47. * // result == "Resolution result for the task: Value!!!"
  48. *
  49. * // The result is undefined if no value was returned.
  50. *
  51. * }, function (exception) {
  52. *
  53. * // Failure! We can inspect or report the exception.
  54. *
  55. * });
  56. *
  57. * -----------------------------------------------------------------------------
  58. *
  59. * This module implements only the "Task.js" interfaces described above, with no
  60. * additional features to control the task externally, or do custom scheduling.
  61. * It also provides the following extensions that simplify task usage in the
  62. * most common cases:
  63. *
  64. * - The "Task.spawn" function also accepts an iterator returned by a generator
  65. * function, in addition to a generator function. This way, you can call into
  66. * the generator function with the parameters you want, and with "this" bound
  67. * to the correct value. Also, "this" is never bound to the task object when
  68. * "Task.spawn" calls the generator function.
  69. *
  70. * - In addition to a promise object, a task can yield the iterator returned by
  71. * a generator function. The iterator is turned into a task automatically.
  72. * This reduces the syntax overhead of calling "Task.spawn" explicitly when
  73. * you want to recurse into other task functions.
  74. *
  75. * - The "Task.spawn" function also accepts a primitive value, or a function
  76. * returning a primitive value, and treats the value as the result of the
  77. * task. This makes it possible to call an externally provided function and
  78. * spawn a task from it, regardless of whether it is an asynchronous generator
  79. * or a synchronous function. This comes in handy when iterating over
  80. * function lists where some items have been converted to tasks and some not.
  81. */
  82. ////////////////////////////////////////////////////////////////////////////////
  83. //// Globals
  84. const Promise = require("promise");
  85. const defer = require("devtools/shared/defer");
  86. // The following error types are considered programmer errors, which should be
  87. // reported (possibly redundantly) so as to let programmers fix their code.
  88. const ERRORS_TO_REPORT = ["EvalError", "RangeError", "ReferenceError",
  89. "TypeError"];
  90. /**
  91. * The Task currently being executed
  92. */
  93. var gCurrentTask = null;
  94. /**
  95. * If `true`, capture stacks whenever entering a Task and rewrite the
  96. * stack any exception thrown through a Task.
  97. */
  98. var gMaintainStack = false;
  99. /**
  100. * Iterate through the lines of a string.
  101. *
  102. * @return Iterator<string>
  103. */
  104. function* linesOf(string) {
  105. let reLine = /([^\r\n])+/g;
  106. let match;
  107. while ((match = reLine.exec(string))) {
  108. yield [match[0], match.index];
  109. }
  110. }
  111. /**
  112. * Detect whether a value is a generator.
  113. *
  114. * @param aValue
  115. * The value to identify.
  116. * @return A boolean indicating whether the value is a generator.
  117. */
  118. function isGenerator(value) {
  119. return Object.prototype.toString.call(value) == "[object Generator]";
  120. }
  121. ////////////////////////////////////////////////////////////////////////////////
  122. //// Task
  123. /**
  124. * This object provides the public module functions.
  125. */
  126. var Task = {
  127. /**
  128. * Creates and starts a new task.
  129. *
  130. * @param task
  131. * - If you specify a generator function, it is called with no
  132. * arguments to retrieve the associated iterator. The generator
  133. * function is a task, that is can yield promise objects to wait
  134. * upon.
  135. * - If you specify the iterator returned by a generator function you
  136. * called, the generator function is also executed as a task. This
  137. * allows you to call the function with arguments.
  138. * - If you specify a function that is not a generator, it is called
  139. * with no arguments, and its return value is used to resolve the
  140. * returned promise.
  141. * - If you specify anything else, you get a promise that is already
  142. * resolved with the specified value.
  143. *
  144. * @return A promise object where you can register completion callbacks to be
  145. * called when the task terminates.
  146. */
  147. spawn: function (task) {
  148. return createAsyncFunction(task).call(undefined);
  149. },
  150. /**
  151. * Create and return an 'async function' that starts a new task.
  152. *
  153. * This is similar to 'spawn' except that it doesn't immediately start
  154. * the task, it binds the task to the async function's 'this' object and
  155. * arguments, and it requires the task to be a function.
  156. *
  157. * It simplifies the common pattern of implementing a method via a task,
  158. * like this simple object with a 'greet' method that has a 'name' parameter
  159. * and spawns a task to send a greeting and return its reply:
  160. *
  161. * let greeter = {
  162. * message: "Hello, NAME!",
  163. * greet: function(name) {
  164. * return Task.spawn((function* () {
  165. * return yield sendGreeting(this.message.replace(/NAME/, name));
  166. * }).bind(this);
  167. * })
  168. * };
  169. *
  170. * With Task.async, the method can be declared succinctly:
  171. *
  172. * let greeter = {
  173. * message: "Hello, NAME!",
  174. * greet: Task.async(function* (name) {
  175. * return yield sendGreeting(this.message.replace(/NAME/, name));
  176. * })
  177. * };
  178. *
  179. * While maintaining identical semantics:
  180. *
  181. * greeter.greet("Mitchell").then((reply) => { ... }); // behaves the same
  182. *
  183. * @param task
  184. * The task function to start.
  185. *
  186. * @return A function that starts the task function and returns its promise.
  187. */
  188. async: function (task) {
  189. if (typeof (task) != "function") {
  190. throw new TypeError("task argument must be a function");
  191. }
  192. return createAsyncFunction(task);
  193. },
  194. /**
  195. * Constructs a special exception that, when thrown inside a legacy generator
  196. * function (non-star generator), allows the associated task to be resolved
  197. * with a specific value.
  198. *
  199. * Example: throw new Task.Result("Value");
  200. */
  201. Result: function (value) {
  202. this.value = value;
  203. }
  204. };
  205. function createAsyncFunction(task) {
  206. let asyncFunction = function () {
  207. let result = task;
  208. if (task && typeof (task) == "function") {
  209. if (task.isAsyncFunction) {
  210. throw new TypeError(
  211. "Cannot use an async function in place of a promise. " +
  212. "You should either invoke the async function first " +
  213. "or use 'Task.spawn' instead of 'Task.async' to start " +
  214. "the Task and return its promise.");
  215. }
  216. try {
  217. // Let's call into the function ourselves.
  218. result = task.apply(this, arguments);
  219. } catch (ex) {
  220. if (ex instanceof Task.Result) {
  221. return Promise.resolve(ex.value);
  222. }
  223. return Promise.reject(ex);
  224. }
  225. }
  226. if (isGenerator(result)) {
  227. // This is an iterator resulting from calling a generator function.
  228. return new TaskImpl(result).deferred.promise;
  229. }
  230. // Just propagate the given value to the caller as a resolved promise.
  231. return Promise.resolve(result);
  232. };
  233. asyncFunction.isAsyncFunction = true;
  234. return asyncFunction;
  235. }
  236. ////////////////////////////////////////////////////////////////////////////////
  237. //// TaskImpl
  238. /**
  239. * Executes the specified iterator as a task, and gives access to the promise
  240. * that is fulfilled when the task terminates.
  241. */
  242. function TaskImpl(iterator) {
  243. if (gMaintainStack) {
  244. this._stack = (new Error()).stack;
  245. }
  246. this.deferred = defer();
  247. this._iterator = iterator;
  248. this._isStarGenerator = !("send" in iterator);
  249. this._run(true);
  250. }
  251. TaskImpl.prototype = {
  252. /**
  253. * Includes the promise object where task completion callbacks are registered,
  254. * and methods to resolve or reject the promise at task completion.
  255. */
  256. deferred: null,
  257. /**
  258. * The iterator returned by the generator function associated with this task.
  259. */
  260. _iterator: null,
  261. /**
  262. * Whether this Task is using a star generator.
  263. */
  264. _isStarGenerator: false,
  265. /**
  266. * Main execution routine, that calls into the generator function.
  267. *
  268. * @param sendResolved
  269. * If true, indicates that we should continue into the generator
  270. * function regularly (if we were waiting on a promise, it was
  271. * resolved). If true, indicates that we should cause an exception to
  272. * be thrown into the generator function (if we were waiting on a
  273. * promise, it was rejected).
  274. * @param sendValue
  275. * Resolution result or rejection exception, if any.
  276. */
  277. _run: function (sendResolved, sendValue) {
  278. try {
  279. gCurrentTask = this;
  280. if (this._isStarGenerator) {
  281. try {
  282. let result = sendResolved ? this._iterator.next(sendValue)
  283. : this._iterator.throw(sendValue);
  284. if (result.done) {
  285. // The generator function returned.
  286. this.deferred.resolve(result.value);
  287. } else {
  288. // The generator function yielded.
  289. this._handleResultValue(result.value);
  290. }
  291. } catch (ex) {
  292. // The generator function failed with an uncaught exception.
  293. this._handleException(ex);
  294. }
  295. } else {
  296. try {
  297. let yielded = sendResolved ? this._iterator.send(sendValue)
  298. : this._iterator.throw(sendValue);
  299. this._handleResultValue(yielded);
  300. } catch (ex) {
  301. if (ex instanceof Task.Result) {
  302. // The generator function threw the special exception that
  303. // allows it to return a specific value on resolution.
  304. this.deferred.resolve(ex.value);
  305. } else if (ex instanceof StopIteration) {
  306. // The generator function terminated with no specific result.
  307. this.deferred.resolve(undefined);
  308. } else {
  309. // The generator function failed with an uncaught exception.
  310. this._handleException(ex);
  311. }
  312. }
  313. }
  314. } finally {
  315. //
  316. // At this stage, the Task may have finished executing, or have
  317. // walked through a `yield` or passed control to a sub-Task.
  318. // Regardless, if we still own `gCurrentTask`, reset it. If we
  319. // have not finished execution of this Task, re-entering `_run`
  320. // will set `gCurrentTask` to `this` as needed.
  321. //
  322. // We just need to be careful here in case we hit the following
  323. // pattern:
  324. //
  325. // Task.spawn(foo);
  326. // Task.spawn(bar);
  327. //
  328. // Here, `foo` and `bar` may be interleaved, so when we finish
  329. // executing `foo`, `gCurrentTask` may actually either `foo` or
  330. // `bar`. If `gCurrentTask` has already been set to `bar`, leave
  331. // it be and it will be reset to `null` once `bar` is complete.
  332. //
  333. if (gCurrentTask == this) {
  334. gCurrentTask = null;
  335. }
  336. }
  337. },
  338. /**
  339. * Handle a value yielded by a generator.
  340. *
  341. * @param value
  342. * The yielded value to handle.
  343. */
  344. _handleResultValue: function (value) {
  345. // If our task yielded an iterator resulting from calling another
  346. // generator function, automatically spawn a task from it, effectively
  347. // turning it into a promise that is fulfilled on task completion.
  348. if (isGenerator(value)) {
  349. value = Task.spawn(value);
  350. }
  351. if (value && typeof (value.then) == "function") {
  352. // We have a promise object now. When fulfilled, call again into this
  353. // function to continue the task, with either a resolution or rejection
  354. // condition.
  355. value.then(this._run.bind(this, true),
  356. this._run.bind(this, false));
  357. } else {
  358. // If our task yielded a value that is not a promise, just continue and
  359. // pass it directly as the result of the yield statement.
  360. this._run(true, value);
  361. }
  362. },
  363. /**
  364. * Handle an uncaught exception thrown from a generator.
  365. *
  366. * @param exception
  367. * The uncaught exception to handle.
  368. */
  369. _handleException: function (exception) {
  370. gCurrentTask = this;
  371. if (exception && typeof exception == "object" && "stack" in exception) {
  372. let stack = exception.stack;
  373. if (gMaintainStack &&
  374. exception._capturedTaskStack != this._stack &&
  375. typeof stack == "string") {
  376. // Rewrite the stack for more readability.
  377. let bottomStack = this._stack;
  378. stack = Task.Debugging.generateReadableStack(stack);
  379. exception.stack = stack;
  380. // If exception is reinjected in the same task and rethrown,
  381. // we don't want to perform the rewrite again.
  382. exception._capturedTaskStack = bottomStack;
  383. } else if (!stack) {
  384. stack = "Not available";
  385. }
  386. if ("name" in exception &&
  387. ERRORS_TO_REPORT.indexOf(exception.name) != -1) {
  388. // We suspect that the exception is a programmer error, so we now
  389. // display it using dump(). Note that we do not use Cu.reportError as
  390. // we assume that this is a programming error, so we do not want end
  391. // users to see it. Also, if the programmer handles errors correctly,
  392. // they will either treat the error or log them somewhere.
  393. dump("*************************\n");
  394. dump("A coding exception was thrown and uncaught in a Task.\n\n");
  395. dump("Full message: " + exception + "\n");
  396. dump("Full stack: " + exception.stack + "\n");
  397. dump("*************************\n");
  398. }
  399. }
  400. this.deferred.reject(exception);
  401. },
  402. get callerStack() {
  403. // Cut `this._stack` at the last line of the first block that
  404. // contains task.js, keep the tail.
  405. for (let [line, index] of linesOf(this._stack || "")) {
  406. if (line.indexOf("/task.js:") == -1) {
  407. return this._stack.substring(index);
  408. }
  409. }
  410. return "";
  411. }
  412. };
  413. Task.Debugging = {
  414. /**
  415. * Control stack rewriting.
  416. *
  417. * If `true`, any exception thrown from a Task will be rewritten to
  418. * provide a human-readable stack trace. Otherwise, stack traces will
  419. * be left unchanged.
  420. *
  421. * There is a (small but existing) runtime cost associated to stack
  422. * rewriting, so you should probably not activate this in production
  423. * code.
  424. *
  425. * @type {bool}
  426. */
  427. get maintainStack() {
  428. return gMaintainStack;
  429. },
  430. set maintainStack(x) {
  431. if (!x) {
  432. gCurrentTask = null;
  433. }
  434. gMaintainStack = x;
  435. return x;
  436. },
  437. /**
  438. * Generate a human-readable stack for an error raised in
  439. * a Task.
  440. *
  441. * @param {string} topStack The stack provided by the error.
  442. * @param {string=} prefix Optionally, a prefix for each line.
  443. */
  444. generateReadableStack: function (topStack, prefix = "") {
  445. if (!gCurrentTask) {
  446. return topStack;
  447. }
  448. // Cut `topStack` at the first line that contains task.js, keep the head.
  449. let lines = [];
  450. for (let [line] of linesOf(topStack)) {
  451. if (line.indexOf("/task.js:") != -1) {
  452. break;
  453. }
  454. lines.push(prefix + line);
  455. }
  456. if (!prefix) {
  457. lines.push(gCurrentTask.callerStack);
  458. } else {
  459. for (let [line] of linesOf(gCurrentTask.callerStack)) {
  460. lines.push(prefix + line);
  461. }
  462. }
  463. return lines.join("\n");
  464. }
  465. };
  466. exports.Task = Task;