Simplify examples: avoid unnecessary HostRef wrap/unwrap

Several of the examples wrap the Instance in a HostRef, only to
immediately borrow it again to get the exports,and then never touch it
again. Simplify this by owning the Instance directly.
This commit is contained in:
Josh Triplett
2019-11-19 21:01:09 -08:00
committed by Jakub Konka
parent 204b4d376a
commit 7c8ac3d71c
4 changed files with 10 additions and 21 deletions

View File

@@ -55,13 +55,10 @@ fn main() -> Result<()> {
.0; .0;
// Instantiate the module. // Instantiate the module.
let instance = HostRef::new(Instance::new(&store, &module, &[])?); let instance = Instance::new(&store, &module, &[])?;
// Invoke `gcd` export // Invoke `gcd` export
let gcd = instance.borrow().exports()[gcd_index] let gcd = instance.exports()[gcd_index].func().expect("gcd").clone();
.func()
.expect("gcd")
.clone();
let result = gcd let result = gcd
.borrow() .borrow()
.call(&[Val::from(6i32), Val::from(27i32)]) .call(&[Val::from(6i32), Val::from(27i32)])

View File

@@ -1,7 +1,6 @@
//! Translation of hello example //! Translation of hello example
use anyhow::{ensure, format_err, Context as _, Result}; use anyhow::{ensure, format_err, Context as _, Result};
use std::cell::Ref;
use std::rc::Rc; use std::rc::Rc;
use wasmtime::*; use wasmtime::*;
@@ -49,14 +48,12 @@ fn main() -> Result<()> {
// Note that this is where the wasm `start` function, if any, would run. // Note that this is where the wasm `start` function, if any, would run.
println!("Instantiating module..."); println!("Instantiating module...");
let imports = vec![hello_func.into()]; let imports = vec![hello_func.into()];
let instance = HostRef::new( let instance = Instance::new(&store, &module, imports.as_slice())
Instance::new(&store, &module, imports.as_slice()) .context("> Error instantiating module!")?;
.context("> Error instantiating module!")?,
);
// Next we poke around a bit to extract the `run` function from the module. // Next we poke around a bit to extract the `run` function from the module.
println!("Extracting export..."); println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports()); let exports = instance.exports();
ensure!(!exports.is_empty(), "> Error accessing exports!"); ensure!(!exports.is_empty(), "> Error accessing exports!");
let run_func = exports[0].func().context("> Error accessing exports!")?; let run_func = exports[0].func().context("> Error accessing exports!")?;

View File

@@ -1,7 +1,6 @@
//! Translation of the memory example //! Translation of the memory example
use anyhow::{bail, ensure, Context as _, Error}; use anyhow::{bail, ensure, Context as _, Error};
use std::cell::Ref;
use wasmtime::*; use wasmtime::*;
fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> { fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> {
@@ -92,12 +91,11 @@ fn main() -> Result<(), Error> {
// Instantiate. // Instantiate.
println!("Instantiating module..."); println!("Instantiating module...");
let instance = let instance = Instance::new(&store, &module, &[]).context("> Error instantiating module!")?;
HostRef::new(Instance::new(&store, &module, &[]).context("> Error instantiating module!")?);
// Extract export. // Extract export.
println!("Extracting export..."); println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports()); let exports = instance.exports();
ensure!(!exports.is_empty(), "> Error accessing exports!"); ensure!(!exports.is_empty(), "> Error accessing exports!");
let memory = get_export_memory(&exports, 0)?; let memory = get_export_memory(&exports, 0)?;
let size_func = get_export_func(&exports, 1)?; let size_func = get_export_func(&exports, 1)?;

View File

@@ -1,7 +1,6 @@
//! Translation of multi example //! Translation of multi example
use anyhow::{ensure, format_err, Context as _, Result}; use anyhow::{ensure, format_err, Context as _, Result};
use std::cell::Ref;
use std::rc::Rc; use std::rc::Rc;
use wasmtime::*; use wasmtime::*;
@@ -68,14 +67,12 @@ fn main() -> Result<()> {
// Instantiate. // Instantiate.
println!("Instantiating module..."); println!("Instantiating module...");
let imports = vec![callback_func.into()]; let imports = vec![callback_func.into()];
let instance = HostRef::new( let instance = Instance::new(&store, &module, imports.as_slice())
Instance::new(&store, &module, imports.as_slice()) .context("Error instantiating module!")?;
.context("Error instantiating module!")?,
);
// Extract exports. // Extract exports.
println!("Extracting export..."); println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports()); let exports = instance.exports();
ensure!(!exports.is_empty(), "Error accessing exports!"); ensure!(!exports.is_empty(), "Error accessing exports!");
let g = exports[0].func().context("> Error accessing export $g!")?; let g = exports[0].func().context("> Error accessing export $g!")?;
let round_trip_many = exports[1] let round_trip_many = exports[1]