Move workspace creation outside of runtime instantiation
This commit is contained in:
@@ -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
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{Component, Path};
|
||||
use std::time::SystemTime;
|
||||
|
||||
fn read_to_end<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, io::Error> {
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
let mut file = File::open(path)?;
|
||||
file.read_to_end(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
pub fn read_wasm<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, String> {
|
||||
let data = read_to_end(path).map_err(|err| err.to_string())?;
|
||||
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
||||
Ok(data)
|
||||
} else {
|
||||
Err("Invalid Wasm file encountered".to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_workspace<S: AsRef<str>>(exe_name: S) -> Result<String, String> {
|
||||
let mut workspace = env::temp_dir();
|
||||
let time_now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|err| err.to_string())?;
|
||||
let subdir = format!(
|
||||
"wasi_common_tests_{}_{}",
|
||||
exe_name.as_ref(),
|
||||
time_now.as_secs()
|
||||
);
|
||||
workspace.push(subdir);
|
||||
fs::create_dir(workspace.as_path()).map_err(|err| err.to_string())?;
|
||||
|
||||
Ok(workspace
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.ok_or("couldn't convert to str".to_owned())?
|
||||
.to_string())
|
||||
}
|
||||
|
||||
pub fn extract_exec_name_from_path<P: AsRef<Path>>(path: P) -> Result<String, String> {
|
||||
Ok(path
|
||||
.as_ref()
|
||||
.components()
|
||||
.next_back()
|
||||
.map(Component::as_os_str)
|
||||
.and_then(OsStr::to_str)
|
||||
.ok_or("couldn't convert to str".to_owned())?
|
||||
.to_owned())
|
||||
}
|
||||
Reference in New Issue
Block a user