emscripten – Compiling Python to WebAssembly
WebAssembly vs asm.js
First, lets take a look how, in principle, WebAssembly is different from asm.js, and whether theres potential to reuse existing knowledge and tooling. The following gives pretty good overview:
- Why create a new standard when there is already asm.js?
- What is the difference between asm.js and web assembly?
- Why WebAssembly is Faster Than asm.js
Lets recapitulate, WebAssembly (MVP, as theres more on its roadmap, roughly):
- is convertible (to some extent) to/from asm.js.
Thus, currently WebAssembly is an iteration on asm.js and targets only C/C++ (and similar languages).
Python on the Web
It doesnt look like GC is the only thing that stops Python code from targeting WebAssembly/asm.js. Both represent low-level statically typed code, in which Python code cant (realistically) be represented. As current toolchain of WebAssembly/asm.js is based on LLVM, a language that can be easily compiled to LLVM IR can be converted to WebAssembly/asm.js. But alas, Python is too dynamic to fit into it as well, as proven by Unladen Swallow and several attempts of PyPy.
This asm.js presentation has slides about the state of dynamic languages. What it means is that currently its only possible to compile whole VM (language implementation in C/C++) to WebAssembly/asm.js and interpret (with JIT where possible) original sources. For Python therere several existing projects:
Micropython: this fork.
There was no built JS file there, so I was able to build it with
trzeci/emscripten/, a ready-made Emscripten toolchain. Something like:
git clone https://github.com/matthewelse/micropython.git cd micropython docker run --rm -it -v $(pwd):/src trzeci/emscripten bash apt-get update && apt-get install -y python3 cd emscripten make -j # to run REPL: npm install && nodejs server.js
micropython.jsof 1.1 MB (225 KB after
gzip -d). The latter is already something to consider, if you need only very compliant implementation without stdlib.
To produce WebAssembly build you can change line 13 of the
CC = emcc -s RESERVED_FUNCTION_POINTERS=20 -s WASM=1
113 KB micropython.js 240 KB micropython.wasm
You can look at HTML output of
emcc hello.c -s WASM=1 -o hello.html, to see how to use these files.
This way you can also potentially build PyPy and CPython in WebAssembly to interpret your Python application in a compliant browser.
Another potentially interesting thing here is Nuitka, a Python to C++ compiler. Potentially it can be possible to build your Python app to C++ and then compile it along with CPython with Emscripten. But practically Ive no idea how to do it.
Otherwise, if download size is not an issue, and youre ready to tackle a lot of rough edges, choose between the three above.
Q3 2020 update
Theres an actively developed Python implementation in Rust, called
RustPython. Because Rust officially supports WebAssembly as
compile target, no surprise theres demo link right in the
top of the readme. Though, its early. Their disclaimer follows.
RustPython is in a development phase and should not be used in
production or a fault intolerant setting.
Our current build supports only a subset of Python syntax.
In short: There are transpilers, but you cant automatically convert any arbitrary Python to Web Assembly, and I doubt you will be able to for a long time to come. Although theoretically the languages are equally powerful, and manual translation is always possible, Python allows for some data structures and expressive modes that requires a very smart inter-language compiler (or transpiler) [see below]. A workaround might be Python to C to Web Assembly since python-to-C technology is moderately mature, but that isnt generally going to work either since Python-to-C is also fragile (see below).
WebAssembly is specifically targeted to C-like languages as you can see at http://webassembly.org/docs/high-level-goals/
Translating from Python to C can be done with tools like PyPy, which has been under development for a long time, but which still does not work for arbitrary Python code. There are several reasons for this:
- Python has some very handy, abstract and nice data structures, but they are hard to translate into static code.
- Python depends on dynamic garbage collection.
- Most Python code depends heavily on various libraries, each of which has its own quirks and issues (such as being written in C, or even assembler).
If you look more carefully into why Python-to-C (or Python to C++) has been so tricky you can see the detailed reasons behind this terse answer, but I think thats outside the scope of your question.
emscripten – Compiling Python to WebAssembly
This wont be possible until web assembly implements garbage collection. You can follow progress here: https://github.com/WebAssembly/proposals/issues/16