]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Emscripten: use better `_Py_Version` computation for worker module (#129757)
authorAgriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
Fri, 7 Feb 2025 04:55:27 +0000 (04:55 +0000)
committerGitHub <noreply@github.com>
Fri, 7 Feb 2025 04:55:27 +0000 (12:55 +0800)
Use integer bit shifting instead of conversion to strings to compute Python version.

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

index 8043e4199667438c9725d8cbe590d977e61c4168..5f9012a492a39974e912f3297c1fe4e2039a3934 100644 (file)
@@ -70,12 +70,9 @@ const emscriptenSettings = {
     postMessage({ type: "ready", stdinBuffer: stdinBuffer.sab });
   },
   async preRun(Module) {
-    const versionHex = Module.HEAPU32[Module._Py_Version / 4].toString(16);
-    const versionTuple = versionHex
-      .padStart(8, "0")
-      .match(/.{1,2}/g)
-      .map((x) => parseInt(x, 16));
-    const [major, minor, ..._] = versionTuple;
+    const versionInt = Module.HEAPU32[Module._Py_Version >>> 2];
+    const major = (versionInt >>> 24) & 0xff;
+    const minor = (versionInt >>> 16) & 0xff;
     // Prevent complaints about not finding exec-prefix by making a lib-dynload directory
     Module.FS.mkdirTree(`/lib/python${major}.${minor}/lib-dynload/`);
     Module.addRunDependency("install-stdlib");