Skip to content

gh-127111: Emscripten Make web example work again #127113

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

Merged
merged 11 commits into from
Dec 2, 2024

Conversation

hoodmane
Copy link
Contributor

@hoodmane hoodmane commented Nov 21, 2024

I moved the web example from Tools/wasm into Tools/wasm/emscripten/web_example. I also added a new target build_emscripten which is build_wasm but also builds the web_example. The web_example needs:

  1. python.html, copied
  2. python.worker.mjs copied
  3. python.mjs and python.wasm output from the main linking of the Python interpreter
  4. The webserver that sets COOP and COEP
  5. python3.14.zip

This last is created by the wasm_assets.py script, which required a pretty small set of changes to work fine for us.

The last thing that should be done is the python.worker.mjs script should be made independent of the Python version: currently 3.14 is hard coded. I ran into trouble doing this, so maybe I can leave it to a followup.

@brettcannon brettcannon removed their request for review November 21, 2024 18:05
I moved the web example from `Tools/wasm` into `Tools/wasm/emscripten/web_example`.
I also added a new target `build_emscripten` which is `build_wasm` but also
builds the web_example. The web_example needs:
1. python.html, copied
2. python.worker.mjs copied
3. python.mjs and python.wasm output from the main linking of the Python interpreter
4. The webserver that sets COOP and COEP
5. python3.14.zip

This last is created by the `wasm_assets.py` script, which required a pretty
small set of changes to work fine for us.

The last thing that should be done is the `python.worker.mjs` script should be
made independent of the Python version: currently 3.14 is hard coded. I ran
into trouble doing this, so maybe I can leave it to a followup.
@JeffersGlass
Copy link
Contributor

The Emscripten seems to work fine, however when viewing the web_example (using the provided server.py), I still get a CORS error:

// Console
The FetchEvent for "https://unpkg.com/[email protected]/css/xterm.css" resulted in a network error response: Cross-Origin-Resource-Policy prevented from serving the response to the client.
# Shell
(base) jglass@jglass-XPS-8930:~/Documents/cpython$ python Tools/wasm/emscripten/web_example/server.py 
Serving HTTP on 127.0.0.1 port 8000 (http://127.0.0.1:8000/) ...
127.0.0.1 - - [26/Nov/2024 07:28:13] "GET /Tools/wasm/emscripten/web_example/python.html HTTP/1.1" 200 -
127.0.0.1 - - [26/Nov/2024 07:28:15] "GET /Tools/wasm/emscripten/web_example/python.worker.mjs HTTP/1.1" 200 -
127.0.0.1 - - [26/Nov/2024 07:28:15] code 404, message File not found
127.0.0.1 - - [26/Nov/2024 07:28:15] "GET /Tools/wasm/emscripten/web_example/python.mjs HTTP/1.1" 404 -
127.0.0.1 - - [26/Nov/2024 07:28:17] code 404, message File not found
127.0.0.1 - - [26/Nov/2024 07:28:17] "GET /mini-coi.js HTTP/1.1" 404 -

@hoodmane
Copy link
Contributor Author

Thanks for testing it @JeffersGlass. Maybe I'll have to try a different CDN. It worked fine for me though...

Copy link
Contributor

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general shape of this makes sense.

From a general process perspective, this needs a news entry to describe that that web example has been updated, and documentation in the WASM README on how to run the example.

However, assuming the usage instructions are python Tools/wasm/emscripten build and open web_example/python.html - I get 2 errors.

First, during the build process, the macOS version of realpath apparently doesn't honor the -s flag, so the usage on L223 of the Tools/emscripten/__main__.py raises an error and python Tools/wasm/emscripten build doesn't complete.

Second, I wasn't able to see the example working in practice. When I open file:///.../web_example/python.html in Chrome, I get the following error:

python.html:50 Uncaught (in promise) SecurityError: Failed to construct 'Worker': Script at 'file:///Users/rkm/projects/python/cpython/cross-build/wasm32-emscripten/web_example/python.worker.mjs' cannot be accessed from origin 'null'.
    at WorkerManager.initialiseWorker (python.html:50:27)
    at new WorkerManager (python.html:45:14)
    at window.onload (python.html:350:33)

If I use python -m http.server to serve the web_example folder, and open python.html, I get:

Uncaught ReferenceError: SharedArrayBuffer is not defined
    at new StdinBuffer (python.worker.mjs:5:24)
    at python.worker.mjs:62:21

EDIT: Strike that - I see from @JeffersGlass comment that python web_example/server.py is the expected operation; that works for me. That only leaves the issue with realpath failing during the build (that's a problem that I didn't pick up with my review of #126903, but we can fix it here)

@bedevere-app
Copy link

bedevere-app bot commented Nov 29, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@hoodmane
Copy link
Contributor Author

hoodmane commented Nov 29, 2024

the macOS version of realpath apparently doesn't honor the -s flag

Apple's crappy gnu coreutils strike again.

@hoodmane
Copy link
Contributor Author

The Emscripten seems to work fine, however when viewing the web_example (using the provided server.py), I still get a CORS error:

@JeffersGlass This isn't a CORS error, you're serving it out of the source directory whereas you need to serve it out of the output directory:

./cross-build/wasm32-emscripten/web_example/server.py

Copy link
Contributor

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of documentation tweaks; I'm still having issues with realpath as well. Not sure if I'm doing something wrong, though.

@hoodmane
Copy link
Contributor Author

hoodmane commented Dec 2, 2024

Thanks for the review @freakboy3742! I continue to appreciate your detailed PR feedback. I've been enjoying working with you so far on this.

@hoodmane
Copy link
Contributor Author

hoodmane commented Dec 2, 2024

@freakboy3742 I reverted changes to __main__.py and moved them over to #127506.
I have made the requested changes; please review again (at least I think I made them...)

@bedevere-app
Copy link

bedevere-app bot commented Dec 2, 2024

Thanks for making the requested changes!

@freakboy3742: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from freakboy3742 December 2, 2024 13:30
@erlend-aasland erlend-aasland removed their request for review December 2, 2024 21:44
Copy link
Contributor

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This obviously needs the changes from #127506 to be fully usable on macOS; but I'm happy with this as a set of changes in itself.

@freakboy3742 freakboy3742 merged commit bfb0788 into python:main Dec 2, 2024
54 of 57 checks passed
@hoodmane hoodmane deleted the web-example branch December 6, 2024 08:49
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
Moves the Emscripten web example into a standalone folder, and updates 
Makefile targets to build the web example. Instructions for usage have
also been added.
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Moves the Emscripten web example into a standalone folder, and updates 
Makefile targets to build the web example. Instructions for usage have
also been added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build The build process and cross-build OS-emscripten
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants