lumbda/www/playground/c/lumbda-c.loader.js
russell@unturf.com 346b873247
wasm: three-tier Lumbda to WebAssembly + browser playground
Adds a parallel build of all three Lumbda implementations to WASM, a
single-page playground at www/playground/, and a verified test suite.

Tiers
  - Python: Pyodide (CPython-in-WASM) hosting lumbda.py
  - C:      Emscripten build of c/ (tree-walker + bytecode VM; jit.c
            stubbed, gc.c uses its existing no-Boehm fallback)
  - Asm:    hand-written asm/lumbda.wat — parallel impl to asm/lumbda.s.
            Reader, eval (lambda/define/if/cond/let/and/or/quote/set!),
            recursion across mutated top-level env, bump allocator with
            memory.grow, 24 primitives. ~1200 lines of raw WAT.

SPA (wasm/app/, deployed to www/playground/)
  - CodeMirror 6 editor (Scheme highlighting) on left, output on right
  - Radios: 4 demos (Mandelbrot, Fib+Ack, Sieve, self-interp meta-eval)
            x 4 tiers (Python | C | Asm | All three)
  - All-three mode renders the three tier outputs side by side with
    per-tier elapsed timing

Tests (38 verified assertions)
  - 20 unit (Node): per-tier module loads, eval smoke
  - 8 integration (Node): each demo on c+asm WASM byte-matches the
                          canonical native Python run
  - 10 functional (Playwright headless Chromium): page mounts, every
                          demo runs on every tier, all-three renders

Makefile
  - Root targets: wasm-build, wasm-test, wasm-test-fn, wasm-serve,
                  wasm-deploy, wasm-clean
  - wasm/Makefile orchestrates the three tier builds; deploy copies
    dist/ into www/playground/

Asm tier notes
  - WAT linear symbol intern + linear env lookup is MOAD-0001 at scale;
    documented in the asm/lumbda.wat header and in the SPA footer. The
    demos hit ~30 globals so the linear walks are cheap enough.
  - Bump allocator never frees (matches asm/lumbda.s heap discipline);
    memory.grow expands by 1 MB chunks. Browser tab tears down at unload.

Toolchain (developer prerequisites)
  - Emscripten 6.0.0 via emsdk at ~/git/emsdk
  - wabt 1.0.36 at ~/git/wabt
  - Playwright for functional tests (symlinked from ~/git/agnt)
2026-06-14 11:40:34 -04:00

65 lines
2.2 KiB
JavaScript

// wasm/c/lumbda-c.loader.js
// C tier loader — Emscripten module wrapper.
//
// 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(baseURL) {
// Pull in the emitted JS glue dynamically. Emscripten with EXPORT_ES6=0
// produces a UMD-ish factory script that sets globalThis.createLumbdaC.
if (typeof createLumbdaC === "undefined") {
await new Promise((resolve, reject) => {
const s = document.createElement("script");
s.src = baseURL + "lumbda-c.js";
s.onload = resolve;
s.onerror = () => reject(new Error("lumbda-c.js load failed"));
document.head.appendChild(s);
});
}
let outBuf = [];
let errBuf = [];
const module = await createLumbdaC({
locateFile: (p) => baseURL + p,
print: (line) => outBuf.push(line),
printErr: (line) => errBuf.push(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();
return {
async evalLisp(src) {
outBuf = [];
errBuf = [];
const errPtr = _eval(src);
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;
},
};
}
// Closure-encapsulated singleton: no module-level mutable state. Each
// caller of createCTier() gets the same booted tier, but the cache lives
// inside the closure rather than at module scope.
export const createCTier = (() => {
let tier = null;
return async (opts) => {
const baseURL = (opts && opts.baseURL) || "./c/";
if (!tier) tier = await _bootstrap(baseURL);
return tier;
};
})();