Wasmer Cranelift Compiler

1.1.0 · active · verified Sat Apr 11

The `wasmer-compiler-cranelift` library provides the Cranelift compiler backend for the `wasmer` Python package, enabling the compilation of WebAssembly (Wasm) modules. Cranelift is known for its balanced approach, offering faster compilation times compared to LLVM, moderate runtime performance, and enhanced security against malicious Wasm input. The current Python package version is `1.1.0` (released Jan 2022), though the underlying Wasmer runtime frequently updates, with Wasmer 7.0 (Jan 2026) featuring an upgraded Cranelift backend and new capabilities for Python integration.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Wasmer Store with the Cranelift compiler, compile a simple WebAssembly module defined in WAT format, instantiate it, and call an exported function.

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

# 1. Create a Store with the Cranelift compiler
# You can choose between JIT (Just-In-Time) or Native engine.
# JIT is good for development and dynamic compilation.
store = Store(engine.JIT(Compiler))

# 2. Define a WebAssembly module in WebAssembly Text Format (WAT)
wasm_bytes = wat2wasm(
    """
    (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
      )
    )
    """
)

# 3. Compile the Wasm module
module = Module(store, wasm_bytes)

# 4. Instantiate the module
instance = Instance(module)

# 5. Call an exported function
result = instance.exports.sum(5, 37)
print(f"Result of sum(5, 37): {result}") # Expected: 42

view raw JSON →