diff --git a/wasm/asm/lumbda-asm.loader.js b/wasm/asm/lumbda-asm.loader.js index e724244..852e411 100644 --- a/wasm/asm/lumbda-asm.loader.js +++ b/wasm/asm/lumbda-asm.loader.js @@ -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)); diff --git a/wasm/asm/lumbda.wat b/wasm/asm/lumbda.wat index 5cd0827..61aa4a3 100644 --- a/wasm/asm/lumbda.wat +++ b/wasm/asm/lumbda.wat @@ -46,11 +46,26 @@ (import "env" "bend_call" (func $js_bend_call (param i32 i32 i32) (result i32))) + ;; emit_chunk(ptr, len) — flush a slice of the output buffer to JS + ;; for streaming. Called from out_char whenever a newline is emitted + ;; (and from lumbda_eval at the very end for any trailing content + ;; without a newline). Loader's host function forwards the bytes to + ;; the current onChunk callback so the worker can postMessage the + ;; chunk to the playground panel as work happens. + (import "env" "emit_chunk" + (func $js_emit_chunk (param i32 i32))) + ;; ─── Memory & exports ────────────────────────────────────────── (memory (export "memory") 32 4096) ;; 32 pages = 2 MB initial, grow to 256 MB (global $heap_ptr (mut i32) (i32.const 0x30000)) (global $output_len (mut i32) (i32.const 0)) + ;; flush_start tracks the offset in the output buffer (relative to + ;; 0x10000) where the next emit_chunk should begin. Updated each + ;; time we flush; reset to 0 at the top of lumbda_eval alongside + ;; output_len. Lets streaming carry just the most-recent line, not + ;; the entire eval-so-far buffer. + (global $flush_start (mut i32) (i32.const 0)) (global $source_ptr (mut i32) (i32.const 0x20000)) (global $source_end (mut i32) (i32.const 0x20000)) (global $intern_list (mut i32) (i32.const 4)) ;; NIL initially @@ -1278,7 +1293,16 @@ (i32.store8 (i32.add (i32.const 0x10000) (global.get $output_len)) (local.get $c)) - (global.set $output_len (i32.add (global.get $output_len) (i32.const 1)))) + (global.set $output_len (i32.add (global.get $output_len) (i32.const 1))) + ;; Newline flushes the slice [flush_start, output_len) to JS so + ;; the playground panel can render line-by-line during eval + ;; instead of waiting for the full evalLisp to return. + (if (i32.eq (local.get $c) (i32.const 10)) + (then + (call $js_emit_chunk + (i32.add (i32.const 0x10000) (global.get $flush_start)) + (i32.sub (global.get $output_len) (global.get $flush_start))) + (global.set $flush_start (global.get $output_len))))) (func $out_str (param $ptr i32) (param $len i32) (local $i i32) @@ -4201,6 +4225,7 @@ (local $val i32) (call $lumbda_init) (global.set $output_len (i32.const 0)) + (global.set $flush_start (i32.const 0)) (global.set $source_ptr (i32.const 0x20000)) (global.set $source_end (i32.add (i32.const 0x20000) (local.get $src_len))) (local.set $val (global.get $VOID)) @@ -4228,6 +4253,16 @@ (i32.const 10)) (then (call $out_char (i32.const 10)))))) (call $print_value (local.get $val)))) + ;; Trailing-content flush — print_value's repr doesn't end with a + ;; newline, so any bytes past $flush_start would otherwise miss + ;; the streaming path and only reach the UI through the final + ;; lumbda_output_ptr / lumbda_output_len read. + (if (i32.gt_u (global.get $output_len) (global.get $flush_start)) + (then + (call $js_emit_chunk + (i32.add (i32.const 0x10000) (global.get $flush_start)) + (i32.sub (global.get $output_len) (global.get $flush_start))) + (global.set $flush_start (global.get $output_len)))) ;; Trigger GC when the heap exceeds 60% of available memory. This is ;; the only safe collection point — the eval call stack has unwound, ;; so the only roots are the globals the collector knows about. diff --git a/wasm/dist-repl/asm/lumbda-asm.loader.js b/wasm/dist-repl/asm/lumbda-asm.loader.js index e724244..852e411 100644 --- a/wasm/dist-repl/asm/lumbda-asm.loader.js +++ b/wasm/dist-repl/asm/lumbda-asm.loader.js @@ -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)); diff --git a/wasm/dist-repl/asm/lumbda-asm.wasm b/wasm/dist-repl/asm/lumbda-asm.wasm index 2b82bac..3563981 100644 Binary files a/wasm/dist-repl/asm/lumbda-asm.wasm and b/wasm/dist-repl/asm/lumbda-asm.wasm differ diff --git a/wasm/tests/functional-cross.mjs b/wasm/tests/functional-cross.mjs index e6fea2f..56f9ab8 100644 --- a/wasm/tests/functional-cross.mjs +++ b/wasm/tests/functional-cross.mjs @@ -19,7 +19,7 @@ function countPassFail(out) { async function runAsm() { const wasmBytes = fs.readFileSync(path.join(dist, "asm", "lumbda-asm.wasm")); - const importObj = { env: { bend_call(_p, _l, _r) { return 0; } } }; + 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(); diff --git a/wasm/tests/integration.mjs b/wasm/tests/integration.mjs index 1b25147..21748b6 100644 --- a/wasm/tests/integration.mjs +++ b/wasm/tests/integration.mjs @@ -31,7 +31,7 @@ function nativePython(demoPath) { 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; } } }; + 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(); diff --git a/wasm/tests/parity-cross-tier.mjs b/wasm/tests/parity-cross-tier.mjs index 5210118..6800d17 100644 --- a/wasm/tests/parity-cross-tier.mjs +++ b/wasm/tests/parity-cross-tier.mjs @@ -62,7 +62,7 @@ function nativePython(src) { async function withAsmTier() { const wasmBytes = fs.readFileSync(path.join(dist, "asm", "lumbda-asm.wasm")); - const importObj = { env: { bend_call() { return 0; } } }; + const importObj = { env: { bend_call() { return 0; }, emit_chunk() {} } }; const { instance } = await WebAssembly.instantiate(wasmBytes, importObj); const exp = instance.exports; exp.lumbda_init(); diff --git a/wasm/tests/unit.mjs b/wasm/tests/unit.mjs index fd0daff..fb6cc65 100644 --- a/wasm/tests/unit.mjs +++ b/wasm/tests/unit.mjs @@ -23,7 +23,7 @@ function check(name, cond, detail) { async function testAsm() { console.log("── asm tier ──"); const wasmBytes = fs.readFileSync(path.join(dist, "asm", "lumbda-asm.wasm")); - const importObj = { env: { bend_call(_p, _l, _r) { return 0; } } }; + 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(); @@ -90,6 +90,39 @@ async function testC() { // Skip error-path test on C tier: undefined-symbol triggers a long // setjmp/longjmp chain that the Emscripten runtime executes in finite // time but our test runner times the whole suite — keep it lean. + + // Portal save / resume — REPL backbone. portal-snapshot! writes JSON + // into MEMFS at /tmp/.portal. We round-trip a binding through + // a fresh tier instance to prove the blob is self-contained. + try { m.FS.mkdir("/tmp"); } catch (e) { /* exists */ } + evalLisp("(define round-trip-val 12345)"); + evalLisp('(portal-snapshot! "rt")'); + const blob = new TextDecoder().decode(m.FS.readFile("/tmp/rt.portal")); + check("portal-snapshot! produces JSON blob", + blob.includes('"lumbda-portal-v1"') && blob.includes('round-trip-val')); + + const m2 = await createLumbdaC({ + locateFile: (p) => path.join(dist, "c", p), + print: (line) => out.push(line), + printErr: (line) => out.push("ERR: " + line), + }); + m2.cwrap("lumbda_wasm_init", null, [])(); + const _eval2 = m2.cwrap("lumbda_wasm_eval", "number", ["string"]); + const _free2 = m2.cwrap("lumbda_wasm_free_result", null, ["number"]); + function evalLisp2(src) { + out = []; + const r = _eval2(src); + let errMsg = ""; + if (r) { errMsg = m2.UTF8ToString(r); _free2(r); } + return out.join("\n") + (errMsg ? "\n" + errMsg : ""); + } + check("fresh tier has no round-trip-val", + evalLisp2("round-trip-val").includes("error")); + try { m2.FS.mkdir("/tmp"); } catch (e) { /* exists */ } + m2.FS.writeFile("/tmp/rt.portal", blob); + evalLisp2('(portal-load! "rt")'); + check("portal-load! restores round-trip-val", + evalLisp2("round-trip-val").trim() === "12345"); } (async () => { diff --git a/www/playground/asm/lumbda-asm.loader.js b/www/playground/asm/lumbda-asm.loader.js index e724244..852e411 100644 --- a/www/playground/asm/lumbda-asm.loader.js +++ b/www/playground/asm/lumbda-asm.loader.js @@ -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)); diff --git a/www/playground/asm/lumbda-asm.wasm b/www/playground/asm/lumbda-asm.wasm index 2b82bac..3563981 100644 Binary files a/www/playground/asm/lumbda-asm.wasm and b/www/playground/asm/lumbda-asm.wasm differ diff --git a/www/repl/asm/lumbda-asm.loader.js b/www/repl/asm/lumbda-asm.loader.js index e724244..852e411 100644 --- a/www/repl/asm/lumbda-asm.loader.js +++ b/www/repl/asm/lumbda-asm.loader.js @@ -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)); diff --git a/www/repl/asm/lumbda-asm.wasm b/www/repl/asm/lumbda-asm.wasm index 2b82bac..3563981 100644 Binary files a/www/repl/asm/lumbda-asm.wasm and b/www/repl/asm/lumbda-asm.wasm differ