[][src]Crate wasmer_runtime

Wasmer-runtime is a library that makes 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.

Here's an 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.
    // ...
];

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

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

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

    let add_one: Func<i32, i32> = instance.func("add_one")?;

    let value = add_one.call(42)?;

    assert_eq!(value, 43);
     
    Ok(())
}

Additional Notes:

The wasmer-runtime is build to support compiler multiple backends. Currently, we support the Cranelift compiler with the wasmer-clif-backend crate.

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

Modules

cache
error
memory
units

Various unit types.

wasm

Various types exposed by the Wasmer Runtime.

Macros

func
imports

Generate an ImportObject safely.

Structs

Ctx

The context of the currently running WebAssembly instance.

DynFunc

A representation of an exported WebAssembly function.

Func
Global
ImportObject

All of the import data used when instantiating.

Instance

An instantiated WebAssembly module.

Memory

A shared or unshared wasm linear memory.

MiddlewareChain
Module

A compiled WebAssembly module.

StreamingCompiler
Table

Enums

Backend
Export
Value

Represents a WebAssembly value.

Constants

VERSION

The current version of this crate

Functions

compile

Compile WebAssembly binary code into a Module. This function is useful if it is necessary to compile a module before it can be instantiated (otherwise, the instantiate function should be used).

compile_with

Compile a Module using the provided compiler from WebAssembly binary code. This function is useful if it is necessary to a compile a module before it can be instantiated and must be used if you wish to use a different backend from the default.

compile_with_config

The same as compile but takes a CompilerConfig for the purpose of changing the compiler's behavior

compile_with_config_with

The same as compile_with_config but takes a Compiler for the purpose of changing the backend.

compiler_for_backend
default_compiler

Get a single instance of the default compiler to use.

instantiate

Compile and instantiate WebAssembly code without creating a Module.

validate

Perform validation as defined by the WebAssembly specification. Returns true if validation succeeded, false if validation failed.