/** * Shared helpers for the WASM dispatch bridges (dup-audit Opportunity #2, * Clusters C + D — `getWasm()` ×7 and the per-kernel dispatch boilerplate). * * Phase 3a scope: this module starts with exactly what the elementwise bridge * needs — the shared `getWasm()` accessor, backend detection sentinels, the * pointer-ABI unary kernel type, and the self-managed scratch-region runners * (`runUnaryPtr` / `runChainPtr`) that the elementwise + fusion path uses. The * remaining bridges (special / sort / signal / poly / interpolation / bitwise) * adopt this module — and gain an `alloc/try/release/probe/JS-fallback` * `makeDispatch` factory — in migration Phases 3b/3c. */ import { type WasmModule } from '../WasmLoader.js'; /** * Fetch the loaded WASM module, or `null` if nothing is loaded / the loader * threw. Never throws. Shared by every bridge (was duplicated ×7). */ export declare function getWasm(): WasmModule | null; /** * Minimal structural view of a loaded module used by the pointer-ABI path: a * `memory` plus arbitrary string-keyed kernel exports. */ export interface RawWasm { memory: WebAssembly.Memory; [k: string]: unknown; } /** * AssemblyScript-binary detection sentinel. The AS binary (the binary the * `functions` package bundles and dispatches to) exports the pointer-ABI * elementwise kernels (`array__ptr`) and a managed runtime (`__new`). * * This gate lets a bridge confirm the loaded module is the AS binary before * issuing a managed-ABI call. If the binary can't be loaded or lacks the AS * exports, the gate is false and the bridge falls back to JS. AssemblyScript is * the sole WASM backend; dispatch is AS→JS. */ export declare function isAsWasm(wasm: Record | null | undefined): boolean; /** Pointer-ABI unary kernel: `fn(inPtr, outPtr, n)` writes `n` results to `outPtr`. */ export type PtrUnaryKernel = (inPtr: number, outPtr: number, n: number) => void; /** * Reset the cached scratch base. Call after `wasmLoader.reset()` (e.g. in * tests) so the next dispatch re-captures the base against the new module's * memory rather than a detached buffer's stale length. */ export declare function resetScratch(): void; /** * Apply a single pointer-ABI unary kernel over `xs` via the scratch region. * One copy-in, one copy-out. Returns `null` on any failure (caller falls back * to JS). Never throws. */ export declare function runUnaryPtr(mem: WebAssembly.Memory, fn: PtrUnaryKernel, xs: Float64Array): Float64Array | null; /** * Apply a *chain* of pointer-ABI unary kernels with the data resident in WASM * memory the whole time — one copy-in, one copy-out regardless of chain length. * Ping-pongs two scratch buffers (the kernels are out-of-place). Returns `null` * on any failure (caller applies the chain in JS). Never throws. */ export declare function runChainPtr(mem: WebAssembly.Memory, fns: PtrUnaryKernel[], xs: Float64Array): Float64Array | null; /** Minimal view of the AS managed runtime exported by the AS binary. */ interface AsRuntime extends RawWasm { __new: (size: number, id: number) => number; __pin: (ptr: number) => number; __unpin: (ptr: number) => void; } /** * Copy a `Float64Array` RETURNED by an AS managed kernel (a header pointer) * into a fresh JS `Float64Array`. Must be called before the source allocation * is released. */ export declare function asReadReturnedF64(mod: RawWasm, headerPtr: number): Float64Array; /** Int32Array variant of {@link asReadReturnedF64}. */ export declare function asReadReturnedI32(mod: RawWasm, headerPtr: number): Int32Array; /** * Marshal `inputs` (f64) into AS managed arrays, run `body` with the module and * the header pointers (same order as `inputs`), then release. `body` must read * any AS-returned array (via `asReadReturnedF64` / `asReadReturnedI32`) into a * detached JS copy BEFORE returning — the allocations are unpinned afterward. * * Returns `body`'s result, or `null` on any failure / missing runtime (caller * falls back to JS). Never throws. */ export declare function withAsF64(wasm: RawWasm, inputs: Float64Array[], body: (mod: AsRuntime, headers: number[]) => T): T | null; /** Int32 variant of {@link withAsF64}. */ export declare function withAsI32(wasm: RawWasm, inputs: Int32Array[], body: (mod: AsRuntime, headers: number[]) => T): T | null; /** * Factory for the dominant special-function bridge shape: a single f64 array in, * a same-length f64 array out, dispatched to the AS managed kernel `cfg.name`. * Builds the full threshold → AS-managed → JS-fallback dispatch, removing the * duplicated alloc/try/probe boilerplate (dup-audit Cluster D). */ export declare function makeUnaryArrayDispatch(cfg: { threshold: number; /** AS managed kernel export name. */ name: string; /** Pure-JS fallback. */ js: (xs: Float64Array) => Float64Array; }): (xs: Float64Array) => Float64Array; export type { WasmModule }; //# sourceMappingURL=common.d.ts.map