xref: /wasmtime-44.0.1/examples/serialize.rs (revision 9ce3ffe1)
1399ee0a5SYury Delendik //! Small example of how to serialize compiled wasm module to the disk,
2399ee0a5SYury Delendik //! and then instantiate it from the compilation artifacts.
3399ee0a5SYury Delendik 
4399ee0a5SYury Delendik // You can execute this example with `cargo run --example serialize`
5399ee0a5SYury Delendik 
6399ee0a5SYury Delendik use wasmtime::*;
7399ee0a5SYury Delendik 
serialize() -> Result<Vec<u8>>8399ee0a5SYury Delendik fn serialize() -> Result<Vec<u8>> {
9399ee0a5SYury Delendik     // Configure the initial compilation environment, creating the global
10399ee0a5SYury Delendik     // `Store` structure. Note that you can also tweak configuration settings
11399ee0a5SYury Delendik     // with a `Config` and an `Engine` if desired.
12399ee0a5SYury Delendik     println!("Initializing...");
13399ee0a5SYury Delendik     let engine = Engine::default();
14399ee0a5SYury Delendik 
15399ee0a5SYury Delendik     // Compile the wasm binary into an in-memory instance of a `Module`.
16399ee0a5SYury Delendik     println!("Compiling module...");
17399ee0a5SYury Delendik     let module = Module::from_file(&engine, "examples/hello.wat")?;
18399ee0a5SYury Delendik     let serialized = module.serialize()?;
19399ee0a5SYury Delendik 
20399ee0a5SYury Delendik     println!("Serialized.");
21399ee0a5SYury Delendik     Ok(serialized)
22399ee0a5SYury Delendik }
23399ee0a5SYury Delendik 
deserialize(buffer: &[u8]) -> Result<()>24399ee0a5SYury Delendik fn deserialize(buffer: &[u8]) -> Result<()> {
25399ee0a5SYury Delendik     // Configure the initial compilation environment, creating the global
26399ee0a5SYury Delendik     // `Store` structure. Note that you can also tweak configuration settings
27399ee0a5SYury Delendik     // with a `Config` and an `Engine` if desired.
28399ee0a5SYury Delendik     println!("Initializing...");
297a1b7cdfSAlex Crichton     let mut store: Store<()> = Store::default();
30399ee0a5SYury Delendik 
318384f3a3SAlex Crichton     // Compile the wasm binary into an in-memory instance of a `Module`. Note
328384f3a3SAlex Crichton     // that this is `unsafe` because it is our responsibility for guaranteeing
338384f3a3SAlex Crichton     // that these bytes are valid precompiled module bytes. We know that from
348384f3a3SAlex Crichton     // the structure of this example program.
35399ee0a5SYury Delendik     println!("Deserialize module...");
368384f3a3SAlex Crichton     let module = unsafe { Module::deserialize(store.engine(), buffer)? };
37399ee0a5SYury Delendik 
38399ee0a5SYury Delendik     // Here we handle the imports of the module, which in this case is our
39399ee0a5SYury Delendik     // `HelloCallback` type and its associated implementation of `Callback.
40399ee0a5SYury Delendik     println!("Creating callback...");
417a1b7cdfSAlex Crichton     let hello_func = Func::wrap(&mut store, || {
42399ee0a5SYury Delendik         println!("Calling back...");
43399ee0a5SYury Delendik         println!("> Hello World!");
44399ee0a5SYury Delendik     });
45399ee0a5SYury Delendik 
46399ee0a5SYury Delendik     // Once we've got that all set up we can then move to the instantiation
47399ee0a5SYury Delendik     // phase, pairing together a compiled module as well as a set of imports.
48399ee0a5SYury Delendik     // Note that this is where the wasm `start` function, if any, would run.
49399ee0a5SYury Delendik     println!("Instantiating module...");
50399ee0a5SYury Delendik     let imports = [hello_func.into()];
517a1b7cdfSAlex Crichton     let instance = Instance::new(&mut store, &module, &imports)?;
52399ee0a5SYury Delendik 
53399ee0a5SYury Delendik     // Next we poke around a bit to extract the `run` function from the module.
54399ee0a5SYury Delendik     println!("Extracting export...");
55*b0939f66SAlex Crichton     let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
56399ee0a5SYury Delendik 
57399ee0a5SYury Delendik     // And last but not least we can call it!
58399ee0a5SYury Delendik     println!("Calling export...");
597a1b7cdfSAlex Crichton     run.call(&mut store, ())?;
60399ee0a5SYury Delendik 
61399ee0a5SYury Delendik     println!("Done.");
62399ee0a5SYury Delendik     Ok(())
63399ee0a5SYury Delendik }
64399ee0a5SYury Delendik 
main() -> Result<()>65399ee0a5SYury Delendik fn main() -> Result<()> {
66399ee0a5SYury Delendik     let file = serialize()?;
67399ee0a5SYury Delendik     deserialize(&file)
68399ee0a5SYury Delendik }
69