2019-03-29 09:17:24 +00:00
|
|
|
<p align="center">
|
|
|
|
<a href="https://wasmer.io" target="_blank" rel="noopener noreferrer">
|
2019-09-24 20:36:31 +00:00
|
|
|
<img width="300" src="https://raw.githubusercontent.com/wasmerio/wasmer/master/logo.png" alt="Wasmer logo">
|
2019-03-29 09:17:24 +00:00
|
|
|
</a>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p align="center">
|
2019-09-24 20:36:31 +00:00
|
|
|
<a href="https://dev.azure.com/wasmerio/wasmer/_build/latest?definitionId=3&branchName=master">
|
|
|
|
<img src="https://img.shields.io/azure-devops/build/wasmerio/wasmer/3.svg?style=flat-square" alt="Build Status">
|
2019-03-29 09:17:24 +00:00
|
|
|
</a>
|
|
|
|
<a href="https://github.com/wasmerio/wasmer/blob/master/LICENSE">
|
2019-09-24 20:36:31 +00:00
|
|
|
<img src="https://img.shields.io/github/license/wasmerio/wasmer.svg?style=flat-square" alt="License">
|
2019-03-29 09:17:24 +00:00
|
|
|
</a>
|
|
|
|
<a href="https://spectrum.chat/wasmer">
|
|
|
|
<img src="https://withspectrum.github.io/badge/badge.svg" alt="Join the Wasmer Community">
|
|
|
|
</a>
|
|
|
|
<a href="https://crates.io/crates/wasmer-runtime">
|
2019-09-24 20:42:17 +00:00
|
|
|
<img src="https://img.shields.io/crates/d/wasmer-runtime.svg?style=flat-square" alt="Number of downloads from crates.io">
|
2019-03-29 09:17:24 +00:00
|
|
|
</a>
|
|
|
|
<a href="https://docs.rs/wasmer-runtime">
|
|
|
|
<img src="https://docs.rs/wasmer-runtime/badge.svg" alt="Read our API documentation">
|
|
|
|
</a>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
# Wasmer Runtime
|
|
|
|
|
|
|
|
Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully
|
|
|
|
compatible with Emscripten, Rust and Go. [Learn
|
|
|
|
more](https://github.com/wasmerio/wasmer).
|
|
|
|
|
|
|
|
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`][func.call] to call an exported function safely.
|
2019-01-24 18:51:20 +00:00
|
|
|
|
|
|
|
[`instantiate`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/fn.instantiate.html
|
|
|
|
[`Instance`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html
|
|
|
|
[`call`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html#method.call
|
|
|
|
[`func`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Instance.html#method.func
|
|
|
|
[func.call]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/struct.Function.html#method.call
|
|
|
|
|
2019-03-29 09:17:24 +00:00
|
|
|
## Example
|
2019-01-24 18:51:20 +00:00
|
|
|
|
|
|
|
Given this WebAssembly:
|
|
|
|
|
|
|
|
```wat
|
|
|
|
(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))
|
|
|
|
```
|
|
|
|
|
2019-03-29 09:17:24 +00:00
|
|
|
compiled into Wasm bytecode, we can call the exported `add_one` function:
|
2019-01-24 18:51:20 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
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,
|
|
|
|
Value,
|
|
|
|
imports,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() -> error::Result<()> {
|
|
|
|
// We're not importing anything, so make an empty import object.
|
|
|
|
let import_object = imports! {};
|
|
|
|
|
2019-10-01 20:45:44 +00:00
|
|
|
let instance = instantiate(WASM, &import_object)?;
|
2019-01-24 18:51:20 +00:00
|
|
|
|
|
|
|
let values = instance
|
2019-10-01 20:45:44 +00:00
|
|
|
.dyn_func("add_one")?
|
2019-01-24 18:51:20 +00:00
|
|
|
.call(&[Value::I32(42)])?;
|
|
|
|
|
|
|
|
assert_eq!(values[0], Value::I32(43));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-03-29 09:17:24 +00:00
|
|
|
## Additional Notes
|
2019-01-24 18:51:20 +00:00
|
|
|
|
2019-03-29 09:17:24 +00:00
|
|
|
The `wasmer-runtime` crate is build to support multiple compiler
|
2019-09-27 00:15:43 +00:00
|
|
|
backends. We support have 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.
|
2019-01-24 18:51:20 +00:00
|
|
|
|
|
|
|
You can specify the compiler you wish to use with the [`compile_with`] function.
|
|
|
|
|
|
|
|
[Cranelift]: https://github.com/CraneStation/cranelift
|
2019-09-27 00:15:43 +00:00
|
|
|
[LLVM]: https://llvm.org
|
|
|
|
[Singlepass]: https://github.com/wasmerio/wasmer/tree/master/lib/singlepass-backend
|
2019-01-24 18:51:20 +00:00
|
|
|
[`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend
|
2019-03-29 09:17:24 +00:00
|
|
|
[`compile_with`]: https://docs.rs/wasmer-runtime/*/wasmer_runtime/fn.compile_with.html
|