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.
This commit is contained in:
parent
ffdfc1df43
commit
d14c0eb5e4
12 changed files with 133 additions and 13 deletions
|
|
@ -13,7 +13,7 @@ async function _bootstrap() {
|
|||
|
||||
// Closure-captured holder so the imported function can see the
|
||||
// instance's memory after instantiation completes.
|
||||
const refs = { instance: null, bendUrl: null };
|
||||
const refs = { instance: null, bendUrl: null, currentOnChunk: null };
|
||||
|
||||
const importObj = {
|
||||
env: {
|
||||
|
|
@ -45,6 +45,16 @@ async function _bootstrap() {
|
|||
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));
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -57,16 +67,19 @@ async function _bootstrap() {
|
|||
const exp = instance.exports;
|
||||
|
||||
return {
|
||||
async evalLisp(src) {
|
||||
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));
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -13,7 +13,7 @@ async function _bootstrap() {
|
|||
|
||||
// Closure-captured holder so the imported function can see the
|
||||
// instance's memory after instantiation completes.
|
||||
const refs = { instance: null, bendUrl: null };
|
||||
const refs = { instance: null, bendUrl: null, currentOnChunk: null };
|
||||
|
||||
const importObj = {
|
||||
env: {
|
||||
|
|
@ -45,6 +45,16 @@ async function _bootstrap() {
|
|||
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));
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -57,16 +67,19 @@ async function _bootstrap() {
|
|||
const exp = instance.exports;
|
||||
|
||||
return {
|
||||
async evalLisp(src) {
|
||||
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));
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue