Add examples; refactor HostRef

This commit is contained in:
Yury Delendik
2019-08-28 11:19:40 -05:00
committed by Dan Gohman
parent 042c87763e
commit 6a41417b52
19 changed files with 712 additions and 240 deletions

View File

@@ -36,17 +36,15 @@ use docopt::Docopt;
use failure::{bail, Error, ResultExt};
use pretty_env_logger;
use serde::Deserialize;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::File;
use std::path::Component;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::rc::Rc;
use wabt;
use wasi_common::preopen_dir;
use wasmtime_api::{Config, Engine, Instance, Module, Store};
use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store};
use wasmtime_environ::cache_config;
use wasmtime_interface_types::ModuleData;
use wasmtime_jit::Features;
@@ -258,8 +256,8 @@ fn rmain() -> Result<(), Error> {
}
let config = Config::new(settings::Flags::new(flag_builder), features, debug_info);
let engine = Rc::new(RefCell::new(Engine::new(config)));
let store = Rc::new(RefCell::new(Store::new(engine)));
let engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(engine));
let mut module_registry = HashMap::new();
@@ -308,14 +306,14 @@ fn rmain() -> Result<(), Error> {
}
fn instantiate_module(
store: Rc<RefCell<Store>>,
store: HostRef<Store>,
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
path: &Path,
) -> Result<(Rc<RefCell<Instance>>, Rc<RefCell<Module>>, Vec<u8>), Error> {
) -> Result<(HostRef<Instance>, HostRef<Module>, Vec<u8>), Error> {
// Read the wasm module binary.
let data = read_wasm(path.to_path_buf())?;
let module = Rc::new(RefCell::new(Module::new(store.clone(), &data)?));
let module = HostRef::new(Module::new(store.clone(), &data)?);
// Resolve import using module_registry.
let imports = module
@@ -341,17 +339,13 @@ fn instantiate_module(
})
.collect::<Result<Vec<_>, _>>()?;
let instance = Rc::new(RefCell::new(Instance::new(
store.clone(),
module.clone(),
&imports,
)?));
let instance = HostRef::new(Instance::new(store.clone(), module.clone(), &imports)?);
Ok((instance, module, data))
}
fn handle_module(
store: Rc<RefCell<Store>>,
store: HostRef<Store>,
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
args: &Args,
path: &Path,
@@ -368,8 +362,8 @@ fn handle_module(
}
fn invoke_export(
store: Rc<RefCell<Store>>,
instance: Rc<RefCell<Instance>>,
store: HostRef<Store>,
instance: HostRef<Instance>,
data: &ModuleData,
name: &str,
args: &Args,