# P2w: A Python-to-WebAssembly AOT Compiler (v0.2.1)

**URL:** <https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768>\
**Category:** WebAssembly\
**Tags:** release\
**Created:** [March 31, 2026, 6:49am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768 "2026-03-31T06:49:59Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [March 31, 2026, 6:49am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/1 "2026-03-31T06:49:59Z")

</div>

I’ve been working on **p2w** , a side project that compiles a subset of Python to WebAssembly, using the WASM GC proposal for memory management. The compiler translates Python source directly to WAT, with no intermediate libraries (IL), which makes the codegen surprisingly easy to follow, though probably not good enough if we ever want proper optimisation passes.

The project was inspired by Eli Bendersky’s blog post [Compiling Scheme to WebAssembly](https://eli.thegreenplace.net/2026/compiling-scheme-to-webassembly/) and his [bobscheme](https://github.com/eliben/bobscheme) compiler, as well as other previous projects, including several Python-to-JavaScript compilers.

## What’s the point?

Mostly curiosity: how far can Python semantics be mapped onto WebAssembly and its GC extensions? Can we run Python in the browser without shipping a full interpreter?

I don’t pretend it has any industrial quality, and I’m not sure if this could be useful to anyone (even myself). But it does compile and run most of the close to 100 [toy example programs](https://github.com/abilian/p2w/tree/main/programs/internal) I use to measure its progress, which is satisfying enough.

## What works today (at least in part)

- Data types: integers (arbitrary precision via i31/i64 boxing), floats, booleans, strings, bytes, lists, tuples, dicts, sets
- Control flow: if/elif/else, for/while with break/continue/else, match statements
- Functions: default args, \*args/\*\*kwargs, closures, lambdas, decorators
- Classes: inheritance, properties, static/class methods, special methods
- Generators, context managers, f-strings, walrus operator
- Exception handling: try/except/finally, raise, chaining
- JavaScript interop for browser integration
- 100+ golden test programs, property-based testing with Hypothesis

## On performance

[Benchmarks](https://github.com/abilian/p2w/tree/main/programs/benchmarks) show x3 to x10 acceleration over CPython — but only when using unboxed native integers (i32) via type annotations like `x: i32`, so it’s not really a fair comparison, but it’s also largely the point of the experiment. This comes at the expense of strict Python compatibility, since `i32` will happily overflow where Python integers would not.

## What this is not

This is a side project. Many Python features are still missing or incomplete. There’s no package ecosystem, no stdlib beyond builtins, etc. No serious application has been written in it yet. Expect (very) rough edges.

Source is on [GitHub](https://github.com/abilian/p2w). Package is on [PyPI](https://pypi.org/project/p2w/).

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 8:22am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/2 "2026-03-31T08:22:32Z")

</div>

> [@sfermigier](#):
>
> I’ve been working on **p2w** , a side project that compiles a subset of Python to WebAssembly

Very cool! I gotta say, I am amused that the target language is called “Wat” 🙂

(from your README:)

> [@](#):
>
> p2w provides seamless JavaScript interoperability via the js module:

Nice. I was under the impression that WebAssembly code ran on a separate thread to anything that can use the DOM. Is that no longer the case? Am I out of date? If it’s now possible for Python code to compile directly to something that runs in a browser and has full access to the DOM, that’s extremely cool and I want to delve into it.

Do you have any sort of list of features that _aren’t_ supported? And, is it your goal ultimately to support all features, or do you intend for this to forever be restricted (like PyPy’s rpy)?

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [March 31, 2026, 9:48am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/3 "2026-03-31T09:48:27Z")

</div>

> [@Rosuav](#):
>
> I was under the impression that WebAssembly code ran on a separate thread to anything that can use the DOM.

“Seamless” in the sense you can do:

```python
import js

# Get canvas and context
canvas = js.document.getElementById("chart")
ctx = canvas.getContext("2d")

```

Which is (in theory) pretty cool. Internally, it still has to interact with JavaScript.

> Do you have any sort of list of features that _aren’t_ supported?

By design: anything that an AOT compiler can’t (reasonably) support, including “eval”.

Otherwise, a generic test suite would be useful to validate what works and what doesn’t (for every Python implementation). At some point, I used the micropython test suite ( [micropython/tests/micropython at master · micropython/micropython · GitHub](https://github.com/micropython/micropython/tree/master/tests/micropython) ) but not anymore.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 10:04am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/4 "2026-03-31T10:04:58Z")

</div>

> [@sfermigier](#):
>
> “Seamless” in the sense you can do:
> 
> ```python
> import js
> 
> # Get canvas and context
> canvas = js.document.getElementById("chart")
> ctx = canvas.getContext("2d")
> 
> ```
> 
> Which is (in theory) pretty cool. Internally, it still has to interact with JavaScript.

Yeah, that’s what I saw that piqued my interest.

How does this interaction with JS work? Is every call done asynchronously by sending a message to the JS event loop and getting something back? At what point does this happen? For example,with `js.document.getElementById("chart")`, a naive approach would be “hey JS, what is `document`? hey JS, take that thing, what is its `getElementById`? hey JS, call that thing with the parameter `"chart"` and give me back the result”, which would make for a pretty inefficient messaging system. OTOH, batching things up would mean you have to figure out when to actually send the batch across, increasing complexity.

> [@sfermigier](#):
>
> > Do you have any sort of list of features that _aren’t_ supported?
> 
> By design: anything that an AOT compiler can’t (reasonably) support, including “eval”.

Completely fair.

> [@sfermigier](#):
>
> Otherwise, a generic test suite would be useful to validate what works and what doesn’t (for every Python implementation).

Cool, maybe I’ll try the core CPython test suite and see how much fails. There’s going to be a ton of stuff in the stdlib that will fail (presumably things like opening files and sockets won’t be permitted), but it would be neat to get a rundown of each failure categorizing them as “Not possible”, “Planned/desired”, “Not out of the question but I only have 168 hours a week”, etc.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [March 31, 2026, 10:09am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/5 "2026-03-31T10:09:47Z")

</div>

> [@Rosuav](#):
>
> Cool, maybe I’ll try the core CPython test suite and see how much fails.

The parts of the CPython test suite that are used for emscripten builds might be a better check, as I assume that this compiler will have most, if not all, of the same limitations that emscripten does.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 31, 2026, 10:16am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/6 "2026-03-31T10:16:16Z")

</div>

It almost certainly will do. But simple web assembly modules can be much much smaller, than a full posix emulation setup from emscripten (which can also be finecky).

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 10:40am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/7 "2026-03-31T10:40:11Z")

</div>

Okay, so, one big one that’s gonna be needed is imports 🤣 I don’t think I can really try this without the ability to import other modules. But I did find a couple of fairly simple scripts of mine that don’t have a single import in them, and got this error when trying to compile:

```python
$ python3 -m p2w squaresum.py |wat2wasm - -o squaresum.wasm
-:71:30: error: unexpected token "(", expected i32, i64, f32, f64, v128, externref or funcref.
  (global $tmp_pop_dict (mut (ref null eq)) (ref.null eq))
                             ^
-:71:35: error: unexpected token null.
  (global $tmp_pop_dict (mut (ref null eq)) (ref.null eq))
                                  ^^^^
-:71:40: error: unexpected token eq.
  (global $tmp_pop_dict (mut (ref null eq)) (ref.null eq))
                                       ^^
-:71:55: error: unexpected token eq.
  (global $tmp_pop_dict (mut (ref null eq)) (ref.null eq))
                                                      ^^

```

This is using wabt 1.0.32 as provided by Debian Bookworm, and also 1.0.36 as in Devuan Excalibur. What version of wabt have you been using? I see on GitHub that they’re up to 1.0.40, would have to try that next.

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [March 31, 2026, 10:44am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/8 "2026-03-31T10:44:43Z")

</div>

> How does this interaction with JS work?

It’s extremely ad-hoc at this point, but all happens in a single thread, to answer your question. There must be (given the current specs, AFAIK, though I also understand there are proposals under way to get rid of it) a JS module between the WASM context and the browser.

> Do you have any sort of list of features that _aren’t_ supported?

In addition to “eval()”, I should add that the general philosophy is that things work better (or at all) when the Python source to be compiled is properly annotated.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [March 31, 2026, 3:41pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/9 "2026-03-31T15:41:35Z")

</div>

> [@Rosuav](#):
>
> What version of wabt have you been using?

> wasm-gc is not supported by wabt

> <https://github.com/WebAssembly/wabt/issues/2530#issuecomment-2590248680>
>
> code example:
> \`\`\`wasm
> ;; wat2wasm --enable-gc ./test.wat
> (module
> (type $…i32\_arr (array (mut i32)) )
> 
> (func $createArray (param $len i32) (result i32) 
> (array.new
> $i32\_arr
> (i32.const 42) ;; initial value 
> (local.get $len) ;; array len
> )
> (array.get
> $i32\_arr
> (i32.const 0) ;; element idx
> )
> )
> 
> (export "createArray" (func $createArray) )
> )
> \`\`\`
> error msg:
> \`\`\`
> ./test.wat:6:10: error: unexpected token "array.new", expected an instr.
> (array.new
> \`\`\`
> 
> wat2wasm doesn't support \`array\` instr?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 4:04pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/10 "2026-03-31T16:04:48Z")

</div>

Hmm, I don’t know enough about wat to be able to understand how that relates. Are you suggesting something that I can do differently?

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [March 31, 2026, 4:11pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/11 "2026-03-31T16:11:06Z")

</div>

Current version on my machine is:

```python
❮ wat2wasm --version
1.0.39

```

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [March 31, 2026, 4:14pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/12 "2026-03-31T16:14:59Z")

</div>

The current project requirements list a WASM runtime with GC support. I’m not sure how this would work if wat2wasm lacks GC support.

@sfermigier Could you provide compiled WASM files for the demos?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 4:26pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/13 "2026-03-31T16:26:16Z")

</div>

> [@elis.byberi](#):
>
> The current project requirements list a WASM runtime with GC support. I’m not sure how this would work if wat2wasm lacks GC support.

Oh. Hmm. The project also stipulates that wabt is needed “for wat2wasm”. But also, I’m not sure if that’s the problem I was seeing.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [March 31, 2026, 5:02pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/14 "2026-03-31T17:02:43Z")

</div>

> [@Rosuav](#):
>
> `ref null eq`

It seems this was added with the GC proposal, so `wat2wasm` cannot compile it:

> - Add a new form of _typed reference type_ `ref $t` and a nullable variant `(ref null $t)`, where `$t` is a type index; can be used as both a value type or an element type for tables

> <https://github.com/WebAssembly/gc/blob/main/proposals/function-references/Overview.md>

I came to this conclusion while trying to compile the demos, since I’m not very familiar with the WebAssembly specs.

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [March 31, 2026, 5:08pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/15 "2026-03-31T17:08:02Z")

</div>

Sorry, the README was not up-to-date. The proper tool to use is `wasm-tools`, not `wabt`.

I have made a new release (0.2.2) and updated the README.

It’s now possible to call `p2w` with the `-r` option which calls the tools and the runner (nodejs for now) in sequence:

```python
❮ cat fib.py
def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

print(fib(30))

❮ uv run p2w -r fib.py
832040

```

Or to call the tools one by one:

```python
❮ p2w fib.py > fib.wat
❮ wasm-tools parse fib.wat -o fib.wasm

```

Then you have to run the `.wasm` file, which is a bit tricky. It needs a piece of JavaScript which is currently in `src/p2w/runner.py`

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 31, 2026, 8:24pm UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/16 "2026-03-31T20:24:42Z")

</div>

> [@sfermigier](#):
>
> Sorry, the README was not up-to-date. The proper tool to use is `wasm-tools`, not `wabt`.

Ah. That doesn’t seem to be packaged by Debian, at least not under that name.

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [April 1, 2026, 9:54am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/17 "2026-04-01T09:54:57Z")

</div>

wasm-tools can be installed with:

```python
cargo install --locked wasm-tools

```

Alternatively, on Linux distros that tend to favor stable packages, I install homebrew to get access to recent packages without breaking the system, and contains wasm-tools:

> **[Homebrew on Linux](https://docs.brew.sh/Homebrew-on-Linux)**
>
> Documentation for the missing package manager for macOS (or Linux).

---

<div class="post-metadata">

**Author:** ![anentropic](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/anentropic/32/1984_2.png) [@anentropic](https://discuss.python.org/u/anentropic)\
**Post date:** [April 8, 2026, 11:43am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/18 "2026-04-08T11:43:10Z")

</div>

What sort of size are the generated files?

---

<div class="post-metadata">

**Author:** ![sfermigier](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sfermigier/32/6505_2.png) [@sfermigier](https://discuss.python.org/u/sfermigier)\
**Post date:** [April 8, 2026, 11:58am UTC](https://discuss.python.org/t/p2w-a-python-to-webassembly-aot-compiler-v0-2-1/106768/19 "2026-04-08T11:58:58Z")

</div>

Too big. There is plenty of room for optimisation.

Remember it’s a POC.
