| 1 | // Lexical globals for the upstream Pyodide loader in a Python Worker bundle. |
| 2 | // |
| 3 | // `celld deploy` injects this module into the generated bundle with esbuild's |
| 4 | // `--inject`, so each name below shadows the global of the same name only |
| 5 | // inside the bundle. The isolate's own globals are untouched. |
| 6 | // |
| 7 | // The loader runs as if in a browser Web Worker: it finds `importScripts` |
| 8 | // and `WorkerGlobalScope`, reads `self.location`, and fetches its assets. |
| 9 | // Every asset is already in the bundle, so the fetch below serves them and |
| 10 | // refuses any other runtime URL. A Python Worker never downloads runtime |
| 11 | // code at request time. |
| 12 | import interpreter from "./pyodide.asm.wasm"; |
| 13 | import sentinel from "./sentinel.wasm"; |
| 14 | import stdlib from "./python_stdlib.zip"; |
| 15 | import { SENTINEL_BYTES } from "./config.js"; |
| 16 | |
| 17 | export const RUNTIME_URL = "https://python-runtime.invalid/"; |
| 18 | |
| 19 | const interpreterResponses = new WeakSet(); |
| 20 | |
| 21 | export function importScripts(url) { |
| 22 | throw new Error(`Python runtime scripts are bundled; refusing ${url}`); |
| 23 | } |
| 24 | export class WorkerGlobalScope {} |
| 25 | export const self = Object.create(globalThis); |
| 26 | Object.defineProperty(self, "location", { value: { href: RUNTIME_URL } }); |
| 27 | |
| 28 | export async function fetch(input, init) { |
| 29 | const url = String(input instanceof Request ? input.url : input); |
| 30 | if (url === `${RUNTIME_URL}python_stdlib.zip`) return new Response(stdlib); |
| 31 | if (url === `${RUNTIME_URL}pyodide.asm.wasm`) { |
| 32 | const response = new Response(null); |
| 33 | interpreterResponses.add(response); |
| 34 | return response; |
| 35 | } |
| 36 | if (url.startsWith(RUNTIME_URL)) { |
| 37 | throw new Error(`Python runtime asset is not bundled: ${url}`); |
| 38 | } |
| 39 | return globalThis.fetch(input, init); |
| 40 | } |
| 41 | |
| 42 | // JSPI members are omitted. Pyodide 0.28 turns on stack switching when it |
| 43 | // finds `WebAssembly.Suspending`, and in celld each awaited async entry then |
| 44 | // leaks 48 bytes of the 5 MiB Emscripten stack until an entry hangs. Without |
| 45 | // them Pyodide uses its non-switching path, so `pyodide.ffi.run_sync` and |
| 46 | // JS imports that need it are unavailable. Revisit when the pinned runtime |
| 47 | // changes. |
| 48 | const JSPI_MEMBERS = new Set(["Suspending", "promising", "Suspender"]); |
| 49 | export const WebAssembly = {}; |
| 50 | for (const name of Object.getOwnPropertyNames(globalThis.WebAssembly)) { |
| 51 | if (!JSPI_MEMBERS.has(name)) WebAssembly[name] = globalThis.WebAssembly[name]; |
| 52 | } |
| 53 | |
| 54 | function sameBytes(input, expected) { |
| 55 | const bytes = ArrayBuffer.isView(input) |
| 56 | ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength) |
| 57 | : new Uint8Array(input); |
| 58 | return bytes.length === expected.length && |
| 59 | bytes.every((byte, index) => byte === expected[index]); |
| 60 | } |
| 61 | |
| 62 | // The loader compiles two programs: the interpreter and a tiny GC sentinel. |
| 63 | // Both are deployed as compiled Wasm modules, so compilation returns those |
| 64 | // modules. Any other byte string is refused rather than compiled at run time. |
| 65 | WebAssembly.compile = async (input) => { |
| 66 | if (sameBytes(input, SENTINEL_BYTES)) return sentinel; |
| 67 | throw new globalThis.WebAssembly.CompileError( |
| 68 | "Python runtime requested an unbundled Wasm module", |
| 69 | ); |
| 70 | }; |
| 71 | WebAssembly.instantiate = async (input, imports) => { |
| 72 | if (input instanceof globalThis.WebAssembly.Module) { |
| 73 | return globalThis.WebAssembly.instantiate(input, imports); |
| 74 | } |
| 75 | const module = await WebAssembly.compile(input); |
| 76 | return { module, instance: await globalThis.WebAssembly.instantiate(module, imports) }; |
| 77 | }; |
| 78 | WebAssembly.instantiateStreaming = async (source, imports) => { |
| 79 | const response = await source; |
| 80 | if (interpreterResponses.has(response)) { |
| 81 | const instance = await globalThis.WebAssembly.instantiate(interpreter, imports); |
| 82 | return { module: interpreter, instance }; |
| 83 | } |
| 84 | return WebAssembly.instantiate(await response.arrayBuffer(), imports); |
| 85 | }; |