[−][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, }; 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 values = instance .func("add_one")? .call(&[Value::I32(42)])?; assert_eq!(values[0], Value::I32(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
error | |
wasm |
Macros
imports | Generate an |
Structs
Ctx | The context of the currently running WebAssembly instance. |
Function | A representation of an exported WebAssembly function. |
ImportObject | All of the import data used when instantiating. |
Instance | An instantiated WebAssembly module. |
Module | A compiled WebAssembly module. |
Enums
Value | Represents a WebAssembly value. |
Constants
VERSION | The current version of this crate |
Functions
compile | Compile WebAssembly binary code into a |
compile_with | Compile a |
instantiate | Compile and instantiate WebAssembly code without
creating a |
validate | Perform validation as defined by the
WebAssembly specification. Returns |