lumbda/wasm/c/lumbda-c.loader.js
russell@unturf.com ffdfc1df43
playground: stream tier output line-by-line during eval
Previously every (display ...) call buffered into the tier's outBuf /
sys.stdout / WAT output-buffer and only landed in the playground panel
after evalLisp returned. During the 18-second cuda-secp256k1-bench
dispatch the panel stayed blank, then everything (probe + telemetry +
heavy result) appeared at once. Fox: 'demo waits for the full program
to return before emitting instead of line by line as it finishes'.

Each tier's print sink now ALSO calls a per-eval onChunk callback
that the worker forwards to the main thread as a {kind:"chunk"}
postMessage. The main thread spawns an in-progress tier-block on
the first chunk and appends each subsequent chunk to its <pre>,
auto-scrolling. On done/error the timing header finalizes in place;
the block looks identical to a non-streamed appendBlock at the end.

C tier (Emscripten): the Module.print + Module.printErr callbacks
that already fired per-line now invoke currentOnChunk in addition
to buffering. evalLisp accepts an onChunk arg and threads it.

Python tier (pyodide): _lumbda_eval swaps sys.stdout for a
_StreamingStdout subclass of io.StringIO whose write() also calls
into globalThis._lumbdaPyEmitChunk on the JS side. The final
auto-printed value also emits.

Asm tier (WAT): output stays buffered until evalLisp returns —
streaming there needs a new wasm import (out_line) and a wasm
rebuild. Caller-side fallback: app.js's appendBlock path still
runs for asm so its block appears at the end as before.

worker.mjs forwards each chunk via postMessage; app.js's
runOnTierInWorker handles 'chunk' messages by lazily creating
a startLiveBlock on first chunk, appending text on each, and
finalizing the timing in finalize() once done arrives.
2026-06-14 20:25:47 -04:00

107 lines
4.4 KiB
JavaScript

// wasm/c/lumbda-c.loader.js
// C tier loader — Emscripten ES module factory.
//
// Exports createCTier({ baseURL }) -> Promise<{ evalLisp(src) -> Promise<string> }>.
// Output capture: Emscripten routes stdout/stderr through Module.print /
// Module.printErr callbacks. We accumulate per-eval and return joined.
async function _bootstrap() {
// Use import.meta.url so paths resolve relative to THIS loader file —
// not the caller. Works in both window and Worker contexts because both
// have a defined import.meta.url for ES modules.
const factoryURL = new URL("./lumbda-c.js", import.meta.url).href;
const wasmDir = new URL("./", import.meta.url).href;
const { default: createLumbdaC } = await import(/* @vite-ignore */ factoryURL);
// Streaming output: every printf in the wasm fires Module.print
// synchronously. We buffer normally for the final return, but if
// the current eval registered an onChunk callback we also call
// through immediately so the worker can postMessage a chunk to
// the UI as each line emerges (vs the user staring at a blank
// panel for 18 seconds during the heavy bend call).
let outBuf = [];
let errBuf = [];
let currentOnChunk = null;
const module = await createLumbdaC({
locateFile: (p) => wasmDir + p,
print: (line) => {
outBuf.push(line);
if (currentOnChunk) currentOnChunk(line);
},
printErr: (line) => {
errBuf.push(line);
if (currentOnChunk) currentOnChunk(line);
},
});
const _init = module.cwrap("lumbda_wasm_init", null, []);
const _eval = module.cwrap("lumbda_wasm_eval", "number", ["string"]);
const _free = module.cwrap("lumbda_wasm_free_result", null, ["number"]);
_init();
// /tmp must exist before (portal-snapshot! ...) writes there — the
// C builtin fopen("w")s the path directly so a missing dir is a
// hard error. Emscripten's MEMFS gives us /tmp on recent versions
// but make it idempotent.
try { module.FS.mkdir("/tmp"); } catch (e) { /* exists */ }
// bend!-call host bridge — js_lumbda_bend_call inside the wasm
// (EM_JS in lumbda_wasm_entry.c) reads globalThis._lumbdaCBendUrl
// for the destination, then does sync XHR POST and writes the
// response bytes back into the wasm heap. setBendUrl mutates that
// global so runner.js can propagate a saved playground URL.
return {
setBendUrl(url) { globalThis._lumbdaCBendUrl = url || null; },
// portalSave / portalLoad bridge MEMFS to the JS side so the
// REPL can persist checkpoints to the encrypted vault and
// restore them on a fresh worker. Names come from the worker
// (REPL caller validates alphanumeric) so path traversal isn't
// a concern.
portalSave(name) {
const path = `/tmp/${name}.portal`;
try {
const bytes = module.FS.readFile(path);
return new TextDecoder().decode(bytes);
} catch (e) { return null; }
},
portalLoad(name, blob) {
const path = `/tmp/${name}.portal`;
module.FS.writeFile(path, blob);
},
async evalLisp(src, onChunk) {
outBuf = [];
errBuf = [];
currentOnChunk = onChunk || null;
const errPtr = _eval(src);
currentOnChunk = null;
let errMsg = "";
if (errPtr) {
errMsg = module.UTF8ToString(errPtr);
_free(errPtr);
}
let out = outBuf.join("\n");
if (out) out += "\n";
if (errBuf.length) out += errBuf.join("\n") + "\n";
if (errMsg) out += errMsg + "\n";
return out;
},
heapStats() {
// Emscripten exposes the linear memory directly. There's no
// free path right now (LUMBDA_NO_BOEHM), so used = total —
// every malloc accumulates until reload. Documented and
// surfaced in the REPL so the user sees the pressure.
const total = module.HEAPU8.byteLength;
return { used: total, total };
},
};
}
// Closure-encapsulated singleton (see lumbda-c.loader.js for rationale).
export const createCTier = (() => {
let tier = null;
return async () => {
if (!tier) tier = await _bootstrap();
return tier;
};
})();