Skip to main content

Initialize the WASM runtime

Load flora.js as a script (UMD) and then call createFloraModule with locateFile.

const floraJsUrl = new URL('/wasm/flora.js', window.location.origin).href;
const floraWasmUrl = new URL('/wasm/flora.wasm', window.location.origin).href;

await new Promise<void>((resolve, reject) => {
const script = document.createElement('script');
script.src = floraJsUrl;
script.type = 'text/javascript';
script.onload = () => {
if (typeof window.createFloraModule === 'undefined') {
reject(new Error('createFloraModule not found after loading flora.js'));
return;
}
resolve();
};
script.onerror = () => reject(new Error(`Failed to load script from ${floraJsUrl}`));
document.head.appendChild(script);
});

const module = await window.createFloraModule({
locateFile: (path: string) => (path.endsWith('.wasm') ? floraWasmUrl : path),
print: () => {},
printErr: () => {},
onRuntimeInitialized() {
const mod = this as unknown as {
wasmMemory?: WebAssembly.Memory;
HEAPU32?: Uint32Array;
HEAP32?: Int32Array;
HEAPF32?: Float32Array;
HEAPU8?: Uint8Array;
};

if (!mod.HEAPU32 && mod.wasmMemory) {
const buffer = mod.wasmMemory.buffer;
mod.HEAPU32 = new Uint32Array(buffer);
mod.HEAP32 = new Int32Array(buffer);
mod.HEAPF32 = new Float32Array(buffer);
mod.HEAPU8 = new Uint8Array(buffer);
}
},
});

Next: Load model files into the virtual filesystem