Skip to content

Commit e2064d6

Browse files
Emscripten: use better _Py_Version computation for worker module (#129757)
Use integer bit shifting instead of conversion to strings to compute Python version.
1 parent 0fef47e commit e2064d6

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

Tools/wasm/emscripten/web_example/python.worker.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ const emscriptenSettings = {
7070
postMessage({ type: "ready", stdinBuffer: stdinBuffer.sab });
7171
},
7272
async preRun(Module) {
73-
const versionHex = Module.HEAPU32[Module._Py_Version / 4].toString(16);
74-
const versionTuple = versionHex
75-
.padStart(8, "0")
76-
.match(/.{1,2}/g)
77-
.map((x) => parseInt(x, 16));
78-
const [major, minor, ..._] = versionTuple;
73+
const versionInt = Module.HEAPU32[Module._Py_Version >>> 2];
74+
const major = (versionInt >>> 24) & 0xff;
75+
const minor = (versionInt >>> 16) & 0xff;
7976
// Prevent complaints about not finding exec-prefix by making a lib-dynload directory
8077
Module.FS.mkdirTree(`/lib/python${major}.${minor}/lib-dynload/`);
8178
Module.addRunDependency("install-stdlib");

0 commit comments

Comments
 (0)