Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created June 25, 2020 20:39
Show Gist options
  • Save alexcrichton/582b2ef8136bc20a0033903c94c4149a to your computer and use it in GitHub Desktop.
Save alexcrichton/582b2ef8136bc20a0033903c94c4149a to your computer and use it in GitHub Desktop.
use std::cell::RefCell;
use std::rc::Rc;
use wasmtime::*;
struct A {
table: Table,
put_the_thing: Rc<RefCell<Option<Val>>>,
contents: String,
}
impl Drop for A {
fn drop(&mut self) {
eprintln!("calling destructor");
let me = self.table.get(0).unwrap();
*self.put_the_thing.borrow_mut() = Some(me);
}
}
fn main() {
let mut config = Config::new();
config.wasm_reference_types(true);
let engine = Engine::new(&config);
let store = Store::new(&engine);
let module = Module::new(
store.engine(),
r#"
(module
(table (export "t") 1 externref)
(func (export "foo") (param externref)
i32.const 0
local.get 0
table.set)
)
"#,
)
.unwrap();
let instance = Instance::new(&store, &module, &[]).unwrap();
let foo = instance.get_func("foo").unwrap();
let slot = Rc::new(RefCell::new(None));
let me = ExternRef::new(
&store,
A {
put_the_thing: slot.clone(),
table: instance.get_table("t").unwrap(),
contents: "hello".to_string(),
},
);
foo.call(&[me.into()]).unwrap();
store.gc();
foo.call(&[Val::ExternRef(None)]).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment