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

Function main

examples/externref.rs:7–77  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5use wasmtime::*;
6
7fn main() -> Result<()> {
8 println!("Initializing...");
9 let mut config = Config::new();
10 config.wasm_reference_types(true);
11 let engine = Engine::new(&config)?;
12 let mut store = Store::new(&engine, ());
13
14 println!("Compiling module...");
15 let module = Module::from_file(&engine, "examples/externref.wat")?;
16
17 println!("Instantiating module...");
18 let instance = Instance::new(&mut store, &module, &[])?;
19
20 println!("Creating new `externref`...");
21 let externref = match ExternRef::new(&mut store, "Hello, World!") {
22 Ok(x) => x,
23 Err(e) => match e.downcast::<GcHeapOutOfMemory<&str>>() {
24 Ok(oom) => {
25 let (inner, oom) = oom.take_inner();
26 store.gc(Some(&oom))?;
27 ExternRef::new(&mut store, inner)?
28 }
29 Err(e) => return Err(e),
30 },
31 };
32 assert!(
33 externref
34 .data(&store)?
35 .expect("should have host data")
36 .is::<&'static str>()
37 );
38 assert_eq!(
39 *externref
40 .data(&store)?
41 .expect("should have host data")
42 .downcast_ref::<&'static str>()
43 .unwrap(),
44 "Hello, World!"
45 );
46
47 println!("Touching `externref` table...");
48 let table = instance.get_table(&mut store, "table").unwrap();
49 table.set(&mut store, 3, Some(externref).into())?;
50 let elem = table
51 .get(&mut store, 3)
52 .unwrap() // assert in bounds
53 .unwrap_extern() // assert it's an externref table
54 .copied()
55 .unwrap(); // assert the externref isn't null
56 assert!(Rooted::ref_eq(&store, &elem, &externref)?);
57
58 println!("Touching `externref` global...");
59 let global = instance.get_global(&mut store, "global").unwrap();
60 global.set(&mut store, Some(externref).into())?;
61 let global_val = global.get(&mut store).unwrap_externref().copied().unwrap();
62 assert!(Rooted::ref_eq(&store, &global_val, &externref)?);
63
64 println!("Calling `externref` func...");

Callers

nothing calls this directly

Calls 14

OkFunction · 0.85
take_innerMethod · 0.80
copiedMethod · 0.80
unwrap_externMethod · 0.80
unwrap_externrefMethod · 0.80
newFunction · 0.50
wasm_reference_typesMethod · 0.45
gcMethod · 0.45
unwrapMethod · 0.45
get_tableMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected