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.
102 lines
4 KiB
JavaScript
102 lines
4 KiB
JavaScript
// wasm/tests/integration.mjs
|
|
// Cross-tier integration — each demo runs on c-WASM and asm-WASM (Node
|
|
// hosts), output is compared to the canonical native Python lumbda run.
|
|
// Python tier (Pyodide) loads from a CDN inside a browser; covered in the
|
|
// functional/Playwright test suite.
|
|
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
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 demosDir = path.join(here, "..", "app", "demos");
|
|
|
|
const DEMOS = ["mandelbrot", "fib-ack", "sieve", "self-interp"];
|
|
|
|
const state = { pass: 0, fail: 0, failures: [] };
|
|
|
|
function check(name, cond, detail) {
|
|
if (cond) { state.pass++; console.log(` ✓ ${name}`); }
|
|
else { state.fail++; console.log(` ✗ ${name}`); if (detail) { console.log(detail); } state.failures.push({ name, detail }); }
|
|
}
|
|
|
|
function nativePython(demoPath) {
|
|
return execFileSync("python3", [path.join(repoRoot, "lumbda.py"), "--fast", demoPath], {
|
|
encoding: "utf8", timeout: 120000,
|
|
});
|
|
}
|
|
|
|
async function runAsm(src) {
|
|
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(src);
|
|
new Uint8Array(exp.memory.buffer).set(bytes, exp.lumbda_source_ptr());
|
|
exp.lumbda_eval(bytes.length);
|
|
return new TextDecoder().decode(
|
|
new Uint8Array(exp.memory.buffer, exp.lumbda_output_ptr(), exp.lumbda_output_len()));
|
|
}
|
|
|
|
async function runC(src) {
|
|
const factoryURL = "file://" + path.join(dist, "c", "lumbda-c.js");
|
|
const createLumbdaC = (await import(factoryURL)).default;
|
|
let out = [];
|
|
const m = await createLumbdaC({
|
|
locateFile: (p) => path.join(dist, "c", p),
|
|
print: (line) => out.push(line),
|
|
printErr: (line) => out.push("ERR: " + line),
|
|
});
|
|
m.cwrap("lumbda_wasm_init", null, [])();
|
|
const r = m.cwrap("lumbda_wasm_eval", "number", ["string"])(src);
|
|
let errMsg = "";
|
|
if (r) errMsg = m.UTF8ToString(r);
|
|
return out.join("\n") + (errMsg ? "\n" + errMsg : "") + (out.length ? "\n" : "");
|
|
}
|
|
|
|
function diffSnippet(a, b) {
|
|
const aL = a.split("\n");
|
|
const bL = b.split("\n");
|
|
const n = Math.max(aL.length, bL.length);
|
|
const lines = [];
|
|
for (let i = 0; i < n; i++) {
|
|
if (aL[i] !== bL[i]) {
|
|
lines.push(` line ${i + 1}:`);
|
|
lines.push(` canonical: ${JSON.stringify(aL[i])}`);
|
|
lines.push(` got: ${JSON.stringify(bL[i])}`);
|
|
if (lines.length > 10) { lines.push(" ... (truncated)"); break; }
|
|
}
|
|
}
|
|
return lines.join("\n");
|
|
}
|
|
|
|
(async () => {
|
|
for (const demo of DEMOS) {
|
|
console.log(`── ${demo} ──`);
|
|
const demoPath = path.join(demosDir, demo + ".lsp");
|
|
const src = fs.readFileSync(demoPath, "utf8");
|
|
let canonical;
|
|
try { canonical = nativePython(demoPath); }
|
|
catch (e) { console.log(" ✗ canonical (native python lumbda) FAILED:", e.message.slice(0, 200)); state.fail++; continue; }
|
|
|
|
try {
|
|
const asmOut = await runAsm(src);
|
|
check(`${demo}: asm tier matches canonical`,
|
|
asmOut === canonical,
|
|
diffSnippet(canonical, asmOut));
|
|
} catch (e) { check(`${demo}: asm tier`, false, " threw: " + e.message); }
|
|
|
|
try {
|
|
const cOut = await runC(src);
|
|
check(`${demo}: c tier matches canonical`,
|
|
cOut === canonical,
|
|
diffSnippet(canonical, cOut));
|
|
} catch (e) { check(`${demo}: c tier`, false, " threw: " + e.message); }
|
|
}
|
|
console.log(`\n${state.pass} passed, ${state.fail} failed`);
|
|
process.exit(state.fail ? 1 : 0);
|
|
})();
|