12926725dSPeter Schwarz //! This is an example of working with multi-value modules and dealing with
23c51d3adSAlex Crichton //! multi-value functions.
33c51d3adSAlex Crichton //!
43c51d3adSAlex Crichton //! Note that the `Func::wrap*` interfaces cannot be used to return multiple
53c51d3adSAlex Crichton //! values just yet, so we need to use the more dynamic `Func::new` and
63c51d3adSAlex Crichton //! `Func::call` methods.
73c51d3adSAlex Crichton
83c51d3adSAlex Crichton // You can execute this example with `cargo run --example multi`
93c51d3adSAlex Crichton
10*94740588SNick Fitzgerald use wasmtime::Result;
11195bf0e2SAlex Crichton
main() -> Result<()>12195bf0e2SAlex Crichton fn main() -> Result<()> {
133c51d3adSAlex Crichton use wasmtime::*;
143c51d3adSAlex Crichton
153c51d3adSAlex Crichton println!("Initializing...");
1615c68f2cSYury Delendik let engine = Engine::default();
177a1b7cdfSAlex Crichton let mut store = Store::new(&engine, ());
183c51d3adSAlex Crichton
193c51d3adSAlex Crichton // Compile.
203c51d3adSAlex Crichton println!("Compiling module...");
2115c68f2cSYury Delendik let module = Module::from_file(&engine, "examples/multi.wat")?;
223c51d3adSAlex Crichton
237a1b7cdfSAlex Crichton // Create a host function which takes multiple parameters and returns
247a1b7cdfSAlex Crichton // multiple results.
253c51d3adSAlex Crichton println!("Creating callback...");
267a1b7cdfSAlex Crichton let callback_func = Func::wrap(&mut store, |a: i32, b: i64| -> (i64, i32) {
277a1b7cdfSAlex Crichton (b + 1, a + 1)
28afd980b4SAlex Crichton });
293c51d3adSAlex Crichton
303c51d3adSAlex Crichton // Instantiate.
313c51d3adSAlex Crichton println!("Instantiating module...");
327a1b7cdfSAlex Crichton let instance = Instance::new(&mut store, &module, &[callback_func.into()])?;
333c51d3adSAlex Crichton
343c51d3adSAlex Crichton // Extract exports.
353c51d3adSAlex Crichton println!("Extracting export...");
36b0939f66SAlex Crichton let g = instance.get_typed_func::<(i32, i64), (i64, i32)>(&mut store, "g")?;
373c51d3adSAlex Crichton
383c51d3adSAlex Crichton // Call `$g`.
393c51d3adSAlex Crichton println!("Calling export \"g\"...");
407a1b7cdfSAlex Crichton let (a, b) = g.call(&mut store, (1, 3))?;
413c51d3adSAlex Crichton
423c51d3adSAlex Crichton println!("Printing result...");
4346098121SHamir Mahal println!("> {a} {b}");
443c51d3adSAlex Crichton
452697a18dSAlex Crichton assert_eq!(a, 4);
462697a18dSAlex Crichton assert_eq!(b, 2);
473c51d3adSAlex Crichton
483c51d3adSAlex Crichton // Call `$round_trip_many`.
493c51d3adSAlex Crichton println!("Calling export \"round_trip_many\"...");
503c51d3adSAlex Crichton let round_trip_many = instance
512697a18dSAlex Crichton .get_typed_func::<
522697a18dSAlex Crichton (i64, i64, i64, i64, i64, i64, i64, i64, i64, i64),
532697a18dSAlex Crichton (i64, i64, i64, i64, i64, i64, i64, i64, i64, i64),
542697a18dSAlex Crichton >
557a1b7cdfSAlex Crichton (&mut store, "round_trip_many")?;
567a1b7cdfSAlex Crichton let results = round_trip_many.call(&mut store, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))?;
573c51d3adSAlex Crichton
583c51d3adSAlex Crichton println!("Printing result...");
5946098121SHamir Mahal println!("> {results:?}");
602697a18dSAlex Crichton assert_eq!(results, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
613c51d3adSAlex Crichton
623c51d3adSAlex Crichton Ok(())
633c51d3adSAlex Crichton }
64