Step 1 of the GC effort. Each tier loader now exposes heapStats():
- asm-wasm — lumbda_heap_used / lumbda_heap_total wat exports
- c-wasm — emscripten linear memory size (no free path right now,
so used = total; documented in the loader)
- python — pyodide module linear memory size; CPython GC cycles
this naturally
Worker handles a "heap" message kind that round-trips the active tab's
loaded tiers; repl tabbar shows a compact "py 12M · c 32M · asm 4M"
strip next to the buttons. Polls every 2s.
Doesn't solve the leak — just makes pressure visible so the user knows
when to use "reboot tier". Real GC (Cheney over the WAT bump allocator,
Boehm-em or custom mark-sweep for c-wasm) coming next.
76 lines
2.7 KiB
JavaScript
76 lines
2.7 KiB
JavaScript
// 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> }>.
|
|
|
|
const PYODIDE_VERSION = "0.27.2";
|
|
const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`;
|
|
|
|
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. 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);
|
|
|
|
await pyodide.runPythonAsync(`
|
|
import sys, io
|
|
sys.path.insert(0, "/home/pyodide")
|
|
import lumbda
|
|
_env = lumbda.make_global_env()
|
|
for _e in lumbda.read_all(lumbda.PRELUDE):
|
|
lumbda.leval(_e, _env)
|
|
|
|
def _lumbda_eval(src):
|
|
buf = io.StringIO()
|
|
old = sys.stdout
|
|
sys.stdout = buf
|
|
last = None
|
|
try:
|
|
for e in lumbda.read_all(src):
|
|
last = lumbda.leval(e, _env)
|
|
except Exception as ex:
|
|
sys.stdout = old
|
|
return f"{buf.getvalue()}error: {ex}"
|
|
sys.stdout = old
|
|
out = buf.getvalue()
|
|
if last is not None and not isinstance(last, lumbda._Void):
|
|
rep = lumbda.show(last)
|
|
if out and not out.endswith("\\n"):
|
|
out += "\\n"
|
|
out += rep
|
|
return out
|
|
`);
|
|
|
|
return {
|
|
async evalLisp(src) {
|
|
pyodide.globals.set("_src_in", src);
|
|
return await pyodide.runPythonAsync("_lumbda_eval(_src_in)");
|
|
},
|
|
heapStats() {
|
|
// Pyodide's runtime memory is the Emscripten linear memory.
|
|
// CPython's GC reclaims behind the scenes, so this number
|
|
// rises and falls naturally as objects die.
|
|
const total = pyodide._module.HEAPU8.byteLength;
|
|
return { used: total, total };
|
|
},
|
|
};
|
|
}
|
|
|
|
// Closure-encapsulated singleton (see lumbda-c.loader.js for rationale).
|
|
export const createPythonTier = (() => {
|
|
let tier = null;
|
|
return async () => {
|
|
if (!tier) tier = await _bootstrap();
|
|
return tier;
|
|
};
|
|
})();
|