Bug 1752140 [wpt PR 32556] - battery status: Remove battery-insecure-context.html...
[gecko.git] / remote / shared / Stack.jsm
blobde0f537d791376209a6fc270d943547483162e23
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const EXPORTED_SYMBOLS = ["getFramesFromStack", "isChromeFrame"];
9 /**
10  * An object that contains details of a stack frame.
11  *
12  * @typedef {Object} StackFrame
13  * @see nsIStackFrame
14  *
15  * @property {String=} asyncCause
16  *     Type of asynchronous call by which this frame was invoked.
17  * @property {Number} columnNumber
18  *     The column number for this stack frame.
19  * @property {String} filename
20  *     The source URL for this stack frame.
21  * @property {String} function
22  *     SpiderMonkey’s inferred name for this stack frame’s function, or null.
23  * @property {Number} lineNumber
24  *     The line number for this stack frame (starts with 1).
25  * @property {Number} sourceId
26  *     The process-unique internal integer ID of this source.
27  */
29 /**
30  * Return a list of stack frames from the given stack.
31  *
32  * Convert stack objects to the JSON attributes expected by consumers.
33  *
34  * @param {Object} stack
35  *     The native stack object to process.
36  *
37  * @returns {Array<StackFrame>=}
38  */
39 function getFramesFromStack(stack) {
40   if (!stack || (Cu && Cu.isDeadWrapper(stack))) {
41     // If the global from which this error came from has been nuked,
42     // stack is going to be a dead wrapper.
43     return null;
44   }
46   const frames = [];
47   while (stack) {
48     frames.push({
49       asyncCause: stack.asyncCause,
50       columnNumber: stack.column,
51       filename: stack.source,
52       functionName: stack.functionDisplayName || "",
53       lineNumber: stack.line,
54       sourceId: stack.sourceId,
55     });
57     stack = stack.parent || stack.asyncParent;
58   }
60   return frames;
63 /**
64  * Check if a frame is from chrome scope.
65  *
66  * @param {Object} frame
67  *     The frame to check
68  *
69  * @returns {boolean}
70  *     True, if frame is from chrome scope
71  */
72 function isChromeFrame(frame) {
73   return (
74     frame.filename.startsWith("chrome://") ||
75     frame.filename.startsWith("resource://")
76   );