From 11470a5fba4e4bd1204ed45cd7e2b4f1897dfd7d Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Sun, 14 Jun 2026 18:25:46 -0400 Subject: [PATCH] =?UTF-8?q?bend:=20wire=20bend!-call=20into=20the=20pyodid?= =?UTF-8?q?e=20tier=20=E2=80=94=20playground=20python=20=E2=9A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lumbda-py.js (and the three deployed mirrors) now expose setBendUrl and register a bend!-call primitive in the pyodide-hosted lumbda environment. Implementation: - JS loader stashes a `globalThis._lumbdaPyBendCall` function that does sync XHR POST to the configured bend URL (legal in Web Workers, where the pyodide tier runs in this playground) - Python bootstrap imports `_lumbdaPyBendCall` from `js` and binds it as a builtin under the symbol `bend!-call`, accepting any value and stringifying via lumbda.show before sending - setBendUrl(url) on the tier object updates the JS closure; the runner.js plumbing already calls it on every tier when the user saves a bend URL in the ⚡ bar This brings the pyodide tier to parity with the asm (WAT) tier for HTTP-mode bend. The emcc C tier still lacks the bind — wiring it needs a new wasm primitive built via emcc; lands in the next commit. Tested: bend!-call "(ping)" against the same gpu-worker endpoint returns the same (ok pong) S-expression the asm tier sees. --- wasm/dist-repl/python/lumbda-py.js | 36 +++++++++++++++++++++++++++++- wasm/python/lumbda-py.js | 36 +++++++++++++++++++++++++++++- www/playground/python/lumbda-py.js | 36 +++++++++++++++++++++++++++++- www/repl/python/lumbda-py.js | 36 +++++++++++++++++++++++++++++- 4 files changed, 140 insertions(+), 4 deletions(-) diff --git a/wasm/dist-repl/python/lumbda-py.js b/wasm/dist-repl/python/lumbda-py.js index c716882..609cefc 100644 --- a/wasm/dist-repl/python/lumbda-py.js +++ b/wasm/dist-repl/python/lumbda-py.js @@ -2,7 +2,9 @@ // 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 }>. +// Exports createPythonTier() -> Promise<{ evalLisp(src) -> Promise, +// setBendUrl(url) -> void, +// heapStats() -> {used,total} }>. const PYODIDE_VERSION = "0.27.2"; const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`; @@ -22,6 +24,25 @@ async function _bootstrap() { pyodide.FS.writeFile("/home/pyodide/lumbda.py", lumbdaSrc); pyodide.FS.writeFile("/home/pyodide/stdlib.lsp", stdlibSrc); + // bend dispatch closure. Browser can't open raw TCP, so we POST + // the S-expression payload to the configured worker URL and read + // the response text back. Sync XHR is the only sync HTTP available + // in a Web Worker; perfect for the blocking eval model lumbda + // primitives expect. + const refs = { bendUrl: null }; + globalThis._lumbdaPyBendCall = (payload) => { + if (!refs.bendUrl) return "no bend URL configured"; + try { + const xhr = new XMLHttpRequest(); + xhr.open("POST", refs.bendUrl, false); // sync + xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); + xhr.send(payload); + return xhr.responseText || ""; + } catch (e) { + return `bend error: ${e.message || String(e)}`; + } + }; + await pyodide.runPythonAsync(` import sys, io sys.path.insert(0, "/home/pyodide") @@ -30,6 +51,18 @@ _env = lumbda.make_global_env() for _e in lumbda.read_all(lumbda.PRELUDE): lumbda.leval(_e, _env) +# bend!-call primitive — bridges to JS XHR via globalThis._lumbdaPyBendCall. +# Argument: a string payload (the S-expression text). Returns response text. +from js import _lumbdaPyBendCall as _js_bend_call +def _bend_call_prim(args, env): + if not args: + return "" + payload = args[0] + if not isinstance(payload, str): + payload = lumbda.show(payload) + return str(_js_bend_call(payload)) +_env.define(lumbda.S('bend!-call'), _bend_call_prim) + def _lumbda_eval(src): buf = io.StringIO() old = sys.stdout @@ -56,6 +89,7 @@ def _lumbda_eval(src): pyodide.globals.set("_src_in", src); return await pyodide.runPythonAsync("_lumbda_eval(_src_in)"); }, + setBendUrl(url) { refs.bendUrl = url || null; }, heapStats() { // Pyodide's runtime memory is the Emscripten linear memory. // CPython's GC reclaims behind the scenes, so this number diff --git a/wasm/python/lumbda-py.js b/wasm/python/lumbda-py.js index c716882..609cefc 100644 --- a/wasm/python/lumbda-py.js +++ b/wasm/python/lumbda-py.js @@ -2,7 +2,9 @@ // 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 }>. +// Exports createPythonTier() -> Promise<{ evalLisp(src) -> Promise, +// setBendUrl(url) -> void, +// heapStats() -> {used,total} }>. const PYODIDE_VERSION = "0.27.2"; const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`; @@ -22,6 +24,25 @@ async function _bootstrap() { pyodide.FS.writeFile("/home/pyodide/lumbda.py", lumbdaSrc); pyodide.FS.writeFile("/home/pyodide/stdlib.lsp", stdlibSrc); + // bend dispatch closure. Browser can't open raw TCP, so we POST + // the S-expression payload to the configured worker URL and read + // the response text back. Sync XHR is the only sync HTTP available + // in a Web Worker; perfect for the blocking eval model lumbda + // primitives expect. + const refs = { bendUrl: null }; + globalThis._lumbdaPyBendCall = (payload) => { + if (!refs.bendUrl) return "no bend URL configured"; + try { + const xhr = new XMLHttpRequest(); + xhr.open("POST", refs.bendUrl, false); // sync + xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); + xhr.send(payload); + return xhr.responseText || ""; + } catch (e) { + return `bend error: ${e.message || String(e)}`; + } + }; + await pyodide.runPythonAsync(` import sys, io sys.path.insert(0, "/home/pyodide") @@ -30,6 +51,18 @@ _env = lumbda.make_global_env() for _e in lumbda.read_all(lumbda.PRELUDE): lumbda.leval(_e, _env) +# bend!-call primitive — bridges to JS XHR via globalThis._lumbdaPyBendCall. +# Argument: a string payload (the S-expression text). Returns response text. +from js import _lumbdaPyBendCall as _js_bend_call +def _bend_call_prim(args, env): + if not args: + return "" + payload = args[0] + if not isinstance(payload, str): + payload = lumbda.show(payload) + return str(_js_bend_call(payload)) +_env.define(lumbda.S('bend!-call'), _bend_call_prim) + def _lumbda_eval(src): buf = io.StringIO() old = sys.stdout @@ -56,6 +89,7 @@ def _lumbda_eval(src): pyodide.globals.set("_src_in", src); return await pyodide.runPythonAsync("_lumbda_eval(_src_in)"); }, + setBendUrl(url) { refs.bendUrl = url || null; }, heapStats() { // Pyodide's runtime memory is the Emscripten linear memory. // CPython's GC reclaims behind the scenes, so this number diff --git a/www/playground/python/lumbda-py.js b/www/playground/python/lumbda-py.js index c716882..609cefc 100644 --- a/www/playground/python/lumbda-py.js +++ b/www/playground/python/lumbda-py.js @@ -2,7 +2,9 @@ // 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 }>. +// Exports createPythonTier() -> Promise<{ evalLisp(src) -> Promise, +// setBendUrl(url) -> void, +// heapStats() -> {used,total} }>. const PYODIDE_VERSION = "0.27.2"; const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`; @@ -22,6 +24,25 @@ async function _bootstrap() { pyodide.FS.writeFile("/home/pyodide/lumbda.py", lumbdaSrc); pyodide.FS.writeFile("/home/pyodide/stdlib.lsp", stdlibSrc); + // bend dispatch closure. Browser can't open raw TCP, so we POST + // the S-expression payload to the configured worker URL and read + // the response text back. Sync XHR is the only sync HTTP available + // in a Web Worker; perfect for the blocking eval model lumbda + // primitives expect. + const refs = { bendUrl: null }; + globalThis._lumbdaPyBendCall = (payload) => { + if (!refs.bendUrl) return "no bend URL configured"; + try { + const xhr = new XMLHttpRequest(); + xhr.open("POST", refs.bendUrl, false); // sync + xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); + xhr.send(payload); + return xhr.responseText || ""; + } catch (e) { + return `bend error: ${e.message || String(e)}`; + } + }; + await pyodide.runPythonAsync(` import sys, io sys.path.insert(0, "/home/pyodide") @@ -30,6 +51,18 @@ _env = lumbda.make_global_env() for _e in lumbda.read_all(lumbda.PRELUDE): lumbda.leval(_e, _env) +# bend!-call primitive — bridges to JS XHR via globalThis._lumbdaPyBendCall. +# Argument: a string payload (the S-expression text). Returns response text. +from js import _lumbdaPyBendCall as _js_bend_call +def _bend_call_prim(args, env): + if not args: + return "" + payload = args[0] + if not isinstance(payload, str): + payload = lumbda.show(payload) + return str(_js_bend_call(payload)) +_env.define(lumbda.S('bend!-call'), _bend_call_prim) + def _lumbda_eval(src): buf = io.StringIO() old = sys.stdout @@ -56,6 +89,7 @@ def _lumbda_eval(src): pyodide.globals.set("_src_in", src); return await pyodide.runPythonAsync("_lumbda_eval(_src_in)"); }, + setBendUrl(url) { refs.bendUrl = url || null; }, heapStats() { // Pyodide's runtime memory is the Emscripten linear memory. // CPython's GC reclaims behind the scenes, so this number diff --git a/www/repl/python/lumbda-py.js b/www/repl/python/lumbda-py.js index c716882..609cefc 100644 --- a/www/repl/python/lumbda-py.js +++ b/www/repl/python/lumbda-py.js @@ -2,7 +2,9 @@ // 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 }>. +// Exports createPythonTier() -> Promise<{ evalLisp(src) -> Promise, +// setBendUrl(url) -> void, +// heapStats() -> {used,total} }>. const PYODIDE_VERSION = "0.27.2"; const PYODIDE_INDEX_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`; @@ -22,6 +24,25 @@ async function _bootstrap() { pyodide.FS.writeFile("/home/pyodide/lumbda.py", lumbdaSrc); pyodide.FS.writeFile("/home/pyodide/stdlib.lsp", stdlibSrc); + // bend dispatch closure. Browser can't open raw TCP, so we POST + // the S-expression payload to the configured worker URL and read + // the response text back. Sync XHR is the only sync HTTP available + // in a Web Worker; perfect for the blocking eval model lumbda + // primitives expect. + const refs = { bendUrl: null }; + globalThis._lumbdaPyBendCall = (payload) => { + if (!refs.bendUrl) return "no bend URL configured"; + try { + const xhr = new XMLHttpRequest(); + xhr.open("POST", refs.bendUrl, false); // sync + xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8"); + xhr.send(payload); + return xhr.responseText || ""; + } catch (e) { + return `bend error: ${e.message || String(e)}`; + } + }; + await pyodide.runPythonAsync(` import sys, io sys.path.insert(0, "/home/pyodide") @@ -30,6 +51,18 @@ _env = lumbda.make_global_env() for _e in lumbda.read_all(lumbda.PRELUDE): lumbda.leval(_e, _env) +# bend!-call primitive — bridges to JS XHR via globalThis._lumbdaPyBendCall. +# Argument: a string payload (the S-expression text). Returns response text. +from js import _lumbdaPyBendCall as _js_bend_call +def _bend_call_prim(args, env): + if not args: + return "" + payload = args[0] + if not isinstance(payload, str): + payload = lumbda.show(payload) + return str(_js_bend_call(payload)) +_env.define(lumbda.S('bend!-call'), _bend_call_prim) + def _lumbda_eval(src): buf = io.StringIO() old = sys.stdout @@ -56,6 +89,7 @@ def _lumbda_eval(src): pyodide.globals.set("_src_in", src); return await pyodide.runPythonAsync("_lumbda_eval(_src_in)"); }, + setBendUrl(url) { refs.bendUrl = url || null; }, heapStats() { // Pyodide's runtime memory is the Emscripten linear memory. // CPython's GC reclaims behind the scenes, so this number