{"id":47733532,"url":"https://github.com/abilian/p2w","last_synced_at":"2026-04-02T22:03:27.177Z","repository":{"id":345291209,"uuid":"1155217729","full_name":"abilian/p2w","owner":"abilian","description":"Python-\u003eWASM compiler","archived":false,"fork":false,"pushed_at":"2026-03-31T07:49:31.000Z","size":536,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-31T09:44:06.209Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abilian.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-11T09:03:05.000Z","updated_at":"2026-03-31T09:09:32.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/abilian/p2w","commit_stats":null,"previous_names":["abilian/p2w"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/abilian/p2w","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abilian%2Fp2w","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abilian%2Fp2w/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abilian%2Fp2w/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abilian%2Fp2w/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abilian","download_url":"https://codeload.github.com/abilian/p2w/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abilian%2Fp2w/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31317831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-02T21:35:00.834Z","status":"ssl_error","status_checked_at":"2026-04-02T21:34:59.806Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-04-02T22:03:25.371Z","updated_at":"2026-04-02T22:03:27.165Z","avatar_url":"https://github.com/abilian.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# p2w - Python to WebAssembly Compiler\n\n**p2w** compiles a substantial subset of Python to WebAssembly, leveraging WASM GC for automatic memory management.\n\n## Features\n\n### Supported Python Features\n\n- **Data types**: integers (arbitrary precision), floats, booleans, strings, bytes, lists, tuples, dicts, sets\n- **Control flow**: if/elif/else, for/while loops, break/continue, match statements\n- **Functions**: definitions, default arguments, *args/**kwargs, closures, lambdas, decorators\n- **Classes**: inheritance, properties, static/class methods, special methods (`__init__`, `__str__`, etc.)\n- **Comprehensions**: list, dict, set, generator expressions\n- **Exception handling**: try/except/finally, raise, exception chaining\n- **Context managers**: `with` statement\n- **Generators**: yield, generator functions\n- **Other**: f-strings, type annotations (ignored at runtime), walrus operator, unpacking\n\n### JavaScript Interop\n\np2w provides seamless JavaScript interoperability via the `js` module:\n\n```python\nimport js\n\ncanvas = js.document.getElementById(\"chart\")\nctx = canvas.getContext(\"2d\")\nctx.fillRect(0, 0, 100, 100)\njs.console.log(\"Hello from Python!\")\n```\n\n## What Works\n\n### Browser Demos\n\nThe `demos/` directory contains two (somewhat) working browser demos:\n\n- **data-dashboard**: Bar chart visualization using Canvas API\n- **simulation**: Physics simulation with real-time rendering\n\n### Golden Programs\n\nThe `programs/internal/` directory contains **104 test programs** covering the supported Python subset. These serve as both regression tests and documentation of working features, including:\n\n- Classes with inheritance, properties, and special methods\n- Generators and comprehensions\n- Exception handling with chaining\n- Context managers\n- Match statements\n- F-strings and string operations\n- Collection types and methods\n\n### Benchmarks\n\nTwo benchmark suites validate correctness and measure performance:\n\n**`programs/benchmarks/`** - Classic benchmarks adapted for p2w:\n- fibonacci, primes, sieve, matmul\n- fannkuch, binarytrees, nbody\n- mandelbrot, spectralnorm, fasta\n- pystone\n\n**`programs/benchmarks-alioth/`** - Benchmarks from the [Debian Benchmark Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/) with GCC baseline comparison:\n- binarytrees, nbody, spectralnorm, mandelbrot, fannkuchredux\n- Includes a runner script for automated comparison against GCC (`-O3 -ffast-math`)\n\n## Installation\n\n```bash\n# Clone the repository\ngit clone https://git.sr.ht/~sfermigier/p2w\ncd p2w\n\n# Install with uv\nuv sync\n```\n\n## Usage\n\n### Quick Start\n\nGiven a Python source file `fib.py`:\n\n```python\ndef fib(n):\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\nprint(fib(30))\n```\n\nThe simplest way to compile and run it:\n\n```bash\nuv run p2w -r fib.py\n# Output: 832040\n```\n\n### Step by Step\n\nIf you want to inspect or keep the intermediate files:\n\n```bash\n# 1. Compile Python to WAT (WebAssembly Text format)\nuv run p2w fib.py -o fib.wat\n\n# 2. Convert WAT to WASM binary (requires wasm-tools)\nwasm-tools parse fib.wat -o fib.wasm\n```\n\nThe generated WASM cannot run standalone — it imports host functions for I/O (`write_char`, `write_i32`, etc.). The `p2w -r` flag handles this automatically by generating a Node.js loader that provides these imports and runs the module. For browser execution, see the demos in `demos/`.\n\n### CLI Options\n\n```\np2w [-h] [-o OUTPUT] [-r] [-v] [-d] [-V] source\n\n  -o, --output FILE   write WAT to file instead of stdout\n  -r, --run           compile and run immediately (requires wasm-tools + Node.js)\n  -v, --verbose       show compilation details on stderr\n  -d, --debug         dump AST and debug info to stderr\n  -V, --version       show version\n```\n\n### As a Library\n\n```python\nfrom p2w import compile_to_wat\n\nwat_code = compile_to_wat('print(\"hello\")')\n```\n\n## Examples\n\n### Fibonacci\n\n```python\ndef fib(n: int) -\u003e int:\n    a, b = 0, 1\n    for i in range(n):\n        a, b = b, a + b\n    return a\n\nprint(f\"fib(30) = {fib(30)}\")\n```\n\n### Browser Demo\n\nThe `demos/` directory contains browser examples:\n\n- **data-dashboard**: Interactive bar chart visualization\n- **simulation**: Physics simulation\n\nTo run a demo:\n\n```bash\ncd demos/data-dashboard\nmake  # Builds app.wasm\n# Serve with any HTTP server and open index.html\n```\n\n## Development\n\n```bash\n# Run tests\nmake test\n\n# Run linting and type checking\nmake lint\n\n# Format code\nmake format\n\n# Run tests with coverage\nmake test-cov\n```\n\n### Test Structure\n\n- `tests/a_unit/` - Unit tests\n- `tests/b_integration/` - Integration tests\n- `tests/c_e2e/` - End-to-end tests\n\n## Architecture\n\np2w follows a straightforward compilation pipeline:\n\n1. **Parse**: Python source -\u003e AST (using Python's `ast` module)\n2. **Analyze**: Scope analysis, type inference\n3. **Compile**: AST -\u003e WAT (WebAssembly Text format)\n4. **Assemble**: WAT -\u003e WASM (via `wasm-tools`)\n\nThe compiler generates WAT code that uses WASM 3.0 GC features for automatic memory management of Python objects.\n\n## Requirements\n\n- Python 3.12+\n- [wasm-tools](https://github.com/bytecodealliance/wasm-tools) (for WAT to WASM conversion)\n- [Node.js](https://nodejs.org/) 22+ (for running compiled WASM via `p2w -r`)\n- For browser execution: any browser with WASM GC support (recent Chrome/Firefox)\n\n## Prior Art and References\n\nIt's too long at this point to cite every influence and/or alternative. Here are a few that stand out:\n\n- [Compiling Scheme to WebAssembly](https://eli.thegreenplace.net/2026/compiling-scheme-to-webassembly/) - Huge influence on unstucking the project\n- [Compylo](https://github.com/abilian/compylo) - A previous attempt, started by Ethan Zouzoulkowsky (then student at EPITA) in 2023\n- [Prescrypt](https://github.com/abilian/prescrypt) - A Python-\u003eJS compiler, itself based on [PScript](https://github.com/flexxui/pscript/)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabilian%2Fp2w","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabilian%2Fp2w","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabilian%2Fp2w/lists"}