Skip to main content

WebAssembly Interpreter by pure Python

Project description

Pywasm: A WebAssembly interpreter written in pure Python.

A WebAssembly interpreter written in pure Python, no third-party libraries are used.

img

The wasm version currently in use is: WebAssembly Specification, Release 2.0 (Draft 2025-04-25).

The wasi version currently in use is: WASI Preview 1.

Also requires Python version >= 3.12.

Installation

$ pip install pywasm

Some simple examples

  1. First we need a wasm module! Grab our example/fibonacci/bin/fibonacci.wasm file and save a copy in a new directory on your local machine.

  2. Now, instantiate WebAssembly modules directly from underlying sources. This is achieved using the Runtime.instance_from_file method.

import pywasm
pywasm.log.lvl = 1

runtime = pywasm.core.Runtime()
m = runtime.instance_from_file('example/fibonacci/bin/fibonacci.wasm')
r = runtime.invocate(m, 'fibonacci', [10])
print(f'fibonacci(10) = {r[0]}')

A brief description for example

File Description
example/blake2b.py Blake2b hashing algorithm
example/blake2b_direct.py Make the hash result returned as a value, not as an output parameter
example/blake2b_iter.py A benchmarking example using the blake2b hash function
example/blake2b_simd.py Use SIMD instructions to accelerate the blake2b hash function
example/fibonacci.py Fibonacci, which contains loop and recursion
example/fibonacci_env.py Call python/native function in wasm
example/pi.py Calculate π using the agm algorithm
example/wasi_httpbin.py Execute a wasi program and http get from http://httpbin.org
example/wasi_ll.py Execute a wasi program and list information about a directory
example/wasi_stdout.py Execute a wasi program and capture stdout
example/wasi_zen.py Execute a wasi program and print poem "The Zen of Python"

Test

$ python script/build_wabt.py # Download wabt tools at res/wabt.
$ python script/build_spec.py # Download spec tests at res/spec.
$ python script/build_wasi.py # Download wasi tests at res/wasi-testsuite.

$ python test/example.py      # Test example.
$ python test/main.py         # Test main.
$ python test/spec.py         # Test spec.
$ python test/wasi.py         # Test wasi.

Performance

Pywasm is an interpreter implemented using pure python and its standard library, capable of running on any python platform. Due to the inherent characteristics of python, you should not have overly high expectations for pywasm's performance.

However, pywasm's design makes it jit-friendly. Compared to cpython, pywasm's performance on pypy can improve by approximately 10 times, significantly surpassing the average 3-fold improvement.

Thanks

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywasm-2.2.2.tar.gz (5.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pywasm-2.2.2-py2.py3-none-any.whl (44.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pywasm-2.2.2.tar.gz.

File metadata

  • Download URL: pywasm-2.2.2.tar.gz
  • Upload date:
  • Size: 5.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pywasm-2.2.2.tar.gz
Algorithm Hash digest
SHA256 81351bfe198e3789fd11e9be2511888cd8514e4368ebf8483050b442ffa5c79f
MD5 f2a96d182ad06fcbe4e1137cb3cec26f
BLAKE2b-256 ac5230d4eab9ee4720e91d2f6ff7764407aa192714bd7624f89a37bb1e86efd0

See more details on using hashes here.

File details

Details for the file pywasm-2.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pywasm-2.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 44.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pywasm-2.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 198327e40cfe70525b8af58e7c2c912ce2bf76949688088f3ddbb6679f2c68bb
MD5 7387acabf283c8ed602bd16b40ebf1c0
BLAKE2b-256 fa7e5e3dea69d3e468a2a917a2d4a1d1a4c1e40e4ae151aea7b3493cf76d1b10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page