-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathdebug-service.ts
More file actions
208 lines (187 loc) · 5.81 KB
/
debug-service.ts
File metadata and controls
208 lines (187 loc) · 5.81 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import {
CURRENT_VERSION,
type CircuitGroup as CircuitData,
} from "../data-structures/circuit.js";
import type {
DebugService,
IBreakpointSpan,
IQuantumState,
IStackFrame,
IStructStepResult,
IVariable,
} from "../../lib/web/qsc_wasm.js";
import { ProgramConfig } from "../main.js";
import { eventStringToMsg } from "../compiler/common.js";
import {
IQscEventTarget,
QscEventData,
QscEvents,
makeEvent,
} from "../compiler/events.js";
import { log } from "../log.js";
import { IServiceProxy, ServiceProtocol } from "../workers/common.js";
import { toWasmProgramConfig } from "../compiler/compiler.js";
type QscWasm = typeof import("../../lib/web/qsc_wasm.js");
// These need to be async/promise results for when communicating across a WebWorker, however
// for running the debugger in the same thread the result will be synchronous (a resolved promise).
export interface IDebugService {
loadProgram(
program: ProgramConfig,
entry: string | undefined,
): Promise<string>;
getBreakpoints(path: string): Promise<IBreakpointSpan[]>;
getLocalVariables(frameID: number): Promise<Array<IVariable>>;
captureQuantumState(): Promise<Array<IQuantumState>>;
getCircuit(): Promise<CircuitData>;
getStackFrames(): Promise<IStackFrame[]>;
evalContinue(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult>;
evalNext(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult>;
evalStepIn(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult>;
evalStepOut(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult>;
dispose(): Promise<void>;
}
export type IDebugServiceWorker = IDebugService & IServiceProxy;
export class QSharpDebugService implements IDebugService {
private wasm: QscWasm;
private debugService: DebugService;
constructor(wasm: QscWasm) {
log.info("Constructing a QSharpDebugService instance");
this.wasm = wasm;
this.debugService = new wasm.DebugService();
}
async loadProgram(
program: ProgramConfig,
entry: string | undefined,
): Promise<string> {
return this.debugService.load_program(
toWasmProgramConfig(program, "unrestricted"),
entry,
);
}
async getBreakpoints(path: string): Promise<IBreakpointSpan[]> {
return this.debugService.get_breakpoints(path).spans;
}
async getLocalVariables(frameID: number): Promise<Array<IVariable>> {
const variable_list = this.debugService.get_locals(frameID);
return variable_list.variables;
}
async captureQuantumState(): Promise<Array<IQuantumState>> {
const state = this.debugService.capture_quantum_state();
return state.entries;
}
async getCircuit(): Promise<CircuitData> {
const circuit = this.debugService.get_circuit();
return {
circuits: [circuit],
version: CURRENT_VERSION,
};
}
async getStackFrames(): Promise<IStackFrame[]> {
return this.debugService.get_stack_frames().frames;
}
async evalContinue(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult> {
const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
const ids = new Uint32Array(bps);
return this.debugService.eval_continue(event_cb, ids);
}
async evalNext(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult> {
const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
const ids = new Uint32Array(bps);
return this.debugService.eval_next(event_cb, ids);
}
async evalStepIn(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult> {
const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
const ids = new Uint32Array(bps);
return this.debugService.eval_step_in(event_cb, ids);
}
async evalStepOut(
bps: number[],
eventHandler: IQscEventTarget,
): Promise<IStructStepResult> {
const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler);
const ids = new Uint32Array(bps);
return this.debugService.eval_step_out(event_cb, ids);
}
async dispose() {
this.debugService.free();
}
}
export function onCompilerEvent(msg: string, eventTarget: IQscEventTarget) {
const qscMsg = eventStringToMsg(msg);
if (!qscMsg) {
log.error("Unknown event message: %s", msg);
return;
}
let qscEvent: QscEvents;
const msgType = qscMsg.type;
switch (msgType) {
case "Message":
qscEvent = makeEvent("Message", qscMsg.message);
break;
case "DumpMachine":
qscEvent = makeEvent("DumpMachine", {
state: qscMsg.state,
stateLatex: qscMsg.stateLatex,
qubitCount: qscMsg.qubitCount,
});
break;
case "Result":
qscEvent = makeEvent("Result", qscMsg.result);
break;
case "Matrix":
qscEvent = makeEvent("Matrix", {
matrix: qscMsg.matrix,
matrixLatex: qscMsg.matrixLatex,
});
break;
default:
log.never(msgType);
throw "Unexpected message type";
}
log.debug("worker dispatching event " + JSON.stringify(qscEvent));
eventTarget.dispatchEvent(qscEvent);
}
/** The protocol definition to allow running the debugger in a worker. */
export const debugServiceProtocol: ServiceProtocol<
IDebugService,
QscEventData
> = {
class: QSharpDebugService,
methods: {
loadProgram: "request",
getBreakpoints: "request",
getLocalVariables: "request",
captureQuantumState: "request",
getCircuit: "request",
getStackFrames: "request",
evalContinue: "requestWithProgress",
evalNext: "requestWithProgress",
evalStepIn: "requestWithProgress",
evalStepOut: "requestWithProgress",
dispose: "request",
},
eventNames: ["DumpMachine", "Message", "Matrix", "Result"],
};