// One Mandelbrot band, computed in its own wasm instance. // // Each worker instantiates its own copy of the module. That is the whole // reason this needs no SharedArrayBuffer and no COOP/COEP headers: nothing is // shared, so there is nothing to synchronise. The wasm is ~34 KB, so N copies // is cheap. importScripts('nimflutter.js'); const SIG = ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']; let mod = null; const ready = createNimFlutterModule().then((m) => { mod = m; m.ccall('nf_init', null, [], []); }); self.onmessage = async (e) => { await ready; const { id, w, h, cx, cy, scale, iter, y0, y1 } = e.data; const bytes = (y1 - y0) * w * 4; const buf = mod._malloc(bytes); try { mod.ccall('nf_mandelbrot', null, SIG, [buf, bytes, w, h, cx, cy, scale, iter, y0, y1]); // .slice() copies out of the wasm heap, which may be detached by a later // growth; the copy is then transferred rather than structured-cloned. const band = mod.HEAPU8.slice(buf, buf + bytes); self.postMessage({ id, y0, band }, [band.buffer]); } finally { mod._free(buf); } };