Wasmer Python

1.1.0 · active · verified Sat Apr 11

Wasmer is a comprehensive WebAssembly runtime for Python, allowing developers to execute WebAssembly binaries securely and efficiently within Python applications. It compiles Wasm modules into native code for near-native performance. The current version, 1.1.0, was released in January 2022, and the project is actively maintained with ongoing developments, including features for Wasmer Edge.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compile and run a simple WebAssembly module that exports a 'sum' function. It initializes a Wasmer store with the Cranelift compiler, defines a WAT module, compiles it, instantiates it, and then calls the exported function.

from wasmer import Store, Module, Instance
from wasmer_compiler_cranelift import Compiler
from wasmer import engine

# Create a Wasmer Store, which holds the engine and compiler
store = Store(engine.JIT(Compiler))

# Define a WebAssembly module in WAT (WebAssembly Text Format)
wasm_module_wat = """
(module
  (type (func (param i32 i32) (result i32)))
  (func (export "sum") (type 0) (param i32) (param i32) (result i32)
    local.get 0
    local.get 1
    i32.add)
)
"""

# Compile the module
module = Module(store, wasm_module_wat)

# Instantiate the module
instance = Instance(module)

# Call the exported 'sum' function
result = instance.exports.sum(5, 37)
print(f"The sum is: {result}") # Expected: 42

view raw JSON →