MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/multimemory.rs:11–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9use wasmtime::*;
10
11fn main() -> Result<()> {
12 // Enable the multi-memory feature.
13 let mut config = Config::new();
14 config.wasm_multi_memory(true);
15
16 let engine = Engine::new(&config)?;
17
18 // Create our `store_fn` context and then compile a module and create an
19 // instance from the compiled module all in one go.
20 let mut store = Store::new(&engine, ());
21 let module = Module::from_file(store.engine(), "examples/multimemory.wat")?;
22 let instance = Instance::new(&mut store, &module, &[])?;
23
24 let memory0 = instance
25 .get_memory(&mut store, "memory0")
26 .ok_or(wasmtime::format_err!("failed to find `memory0` export"))?;
27 let size0 = instance.get_typed_func::<(), i32>(&mut store, "size0")?;
28 let load0 = instance.get_typed_func::<i32, i32>(&mut store, "load0")?;
29 let store0 = instance.get_typed_func::<(i32, i32), ()>(&mut store, "store0")?;
30
31 let memory1 = instance
32 .get_memory(&mut store, "memory1")
33 .ok_or(wasmtime::format_err!("failed to find `memory1` export"))?;
34 let size1 = instance.get_typed_func::<(), i32>(&mut store, "size1")?;
35 let load1 = instance.get_typed_func::<i32, i32>(&mut store, "load1")?;
36 let store1 = instance.get_typed_func::<(i32, i32), ()>(&mut store, "store1")?;
37
38 println!("Checking memory...");
39 assert_eq!(memory0.size(&store), 2);
40 assert_eq!(memory0.data_size(&store), 0x20000);
41 assert_eq!(memory0.data_mut(&mut store)[0], 0);
42 assert_eq!(memory0.data_mut(&mut store)[0x1000], 1);
43 assert_eq!(memory0.data_mut(&mut store)[0x1001], 2);
44 assert_eq!(memory0.data_mut(&mut store)[0x1002], 3);
45 assert_eq!(memory0.data_mut(&mut store)[0x1003], 4);
46
47 assert_eq!(size0.call(&mut store, ())?, 2);
48 assert_eq!(load0.call(&mut store, 0)?, 0);
49 assert_eq!(load0.call(&mut store, 0x1000)?, 1);
50 assert_eq!(load0.call(&mut store, 0x1001)?, 2);
51 assert_eq!(load0.call(&mut store, 0x1002)?, 3);
52 assert_eq!(load0.call(&mut store, 0x1003)?, 4);
53 assert_eq!(load0.call(&mut store, 0x1ffff)?, 0);
54 assert!(load0.call(&mut store, 0x20000).is_err()); // out of bounds trap
55
56 assert_eq!(memory1.size(&store), 2);
57 assert_eq!(memory1.data_size(&store), 0x20000);
58 assert_eq!(memory1.data_mut(&mut store)[0], 0);
59 assert_eq!(memory1.data_mut(&mut store)[0x1000], 4);
60 assert_eq!(memory1.data_mut(&mut store)[0x1001], 3);
61 assert_eq!(memory1.data_mut(&mut store)[0x1002], 2);
62 assert_eq!(memory1.data_mut(&mut store)[0x1003], 1);
63
64 assert_eq!(size1.call(&mut store, ())?, 2);
65 assert_eq!(load1.call(&mut store, 0)?, 0);
66 assert_eq!(load1.call(&mut store, 0x1000)?, 4);
67 assert_eq!(load1.call(&mut store, 0x1001)?, 3);
68 assert_eq!(load1.call(&mut store, 0x1002)?, 2);

Callers

nothing calls this directly

Calls 8

OkFunction · 0.85
newFunction · 0.50
wasm_multi_memoryMethod · 0.45
engineMethod · 0.45
get_memoryMethod · 0.45
data_mutMethod · 0.45
callMethod · 0.45
growMethod · 0.45

Tested by

no test coverage detected