-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcdt-controller.ts
257 lines (224 loc) · 8.1 KB
/
cdt-controller.ts
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
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Crdp from 'chrome-remote-debug-protocol';
import { Breakpoint } from './breakpoint';
import { JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryMessageBreakpointHit } from './protocol-handler';
import { ChromeDevToolsProxyServer } from './cdt-proxy';
import * as SP from './jrs-protocol-constants';
export interface JerryDebuggerDelegate {
onScriptParsed?(message: JerryMessageScriptParsed): void;
onBreakpointHit?(message: JerryMessageBreakpointHit): void;
}
interface PromiseFunctions {
resolve: Function;
reject: Function;
}
export class CDTController {
// NOTE: protocolHandler must be set before methods are called
public protocolHandler?: JerryDebugProtocolHandler;
// NOTE: proxyServer must be set after debugger is connected, protocolHandler is
// set, and before issuing further commands to the debugger
public proxyServer?: ChromeDevToolsProxyServer;
private scripts: Array<JerryMessageScriptParsed> = [];
private pendingBreakpoint?: Breakpoint;
// stores resolve and reject functions from promised evaluations
private evalResolvers: Array<PromiseFunctions> = [];
// FIXME: this lets test suite run for now
unused() {
Breakpoint;
}
// JerryDebuggerDelegate functions
onError(code: number, message: string) {
console.log(`\nError: ${message} (${code})\n`);
}
onScriptParsed(message: JerryMessageScriptParsed) {
this.scripts.push(message);
// this can happen before the proxy is connected
if (this.proxyServer) {
this.proxyServer.scriptParsed(message);
}
}
onBreakpointHit(message: JerryMessageBreakpointHit) {
// this can happen before the proxy is connected
if (this.proxyServer) {
this.pendingBreakpoint = message.breakpoint;
this.protocolHandler!.requestBacktrace();
}
}
onBacktrace(backtrace: Array<Breakpoint>) {
this.sendPaused(this.pendingBreakpoint, backtrace);
this.pendingBreakpoint = undefined;
}
onEvalResult(subType: number, result: string) {
if (this.evalResolvers.length === 0) {
throw new Error('eval result received with none pending');
}
const functions = this.evalResolvers.shift();
const remoteObject: Crdp.Runtime.RemoteObject = {
type: 'string',
value: result,
};
if (subType === SP.JERRY_DEBUGGER_EVAL_OK) {
functions!.resolve({
result: remoteObject,
});
} else {
functions!.reject({
result: remoteObject,
// FIXME: provide exceptionDetails
});
}
}
onOutputResult(subType: number, message: string) {
let type: 'debug' | 'error' | 'log' | 'trace' | 'warning' = 'log';
switch (subType) {
case SP.JERRY_DEBUGGER_OUTPUT_DEBUG:
type = 'debug';
break;
case SP.JERRY_DEBUGGER_OUTPUT_ERROR:
type = 'error';
break;
case SP.JERRY_DEBUGGER_OUTPUT_TRACE:
type = 'trace';
break;
case SP.JERRY_DEBUGGER_OUTPUT_WARNING:
type = 'warning';
break;
}
// NOTE: this drops an early 'Connected' message
if (this.proxyServer) {
this.proxyServer.sendConsoleAPICalled(type, message);
}
}
onResume() {
this.proxyServer!.sendResumed();
}
// CDTDelegate functions
// 'request' functions are information requests from CDT to Debugger
requestScripts() {
for (let i = 0; i < this.scripts.length; i++) {
this.proxyServer!.scriptParsed(this.scripts[i]);
}
}
requestBreakpoint() {
const breakpoint = this.protocolHandler!.getLastBreakpoint();
if (!breakpoint) {
throw new Error('no last breakpoint found');
}
this.pendingBreakpoint = breakpoint;
this.protocolHandler!.requestBacktrace();
}
requestPossibleBreakpoints(request: Crdp.Debugger.GetPossibleBreakpointsRequest) {
// TODO: support restrictToFunction parameter
const scriptId = Number(request.start.scriptId);
const startLine = request.start.lineNumber + 1;
let endLine = undefined;
if (request.end) {
endLine = request.end.lineNumber + 1;
}
const possible = this.protocolHandler!.getPossibleBreakpoints(scriptId, startLine, endLine);
const array: Array<Crdp.Debugger.BreakLocation> = [];
for (const i in possible) {
const breakpoint = possible[i];
const location: Crdp.Debugger.BreakLocation = {
scriptId: request.start.scriptId,
lineNumber: breakpoint.line - 1,
columnNumber: 0, // FIXME: ignoring columns for now
type: 'debuggerStatement', // TODO: may need to also use 'call' and 'return'
};
array.push(location);
}
return Promise.resolve({
locations: array,
});
}
requestScriptSource(request: Crdp.Debugger.GetScriptSourceRequest) {
return Promise.resolve({
scriptSource: this.protocolHandler!.getSource(Number(request.scriptId)),
});
}
// 'cmd' functions are commands from CDT to Debugger
cmdEvaluateOnCallFrame(request: Crdp.Debugger.EvaluateOnCallFrameRequest): Promise<Crdp.Runtime.EvaluateResponse> {
// FIXME: actually evaluate on call frame someday
this.protocolHandler!.evaluate(request.expression);
return new Promise((resolve, reject) => {
this.evalResolvers.push({ resolve, reject });
});
}
cmdPause() {
this.protocolHandler!.pause();
}
cmdRemoveBreakpoint(breakpointId: number) {
const breakpoint = this.protocolHandler!.getActiveBreakpoint(breakpointId);
if (!breakpoint) {
throw new Error('no breakpoint found');
}
this.updateBreakpoint(breakpoint.scriptId, breakpoint.line, false);
return Promise.resolve();
}
cmdResume() {
this.protocolHandler!.resume();
}
cmdSetBreakpoint(request: Crdp.Debugger.SetBreakpointRequest) {
const scriptId = Number(request.location.scriptId);
const breakpointId = this.updateBreakpoint(scriptId, request.location.lineNumber + 1, true);
const response: Crdp.Debugger.SetBreakpointResponse = {
breakpointId: String(breakpointId),
actualLocation: request.location,
};
return Promise.resolve(response);
}
cmdSetBreakpointByUrl(request: Crdp.Debugger.SetBreakpointByUrlRequest) {
if (!request.url) {
throw new Error('no url found');
}
const scriptId = this.protocolHandler!.getScriptIdByName(request.url);
const breakpointId = this.updateBreakpoint(scriptId, request.lineNumber + 1, true);
const response: Crdp.Debugger.SetBreakpointByUrlResponse = {
breakpointId: String(breakpointId),
locations: [{
scriptId: String(scriptId),
lineNumber: request.lineNumber,
columnNumber: 0, // TODO: use real column
}],
};
return Promise.resolve(response);
}
cmdStepInto() {
this.protocolHandler!.stepInto();
}
cmdStepOut() {
this.protocolHandler!.stepOut();
}
cmdStepOver() {
this.protocolHandler!.stepOver();
}
// 'report' functions are events from Debugger to CDT
private sendPaused(breakpoint: Breakpoint | undefined, backtrace: Array<Breakpoint>) {
// Node uses 'Break on start' but this is not allowable in crdp.d.ts
const reason = breakpoint ? 'debugCommand' : 'other';
this.proxyServer!.sendPaused(breakpoint, backtrace, reason);
}
/**
* Enables or disables a breakpoint.
*
* @param scriptId 1-based script ID
* @param line 1-based line number
* @param enable true to enable, false to disable
*/
private updateBreakpoint(scriptId: number, line: number, enable: boolean) {
const breakpoint = this.protocolHandler!.findBreakpoint(scriptId, line);
return this.protocolHandler!.updateBreakpoint(breakpoint, enable);
}
}