Skip to content

Commit e9c1060

Browse files
knightburtonyichoi
authored andcommitted
Improve the Debug Session's code quality (#37)
- Moved the protocol handler event listeners into separated functios - Await the evaluate and stacktarce handler requests - Clarify the configuration done and initalized event calls IoT.js-Debug-DCO-1.0-Signed-off-by: Imre Kiss [email protected]
1 parent b12b45a commit e9c1060

File tree

2 files changed

+146
-90
lines changed

2 files changed

+146
-90
lines changed

package-lock.json

Lines changed: 57 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/IotjsDebugger.ts

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import {
2020
DebugSession, InitializedEvent, OutputEvent, Thread, Source,
21-
StoppedEvent, ContinuedEvent, StackFrame, TerminatedEvent, Event, ErrorDestination
21+
StoppedEvent, StackFrame, TerminatedEvent, Event, ErrorDestination
2222
} from 'vscode-debugadapter';
2323
import { DebugProtocol } from 'vscode-debugprotocol';
2424
import * as Fs from 'fs';
@@ -27,7 +27,7 @@ import { IAttachRequestArguments, SourceSendingOptions, TemporaryBreakpoint } fr
2727
import { JerryDebuggerClient, JerryDebuggerOptions } from './JerryDebuggerClient';
2828
import {
2929
JerryDebugProtocolDelegate, JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryEvalResult,
30-
JerryMessageExceptionHit
30+
JerryMessageExceptionHit, JerryMessageBreakpointHit
3131
} from './JerryProtocolHandler';
3232
import { EVAL_RESULT_SUBTYPE, CLIENT as CLIENT_PACKAGE } from './JerryProtocolConstants';
3333
import { Breakpoint } from './JerryBreakpoints';
@@ -56,7 +56,7 @@ class IotjsDebugSession extends DebugSession {
5656
// Runtime supports now threads so just return a default thread.
5757
response.body = {
5858
threads: [
59-
new Thread(IotjsDebugSession.THREAD_ID, 'thread 1')
59+
new Thread(IotjsDebugSession.THREAD_ID, 'Main Thread')
6060
]
6161
};
6262
this.sendResponse(response);
@@ -76,7 +76,8 @@ class IotjsDebugSession extends DebugSession {
7676
response.body.supportsFunctionBreakpoints = false;
7777
response.body.supportsEvaluateForHovers = false;
7878
response.body.supportsStepBack = false;
79-
response.body.supportsRestartRequest = true;
79+
response.body.supportsRestartRequest = false;
80+
response.body.supportsDelayedStackTraceLoading = false;
8081

8182
this._sourceSendingOptions = <SourceSendingOptions>{
8283
contextReset: false,
@@ -92,7 +93,6 @@ class IotjsDebugSession extends DebugSession {
9293
this.log('configurationDoneRequest', LOG_LEVEL.SESSION);
9394

9495
super.configurationDoneRequest(response, args);
95-
this.sendResponse(response);
9696
}
9797

9898
protected attachRequest(response: DebugProtocol.AttachResponse, args: IAttachRequestArguments): void {
@@ -113,66 +113,26 @@ class IotjsDebugSession extends DebugSession {
113113
}
114114

115115
this._args = args;
116-
if (args.debugLog in LOG_LEVEL) {
116+
if (args.debugLog && args.debugLog in LOG_LEVEL) {
117117
this._debugLog = args.debugLog;
118118
} else {
119119
this.sendErrorResponse(response, new Error('No log level given'));
120120
}
121121
this.log('attachRequest');
122122

123-
const onBreakpointHit = (breakpointRef, stopType) => {
124-
this.log('onBreakpointHit', LOG_LEVEL.SESSION);
125-
this.sendEvent(new StoppedEvent(stopType, IotjsDebugSession.THREAD_ID));
126-
};
127-
128-
const onExceptionHit = (data: JerryMessageExceptionHit) => {
129-
this.log('onExceptionHit', LOG_LEVEL.SESSION);
130-
this.sendEvent(new StoppedEvent('exception', IotjsDebugSession.THREAD_ID, data.message));
131-
};
132-
133-
const onResume = () => {
134-
this.log('onResume', LOG_LEVEL.SESSION);
135-
136-
this.sendEvent(new ContinuedEvent(IotjsDebugSession.THREAD_ID));
137-
};
138-
139-
const onScriptParsed = data => {
140-
this.log('onScriptParsed', LOG_LEVEL.SESSION);
141-
this.handleSource(data);
142-
};
143-
144-
const onClose = () => {
145-
this.log('onClose', LOG_LEVEL.SESSION);
146-
this.sendEvent(new TerminatedEvent());
147-
};
148-
149-
const onWaitForSource = async () => {
150-
this.log('onWaitForSource', LOG_LEVEL.SESSION);
151-
if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.NOP) {
152-
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
153-
this.sendEvent(new Event('waitForSource'));
154-
} else if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.LAST_SENT) {
155-
if (!this._sourceSendingOptions.contextReset) {
156-
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.NOP;
157-
this._protocolhandler.sendClientSourceControl(CLIENT_PACKAGE.JERRY_DEBUGGER_NO_MORE_SOURCES);
158-
}
159-
}
160-
};
161-
162123
const protocolDelegate = <JerryDebugProtocolDelegate>{
163-
onBreakpointHit,
164-
onExceptionHit,
165-
onResume,
166-
onScriptParsed,
167-
onWaitForSource
124+
onBreakpointHit: (ref: JerryMessageBreakpointHit, type: string) => this.onBreakpointHit(ref, type),
125+
onExceptionHit: (data: JerryMessageExceptionHit) => this.onExceptionHit(data),
126+
onScriptParsed: (data: JerryMessageScriptParsed) => this.onScriptParsed(data),
127+
onWaitForSource: () => this.onWaitForSource()
168128
};
169129

170130
this._protocolhandler = new JerryDebugProtocolHandler(
171131
protocolDelegate, message => this.log(message, LOG_LEVEL.SESSION));
172132
this._debuggerClient = new JerryDebuggerClient(<JerryDebuggerOptions>{
173133
delegate: {
174134
onMessage: (message: Uint8Array) => this._protocolhandler.onMessage(message),
175-
onClose
135+
onClose: () => this.onClose()
176136
},
177137
host: args.address,
178138
port: args.port
@@ -318,47 +278,53 @@ class IotjsDebugSession extends DebugSession {
318278
this.sendResponse(response);
319279
}
320280

321-
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
281+
protected async evaluateRequest(
282+
response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments
283+
): Promise<void> {
322284
this.log('evaluateRequest', LOG_LEVEL.SESSION);
323285

324-
this._protocolhandler.evaluate(args.expression)
325-
.then((result: JerryEvalResult) => {
326-
const value = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK ? result.value : 'Evaluate Error';
286+
try {
287+
const result: JerryEvalResult = await this._protocolhandler.evaluate(args.expression);
288+
const value: string = result.subtype === EVAL_RESULT_SUBTYPE.JERRY_DEBUGGER_EVAL_OK
289+
? result.value
290+
: 'Evaluate Error';
327291

328-
response.body = {
329-
result: value,
330-
variablesReference: 0
331-
};
292+
response.body = {
293+
result: value,
294+
variablesReference: 0
295+
};
332296

333-
this.sendResponse(response);
334-
})
335-
.catch(error => this.sendErrorResponse(response, <Error>error));
297+
this.sendResponse(response);
298+
} catch (error) {
299+
this.sendErrorResponse(response, 0, (<Error>error).message);
300+
}
336301
}
337302

338-
protected stackTraceRequest(
303+
protected async stackTraceRequest(
339304
response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments
340-
): void {
305+
): Promise<void> {
341306
this.log('stackTraceRequest', LOG_LEVEL.SESSION);
342307

343-
this._protocolhandler.requestBacktrace()
344-
.then(backtrace => {
345-
const stk = backtrace.map((f, i) => new StackFrame(
346-
i,
347-
f.func.name || 'global',
348-
this.pathToSource(`${this._args.localRoot}/${this.pathToBasename(f.func.sourceName)}`),
349-
f.line,
350-
f.func.column
351-
)
352-
);
353-
354-
response.body = {
355-
stackFrames: stk,
356-
totalFrames: stk.length,
357-
};
308+
try {
309+
const backtrace = await this._protocolhandler.requestBacktrace();
310+
const stk = backtrace.map((f, i) => new StackFrame(
311+
1000 + i,
312+
f.func.name || 'global',
313+
this.pathToSource(`${this._args.localRoot}/${this.pathToBasename(f.func.sourceName)}`),
314+
f.line,
315+
f.func.column
316+
)
317+
);
318+
319+
response.body = {
320+
stackFrames: stk,
321+
};
358322

359-
this.sendResponse(response);
360-
})
361-
.catch(error => this.sendErrorResponse(response, <Error>error));
323+
this.sendResponse(response);
324+
} catch (error) {
325+
this.log(error);
326+
this.sendErrorResponse(response, 0, (<Error>error).message);
327+
}
362328
}
363329

364330
protected customRequest(command: string, response: DebugProtocol.Response, args: any): void {
@@ -422,7 +388,47 @@ class IotjsDebugSession extends DebugSession {
422388
}
423389
}
424390

425-
// Helper functions
391+
// Helper functions for event handling
392+
393+
private onBreakpointHit(breakpointRef: JerryMessageBreakpointHit, stopType: string): void {
394+
this.log('onBreakpointHit');
395+
396+
this.sendEvent(new StoppedEvent(stopType, IotjsDebugSession.THREAD_ID));
397+
}
398+
399+
private onExceptionHit(data: JerryMessageExceptionHit): void {
400+
this.log('onExceptionHit');
401+
402+
this.sendEvent(new StoppedEvent('exception', IotjsDebugSession.THREAD_ID, data.message));
403+
}
404+
405+
private onScriptParsed(data: JerryMessageScriptParsed): void {
406+
this.log('onScriptParsed');
407+
408+
this.handleSource(data);
409+
}
410+
411+
private async onWaitForSource(): Promise<void> {
412+
this.log('onWaitForSource');
413+
414+
if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.NOP) {
415+
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.WAITING;
416+
this.sendEvent(new Event('waitForSource'));
417+
} else if (this._sourceSendingOptions.state === SOURCE_SENDING_STATES.LAST_SENT) {
418+
if (!this._sourceSendingOptions.contextReset) {
419+
this._sourceSendingOptions.state = SOURCE_SENDING_STATES.NOP;
420+
this._protocolhandler.sendClientSourceControl(CLIENT_PACKAGE.JERRY_DEBUGGER_NO_MORE_SOURCES);
421+
}
422+
}
423+
}
424+
425+
private onClose(): void {
426+
this.log('onClose');
427+
428+
this.sendEvent(new TerminatedEvent());
429+
}
430+
431+
// General helper functions
426432

427433
private handleSource(data: JerryMessageScriptParsed): void {
428434
const path = `${this._args.localRoot}/${this.pathToBasename(data.name)}`;
@@ -451,7 +457,6 @@ class IotjsDebugSession extends DebugSession {
451457
private pathToBasename(path: string): string {
452458
if (path === '' || path === undefined) path = 'debug_eval.js';
453459
return Path.basename(path);
454-
455460
}
456461

457462
private log(message: any, level: number = LOG_LEVEL.VERBOSE): void {

0 commit comments

Comments
 (0)