Refactor mgmt of misc testsuite (#101)

Changes:
* use [tempfile] crate for auto mgmt of temp dirs
* use concrete types in place of generics in `utils` module

[tempfile]: https://github.com/Stebalien/tempfile
This commit is contained in:
Jakub Konka
2019-09-17 00:35:14 +02:00
committed by Dan Gohman
parent dfeae067cc
commit fec5b7ab0a
4 changed files with 31 additions and 50 deletions

View File

@@ -1,11 +1,8 @@
use cranelift_codegen::settings;
use std::path::Path;
use wasmtime_jit::Context;
pub fn instantiate<S: AsRef<str>>(
data: &[u8],
bin_name: S,
workspace: Option<S>,
) -> Result<(), String> {
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> Result<(), String> {
// Prepare runtime
let isa_builder =
cranelift_native::builder().map_err(|_| "host machine is not a supported target")?;
@@ -14,12 +11,12 @@ pub fn instantiate<S: AsRef<str>>(
let mut context = Context::with_isa(isa);
let global_exports = context.get_global_exports();
let get_preopens = |workspace: Option<S>| -> Result<Vec<_>, String> {
let get_preopens = |workspace: Option<&Path>| -> Result<Vec<_>, String> {
if let Some(workspace) = workspace {
let preopen_dir = wasi_common::preopen_dir(workspace.as_ref()).map_err(|e| {
let preopen_dir = wasi_common::preopen_dir(workspace).map_err(|e| {
format!(
"error while preopening directory '{}': {}",
workspace.as_ref(),
workspace.display(),
e
)
})?;
@@ -36,7 +33,7 @@ pub fn instantiate<S: AsRef<str>>(
"",
global_exports,
&get_preopens(workspace)?,
&[bin_name.as_ref().to_owned(), ".".to_owned()],
&[bin_name.to_owned(), ".".to_owned()],
&[],
)
.map_err(|e| format!("error instantiating WASI: {}", e))?,
@@ -46,11 +43,5 @@ pub fn instantiate<S: AsRef<str>>(
context
.instantiate_module(None, &data)
.map(|_| ())
.map_err(|e| {
format!(
"error while processing main module '{}': {}",
bin_name.as_ref(),
e
)
})
.map_err(|e| format!("error while processing main module '{}': {}", bin_name, e))
}