-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
270 lines (229 loc) · 7.22 KB
/
main.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
let pausedThreads = new Map;
var selectedThread = 0;
var selectedFile = "";
var selectedLine = null;
var editor = null;
var breakpoints = [];
function init(ed) {
editor = ed;
}
function unselectThread() {
if (selectedThread == 0) {
return;
}
document.getElementById("status").textContent = "Running";
}
function selectThread(id) {
selectedThread = id;
sendRequest({listFrames: {threadId: id}});
var thread = pausedThreads.get(id);
loadFile(thread.location.path, thread.location.lineNumber, true);
document.getElementById("status").textContent = "Debugging " + thread.name;
}
function loadFile(file, line, highlight) {
if (highlight && selectedLine != null) {
editor.removeLineClass(selectedLine - 1, "background", "selected-line");
}
var updateUI = function() {
var thread = pausedThreads.get(selectedThread);
if (highlight) {
editor.addLineClass(line - 1, "background", "selected-line");
selectedLine = line;
}
editor.scrollIntoView({line: line - 1}, 100);
breakpoints.forEach(bp => {
if (bp.location.path === file) {
addBreakpointDiv(bp.location.lineNumber - 1);
}
});
};
var updateContent = function(content, filename) {
selectedFile = file;
editor.setValue(content);
// file name at the top
var marker = document.createElement("div");
marker.innerHTML = filename;
editor.addLineWidget(0, marker, {above: true, className: "line-widget"});
};
if (selectedFile != file) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/file' + file);
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
updateContent(xhr.responseText, file);
updateUI();
} else {
console.log('Error: ' + xhr.status);
}
}
};
xhr.send(null);
} else {
updateUI();
}
}
var refreshTimer = null;
function refreshThreadList() {
var tbody = document.getElementById('threads-body');
tbody.innerHTML = '';
// Instead of recreating the DOM immediately, we wait a bit in case there are
// other events updating pausedThreads.
clearTimeout(refreshTimer);
refreshTimer = setTimeout(function() {
pausedThreads.forEach((thread, id) => {
var row = tbody.insertRow(0);
var button = document.createElement("a");
button.textContent = thread.id;
button.onclick = function() { selectThread(thread.id); };
button.href = "#";
row.insertCell().appendChild(button);
row.insertCell().textContent = thread.location.path;
row.insertCell().textContent = thread.location.lineNumber;
row.insertCell().textContent = thread.name;
row.insertCell().textContent = thread.pauseReason;
})
}, 5);
}
function threadPausedEvent(th) {
pausedThreads.set(th.id, th);
refreshThreadList();
// If another paused thread is selected, don't update the view.
if (selectedThread == th.id || !pausedThreads.has(selectedThread)) {
selectThread(th.id);
}
}
function listFramesEvent(frames) {
// Update the call stack
var tbody = document.getElementById('stack-body');
tbody.innerHTML = '';
frames.forEach(frame => {
var row = tbody.insertRow();
var button = document.createElement("a");
button.textContent = frame.functionName;
button.onclick = function() { loadFile(frame.location.path, frame.location.lineNumber, true); };
button.href = "#";
row.insertCell().appendChild(button);
row.insertCell().textContent = frame.location.path;
row.insertCell().textContent = frame.location.lineNumber;
});
// Update the locals
tbody = document.getElementById('locals-body');
tbody.innerHTML = '';
frame = frames[0];
frame.scope.forEach(scope => {
if (scope.binding) {
scope.binding.forEach(binding => {
var row = tbody.insertRow();
row.insertCell().textContent = binding.label;
row.insertCell().textContent = binding.description;
row.insertCell().textContent = binding.type;
});
}
});
}
function setBreakpointsEvent(e) {
if (e == null) {
return;
}
breakpoints = e;
console.log("bps", e);
sendRequest({setBreakpoints: {breakpoint: e}});
tbody = document.getElementById('breakpoints-body');
tbody.innerHTML = '';
breakpoints.forEach(bp => {
var row = tbody.insertRow();
var button = document.createElement("a");
button.textContent = bp.location.path;
button.onclick = function() { loadFile(bp.location.path, bp.location.lineNumber, false); };
button.href = "#";
row.insertCell().appendChild(button);
row.insertCell().textContent = bp.location.lineNumber;
row.insertCell(); // TODO(laurentlb): Conditional breakpoints.
});
}
function handleEvent(e) {
if (e.hasOwnProperty("threadPaused")) {
threadPausedEvent(e.threadPaused.thread);
} else if (e.hasOwnProperty("listFrames")) {
listFramesEvent(e.listFrames.frame);
} else if (e.hasOwnProperty("setBreakpoints")) {
setBreakpointsEvent(e.setBreakpoints.breakpoint);
} else {
console.log("other", e);
}
// else if (Array.isArray(e)) {
// breakpoints = e;
// }
}
function listenEvents() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/updates');
// Track the state changes of the request.
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
handleEvent(JSON.parse(xhr.responseText));
return listenEvents();
} else {
console.log('Error: ' + xhr.status);
document.getElementById("status").textContent = "Connection closed.";
}
}
};
xhr.send(null);
}
function sendRequest(data) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/request");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
}
function stepButton(stepping) {
if (stepping == null) { // resume all
pausedThreads.clear();
selectedThread = 0;
} else {
pausedThreads.delete(selectedThread);
}
sendRequest({
continueExecution: {
threadId: selectedThread,
stepping: stepping
}});
if (selectedLine != null) {
editor.removeLineClass(selectedLine - 1, "background", "selected-line");
}
refreshThreadList();
}
function stopAllButton() {
sendRequest({pauseThread: {threadId: 0}});
}
function addBreakpointDiv(n) {
var info = editor.lineInfo(n);
if (info.gutterMarkers) {
return;
}
var marker = document.createElement("div");
marker.classList.add("breakpoint");
marker.innerHTML = "●";
editor.setGutterMarker(n, "breakpoints", marker);
}
function breakpointClick(cm, n) {
var info = cm.lineInfo(n);
if (info.gutterMarkers) {
breakpoints = breakpoints.filter(
bp => bp.location.path != selectedFile || bp.location.lineNumber != n + 1);
sendRequest({setBreakpoints: {breakpoint: breakpoints}});
editor.setGutterMarker(n, "breakpoints", null);
} else {
var loc = {location: {path: selectedFile, lineNumber: n + 1}};
breakpoints.push(loc);
addBreakpointDiv(n);
}
setBreakpointsEvent(breakpoints);
}