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.
104 lines
4.4 KiB
JavaScript
104 lines
4.4 KiB
JavaScript
// wasm/asm/lumbda-asm.loader.js
|
|
// Asm tier loader — instantiates lumbda-asm.wasm and exposes evalLisp.
|
|
//
|
|
// Memory layout (mirrors lumbda.wat):
|
|
// 0x10000 output buffer (read after each call)
|
|
// 0x20000 source buffer (write before each call)
|
|
// 0x40000 bend response scratch (host writes here when JS dispatches)
|
|
|
|
async function _bootstrap() {
|
|
const wasmURL = new URL("./lumbda-asm.wasm", import.meta.url).href;
|
|
const resp = await fetch(wasmURL);
|
|
const bytes = await resp.arrayBuffer();
|
|
|
|
// Closure-captured holder so the imported function can see the
|
|
// instance's memory after instantiation completes.
|
|
const refs = { instance: null, bendUrl: null, currentOnChunk: null };
|
|
|
|
const importObj = {
|
|
env: {
|
|
// bend_call(src_ptr, src_len, resp_ptr) -> resp_len.
|
|
// Synchronous XHR is the only sync HTTP available in workers;
|
|
// perfect for the WAT tier's blocking eval model.
|
|
bend_call(srcPtr, srcLen, respPtr) {
|
|
if (!refs.bendUrl) {
|
|
const msg = "no bend URL configured";
|
|
const mem = new Uint8Array(refs.instance.exports.memory.buffer);
|
|
mem.set(new TextEncoder().encode(msg), respPtr);
|
|
return msg.length;
|
|
}
|
|
const mem = new Uint8Array(refs.instance.exports.memory.buffer);
|
|
const payload = new TextDecoder().decode(mem.slice(srcPtr, srcPtr + srcLen));
|
|
try {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", refs.bendUrl, false); // sync
|
|
xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
|
|
xhr.send(payload);
|
|
const respText = xhr.responseText || "";
|
|
const respBytes = new TextEncoder().encode(respText);
|
|
new Uint8Array(refs.instance.exports.memory.buffer).set(respBytes, respPtr);
|
|
return respBytes.length;
|
|
} catch (e) {
|
|
const err = "bend error: " + (e.message || String(e));
|
|
new Uint8Array(refs.instance.exports.memory.buffer)
|
|
.set(new TextEncoder().encode(err), respPtr);
|
|
return err.length;
|
|
}
|
|
},
|
|
// emit_chunk(ptr, len) — WAT's out_char calls this on every
|
|
// newline (and lumbda_eval calls it once more at the end for
|
|
// trailing non-newline content) so the worker can postMessage
|
|
// a chunk to the playground panel as work happens, matching
|
|
// the C-tier Module.print + pyodide _StreamingStdout streams.
|
|
emit_chunk(ptr, len) {
|
|
if (!refs.currentOnChunk || len <= 0) return;
|
|
const mem = new Uint8Array(refs.instance.exports.memory.buffer, ptr, len);
|
|
refs.currentOnChunk(new TextDecoder().decode(mem));
|
|
},
|
|
},
|
|
};
|
|
|
|
const { instance } = await WebAssembly.instantiate(bytes, importObj);
|
|
refs.instance = instance;
|
|
instance.exports.lumbda_init();
|
|
|
|
const enc = new TextEncoder();
|
|
const dec = new TextDecoder();
|
|
const exp = instance.exports;
|
|
|
|
return {
|
|
async evalLisp(src, onChunk) {
|
|
const srcBytes = enc.encode(src);
|
|
const srcPtr = exp.lumbda_source_ptr();
|
|
const mem = new Uint8Array(exp.memory.buffer);
|
|
mem.set(srcBytes, srcPtr);
|
|
refs.currentOnChunk = onChunk || null;
|
|
try {
|
|
exp.lumbda_eval(srcBytes.length);
|
|
} catch (e) {
|
|
refs.currentOnChunk = null;
|
|
return `error: ${e.message}`;
|
|
}
|
|
refs.currentOnChunk = null;
|
|
const outPtr = exp.lumbda_output_ptr();
|
|
const outLen = exp.lumbda_output_len();
|
|
return dec.decode(new Uint8Array(exp.memory.buffer, outPtr, outLen));
|
|
},
|
|
setBendUrl(url) { refs.bendUrl = url || null; },
|
|
heapStats() {
|
|
return {
|
|
used: exp.lumbda_heap_used(),
|
|
total: exp.lumbda_heap_total(),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
// Closure-encapsulated singleton (see lumbda-c.loader.js for rationale).
|
|
export const createAsmTier = (() => {
|
|
let tier = null;
|
|
return async () => {
|
|
if (!tier) tier = await _bootstrap();
|
|
return tier;
|
|
};
|
|
})();
|