wasm/playground: cancel button, asm state-leak fix, restyle to match homepage
User-visible changes
- Cancel button — terminates the running worker. Pyodide's slow mandelbrot
no longer freezes the UI; click cancel and the elapsed counter freezes
at "(cancelled @ NNNN ms)".
- Live ms counter ticks per animation frame while a tier is busy, so the
Pyodide tier's ~5-15 s wait is visible instead of looking hung.
- Restyled to match lumbda.com: chunkfive wordmark, --green: #227842
(light) / #5ec07a (dark), lumbda-logo-green.png, lowercase "lumbda"
everywhere. Pulls fonts/chunkfive locally so the playground stays
self-contained.
Architecture
- All tier evaluations now run inside a Web Worker (wasm/app/worker.mjs)
so the main thread stays responsive. Cancel = worker.terminate(); next
eval respawns a fresh worker.
- Loaders use new URL("./...", import.meta.url) so paths resolve against
the loader file's own location — works identically in window and
worker contexts, no baseURL argument needed.
- C tier Emscripten build flipped to EXPORT_ES6=1; loader uses dynamic
`import()` of the factory module. Integration test updated accordingly.
- Python loader uses `import("pyodide.mjs")` (ES module) instead of
document.createElement, which doesn't exist in workers.
Bug fixes
- Asm tier state leak: running the same demo twice on a cached WASM
instance produced corrupted output (every other cell on row 2+ rendered
as " " instead of the expected shade char). Root cause: top-level eval
passed `global_env` as the env, so closures captured stale globals;
fixed by passing NIL — env_lookup falls back to the CURRENT global_env
via its existing two-pass walk. Multi-run regression added to the
functional test suite.
- fib-ack demo: (ack 3 4) was too heavy for Pyodide (minutes). Cut to
(ack 3 3) + (fib 20) max so every tier finishes in seconds.
Test discipline
- Root `make test-all` now includes `wasm-test`. Adding a language
feature without exercising it on all six implementations is no longer
possible by accident.
- Functional suite: 11 assertions (was 10) — adds asm multi-run stability.
- Integration + unit: still 20 + 8.
This commit is contained in:
parent
9c25d46e13
commit
1b7de2c9c6
31 changed files with 866 additions and 435 deletions
|
|
@ -1,35 +1,27 @@
|
|||
// wasm/python/lumbda-py.js
|
||||
// Python tier loader — Pyodide (CPython-in-WASM) hosting lumbda.py.
|
||||
//
|
||||
// ES module form so it works in both window and Web Worker contexts.
|
||||
// Exports createPythonTier() -> Promise<{ evalLisp(src) -> Promise<string> }>.
|
||||
// Output is whatever the program printed (via display/print/write) plus the
|
||||
// final value's printed form if non-void.
|
||||
|
||||
const PYODIDE_VERSION = "0.27.2";
|
||||
const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`;
|
||||
|
||||
async function _bootstrap(baseURL) {
|
||||
// Load Pyodide loader script (sets globalThis.loadPyodide).
|
||||
if (typeof loadPyodide === "undefined") {
|
||||
await new Promise((resolve, reject) => {
|
||||
const s = document.createElement("script");
|
||||
s.src = PYODIDE_INDEX_URL + "pyodide.js";
|
||||
s.onload = resolve;
|
||||
s.onerror = () => reject(new Error("pyodide.js load failed"));
|
||||
document.head.appendChild(s);
|
||||
});
|
||||
}
|
||||
|
||||
async function _bootstrap() {
|
||||
// Dynamic ES-module import works in both window and Worker (module type)
|
||||
// contexts. The CDN ships pyodide.mjs alongside pyodide.js.
|
||||
const { loadPyodide } = await import(PYODIDE_INDEX_URL + "pyodide.mjs");
|
||||
const pyodide = await loadPyodide({ indexURL: PYODIDE_INDEX_URL });
|
||||
|
||||
// Pull lumbda.py + stdlib.lsp into Pyodide's virtual FS.
|
||||
const lumbdaSrc = await (await fetch(baseURL + "lumbda.py")).text();
|
||||
const stdlibSrc = await (await fetch(baseURL + "stdlib.lsp")).text();
|
||||
// Pull lumbda.py + stdlib.lsp into Pyodide's virtual FS. Paths resolve
|
||||
// relative to THIS loader (under python/) for both window and Worker.
|
||||
const pyURL = new URL("./lumbda.py", import.meta.url).href;
|
||||
const stdlibURL = new URL("./stdlib.lsp", import.meta.url).href;
|
||||
const lumbdaSrc = await (await fetch(pyURL)).text();
|
||||
const stdlibSrc = await (await fetch(stdlibURL)).text();
|
||||
pyodide.FS.writeFile("/home/pyodide/lumbda.py", lumbdaSrc);
|
||||
pyodide.FS.writeFile("/home/pyodide/stdlib.lsp", stdlibSrc);
|
||||
|
||||
// Initialize the lumbda environment once. We swap sys.stdout to a StringIO
|
||||
// buffer per eval to capture program output.
|
||||
await pyodide.runPythonAsync(`
|
||||
import sys, io
|
||||
sys.path.insert(0, "/home/pyodide")
|
||||
|
|
@ -61,10 +53,8 @@ def _lumbda_eval(src):
|
|||
|
||||
return {
|
||||
async evalLisp(src) {
|
||||
// Pass src in via globals to avoid escaping issues.
|
||||
pyodide.globals.set("_src_in", src);
|
||||
const result = await pyodide.runPythonAsync("_lumbda_eval(_src_in)");
|
||||
return result;
|
||||
return await pyodide.runPythonAsync("_lumbda_eval(_src_in)");
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -72,9 +62,8 @@ def _lumbda_eval(src):
|
|||
// Closure-encapsulated singleton (see lumbda-c.loader.js for rationale).
|
||||
export const createPythonTier = (() => {
|
||||
let tier = null;
|
||||
return async (baseURL) => {
|
||||
baseURL = baseURL || "./python/";
|
||||
if (!tier) tier = await _bootstrap(baseURL);
|
||||
return async () => {
|
||||
if (!tier) tier = await _bootstrap();
|
||||
return tier;
|
||||
};
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue