3 unstable releases

0.18.0 Jul 20, 2021
0.18.0-pre.1 Jul 9, 2021
0.17.1 Oct 28, 2020

#114 in WebAssembly

Download history 1447/week @ 2023-12-10 1610/week @ 2023-12-17 594/week @ 2023-12-24 1012/week @ 2023-12-31 1717/week @ 2024-01-07 2697/week @ 2024-01-14 2754/week @ 2024-01-21 2378/week @ 2024-01-28 1973/week @ 2024-02-04 1815/week @ 2024-02-11 2037/week @ 2024-02-18 2467/week @ 2024-02-25 3084/week @ 2024-03-03 3236/week @ 2024-03-10 3740/week @ 2024-03-17 2187/week @ 2024-03-24

12,338 downloads per month
Used in 2 crates

MIT license

1MB
19K SLoC

Wasmer logo

Build Status License Join the Wasmer Community Number of downloads from crates.io Read our API documentation

Wasmer Runtime

Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully compatible with Emscripten, Rust and Go. Learn more.

This crate represents the high-level runtime API, making embedding WebAssembly in your application easy, efficient, and safe.

How to use Wasmer Runtime

The easiest way is to use the instantiate function to create an Instance. Then you can use call or func and then call to call an exported function safely.

Example

Given this WebAssembly:

(module
  (type $t0 (func (param i32) (result i32)))
  (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
    get_local $p0
    i32.const 1
    i32.add))

compiled into Wasm bytecode, we can call the exported add_one function:

static WASM: &'static [u8] = &[
    // The module above compiled to bytecode goes here.
    0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
    0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0b, 0x01, 0x07,
    0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01,
    0x07, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6a, 0x0b, 0x00, 0x1a, 0x04, 0x6e,
    0x61, 0x6d, 0x65, 0x01, 0x0a, 0x01, 0x00, 0x07, 0x61, 0x64, 0x64, 0x5f,
    0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02, 0x70, 0x30,
];

use wasmer_runtime::{
    instantiate,
    DynFunc,
    Value,
    imports,
    error,
};

fn main() -> error::Result<()> {
    // We're not importing anything, so make an empty import object.
    let import_object = imports! {};

    let instance = instantiate(WASM, &import_object)?;

    let values = instance
        .exports
        .get::<DynFunc>("add_one")?
        .call(&[Value::I32(42)])?;

    assert_eq!(values[0], Value::I32(43));
    
    Ok(())
}

Additional Notes

The wasmer-runtime crate is built to support multiple compiler backends. We support having a Cranelift backend in the wasmer-clif-backend crate, a LLVM backend in the wasmer-llvm-backend crate, and the Singlepass backend in the wasmer-singlepass-backend crate. Currently, the Cranelift backend is the default.

You can specify the compiler you wish to use with the compile_with function.

Dependencies

~8–16MB
~225K SLoC