forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.rs
61 lines (50 loc) · 1.78 KB
/
features.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! WebAssembly is a living standard. Wasmer integrates some
//! WebAssembly features that aren't yet stable but can still be
//! turned on. This example explains how.
//!
//! You can run the example directly by executing in Wasmer root:
//!
//! ```shell
//! cargo run --example features --release --features "cranelift"
//! ```
//!
//! Ready?
use wasmer::{imports, wat2wasm, Features, Instance, Module, Store, Value};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;
fn main() -> anyhow::Result<()> {
// Let's declare the Wasm module with the text representation.
let wasm_bytes = wat2wasm(
br#"
(module
(type $swap_t (func (param i32 i64) (result i64 i32)))
(func $swap (type $swap_t) (param $x i32) (param $y i64) (result i64 i32)
(local.get $y)
(local.get $x))
(export "swap" (func $swap)))
"#,
)?;
// Set up the compiler.
let compiler = Cranelift::default();
// Let's declare the features.
let mut features = Features::new();
// Enable the multi-value feature.
features.multi_value(true);
// Set up the engine. That's where we define the features!
let engine = Universal::new(compiler).features(features);
// Now, let's define the store, and compile the module.
let store = Store::new(&engine.engine());
let module = Module::new(&store, wasm_bytes)?;
// Finally, let's instantiate the module, and execute something
// :-).
let import_object = imports! {};
let instance = Instance::new(&module, &import_object)?;
let swap = instance.exports.get_function("swap")?;
let results = swap.call(&[Value::I32(1), Value::I64(2)])?;
assert_eq!(results.to_vec(), vec![Value::I64(2), Value::I32(1)]);
Ok(())
}
#[test]
fn test_features() -> anyhow::Result<()> {
main()
}