Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danlapid committed Oct 2, 2024
1 parent 5c5fde0 commit e9e9d64
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 235 deletions.
8 changes: 2 additions & 6 deletions src/pyodide/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ copy_file(
# TODO: all of these should be fixed by linking our own Pyodide or by upstreaming.

PRELUDE = """
import { newWasmModule, monotonicDateNow, wasmInstantiate } from "pyodide-internal:builtin_wrappers";
import { newWasmModule, monotonicDateNow, wasmInstantiate, getRandomValues } from "pyodide-internal:builtin_wrappers";
// Pyodide uses `new URL(some_url, location)` to resolve the path in `loadPackage`. Setting
// `location = undefined` makes this throw an error if some_url is not an absolute url. Which is what
Expand Down Expand Up @@ -124,11 +124,7 @@ REPLACEMENTS = [
],
[
"crypto.getRandomValues(",
"Module.getRandomValues(Module, ",
],
[
"(shouldRunNow)",
"(Module.noInitialRun)",
"getRandomValues(Module, ",
],
]

Expand Down
1 change: 1 addition & 0 deletions src/pyodide/internal/builtin_wrappers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { default as UnsafeEval } from 'internal:unsafe-eval';
export { getRandomValues } from 'pyodide-internal:topLevelEntropy/lib';

let lastTime: number;
let lastDelta = 0;
Expand Down
203 changes: 0 additions & 203 deletions src/pyodide/internal/emscriptenSetup.ts

This file was deleted.

14 changes: 7 additions & 7 deletions src/pyodide/internal/python.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
Error.stackTraceLimit = Infinity;
import { enterJaegerSpan } from 'pyodide-internal:jaeger';
import {
TRANSITIVE_REQUIREMENTS,
SITE_PACKAGES,
adjustSysPath,
mountSitePackages,
mountWorkerFiles,
} from 'pyodide-internal:setupPackages';
import { reportError } from 'pyodide-internal:util';
import {
SHOULD_RESTORE_SNAPSHOT,
finishSnapshotSetup,
getSnapshotSettings,
maybeSetupSnapshotUpload,
restoreSnapshot,
preloadDynamicLibs,
} from 'pyodide-internal:snapshot';
import {
entropyMountFiles,
Expand Down Expand Up @@ -202,10 +204,6 @@ async function prepareWasmLinearMemory(Module: Module): Promise<void> {
// Note: if we are restoring from a snapshot, runtime is not initialized yet.
mountSitePackages(Module, SITE_PACKAGES.rootInfo);
entropyMountFiles(Module);
Module.noInitialRun = !SHOULD_RESTORE_SNAPSHOT;
preloadDynamicLibs(Module);
Module.getRandomValues = getRandomValues;
Module.removeRunDependency('dynlibs');
if (SHOULD_RESTORE_SNAPSHOT) {
restoreSnapshot(Module);
}
Expand All @@ -224,8 +222,10 @@ export async function loadPyodide(
lockfile: PackageLock,
indexURL: string
): Promise<Pyodide> {
const Module = await setupEmscriptenModule(lockfile, indexURL);
// Finish setting up Pyodide's ffi so we can use the nice Python interface
const emscriptenSettings = getEmscriptenSettings(lockfile, indexURL);
const Module = await enterJaegerSpan('instantiate_emscripten', () =>
instantiateEmscriptenModule(emscriptenSettings)
);
await enterJaegerSpan('prepare_wasm_linear_memory', () =>
prepareWasmLinearMemory(Module)
);
Expand Down
18 changes: 12 additions & 6 deletions src/pyodide/internal/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ export function preloadDynamicLibs(Module: Module): void {
}
}

export function getSnapshotSettings() {
return {
preRun: [preloadDynamicLibs],
// if SNAPSHOT_SIZE is defined, start with the linear memory big enough to
// fit the snapshot. If it's not defined, this falls back to the default.
INITIAL_MEMORY: SNAPSHOT_SIZE,
// skip running main() if we have a snapshot
noInitialRun: SHOULD_RESTORE_SNAPSHOT,
};
}

type DylinkInfo = {
[name: string]: { handles: string[] };
} & {
Expand Down Expand Up @@ -405,17 +416,12 @@ function decodeSnapshot(): void {
}

export function restoreSnapshot(Module: Module): void {
if (!READ_MEMORY || !SNAPSHOT_SIZE) {
if (!READ_MEMORY) {
throw Error('READ_MEMORY not defined when restoring snapshot');
}
Module.growMemory(SNAPSHOT_SIZE);
READ_MEMORY(Module);
}

export function hasSnapshot() {
return !!READ_MEMORY;
}

let TEST_SNAPSHOT: Uint8Array | undefined = undefined;
(function () {
try {
Expand Down
12 changes: 0 additions & 12 deletions src/pyodide/types/Module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,4 @@ interface Module {
opt: object,
path: string
) => WebAssembly.Exports;
growMemory(newSize: number): void;
resolveGlobalSymbol(symName: string): { sym?: (...args: number[]) => number };
getExecutableName(): string;
HEAPU32: Uint32Array;
stringToUTF8OnStack(str: string): number;
stackAlloc(size: number): number;
exitJS(ret: number, implicit: boolean): never;
handleException(e: any): number;
addRunDependency(x: string): void;
removeRunDependency(x: string): void;
noInitialRun: boolean;
getRandomValues(Module: Module, x: Uint8Array): Uint8Array | undefined;
}
2 changes: 1 addition & 1 deletion src/pyodide/types/emscripten.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interface EmscriptenSettings {
) => void
) => WebAssembly.Exports;
reportUndefinedSymbolsNoOp: () => void;
noInitialRun: boolean;
API: {
config: API['config'];
};
readyPromise: Promise<Module>;
}
1 change: 1 addition & 0 deletions src/workerd/jsg/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <kj/map.h>
#include <kj/mutex.h>

namespace workerd::jsg {

// Construct a default V8 platform, with the given background thread pool size.
Expand Down

0 comments on commit e9e9d64

Please sign in to comment.