Move workspace creation outside of runtime instantiation

This commit is contained in:
Jakub Konka
2019-06-23 22:08:49 +02:00
committed by Dan Gohman
parent d0eac815f0
commit b9c04d431f
3 changed files with 67 additions and 28 deletions

View File

@@ -1,19 +1,14 @@
mod utils;
mod wasi;
use cranelift_codegen::settings;
use cranelift_native;
use std::path::Path;
use wasmtime_jit::Context;
pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
// Load in the wasm testcase
let data = utils::read_wasm(path.as_ref())?;
let bin_name = utils::extract_exec_name_from_path(path.as_ref())?;
// Prepare workspace
let workspace = utils::prepare_workspace(&bin_name)?;
pub fn instantiate<S: AsRef<str>>(
data: &[u8],
bin_name: S,
workspace: Option<S>,
) -> Result<(), String> {
// Prepare runtime
let isa_builder =
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
@@ -21,16 +16,30 @@ pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let mut context = Context::with_isa(isa);
let global_exports = context.get_global_exports();
let preopen_dir = wasi_common::preopen_dir(&workspace)
.map_err(|e| format!("error while preopening directory '{}': {}", workspace, e))?;
let get_preopens = |workspace: Option<S>| -> Result<Vec<_>, String> {
if let Some(workspace) = workspace {
let preopen_dir = wasi_common::preopen_dir(workspace.as_ref()).map_err(|e| {
format!(
"error while preopening directory '{}': {}",
workspace.as_ref(),
e
)
})?;
Ok(vec![(".".to_owned(), preopen_dir)])
} else {
Ok(vec![])
}
};
context.name_instance(
"wasi_unstable".to_owned(),
wasi::instantiate_wasi(
"",
global_exports,
&[(".".to_owned(), preopen_dir)],
&[bin_name.clone(), ".".to_owned()],
&get_preopens(workspace)?,
&[bin_name.as_ref().to_owned(), ".".to_owned()],
&[],
)
.expect("instantiating wasi"),
@@ -40,5 +49,11 @@ pub fn run_test<P: AsRef<Path>>(path: P) -> Result<(), String> {
context
.instantiate_module(None, &data)
.map(|_| ())
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e))
.map_err(|e| {
format!(
"error while processing main module '{}': {}",
bin_name.as_ref(),
e
)
})
}