Skip to content

wasm

Compiles and instantiates WebAssembly modules inlined into a plugin’s bundle.

@mnaoumov/obsidian-dev-toolkit’s wasm feature turns import bytes from './module.wasm' into a Uint8Array. These helpers compile those bytes once per bytes object and instantiate them with typed exports. The module imports nothing, so a worker entry point can import it too, and a compiled WebAssembly.Module can be passed to a worker as a call argument, because it is structured-cloneable.

Interface Description
WasmInstance An instantiated module with typed exports.
Function Description
compileWasmModule Compiles WebAssembly bytes, once per bytes object. The cache is keyed by the bytes object itself, so the constant an inlined .wasm import gives is compiled once however many times it is asked for, and the compiled module is released with that constant when the plugin unloads. A failed compilation is not cached.
instantiateWasmModule Instantiates a WebAssembly module, compiling it first when given bytes. Each call makes a new instance with its own memory; nothing here keeps the instance, so dropping every reference to it lets the garbage collector free that memory. Keep it in a field cleared on unload rather than at module scope.