summaryrefslogtreecommitdiffstats
path: root/devtools/shared/platform/chrome/stack.js
blob: abbe541201840c68198b57ef6b5a45c6cd375d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// A few wrappers for stack-manipulation.  This version of the module
// is used in chrome code.

"use strict";

(function (factory) {
  // This file might be require()d, but might also be loaded via
  // Cu.import.  Account for the differences here.
  if (this.module && module.id.indexOf("stack") >= 0) {
    // require.
    const {components, Cu} = require("chrome");
    factory.call(this, components, Cu, exports);
  } else {
    // Cu.import.
    this.isWorker = false;
    factory.call(this, Components, Components.utils, this);
    this.EXPORTED_SYMBOLS = ["callFunctionWithAsyncStack", "describeNthCaller",
                             "getStack"];
  }
}).call(this, function (components, Cu, exports) {
  /**
   * Return a description of the Nth caller, suitable for logging.
   *
   * @param {Number} n the caller to describe
   * @return {String} a description of the nth caller.
   */
  function describeNthCaller(n) {
    if (isWorker) {
      return "";
    }

    let caller = components.stack;
    // Do one extra iteration to skip this function.
    while (n >= 0) {
      --n;
      caller = caller.caller;
    }

    let func = caller.name;
    let file = caller.filename;
    if (file.includes(" -> ")) {
      file = caller.filename.split(/ -> /)[1];
    }
    let path = file + ":" + caller.lineNumber;

    return func + "() -> " + path;
  }

  /**
   * Return a stack object that can be serialized and, when
   * deserialized, passed to callFunctionWithAsyncStack.
   */
  function getStack() {
    return components.stack.caller;
  }

  /**
   * Like Cu.callFunctionWithAsyncStack but handles the isWorker case
   * -- |Cu| isn't defined in workers.
   */
  function callFunctionWithAsyncStack(callee, stack, id) {
    if (isWorker) {
      return callee();
    }
    return Cu.callFunctionWithAsyncStack(callee, stack, id);
  }

  exports.callFunctionWithAsyncStack = callFunctionWithAsyncStack;
  exports.describeNthCaller = describeNthCaller;
  exports.getStack = getStack;
});