nandi/vflutter_ffipublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

nimflutter_worker.js · 34 lines · 1.2 KBJavaScript Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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);
  }
};