You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.9 KiB
102 lines
2.9 KiB
5 years ago
|
//! This is an example of working with mulit-value modules and dealing with
|
||
|
//! multi-value functions.
|
||
|
//!
|
||
|
//! Note that the `Func::wrap*` interfaces cannot be used to return multiple
|
||
|
//! values just yet, so we need to use the more dynamic `Func::new` and
|
||
|
//! `Func::call` methods.
|
||
5 years ago
|
|
||
5 years ago
|
// You can execute this example with `cargo run --example multi`
|
||
|
|
||
|
use anyhow::{format_err, Result};
|
||
5 years ago
|
use std::rc::Rc;
|
||
5 years ago
|
use wasmtime::*;
|
||
5 years ago
|
|
||
|
struct Callback;
|
||
|
|
||
|
impl Callable for Callback {
|
||
5 years ago
|
fn call(&self, args: &[Val], results: &mut [Val]) -> Result<(), Trap> {
|
||
5 years ago
|
println!("Calling back...");
|
||
5 years ago
|
println!("> {} {}", args[0].unwrap_i32(), args[1].unwrap_i64());
|
||
5 years ago
|
|
||
5 years ago
|
results[0] = Val::I64(args[1].unwrap_i64() + 1);
|
||
|
results[1] = Val::I32(args[0].unwrap_i32() + 1);
|
||
5 years ago
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
fn main() -> Result<()> {
|
||
5 years ago
|
// Configure our `Store`, but be sure to use a `Config` that enables the
|
||
|
// wasm multi-value feature since it's not stable yet.
|
||
5 years ago
|
println!("Initializing...");
|
||
5 years ago
|
let engine = Engine::new(Config::new().wasm_multi_value(true));
|
||
5 years ago
|
let store = Store::new(&engine);
|
||
5 years ago
|
|
||
|
// Compile.
|
||
|
println!("Compiling module...");
|
||
5 years ago
|
let module = Module::from_file(&store, "examples/multi.wat")?;
|
||
5 years ago
|
|
||
|
// Create external print functions.
|
||
|
println!("Creating callback...");
|
||
|
let callback_type = FuncType::new(
|
||
|
Box::new([ValType::I32, ValType::I64]),
|
||
|
Box::new([ValType::I64, ValType::I32]),
|
||
|
);
|
||
5 years ago
|
let callback_func = Func::new(&store, callback_type, Rc::new(Callback));
|
||
5 years ago
|
|
||
|
// Instantiate.
|
||
|
println!("Instantiating module...");
|
||
5 years ago
|
let instance = Instance::new(&module, &[callback_func.into()])?;
|
||
5 years ago
|
|
||
5 years ago
|
// Extract exports.
|
||
5 years ago
|
println!("Extracting export...");
|
||
5 years ago
|
let g = instance
|
||
|
.get_export("g")
|
||
|
.and_then(|e| e.func())
|
||
|
.ok_or(format_err!("failed to find export `g`"))?;
|
||
5 years ago
|
|
||
5 years ago
|
// Call `$g`.
|
||
|
println!("Calling export \"g\"...");
|
||
5 years ago
|
let results = g.call(&[Val::I32(1), Val::I64(3)])?;
|
||
5 years ago
|
|
||
|
println!("Printing result...");
|
||
5 years ago
|
println!("> {} {}", results[0].unwrap_i64(), results[1].unwrap_i32());
|
||
5 years ago
|
|
||
5 years ago
|
assert_eq!(results[0].unwrap_i64(), 4);
|
||
|
assert_eq!(results[1].unwrap_i32(), 2);
|
||
5 years ago
|
|
||
5 years ago
|
// Call `$round_trip_many`.
|
||
|
println!("Calling export \"round_trip_many\"...");
|
||
5 years ago
|
let round_trip_many = instance
|
||
|
.get_export("round_trip_many")
|
||
|
.and_then(|e| e.func())
|
||
|
.ok_or(format_err!("failed to find export `round_trip_many`"))?;
|
||
5 years ago
|
let args = vec![
|
||
|
Val::I64(0),
|
||
|
Val::I64(1),
|
||
|
Val::I64(2),
|
||
|
Val::I64(3),
|
||
|
Val::I64(4),
|
||
|
Val::I64(5),
|
||
|
Val::I64(6),
|
||
|
Val::I64(7),
|
||
|
Val::I64(8),
|
||
|
Val::I64(9),
|
||
|
];
|
||
5 years ago
|
let results = round_trip_many.call(&args)?;
|
||
5 years ago
|
|
||
|
println!("Printing result...");
|
||
|
print!(">");
|
||
|
for r in results.iter() {
|
||
5 years ago
|
print!(" {}", r.unwrap_i64());
|
||
5 years ago
|
}
|
||
|
println!();
|
||
|
|
||
5 years ago
|
assert_eq!(results.len(), 10);
|
||
|
assert!(args
|
||
5 years ago
|
.iter()
|
||
|
.zip(results.iter())
|
||
|
.all(|(a, r)| a.i64() == r.i64()));
|
||
|
|
||
5 years ago
|
Ok(())
|
||
|
}
|