Repository navigation
Expand file tree
/
Copy pathruntime.js
More file actions
139 lines (131 loc) · 4.27 KB
/
Copy pathruntime.js
File metadata and controls
139 lines (131 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Shared Pyodide setup for the worker and the experimental SDL runtime.
// The Pyodide loader and runtime are bundled in instead of being imported at
// runtime, so loading does not depend on how the host serves .mjs files.
import { loadPyodide } from "pyodide";
import createPyodideModule from "pyodide/pyodide.asm.mjs";
import displaySource from "./python/acode_display.py";
import mplBackendSource from "./python/acode_mpl_backend.py";
import pygameSource from "./python/acode_pygame.py";
// Packages not bundled with the plugin are fetched on demand from the CDN
// build matching the bundled runtime (PYODIDE_VERSION is injected at build time)
export const PACKAGE_BASE_URL = `https://cdn.jsdelivr.net/pyodide/v${PYODIDE_VERSION}/full/`;
const NOISE = /already loaded from|^No new packages to load/;
/**
* Loads Pyodide from `<baseUrl>lib/` and installs the display helpers.
* @param {object} options
* @param {string} options.baseUrl plugin base url, ending with "/"
* @param {(text: string) => void} options.stdout
* @param {(text: string) => void} options.stderr
* @param {() => string | null} options.stdin
* @param {(base64Png: string, scale: number) => void} options.showImage
* @param {Record<string, string>} [options.env] extra environment variables
*/
export async function loadRuntime({
baseUrl,
stdout,
stderr,
stdin,
showImage,
env = {},
}) {
let fail;
const failed = new Promise((_, reject) => {
fail = reject;
});
const restore = patchInstantiateStreaming(fail);
try {
const pyodide = await Promise.race([
loadPyodide({
indexURL: `${baseUrl}lib/`,
createPyodideModule,
packageBaseUrl: PACKAGE_BASE_URL,
env: {
MPLBACKEND: "module://acode_mpl_backend",
// written by acode_display to size figures for the console
MATPLOTLIBRC: "/tmp/acode-matplotlibrc",
...env,
},
stdout,
stderr,
stdin,
}),
failed,
]);
pyodide.registerJsModule("_acode", { show_image: showImage });
const sitePackages = pyodide.runPython(
"import site; site.getsitepackages()[0]",
);
pyodide.FS.writeFile(`${sitePackages}/acode_display.py`, displaySource);
pyodide.FS.writeFile(
`${sitePackages}/acode_mpl_backend.py`,
mplBackendSource,
);
pyodide.FS.writeFile(`${sitePackages}/acode_pygame.py`, pygameSource);
pyodide.runPython("import importlib; importlib.invalidate_caches()");
return pyodide;
} finally {
restore();
}
}
/**
* Loads the packages `code` imports and prepares display hooks for them.
* `width` (CSS px) and `pixelRatio` describe the console images are shown in.
*/
export async function prepareCode(
pyodide,
code,
{ message, error, width = 0, pixelRatio = 1 },
) {
await pyodide.loadPackagesFromImports(code, {
// only report real downloads, not "already loaded" on every run
messageCallback: (text) => {
if (!NOISE.test(text)) message(text);
},
errorCallback: error,
});
callDisplay(pyodide, "prepare", code, width, pixelRatio);
}
/** Shows matplotlib figures the code created but never showed */
export function flushFigures(pyodide) {
callDisplay(pyodide, "flush_figures");
}
function callDisplay(pyodide, name, ...args) {
const display = pyodide.pyimport("acode_display");
try {
display[name](...args);
} finally {
display.destroy();
}
}
/**
* Local file hosts in Android WebView often do not serve .wasm as
* `application/wasm`, which makes instantiateStreaming throw. Pyodide only
* logs that error and never settles, so fall back to an ArrayBuffer and
* report any remaining failure through `onError`.
* @returns {() => void} restores the original function
*/
function patchInstantiateStreaming(onError) {
const instantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = async (source, imports) => {
try {
const response = await source;
if (!response.ok) {
throw new Error(`${response.url} (HTTP ${response.status})`);
}
const type = response.headers.get("Content-Type") ?? "";
if (instantiateStreaming && type.startsWith("application/wasm")) {
return await instantiateStreaming(response, imports);
}
return await WebAssembly.instantiate(
await response.arrayBuffer(),
imports,
);
} catch (error) {
onError(error);
throw error;
}
};
return () => {
WebAssembly.instantiateStreaming = instantiateStreaming;
};
}