microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/src/debug-service/debug-service.ts
208lines · modeblame
2b583ddaIan Davis3 years ago | 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
1287d30fScott Carda1 years ago | 4 | import { |
| 5 | CURRENT_VERSION, | |
| 6 | type CircuitGroup as CircuitData, | |
32f9dba0Scott Carda1 years ago | 7 | } from "../data-structures/circuit.js"; |
2b583ddaIan Davis3 years ago | 8 | import type { |
| 9 | DebugService, | |
c23c2ae4Mine Starks2 years ago | 10 | IBreakpointSpan, |
| 11 | IQuantumState, | |
9d35d32eIan Davis3 years ago | 12 | IStackFrame, |
114ec14fIan Davis2 years ago | 13 | IStructStepResult, |
| 14 | IVariable, | |
021250d6Bill Ticehurst2 years ago | 15 | } from "../../lib/web/qsc_wasm.js"; |
21a29618Mine Starks2 years ago | 16 | import { ProgramConfig } from "../browser.js"; |
2b583ddaIan Davis3 years ago | 17 | import { eventStringToMsg } from "../compiler/common.js"; |
c23c2ae4Mine Starks2 years ago | 18 | import { |
| 19 | IQscEventTarget, | |
| 20 | QscEventData, | |
| 21 | QscEvents, | |
| 22 | makeEvent, | |
| 23 | } from "../compiler/events.js"; | |
2b583ddaIan Davis3 years ago | 24 | import { log } from "../log.js"; |
c23c2ae4Mine Starks2 years ago | 25 | import { IServiceProxy, ServiceProtocol } from "../workers/common.js"; |
21a29618Mine Starks2 years ago | 26 | import { toWasmProgramConfig } from "../compiler/compiler.js"; |
277ab24dMine Starks2 years ago | 27 | |
021250d6Bill Ticehurst2 years ago | 28 | type QscWasm = typeof import("../../lib/web/qsc_wasm.js"); |
2b583ddaIan Davis3 years ago | 29 | |
| 30 | // These need to be async/promise results for when communicating across a WebWorker, however | |
| 31 | // for running the debugger in the same thread the result will be synchronous (a resolved promise). | |
| 32 | export interface IDebugService { | |
21a29618Mine Starks2 years ago | 33 | loadProgram( |
| 34 | program: ProgramConfig, | |
b43f3f45Mine Starks2 years ago | 35 | entry: string | undefined, |
ede796dbIan Davis2 years ago | 36 | ): Promise<string>; |
9d35d32eIan Davis3 years ago | 37 | getBreakpoints(path: string): Promise<IBreakpointSpan[]>; |
7706cffaorpuente-MS1 years ago | 38 | getLocalVariables(frameID: number): Promise<Array<IVariable>>; |
278d567aIan Davis2 years ago | 39 | captureQuantumState(): Promise<Array<IQuantumState>>; |
277ab24dMine Starks2 years ago | 40 | getCircuit(): Promise<CircuitData>; |
9d35d32eIan Davis3 years ago | 41 | getStackFrames(): Promise<IStackFrame[]>; |
2b583ddaIan Davis3 years ago | 42 | evalContinue( |
| 43 | bps: number[], | |
b43f3f45Mine Starks2 years ago | 44 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 45 | ): Promise<IStructStepResult>; |
| 46 | evalNext( | |
| 47 | bps: number[], | |
b43f3f45Mine Starks2 years ago | 48 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 49 | ): Promise<IStructStepResult>; |
| 50 | evalStepIn( | |
| 51 | bps: number[], | |
b43f3f45Mine Starks2 years ago | 52 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 53 | ): Promise<IStructStepResult>; |
| 54 | evalStepOut( | |
| 55 | bps: number[], | |
b43f3f45Mine Starks2 years ago | 56 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 57 | ): Promise<IStructStepResult>; |
2b583ddaIan Davis3 years ago | 58 | dispose(): Promise<void>; |
| 59 | } | |
| 60 | | |
| 61 | export type IDebugServiceWorker = IDebugService & IServiceProxy; | |
| 62 | | |
| 63 | export class QSharpDebugService implements IDebugService { | |
114ec14fIan Davis2 years ago | 64 | private wasm: QscWasm; |
2b583ddaIan Davis3 years ago | 65 | private debugService: DebugService; |
| 66 | | |
| 67 | constructor(wasm: QscWasm) { | |
| 68 | log.info("Constructing a QSharpDebugService instance"); | |
114ec14fIan Davis2 years ago | 69 | this.wasm = wasm; |
2b583ddaIan Davis3 years ago | 70 | this.debugService = new wasm.DebugService(); |
| 71 | } | |
| 72 | | |
21a29618Mine Starks2 years ago | 73 | async loadProgram( |
| 74 | program: ProgramConfig, | |
b43f3f45Mine Starks2 years ago | 75 | entry: string | undefined, |
ede796dbIan Davis2 years ago | 76 | ): Promise<string> { |
21a29618Mine Starks2 years ago | 77 | return this.debugService.load_program( |
| 78 | toWasmProgramConfig(program, "unrestricted"), | |
7a91721aAlex Hansen2 years ago | 79 | entry, |
| 80 | ); | |
2b583ddaIan Davis3 years ago | 81 | } |
| 82 | | |
277ab24dMine Starks2 years ago | 83 | async getBreakpoints(path: string): Promise<IBreakpointSpan[]> { |
| 84 | return this.debugService.get_breakpoints(path).spans; | |
| 85 | } | |
| 86 | | |
7706cffaorpuente-MS1 years ago | 87 | async getLocalVariables(frameID: number): Promise<Array<IVariable>> { |
| 88 | const variable_list = this.debugService.get_locals(frameID); | |
277ab24dMine Starks2 years ago | 89 | return variable_list.variables; |
| 90 | } | |
| 91 | | |
| 92 | async captureQuantumState(): Promise<Array<IQuantumState>> { | |
| 93 | const state = this.debugService.capture_quantum_state(); | |
| 94 | return state.entries; | |
| 95 | } | |
| 96 | | |
| 97 | async getCircuit(): Promise<CircuitData> { | |
1287d30fScott Carda1 years ago | 98 | const circuit = this.debugService.get_circuit(); |
| 99 | return { | |
| 100 | circuits: [circuit], | |
| 101 | version: CURRENT_VERSION, | |
| 102 | }; | |
277ab24dMine Starks2 years ago | 103 | } |
| 104 | | |
9d35d32eIan Davis3 years ago | 105 | async getStackFrames(): Promise<IStackFrame[]> { |
101b4a7bMine Starks2 years ago | 106 | return this.debugService.get_stack_frames().frames; |
2b583ddaIan Davis3 years ago | 107 | } |
| 108 | | |
277ab24dMine Starks2 years ago | 109 | async evalContinue( |
114ec14fIan Davis2 years ago | 110 | bps: number[], |
b43f3f45Mine Starks2 years ago | 111 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 112 | ): Promise<IStructStepResult> { |
| 113 | const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler); | |
| 114 | const ids = new Uint32Array(bps); | |
277ab24dMine Starks2 years ago | 115 | return this.debugService.eval_continue(event_cb, ids); |
114ec14fIan Davis2 years ago | 116 | } |
| 117 | | |
277ab24dMine Starks2 years ago | 118 | async evalNext( |
114ec14fIan Davis2 years ago | 119 | bps: number[], |
b43f3f45Mine Starks2 years ago | 120 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 121 | ): Promise<IStructStepResult> { |
| 122 | const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler); | |
| 123 | const ids = new Uint32Array(bps); | |
277ab24dMine Starks2 years ago | 124 | return this.debugService.eval_next(event_cb, ids); |
114ec14fIan Davis2 years ago | 125 | } |
| 126 | | |
277ab24dMine Starks2 years ago | 127 | async evalStepIn( |
114ec14fIan Davis2 years ago | 128 | bps: number[], |
b43f3f45Mine Starks2 years ago | 129 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 130 | ): Promise<IStructStepResult> { |
| 131 | const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler); | |
| 132 | const ids = new Uint32Array(bps); | |
277ab24dMine Starks2 years ago | 133 | return this.debugService.eval_step_in(event_cb, ids); |
114ec14fIan Davis2 years ago | 134 | } |
| 135 | | |
277ab24dMine Starks2 years ago | 136 | async evalStepOut( |
2b583ddaIan Davis3 years ago | 137 | bps: number[], |
b43f3f45Mine Starks2 years ago | 138 | eventHandler: IQscEventTarget, |
114ec14fIan Davis2 years ago | 139 | ): Promise<IStructStepResult> { |
2b583ddaIan Davis3 years ago | 140 | const event_cb = (msg: string) => onCompilerEvent(msg, eventHandler); |
| 141 | const ids = new Uint32Array(bps); | |
277ab24dMine Starks2 years ago | 142 | return this.debugService.eval_step_out(event_cb, ids); |
114ec14fIan Davis2 years ago | 143 | } |
| 144 | | |
2b583ddaIan Davis3 years ago | 145 | async dispose() { |
| 146 | this.debugService.free(); | |
| 147 | } | |
| 148 | } | |
| 149 | | |
| 150 | export function onCompilerEvent(msg: string, eventTarget: IQscEventTarget) { | |
| 151 | const qscMsg = eventStringToMsg(msg); | |
| 152 | if (!qscMsg) { | |
| 153 | log.error("Unknown event message: %s", msg); | |
| 154 | return; | |
| 155 | } | |
| 156 | | |
| 157 | let qscEvent: QscEvents; | |
| 158 | | |
| 159 | const msgType = qscMsg.type; | |
| 160 | switch (msgType) { | |
| 161 | case "Message": | |
| 162 | qscEvent = makeEvent("Message", qscMsg.message); | |
| 163 | break; | |
| 164 | case "DumpMachine": | |
cc8e7546DmitryVasilevsky2 years ago | 165 | qscEvent = makeEvent("DumpMachine", { |
| 166 | state: qscMsg.state, | |
| 167 | stateLatex: qscMsg.stateLatex, | |
0c5ae2c5Stefan J. Wernli1 years ago | 168 | qubitCount: qscMsg.qubitCount, |
cc8e7546DmitryVasilevsky2 years ago | 169 | }); |
2b583ddaIan Davis3 years ago | 170 | break; |
| 171 | case "Result": | |
| 172 | qscEvent = makeEvent("Result", qscMsg.result); | |
| 173 | break; | |
c8c13383Bill Ticehurst1 years ago | 174 | case "Matrix": |
| 175 | qscEvent = makeEvent("Matrix", { | |
| 176 | matrix: qscMsg.matrix, | |
| 177 | matrixLatex: qscMsg.matrixLatex, | |
| 178 | }); | |
| 179 | break; | |
2b583ddaIan Davis3 years ago | 180 | default: |
| 181 | log.never(msgType); | |
| 182 | throw "Unexpected message type"; | |
| 183 | } | |
| 184 | log.debug("worker dispatching event " + JSON.stringify(qscEvent)); | |
| 185 | eventTarget.dispatchEvent(qscEvent); | |
| 186 | } | |
c23c2ae4Mine Starks2 years ago | 187 | |
| 188 | /** The protocol definition to allow running the debugger in a worker. */ | |
| 189 | export const debugServiceProtocol: ServiceProtocol< | |
| 190 | IDebugService, | |
| 191 | QscEventData | |
| 192 | > = { | |
| 193 | class: QSharpDebugService, | |
| 194 | methods: { | |
21a29618Mine Starks2 years ago | 195 | loadProgram: "request", |
c23c2ae4Mine Starks2 years ago | 196 | getBreakpoints: "request", |
| 197 | getLocalVariables: "request", | |
| 198 | captureQuantumState: "request", | |
277ab24dMine Starks2 years ago | 199 | getCircuit: "request", |
c23c2ae4Mine Starks2 years ago | 200 | getStackFrames: "request", |
| 201 | evalContinue: "requestWithProgress", | |
| 202 | evalNext: "requestWithProgress", | |
| 203 | evalStepIn: "requestWithProgress", | |
| 204 | evalStepOut: "requestWithProgress", | |
| 205 | dispose: "request", | |
| 206 | }, | |
c8c13383Bill Ticehurst1 years ago | 207 | eventNames: ["DumpMachine", "Message", "Matrix", "Result"], |
c23c2ae4Mine Starks2 years ago | 208 | }; |