Add examples; refactor HostRef
This commit is contained in:
committed by
Dan Gohman
parent
042c87763e
commit
6a41417b52
@@ -2,20 +2,18 @@
|
||||
//! invoking its exported function.
|
||||
|
||||
use failure::{format_err, Error};
|
||||
use std::cell::RefCell;
|
||||
use std::fs::read;
|
||||
use std::rc::Rc;
|
||||
use wasmtime_api::*;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let wasm = read("examples/gcd.wasm")?;
|
||||
|
||||
// Instantiate engine and store.
|
||||
let engine = Rc::new(RefCell::new(Engine::default()));
|
||||
let store = Rc::new(RefCell::new(Store::new(engine)));
|
||||
let engine = HostRef::new(Engine::default());
|
||||
let store = HostRef::new(Store::new(engine));
|
||||
|
||||
// Load a module.
|
||||
let module = Rc::new(RefCell::new(Module::new(store.clone(), &wasm)?));
|
||||
let module = HostRef::new(Module::new(store.clone(), &wasm)?);
|
||||
|
||||
// Find index of the `gcd` export.
|
||||
let gcd_index = module
|
||||
@@ -28,12 +26,12 @@ fn main() -> Result<(), Error> {
|
||||
.0;
|
||||
|
||||
// Instantiate the module.
|
||||
let instance = Rc::new(RefCell::new(Instance::new(store.clone(), module, &[])?));
|
||||
let instance = HostRef::new(Instance::new(store.clone(), module, &[])?);
|
||||
|
||||
// Invoke `gcd` export
|
||||
let gcd = instance.borrow().exports()[gcd_index]
|
||||
.borrow()
|
||||
.func()
|
||||
.expect("gcd")
|
||||
.clone();
|
||||
let result = gcd
|
||||
.borrow()
|
||||
|
||||
Reference in New Issue
Block a user