Skip to content

Commit 606c110

Browse files
knightburtonyichoi
authored andcommitted
Fix setbreakPointsRequest (#33)
In case of invalid breakpoint (not allowed every line in the engine) a message will be rendered into the breakpoint and the breakpoint itself will be unverified. In case of reconnection the previously added breakpoints will be checked and will be activated or unverified by the engine. IoT.js-Debug-DCO-1.0-Signed-off-by: Imre Kiss [email protected]
1 parent ba80803 commit 606c110

File tree

4 files changed

+55
-40
lines changed

4 files changed

+55
-40
lines changed

src/IotjsDebugger.ts

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818

1919
import {
2020
DebugSession, InitializedEvent, OutputEvent, Thread, Source,
21-
StoppedEvent, ContinuedEvent, StackFrame, TerminatedEvent, Breakpoint as AdapterBreakpoint, Event, ErrorDestination
21+
StoppedEvent, ContinuedEvent, StackFrame, TerminatedEvent, Event, ErrorDestination
2222
} from 'vscode-debugadapter';
2323
import { DebugProtocol } from 'vscode-debugprotocol';
2424
import * as Fs from 'fs';
2525
import * as Path from 'path';
26-
import { IAttachRequestArguments, SourceSendingOptions } from './IotjsDebuggerInterfaces';
26+
import { IAttachRequestArguments, SourceSendingOptions, TemporaryBreakpoint } from './IotjsDebuggerInterfaces';
2727
import { JerryDebuggerClient, JerryDebuggerOptions } from './JerryDebuggerClient';
2828
import {
2929
JerryDebugProtocolDelegate, JerryDebugProtocolHandler, JerryMessageScriptParsed, JerryEvalResult,
3030
JerryMessageExceptionHit
3131
} from './JerryProtocolHandler';
3232
import { EVAL_RESULT_SUBTYPE, CLIENT as CLIENT_PACKAGE } from './JerryProtocolConstants';
33+
import { Breakpoint } from './JerryBreakpoints';
3334

3435
enum SOURCE_SENDING_STATES {
3536
NOP = 0,
@@ -76,8 +77,6 @@ class IotjsDebugSession extends DebugSession {
7677
): void {
7778
this.log('initializeRequest');
7879

79-
this.sendEvent(new InitializedEvent());
80-
8180
// This debug adapter implements the configurationDoneRequest.
8281
response.body.supportsConfigurationDoneRequest = true;
8382
response.body.supportsFunctionBreakpoints = false;
@@ -189,9 +188,10 @@ class IotjsDebugSession extends DebugSession {
189188
.catch(error => {
190189
this.log(error);
191190
this.sendErrorResponse(response, 0, error.message);
191+
})
192+
.then(() => {
193+
this.sendEvent(new InitializedEvent());
192194
});
193-
194-
this.sendEvent(new InitializedEvent());
195195
}
196196

197197
protected launchRequest(response: DebugProtocol.LaunchResponse, args: DebugProtocol.LaunchRequestArguments): void {
@@ -272,45 +272,51 @@ class IotjsDebugSession extends DebugSession {
272272
): Promise<void> {
273273
this.log('setBreakPointsRequest');
274274

275-
const filename = args.source.name;
276-
const clientLines = args.lines || [];
275+
const filename: string = args.source.name;
276+
const vscodeBreakpoints: DebugProtocol.Breakpoint[] = args.breakpoints!.map(b => ({verified: false, line: b.line}));
277277

278278
try {
279-
const scriptId = this._protocolhandler.getScriptIdByName(filename);
280-
const activeBp = this._protocolhandler.getActiveBreakpointsByScriptId(scriptId);
281-
const activeBpLines = activeBp.map(b => b.line);
282-
283-
const newBp = clientLines.filter(b => activeBpLines.indexOf(b) === -1);
284-
const removeBp = activeBpLines.filter(b => clientLines.indexOf(b) === -1);
285-
const persistingBp = clientLines.filter(b => newBp.indexOf(b) === -1);
286-
287-
let newBreakpoints: DebugProtocol.Breakpoint[] = [];
288-
await Promise.all(newBp.map(async b => {
289-
const breakpoint = this._protocolhandler.findBreakpoint(scriptId, b);
290-
return await this._protocolhandler.updateBreakpoint(breakpoint, true)
291-
.then(() => <DebugProtocol.Breakpoint> new AdapterBreakpoint(true, b));
292-
}))
293-
.then(breakpoints => {
294-
newBreakpoints = breakpoints;
295-
});
279+
const scriptId: number = this._protocolhandler.getScriptIdByName(filename);
280+
const activeBps: Breakpoint[] = this._protocolhandler.getActiveBreakpointsByScriptId(scriptId);
281+
282+
// Get the new breakpoints.
283+
const activeBpsLines: number[] = activeBps.map(b => b.line);
284+
const newBps: DebugProtocol.Breakpoint[] = vscodeBreakpoints.filter(b => activeBpsLines.indexOf(b.line) === -1);
285+
286+
const newBreakpoints: TemporaryBreakpoint[] = await Promise.all(newBps.map(async (breakpoint, index) => {
287+
try {
288+
const jerryBreakpoint: Breakpoint = this._protocolhandler.findBreakpoint(scriptId, breakpoint.line);
289+
await this._protocolhandler.updateBreakpoint(jerryBreakpoint, true);
290+
return <TemporaryBreakpoint>{verified: true, line: breakpoint.line};
291+
} catch (error) {
292+
this.log(error);
293+
return <TemporaryBreakpoint>{verified: false, line: breakpoint.line, message: (<Error>error).message};
294+
}
295+
}));
296296

297-
removeBp.forEach(async b => {
298-
const breakpoint = this._protocolhandler.findBreakpoint(scriptId, b);
299-
await this._protocolhandler.updateBreakpoint(breakpoint, false);
300-
});
297+
// Get the persists breakpoints.
298+
const newBreakpointsLines: number[] = newBreakpoints.map(b => b.line);
299+
const persistingBreakpoints: TemporaryBreakpoint[] = vscodeBreakpoints
300+
.filter(b => newBreakpointsLines.indexOf(b.line) === -1)
301+
.map(b => ({verified: true, line: b.line}));
301302

302-
const persistingBreakpoints = persistingBp.map(b => {
303-
return <DebugProtocol.Breakpoint> new AdapterBreakpoint(true, b);
304-
});
303+
// Get the removalbe breakpoints.
304+
const vscodeBreakpointsLines: number[] = vscodeBreakpoints.map(b => b.line);
305+
const removeBps: Breakpoint[] = activeBps.filter(b => vscodeBreakpointsLines.indexOf(b.line) === -1);
305306

306-
response.body = {
307-
breakpoints: [...persistingBreakpoints, ...newBreakpoints]
308-
};
307+
removeBps.forEach(async b => {
308+
const jerryBreakpoint = this._protocolhandler.findBreakpoint(scriptId, b.line);
309+
await this._protocolhandler.updateBreakpoint(jerryBreakpoint, false);
310+
});
309311

310-
this.sendResponse(response);
312+
response.body = { breakpoints: [...persistingBreakpoints, ...newBreakpoints] };
311313
} catch (error) {
312314
this.log(error.message);
315+
this.sendErrorResponse(response, error.message);
316+
return;
313317
}
318+
319+
this.sendResponse(response);
314320
}
315321

316322
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {

src/IotjsDebuggerInterfaces.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ export interface SourceSendingOptions {
3939
// Actual state of source sending.
4040
state: number;
4141
}
42+
43+
export interface TemporaryBreakpoint {
44+
// The breakpoint is verified or not by the engine.
45+
verified: boolean;
46+
// Line position in the file.
47+
line: number;
48+
// Extra error or info message.
49+
message?: string;
50+
}

src/JerryProtocolHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,13 @@ export class JerryDebugProtocolHandler {
674674

675675
if (enable) {
676676
if (breakpoint.activeIndex !== -1) {
677-
return Promise.reject('breakpoint already enabled');
677+
return Promise.reject(new Error('breakpoint already enabled'));
678678
}
679679
breakpointId = breakpoint.activeIndex = this.nextBreakpointIndex++;
680680
this.activeBreakpoints[breakpointId] = breakpoint;
681681
} else {
682682
if (breakpoint.activeIndex === -1) {
683-
return Promise.reject('breakpoint already disabled');
683+
return Promise.reject(new Error('breakpoint already disabled'));
684684
}
685685
breakpointId = breakpoint.activeIndex;
686686
delete this.activeBreakpoints[breakpointId];

src/test/JerryProtocolHandler.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ suite('JerryProtocolHandler', () => {
466466
const handler = new JerryDebugProtocolHandler({});
467467
await handler.updateBreakpoint(bp, true)
468468
.catch(error => {
469-
assert.strictEqual(error, 'breakpoint already enabled');
469+
assert.strictEqual((<Error>error).message, 'breakpoint already enabled');
470470
});
471471
});
472472

@@ -476,7 +476,7 @@ suite('JerryProtocolHandler', () => {
476476
const handler = new JerryDebugProtocolHandler({});
477477
await handler.updateBreakpoint(bp, false)
478478
.catch(error => {
479-
assert.strictEqual(error, 'breakpoint already disabled');
479+
assert.strictEqual((<Error>error).message, 'breakpoint already disabled');
480480
});
481481
});
482482

0 commit comments

Comments
 (0)