microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c8adde746a7aaa0185fd4eac69af19d259ce1faa

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

npm/src/browser.ts

72lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// This module is the entry point for browser environments. For Node.js environment,
5// the "./main.js" module is the entry point.
6
7import initWasm, * as wasm from "../lib/web/qsc_wasm.js";
8import { log } from "./log.js";
9import { Compiler, ICompiler, ICompilerWorker } from "./compiler.js";
10import { ResponseMsgType, createWorkerProxy } from "./worker-common.js";
11
12// Create once. A module is stateless and can be efficiently passed to WebWorkers.
13let wasmModule: WebAssembly.Module | null = null;
14
15// Used to track if an instance is already instantiated
16let wasmInstance: wasm.InitOutput;
17
18export async function loadWasmModule(uriOrBuffer: string | ArrayBuffer) {
19 if (typeof uriOrBuffer === "string") {
20 const wasmRequst = await fetch(uriOrBuffer);
21 const wasmBuffer = await wasmRequst.arrayBuffer();
22 wasmModule = await WebAssembly.compile(wasmBuffer);
23 } else {
24 wasmModule = await WebAssembly.compile(uriOrBuffer);
25 }
26}
27
28export async function getCompiler(): Promise<ICompiler> {
29 if (!wasmModule) throw "Wasm module must be loaded first";
30 if (!wasmInstance) wasmInstance = await initWasm(wasmModule);
31
32 return new Compiler(wasm);
33}
34
35// Create the compiler inside a WebWorker and proxy requests.
36// If the Worker was already created via other means and is ready to receive
37// messages, then the worker may be passed in and it will be initialized.
38export function getCompilerWorker(workerArg: string | Worker): ICompilerWorker {
39 if (!wasmModule) throw "Wasm module must be loaded first";
40
41 // Create or use the WebWorker
42 const worker =
43 typeof workerArg === "string" ? new Worker(workerArg) : workerArg;
44
45 // Send it the Wasm module to instantiate
46 worker.postMessage({
47 type: "init",
48 wasmModule,
49 qscLogLevel: log.getLogLevel(),
50 });
51
52 // If you lose the 'this' binding, some environments have issues
53 const postMessage = worker.postMessage.bind(worker);
54 const setMsgHandler = (handler: (e: ResponseMsgType) => void) =>
55 (worker.onmessage = (ev) => handler(ev.data));
56 const onTerminate = () => worker.terminate();
57
58 return createWorkerProxy(postMessage, setMsgHandler, onTerminate);
59}
60
61export type { ICompilerWorker };
62export { log };
63export { type Dump, type ShotResult, type VSDiagnostic } from "./common.js";
64export {
65 getAllKatas,
66 getKata,
67 type Kata,
68 type KataItem,
69 type Example,
70 type Exercise,
71} from "./katas.js";
72export { QscEventTarget } from "./events.js";
73