wasmer

Wasmer Python API

PyPI version Wasmer Python Documentation Wasmer PyPI downloads Wasmer Slack Channel

A complete and mature WebAssembly runtime for Python based on Wasmer.

Features:

  • Easy to use: The wasmer API mimics the standard WebAssembly API,
  • Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
  • Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed.

Example

The very basic example is the following:

from wasmer import Store, Module, Instance

# Create a store, which holds the engine, the compiler etc.
store = Store()

# Let's assume we don't have WebAssembly bytes at hand. We will
# write WebAssembly manually.
module = Module(
    store,
    """
    (module
      (type (func (param i32 i32) (result i32)))
      (func (type 0)
        local.get 0
        local.get 1
        i32.add)
      (export "sum" (func 0)))
    """
)

# Instantiates the module.
instance = Instance(module)

# Now, let's execute the `sum` function.
assert instance.exports.sum(1, 2) == 3

That's it. Now explore the API! Some pointers for the adventurers:

  • The basic elements are Module and Instance,
  • Exports of an instance are represented by the Exports object,
  • Maybe your module needs to import Function, Memory, Global or Table? Well, there is the Python dict for that!
  • It is possible to read and write Memory data with the Python buffer protocol with Buffer.

Have fun!

View Source
from .wasmer import *

__doc__ = wasmer.__doc__