[wasmtime-api] reduce examples complexity: hostref for store/engine (#489)

* reduce amount of store.clone()

* use HostRef Engine as ref / use Engine::default()
This commit is contained in:
Yury Delendik
2019-11-07 11:39:23 -06:00
committed by GitHub
parent 55eb06ecc2
commit 6632a7da37
13 changed files with 76 additions and 88 deletions

View File

@@ -10,10 +10,10 @@ fn main() -> Result<()> {
// Instantiate engine and store.
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(engine));
let store = HostRef::new(Store::new(&engine));
// Load a module.
let module = HostRef::new(Module::new(store.clone(), &wasm)?);
let module = HostRef::new(Module::new(&store, &wasm)?);
// Find index of the `gcd` export.
let gcd_index = module
@@ -26,7 +26,7 @@ fn main() -> Result<()> {
.0;
// Instantiate the module.
let instance = HostRef::new(Instance::new(store.clone(), module, &[])?);
let instance = HostRef::new(Instance::new(&store, &module, &[])?);
// Invoke `gcd` export
let gcd = instance.borrow().exports()[gcd_index]

View File

@@ -21,8 +21,8 @@ impl Callable for HelloCallback {
fn main() -> Result<()> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::new(Config::default()));
let store = HostRef::new(Store::new(engine));
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
@@ -30,19 +30,18 @@ fn main() -> Result<()> {
// Compile.
println!("Compiling module...");
let module =
HostRef::new(Module::new(store.clone(), &binary).context("> Error compiling module!")?);
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
// Create external print functions.
println!("Creating callback...");
let hello_type = FuncType::new(Box::new([]), Box::new([]));
let hello_func = HostRef::new(Func::new(store.clone(), hello_type, Rc::new(HelloCallback)));
let hello_func = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
// Instantiate.
println!("Instantiating module...");
let imports = vec![hello_func.into()];
let instance = HostRef::new(
Instance::new(store.clone(), module, imports.as_slice())
Instance::new(&store, &module, imports.as_slice())
.context("> Error instantiating module!")?,
);

View File

@@ -64,8 +64,8 @@ macro_rules! call {
fn main() -> Result<(), Error> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::new(Config::default()));
let store = HostRef::new(Store::new(engine));
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
@@ -73,14 +73,12 @@ fn main() -> Result<(), Error> {
// Compile.
println!("Compiling module...");
let module =
HostRef::new(Module::new(store.clone(), &binary).context("> Error compiling module!")?);
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
// Instantiate.
println!("Instantiating module...");
let instance = HostRef::new(
Instance::new(store.clone(), module, &[]).context("> Error instantiating module!")?,
);
let instance =
HostRef::new(Instance::new(&store, &module, &[]).context("> Error instantiating module!")?);
// Extract export.
println!("Extracting export...");
@@ -141,7 +139,7 @@ fn main() -> Result<(), Error> {
// TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
println!("Creating stand-alone memory...");
let memorytype = MemoryType::new(Limits::new(5, 5));
let mut memory2 = Memory::new(store.clone(), memorytype);
let mut memory2 = Memory::new(&store, memorytype);
check!(memory2.size(), 5u32);
check!(memory2.grow(1), false);
check!(memory2.grow(0), true);

View File

@@ -24,8 +24,8 @@ impl Callable for Callback {
fn main() -> Result<()> {
// Initialize.
println!("Initializing...");
let engine = HostRef::new(Engine::new(Config::default()));
let store = HostRef::new(Store::new(engine));
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
// Load binary.
println!("Loading binary...");
@@ -33,8 +33,7 @@ fn main() -> Result<()> {
// Compile.
println!("Compiling module...");
let module =
HostRef::new(Module::new(store.clone(), &binary).context("Error compiling module!")?);
let module = HostRef::new(Module::new(&store, &binary).context("Error compiling module!")?);
// Create external print functions.
println!("Creating callback...");
@@ -42,13 +41,13 @@ fn main() -> Result<()> {
Box::new([ValType::I32, ValType::I64]),
Box::new([ValType::I64, ValType::I32]),
);
let callback_func = HostRef::new(Func::new(store.clone(), callback_type, Rc::new(Callback)));
let callback_func = HostRef::new(Func::new(&store, callback_type, Rc::new(Callback)));
// Instantiate.
println!("Instantiating module...");
let imports = vec![callback_func.into()];
let instance = HostRef::new(
Instance::new(store.clone(), module, imports.as_slice())
Instance::new(&store, &module, imports.as_slice())
.context("Error instantiating module!")?,
);