// Copyright 2026 Ivan Aliaga // SPDX-License-Identifier: Apache-2.0 // // Low-level TypeScript bindings to the Emscripten-generated dazzle.wasm // module. Loaded once via `loadDazzleModule()`; the returned `DazzleWasm` // instance owns the JS-side module and exposes typed wrappers around the // `_dazzle_*` exports. /** * Shape of the Emscripten module returned by `globalThis.dazzleModule()`. * We type it loosely because the codegen surface is large and stable. */ export interface EmscriptenModule { ccall( name: string, returnType: 'number' | 'string' | null, argTypes: string[], args: unknown[], ): unknown; _malloc(bytes: number): number; _free(ptr: number): void; UTF8ToString(ptr: number): string; stringToUTF8(str: string, ptr: number, maxBytes: number): void; lengthBytesUTF8(str: string): number; HEAPU8: Uint8Array; HEAPF32: Float32Array; } declare global { // eslint-disable-next-line @typescript-eslint/consistent-type-definitions interface Window { dazzleModule?: () => Promise; } // eslint-disable-next-line no-var var dazzleModule: (() => Promise) | undefined; } export async function loadDazzleModule(timeoutMs = 10_000): Promise { const g = globalThis as { dazzleModule?: () => Promise }; const start = Date.now(); while (typeof g.dazzleModule !== 'function') { if (Date.now() - start > timeoutMs) { throw new Error( 'dazzle.wasm loader not found on globalThis.dazzleModule. ' + 'Add the loader