Skip to content

Commit

Permalink
Changes for Pyodide 0.26.4 (#3253)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Dec 18, 2024
1 parent ca00eda commit 0033829
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
11 changes: 8 additions & 3 deletions src/pyodide/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 25 additions & 16 deletions src/pyodide/internal/pool/builtin_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 };
}
4 changes: 3 additions & 1 deletion src/pyodide/internal/pool/emscriptenSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { _createPyodideModule } from 'pyodide-internal:generated/pyodide.asm';
import {
setUnsafeEval,
setGetRandomValues,
finishSetup,
} from 'pyodide-internal:pool/builtin_wrappers';

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 0033829

Please sign in to comment.