gh-126898 Emscripten support: Use es6 modules #126903
Open
+59
−30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We delete
node_pre.js
and--pre-js
and move that logic tonode_entry.mjs
. In order to accessFS
from outside ofpython.mjs
, we need to add-sEXPORTED_RUNTIME_METHODS=FS
. Instead of invokingpython.mjs
directly, we will invokenode_entry.mjs
.One question on naming:
python.mjs
now cannot be used directly without some setup. Pyodide's naming convention calls thispyodide.asm.js
and the equivalent ofnode_entry.mjs
is calledpyodide.mjs
. So if we wanted to follow this same naming convention, we might renamepython.mjs
==>python.asm.mjs
andnode_entry.mjs
==>python.mjs
. I think what I've used here is fine though.Special care has to be taken to ensure that we get
sys.executable
correct. It should point to the original path that was invoked, without symlinks resolved. I updated the generatedpython.sh
to calculate it's own absolute path withrealpath -s $0
and pass that in. Thennode_entry.mjs
setsthisProgram
to the path passed in. I manually validated this by creating a symlink topython.sh
and invoking it.sys.executable
andsys._base_exectable
indeed point to the symlink, not to the build directorypython.sh
and not tonode_entry.mjs
.Pyodide solves this same problem by making the Python shell script into a node/bash polyglot. I think the approach I'm using here is better.
https://github.com/pyodide/pyodide/blob/main/src/templates/python
I haven't been able to track down how in the code
thisProgram
actually makes its way intosys.executable
, which is frustrating. But it is working in my tests and is the intended behavior by both Emscripten and Python.