diff --git a/src/pyodide/BUILD.bazel b/src/pyodide/BUILD.bazel index ea1c76548d9..aa0695d5526 100644 --- a/src/pyodide/BUILD.bazel +++ b/src/pyodide/BUILD.bazel @@ -178,14 +178,19 @@ esbuild( config = "internal/pool/esbuild.config.mjs", entry_point = "internal/pool/emscriptenSetup.ts", external = [ + "child_process", + "crypto", "fs", "path", "url", "vm", - "path", - "crypto", "ws", - "child_process", + "node:child_process", + "node:crypto", + "node:fs", + "node:path", + "node:url", + "node:vm", ], format = "esm", output = "generated/emscriptenSetup.js", diff --git a/src/pyodide/internal/pool/builtin_wrappers.ts b/src/pyodide/internal/pool/builtin_wrappers.ts index dbb31d27518..4ddefd635fd 100644 --- a/src/pyodide/internal/pool/builtin_wrappers.ts +++ b/src/pyodide/internal/pool/builtin_wrappers.ts @@ -68,11 +68,35 @@ export function monotonicDateNow(): number { * - ctypes is quite slow even by Python's standards * - Normally ctypes allocates all closures up front */ +let finishedSetup = false; +export function finishSetup() { + finishedSetup = true; +} + export function newWasmModule(buffer: Uint8Array): WebAssembly.Module { - checkCallee(); + if (finishedSetup) { + checkCallee(); + } return UnsafeEval.newWasmModule(buffer); } +export async function wasmInstantiate( + mod: WebAssembly.Module | Uint8Array, + imports: WebAssembly.Imports +): Promise<{ module: WebAssembly.Module; instance: WebAssembly.Instance }> { + let module; + if (mod instanceof WebAssembly.Module) { + module = mod; + } else { + if (finishedSetup) { + checkCallee(); + } + module = UnsafeEval.newWasmModule(mod); + } + const instance = new WebAssembly.Instance(module, imports); + return { module, instance }; +} + /** * Check that the callee is `convertJsFunctionToWasm` by formatting a stack * trace and using `prepareStackTrace` to read out the callee. It should be @@ -120,18 +144,3 @@ function prepareStackTrace(_error: Error, stack: StackItem[]): boolean { return false; } } - -export async function wasmInstantiate( - mod: WebAssembly.Module | Uint8Array, - imports: WebAssembly.Imports -): Promise<{ module: WebAssembly.Module; instance: WebAssembly.Instance }> { - let module; - if (mod instanceof WebAssembly.Module) { - module = mod; - } else { - checkCallee(); - module = UnsafeEval.newWasmModule(mod); - } - const instance = new WebAssembly.Instance(module, imports); - return { module, instance }; -} diff --git a/src/pyodide/internal/pool/emscriptenSetup.ts b/src/pyodide/internal/pool/emscriptenSetup.ts index d72946898c6..276c3c6be6a 100644 --- a/src/pyodide/internal/pool/emscriptenSetup.ts +++ b/src/pyodide/internal/pool/emscriptenSetup.ts @@ -16,6 +16,7 @@ import { _createPyodideModule } from 'pyodide-internal:generated/pyodide.asm'; import { setUnsafeEval, setGetRandomValues, + finishSetup, } from 'pyodide-internal:pool/builtin_wrappers'; /** @@ -168,7 +169,7 @@ function getEmscriptenSettings( function* featureDetectionMonkeyPatchesContextManager() { const global = globalThis as any; // Make Emscripten think we're in the browser main thread - global.window = {}; + global.window = { sessionStorage: {} }; global.document = { createElement() {} }; global.sessionStorage = {}; // Make Emscripten think we're not in a worker @@ -212,6 +213,7 @@ export async function instantiateEmscriptenModule( const emscriptenModule = await emscriptenSettings.readyPromise; emscriptenModule.setUnsafeEval = setUnsafeEval; emscriptenModule.setGetRandomValues = setGetRandomValues; + finishSetup(); return emscriptenModule; } catch (e) { console.warn('Error in instantiateEmscriptenModule');