A pure Python WebAssembly runtime
Project description
pwasm
A pure Python WebAssembly runtime.
Warning: This is alpha software. It is significantly slower than WebAssembly runtimes with native extensions like wasmtime-py.
Overview
pwasm is a WebAssembly runtime written entirely in Python with zero external dependencies. It can load and execute .wasm binary modules without requiring any C extensions.
Features
- Pure Python - No external dependencies or C extensions required
- WebAssembly MVP support - Parses and executes WebAssembly 1.0 binary format
- Pythonic API - Access exported functions directly as Python methods
- i32 arithmetic - Full support for 32-bit integer operations
- Control flow - Blocks, loops, conditionals, and branching instructions
- Local and global variables - Get, set, and tee operations with mutability checking
- Memory support - Linear memory with data segment initialization
Installation
pip install pwasm
Or with uv:
uv add pwasm
Requirements
- Python 3.10+
Usage
Loading and Running a WebAssembly Module
from pwasm import decode_module, instantiate
# Load a WASM module from bytes
with open("module.wasm", "rb") as f:
wasm_bytes = f.read()
module = decode_module(wasm_bytes)
instance = instantiate(module)
# Call exported functions directly
result = instance.exports.add(2, 3)
print(result) # 5
Working with Multiple Functions
from pwasm import decode_module, instantiate
module = decode_module(wasm_bytes)
instance = instantiate(module)
# Arithmetic operations
print(instance.exports.add(10, 20)) # 30
print(instance.exports.sub(50, 8)) # 42
print(instance.exports.mul(6, 7)) # 42
Error Handling
from pwasm import decode_module, instantiate
from pwasm.errors import TrapError, DecodeError
# Handle runtime traps (e.g., division by zero)
try:
instance.exports.div_s(10, 0)
except TrapError as e:
print(f"Runtime trap: {e}")
# Handle malformed WASM
try:
module = decode_module(b"invalid wasm")
except DecodeError as e:
print(f"Decode error: {e}")
Architecture
Components
- decoder.py - Parses WebAssembly binary format with LEB128 decoding
- types.py - WebAssembly type system (i32, i64, f32, f64, funcref, externref)
- executor.py - Stack-based bytecode interpreter
- errors.py - Exception hierarchy (WasmError, DecodeError, ValidationError, TrapError, LinkError)
Execution Model
pwasm uses a stack-based execution model faithful to the WebAssembly specification:
- Value Stack - Operand values for instructions
- Call Stack - Function frames with locals and return addresses
- Control Flow - Pre-computed block/loop/if targets for branching
Development
# Clone and setup
git clone https://github.com/simonw/pwasm
cd pwasm
# Run tests
uv run pytest
# Format code
uv run black .
License
Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pwasm-0.1a0.tar.gz.
File metadata
- Download URL: pwasm-0.1a0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
094ee79c46354678491d7d120e6ac63105684bb88e193452bf7ebe97e4f07ad0
|
|
| MD5 |
fc0be8d95394afb45ead1887f7a9c910
|
|
| BLAKE2b-256 |
6df1bbedcac2198ee27cba4589f8597f125f6b6cfed8e7cb86ea8503010abcbb
|
Provenance
The following attestation bundles were made for pwasm-0.1a0.tar.gz:
Publisher:
publish.yml on simonw/pwasm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pwasm-0.1a0.tar.gz -
Subject digest:
094ee79c46354678491d7d120e6ac63105684bb88e193452bf7ebe97e4f07ad0 - Sigstore transparency entry: 813338218
- Sigstore integration time:
-
Permalink:
simonw/pwasm@3c086c0671e125ec0977f7fb0f71306a940f5f02 -
Branch / Tag:
refs/tags/0.1a0 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c086c0671e125ec0977f7fb0f71306a940f5f02 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pwasm-0.1a0-py3-none-any.whl.
File metadata
- Download URL: pwasm-0.1a0-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13066ed79ce8c82e34af9cf5240e4ebe39e31f6efbc9e30e254dc6ece5eea379
|
|
| MD5 |
fbb64900d3e7603b7072d7338aea407d
|
|
| BLAKE2b-256 |
24963fa1081b680869b90824b031ef4566ad1fba4c9258de4443cd6f6f859431
|
Provenance
The following attestation bundles were made for pwasm-0.1a0-py3-none-any.whl:
Publisher:
publish.yml on simonw/pwasm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pwasm-0.1a0-py3-none-any.whl -
Subject digest:
13066ed79ce8c82e34af9cf5240e4ebe39e31f6efbc9e30e254dc6ece5eea379 - Sigstore transparency entry: 813338221
- Sigstore integration time:
-
Permalink:
simonw/pwasm@3c086c0671e125ec0977f7fb0f71306a940f5f02 -
Branch / Tag:
refs/tags/0.1a0 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c086c0671e125ec0977f7fb0f71306a940f5f02 -
Trigger Event:
release
-
Statement type: