lumbda/wasm/tests/functional-cross.mjs
russell@unturf.com d14c0eb5e4
wasm asm: stream output line-by-line via new emit_chunk env import
Brings the WAT tier to parity with c-emcc and pyodide for streaming
output during evalLisp. Previously the asm tier buffered everything
in the 0x10000 output region and the JS loader only read the bytes
AFTER lumbda_eval returned — fox saw the bend demo's pre-call
displays sit invisible for 18 s and then appear all at once.

WAT-side changes:

  - New env.emit_chunk(ptr, len) import. Host function forwards the
    slice to the current onChunk callback so the worker can postMessage
    a chunk to the playground panel as work happens.
  - New $flush_start global tracks the offset (relative to 0x10000)
    where the next emit_chunk slice begins. Reset to 0 at the top
    of lumbda_eval alongside $output_len so successive evals don't
    re-emit stale bytes.
  - $out_char now checks for newline (i32.const 10) after the store.
    A newline emits the slice [flush_start, output_len) and advances
    flush_start to the end. Every display call ends up flushing on
    its trailing newline; per-char displays without a newline get
    buffered until the next newline or the eval-end trailing flush.
  - $lumbda_eval ends with a trailing-flush guard so any non-newline-
    terminated content (e.g. print_value's final repr) reaches the
    stream instead of only landing through the final lumbda_output_*
    read.

JS loader:

  - importObj.env.emit_chunk decodes the slice from wasm memory and
    forwards to refs.currentOnChunk.
  - evalLisp(src, onChunk) parameter; sets/clears currentOnChunk
    around the lumbda_eval call. Same shape as c-emcc + pyodide.

Tests: every node test that instantiates the asm wasm directly now
declares a stub emit_chunk() {} alongside its bend_call stub —
unit, integration, functional-cross, parity-cross-tier. Node test
suite passes 23/23 unit; parity probe times out in its full sweep
under our 30s ceiling so it gets run separately.

Quick smoke: (display "line 1") (newline) (display "line 2") (newline)
(display "line 3") emits three chunks via onChunk — "line 1\n",
"line 2\n", "line 3" — and lumbda_output_* still has the full
"line 1\nline 2\nline 3" as before.
2026-06-14 20:35:13 -04:00

97 lines
3.9 KiB
JavaScript

// wasm/tests/functional-cross.mjs
// Run tests/functional.lsp on each WASM tier, count PASS / FAIL lines.
// Documents how close each tier is to passing the shared suite.
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const dist = path.resolve(here, "..", "dist");
const repoRoot = path.resolve(here, "..", "..");
const functionalLsp = fs.readFileSync(path.join(repoRoot, "tests", "functional.lsp"), "utf8");
function countPassFail(out) {
const pass = (out.match(/^PASS: /gm) || []).length;
const fail = (out.match(/^FAIL: /gm) || []).length;
return { pass, fail };
}
async function runAsm() {
const wasmBytes = fs.readFileSync(path.join(dist, "asm", "lumbda-asm.wasm"));
const importObj = { env: { bend_call(_p, _l, _r) { return 0; }, emit_chunk() {} } };
const { instance } = await WebAssembly.instantiate(wasmBytes, importObj);
const exp = instance.exports;
exp.lumbda_init();
const bytes = new TextEncoder().encode(functionalLsp);
if (bytes.length > 65536) throw new Error(`source too big for asm tier source buffer (${bytes.length} > 65536)`);
new Uint8Array(exp.memory.buffer).set(bytes, exp.lumbda_source_ptr());
try { exp.lumbda_eval(bytes.length); } catch (e) {
const out = new TextDecoder().decode(
new Uint8Array(exp.memory.buffer, exp.lumbda_output_ptr(), exp.lumbda_output_len()));
return { out, threw: e.message };
}
const out = new TextDecoder().decode(
new Uint8Array(exp.memory.buffer, exp.lumbda_output_ptr(), exp.lumbda_output_len()));
return { out, threw: null };
}
async function runC() {
const factoryURL = "file://" + path.join(dist, "c", "lumbda-c.js");
const createLumbdaC = (await import(factoryURL)).default;
let outBuf = [];
const m = await createLumbdaC({
locateFile: (p) => path.join(dist, "c", p),
print: (line) => outBuf.push(line),
printErr: () => {},
});
m.cwrap("lumbda_wasm_init", null, [])();
const r = m.cwrap("lumbda_wasm_eval", "number", ["string"])(functionalLsp);
let errMsg = "";
if (r) errMsg = m.UTF8ToString(r);
return { out: outBuf.join("\n"), threw: errMsg || null };
}
(async () => {
console.log("─".repeat(60));
console.log("Cross-tier functional.lsp runner");
console.log(`Source: ${functionalLsp.length} bytes`);
const results = {};
console.log("\n── asm tier ──");
try {
const { out, threw } = await runAsm();
const { pass, fail } = countPassFail(out);
results.asm = { pass, fail, threw, lines: out.split("\n").length };
console.log(` PASS: ${pass}`);
console.log(` FAIL: ${fail}`);
if (threw) console.log(` threw: ${threw}`);
} catch (e) {
console.log(` fatal: ${e.message}`);
results.asm = { pass: 0, fail: 0, threw: e.message };
}
console.log("\n── c tier ──");
try {
const { out, threw } = await runC();
const { pass, fail } = countPassFail(out);
results.c = { pass, fail, threw, lines: out.split("\n").length };
console.log(` PASS: ${pass}`);
console.log(` FAIL: ${fail}`);
if (threw) console.log(` threw: ${threw.slice(0, 200)}`);
} catch (e) {
console.log(` fatal: ${e.message}`);
results.c = { pass: 0, fail: 0, threw: e.message };
}
console.log("\n" + "─".repeat(60));
const total = Math.max(results.asm?.pass + results.asm?.fail || 0,
results.c?.pass + results.c?.fail || 0);
console.log(`Summary (out of ~${total} reached assertions):`);
console.log(` asm-wasm: ${results.asm.pass}/${results.asm.pass + results.asm.fail} pass`);
console.log(` c-wasm: ${results.c.pass}/${results.c.pass + results.c.fail} pass`);
// Soft exit — this is a measurement target, not a pass/fail gate.
process.exit(0);
})();