Browse Source

wasmtime: Add `externref` Rust example

pull/1996/head
Nick Fitzgerald 4 years ago
parent
commit
4a349ee299
  1. 1
      examples/externref.c
  2. 51
      examples/externref.rs

1
examples/externref.c

@ -161,7 +161,6 @@ int main() {
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);
wasm_config_delete(config);
return ret;
}

51
examples/externref.rs

@ -0,0 +1,51 @@
//! Small example of how to use `externref`s.
// You can execute this example with `cargo run --example externref`
use anyhow::Result;
use wasmtime::*;
fn main() -> Result<()> {
println!("Initializing...");
let mut config = Config::new();
config.wasm_reference_types(true);
let engine = Engine::new(&config);
let store = Store::new(&engine);
println!("Compiling module...");
let module = Module::from_file(&engine, "examples/externref.wat")?;
println!("Instantiating module...");
let imports = [];
let instance = Instance::new(&store, &module, &imports)?;
println!("Creating new `externref`...");
let externref = ExternRef::new("Hello, World!");
assert!(externref.data().is::<&'static str>());
assert_eq!(
*externref.data().downcast_ref::<&'static str>().unwrap(),
"Hello, World!"
);
println!("Touching `externref` table...");
let table = instance.get_table("table").unwrap();
table.set(3, Some(externref.clone()).into())?;
let elem = table.get(3).unwrap().unwrap_externref().unwrap();
assert!(elem.ptr_eq(&externref));
println!("Touching `externref` global...");
let global = instance.get_global("global").unwrap();
global.set(Some(externref.clone()).into())?;
let global_val = global.get().unwrap_externref().unwrap();
assert!(global_val.ptr_eq(&externref));
println!("Calling `externref` func...");
let func = instance.get_func("func").unwrap();
let func = func.get1::<Option<ExternRef>, Option<ExternRef>>()?;
let ret = func(Some(externref.clone()))?;
assert!(ret.is_some());
assert!(ret.unwrap().ptr_eq(&externref));
println!("Done.");
Ok(())
}
Loading…
Cancel
Save