forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbg.js
More file actions
114 lines (97 loc) · 3.32 KB
/
dbg.js
File metadata and controls
114 lines (97 loc) · 3.32 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* 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/>. */
// @flow
import * as timings from "./timings";
import { prefs, asyncStore, features } from "./prefs";
import { isDevelopment, isTesting } from "devtools-environment";
import { getDocument } from "./editor/source-documents";
function findSource(dbg: any, url: string) {
const sources = dbg.selectors.getSourceList();
return sources.find(s => (s.url || "").includes(url));
}
function findSources(dbg: any, url: string) {
const sources = dbg.selectors.getSourceList();
return sources.filter(s => (s.url || "").includes(url));
}
function sendPacket(dbg: any, packet: any) {
return dbg.client.sendPacket(packet);
}
function sendPacketToThread(dbg: Object, packet: any) {
return sendPacket(dbg, {
to: dbg.connection.tabConnection.threadClient.actor,
...packet
});
}
function evaluate(dbg: Object, expression: any) {
return dbg.client.evaluate(expression);
}
function bindSelectors(obj: Object): Object {
return Object.keys(obj.selectors).reduce((bound, selector) => {
bound[selector] = (a, b, c) =>
obj.selectors[selector](obj.store.getState(), a, b, c);
return bound;
}, {});
}
function getCM() {
const cm: any = document.querySelector(".CodeMirror");
return cm && cm.CodeMirror;
}
function formatMappedLocation(mappedLocation) {
const { location, generatedLocation } = mappedLocation;
return {
original: `(${location.line}, ${location.column})`,
generated: `(${generatedLocation.line}, ${generatedLocation.column})`
};
}
function formatMappedLocations(locations) {
return console.table(locations.map(loc => formatMappedLocation(loc)));
}
function formatSelectedColumnBreakpoints(dbg) {
const positions = dbg.selectors.getBreakpointPositionsForSource(
dbg.selectors.getSelectedSource().id
);
return formatMappedLocations(positions);
}
function getDocumentForUrl(dbg, url) {
const source = findSource(dbg, url);
return getDocument(source.id);
}
export function setupHelper(obj: Object) {
const selectors = bindSelectors(obj);
const dbg: Object = {
...obj,
selectors,
prefs,
asyncStore,
features,
timings,
getCM,
helpers: {
findSource: url => findSource(dbg, url),
findSources: url => findSources(dbg, url),
evaluate: expression => evaluate(dbg, expression),
sendPacketToThread: packet => sendPacketToThread(dbg, packet),
sendPacket: packet => sendPacket(dbg, packet),
dumpThread: () => sendPacketToThread(dbg, { type: "dumpThread" }),
getDocument: url => getDocumentForUrl(dbg, url)
},
formatters: {
mappedLocations: locations => formatMappedLocations(locations),
mappedLocation: location => formatMappedLocation(location),
selectedColumnBreakpoints: () => formatSelectedColumnBreakpoints(dbg)
},
_telemetry: {
events: {}
}
};
window.dbg = dbg;
if (isDevelopment() && !isTesting()) {
console.group("Development Notes");
const baseUrl = "https://firefox-devtools.github.io/debugger";
const localDevelopmentUrl = `${baseUrl}/docs/dbg.html`;
console.log("Debugging Tips", localDevelopmentUrl);
console.log("dbg", window.dbg);
console.groupEnd();
}
}