nandi/vflutter_ffipublic Fork 0
472eb493bac94bdcb79a38829ccca0c541256fef
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.

test_wasm.mjs · 67 lines · 2.6 KBJavaScript Blame HistoryRaw
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago1// Headless check of the wasm build: same contract as the native library.
2// node tool/test_wasm.mjs [out.bin]
3// With an argument, writes the raw RGBA frame there so it can be diffed
4// against the native kernel (tool/test_wasm_parity.sh).
5import { createRequire } from 'node:module';
6import { writeFileSync } from 'node:fs';
7
8const require = createRequire(import.meta.url);
Replace V with Nim 472eb49 nandithebull 17h ago9const createNimFlutterModule = require('../example/web/nimflutter.js');
10const M = await createNimFlutterModule();
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago11
12let failures = 0;
13const check = (name, ok, detail = '') => {
14 console.log(`${ok ? 'ok ' : 'FAIL'} ${name}${detail ? `${detail}` : ''}`);
15 if (!ok) failures++;
16};
17
Replace V with Nim 472eb49 nandithebull 17h ago18M.ccall('nf_init', null, [], []);
19check('nf_add', M.ccall('nf_add', 'number', ['number', 'number'], [20, 22]) === 42);
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago20
Replace V with Nim 472eb49 nandithebull 17h ago21// nf_greet returns a Nim-allocated string the caller must release.
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago22const namePtr = M._malloc(16);
23M.stringToUTF8('wasm', namePtr, 16);
Replace V with Nim 472eb49 nandithebull 17h ago24const resPtr = M.ccall('nf_greet', 'number', ['number'], [namePtr]);
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago25const greeting = M.UTF8ToString(resPtr);
Replace V with Nim 472eb49 nandithebull 17h ago26M.ccall('nf_free', null, ['number'], [resPtr]);
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago27M._free(namePtr);
Replace V with Nim 472eb49 nandithebull 17h ago28check('nf_greet', greeting === 'Hello, wasm, from Nim!', greeting);
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago29
30const W = 240, H = 180, ITER = 300, BYTES = W * H * 4;
31const buf = M._malloc(BYTES);
32
33const fill = (len, y0, y1) => {
34 M.HEAPU8.fill(0, buf, buf + BYTES);
35 M.ccall(
Replace V with Nim 472eb49 nandithebull 17h ago36 'nf_mandelbrot',
Add a WebAssembly backend for the example 728acaa nandithebull 18h ago37 null,
38 ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number'],
39 [buf, len, W, H, -0.5, 0.0, 3.0, ITER, y0, y1],
40 );
41 return M.HEAPU8.slice(buf, buf + BYTES);
42};
43
44const frame = fill(BYTES, 0, H);
45check('every pixel has alpha', frame.every((v, i) => i % 4 !== 3 || v === 255));
46check('frame is a fractal, not a flat fill', new Set(frame).size > 8);
47
48// size_t is 32-bit on wasm32 — the guard has to hold there too.
49check('undersized buf_len writes nothing', fill(BYTES - 1, 0, H).every((v) => v === 0));
50check('zero buf_len writes nothing', fill(0, 0, H).every((v) => v === 0));
51check('inverted band writes nothing', fill(BYTES, 10, 2).every((v) => v === 0));
52
53// Band split must reassemble into the identical frame.
54const top = fill(BYTES, 0, H / 2).slice(0, (BYTES / 2));
55const bottom = fill(BYTES, H / 2, H).slice(0, (BYTES / 2));
56const joined = new Uint8Array(BYTES);
57joined.set(top, 0);
58joined.set(bottom, BYTES / 2);
59check('band split matches a single-shot render', joined.every((v, i) => v === frame[i]));
60
61M._free(buf);
62
63if (process.argv[2]) {
64 writeFileSync(process.argv[2], Buffer.from(frame));
65 console.log(`wrote ${process.argv[2]} (${frame.length} bytes)`);
66}
67process.exit(failures === 0 ? 0 : 1);