Skip to content

Commit 5120f0a

Browse files
committed
Implement instrument conditions
1 parent a912f0d commit 5120f0a

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

src/SourcePlusPlus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace SourcePlusPlus {
157157
});
158158
}
159159

160-
function debugLog(...args: any[]) {
160+
export function debugLog(...args: any[]) {
161161
if (debug) {
162162
console.log(...args);
163163
}

src/control/LiveInstrumentRemote.ts

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CommandType from "../model/command/CommandType";
1111
import LiveBreakpoint from "../model/instruments/LiveBreakpoint";
1212
import {randomUUID} from "crypto";
1313
import VariableUtil from "../util/VariableUtil";
14+
import SourcePlusPlus from "../SourcePlusPlus";
1415

1516
export interface VariableInfo {
1617
block: Runtime.PropertyDescriptor[]
@@ -94,47 +95,73 @@ export default class LiveInstrumentRemote {
9495
}));
9596
}
9697

97-
Promise.all(promises).then(() => {
98-
// Do stuff
99-
let instrumentIds = this.breakpointIdToInstrumentIds.get(message.params.hitBreakpoints[0]); // TODO: Handle multiple hit breakpoints
100-
if (!instrumentIds) {
101-
this.removeBreakpoint(message.params.hitBreakpoints[0]);
102-
return;
103-
}
98+
let instrumentIds = this.breakpointIdToInstrumentIds.get(message.params.hitBreakpoints[0]); // TODO: Handle multiple hit breakpoints
99+
if (!instrumentIds) {
100+
this.removeBreakpoint(message.params.hitBreakpoints[0]);
101+
return;
102+
}
104103

105-
for (let instrumentId of instrumentIds) {
106-
let instrument = this.instruments.get(instrumentId);
107-
if (!instrument) {
108-
continue;
109-
}
104+
let instruments = instrumentIds.map(id => this.instruments.get(id));
105+
let conditionsSatisfied = Promise.all(instruments.map(instrument => {
106+
if (instrument.condition === undefined)
107+
return true;
108+
109+
return new Promise<boolean>((resolve, reject) => {
110+
this.session.post("Debugger.evaluateOnCallFrame", {
111+
callFrameId: frame.callFrameId,
112+
expression: instrument.condition
113+
}, (err, res) => {
114+
if (err) {
115+
reject(err);
116+
} else {
117+
if (res.result.type !== 'boolean') {
118+
reject("Invalid condition for instrument id: " + instrument.id + ": " + instrument.condition);
119+
} else {
120+
resolve(res.result.value);
121+
}
122+
}
123+
});
124+
});
125+
}));
110126

111-
if (instrument.type == LiveInstrumentType.BREAKPOINT) {
112-
ContextReceiver.applyBreakpoint(
113-
instrumentId,
114-
instrument.location.source,
115-
instrument.location.line,
116-
frame,
117-
variables
118-
);
119-
} else if (instrument.type == LiveInstrumentType.LOG) {
120-
let logInstrument = <LiveLog>instrument;
121-
ContextReceiver.applyLog(
122-
instrumentId,
123-
logInstrument.logFormat,
124-
logInstrument.logArguments,
125-
variables
126-
);
127-
} else if (instrument.type == LiveInstrumentType.METER) {
128-
let meterInstrument = <LiveMeter>instrument;
129-
ContextReceiver.applyMeter(
130-
instrumentId,
131-
variables
132-
);
133-
}
134-
if (instrument.isFinished()) {
135-
this.removeBreakpoint(instrumentId);
127+
Promise.all(promises).then(() => {
128+
conditionsSatisfied.then(conditions => {
129+
for (let i = 0; i < instruments.length; i++) {
130+
if (conditions[i]) {
131+
let instrument = instruments[i];
132+
if (!instrument) {
133+
continue;
134+
}
135+
136+
if (instrument.type == LiveInstrumentType.BREAKPOINT) {
137+
ContextReceiver.applyBreakpoint(
138+
instrument.id,
139+
instrument.location.source,
140+
instrument.location.line,
141+
frame,
142+
variables
143+
);
144+
} else if (instrument.type == LiveInstrumentType.LOG) {
145+
let logInstrument = <LiveLog>instrument;
146+
ContextReceiver.applyLog(
147+
instrument.id,
148+
logInstrument.logFormat,
149+
logInstrument.logArguments,
150+
variables
151+
);
152+
} else if (instrument.type == LiveInstrumentType.METER) {
153+
let meterInstrument = <LiveMeter>instrument;
154+
ContextReceiver.applyMeter(
155+
instrument.id,
156+
variables
157+
);
158+
}
159+
if (instrument.isFinished()) {
160+
this.removeBreakpoint(instrument.id);
161+
}
162+
}
136163
}
137-
}
164+
});
138165
});
139166

140167
// let frameId = message.params.callFrames[0].callFrameId;
@@ -315,6 +342,7 @@ export default class LiveInstrumentRemote {
315342
test() {
316343
let instrument = new LiveBreakpoint();
317344
instrument.id = randomUUID();
345+
instrument.condition = "false";
318346
instrument.location = {source: "test/javascript/test.js", line: 8};
319347
instrument.hitLimit = 1;
320348
instrument.applyImmediately = true;

test/javascript/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ SourcePlusPlus.start({}, true).then(() => {
55
console.log("test");
66

77
SourcePlusPlus.liveInstrumentRemote.test();
8-
9-
let i = 2;
8+
setTimeout(() => {
9+
let i = 2;
10+
}, 1000);
1011
});

0 commit comments

Comments
 (0)