Home

Awesome

Pywasm: A WebAssembly interpreter written in pure Python.

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

The wasm version currently in use is: WebAssembly Specification, Release 2.0 (Draft 2024-10-29).

Also requires Python version >= 3.12.

Installation

$ pip install pywasm

Some simple examples

  1. First we need a wasm module! Grab our ./examples/fib.wasm file and save a copy in a new directory on your local machine. Note: fib.wasm was compiled from ./examples/fib.c.

  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('./examples/fib.wasm')
r = runtime.invocate(m, 'fib', [10])
print(f'fib(10) = {r[0]}')

A brief description for ./examples

FileDescription
./examples/add.wasmExport i32.add function
./examples/env.wasmCall python/native function in wasm
./examples/fib.wasmFibonacci, which contains loop and recursion
./examples/str.wasmExport a function which returns string
./examples/sum.wasmEqual difference series summation

Test

$ python ./test/test_spec.py

Thanks

License

MIT