Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-126898 Emscripten support: Use es6 modules #126903

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hoodmane
Copy link
Contributor

@hoodmane hoodmane commented Nov 16, 2024

We delete node_pre.js and --pre-js and move that logic to node_entry.mjs. In order to access FS from outside of python.mjs, we need to add -sEXPORTED_RUNTIME_METHODS=FS. Instead of invoking python.mjs directly, we will invoke node_entry.mjs.

One question on naming: python.mjs now cannot be used directly without some setup. Pyodide's naming convention calls this pyodide.asm.js and the equivalent of node_entry.mjs is called pyodide.mjs. So if we wanted to follow this same naming convention, we might rename python.mjs ==> python.asm.mjs and node_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 generated python.sh to calculate it's own absolute path with realpath -s $0 and pass that in. Then node_entry.mjs sets thisProgram to the path passed in. I manually validated this by creating a symlink to python.sh and invoking it. sys.executable and sys._base_exectable indeed point to the symlink, not to the build directory python.sh and not to node_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 into sys.executable, which is frustrating. But it is working in my tests and is the intended behavior by both Emscripten and Python.

@hoodmane hoodmane changed the title gh-126898 Switch to exporting an es6 module and using a node_entry script gh-126898 Emscripten support: Use es6 modules Nov 16, 2024
We delete `node_pre.js` and `--pre-js`. Instead that logic moves to
`node_entry.mjs`. In order to access `FS` from outside of `python.mjs`, we need
to add `-sEXPORTED_RUNTIME_METHODS=FS`. Instead of invoking `python.js`
directly, we will invoke `node_entry.mjs`.

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 generated `python.sh` to calculate it's own absolute path with
`realpath -s $0` and pass that in. Then `node_entry.mjs` sets `thisProgram` to
the path passed in. I manually validated this by creating a symlink to
`python.sh` and invoking it. `sys.executable` and `sys._base_exectable`
indeed point to the symlink, not to the build directory `python.sh` and not to
`node_entry.mjs`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant