Add/use create_wasi_instance() instead of instantiate_wasi(). (#571)
* Add/use create_wasi_instance() instead of instantiate_wasi(). * rm Result from Instance::from_handle
This commit is contained in:
committed by
Dan Gohman
parent
9896a5cabd
commit
ea56118651
@@ -121,10 +121,7 @@ impl Instance {
|
||||
Some(&self.exports()[i])
|
||||
}
|
||||
|
||||
pub fn from_handle(
|
||||
store: &HostRef<Store>,
|
||||
instance_handle: InstanceHandle,
|
||||
) -> Result<Instance> {
|
||||
pub fn from_handle(store: &HostRef<Store>, instance_handle: InstanceHandle) -> Instance {
|
||||
let contexts = HashSet::new();
|
||||
|
||||
let mut exports = Vec::new();
|
||||
@@ -152,12 +149,12 @@ impl Instance {
|
||||
exports_types.into_boxed_slice(),
|
||||
));
|
||||
|
||||
Ok(Instance {
|
||||
Instance {
|
||||
instance_handle,
|
||||
module,
|
||||
contexts,
|
||||
exports: exports.into_boxed_slice(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> &InstanceHandle {
|
||||
|
||||
@@ -103,11 +103,8 @@ pub fn instantiate(
|
||||
// If this module expects to be able to use wasi then go ahead and hook
|
||||
// that up into the imported crates.
|
||||
let wasi = if let Some(module_name) = data.find_wasi_module_name() {
|
||||
let global_exports = store.borrow().global_exports().clone();
|
||||
let wasi_handle = wasmtime_wasi::instantiate_wasi("", global_exports, &[], &[], &[])
|
||||
let instance = wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[])
|
||||
.map_err(|e| err2py(e.into()))?;
|
||||
let instance =
|
||||
api::Instance::from_handle(&store, wasi_handle).map_err(|e| err2py(e.into()))?;
|
||||
Some((module_name, instance))
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -66,14 +66,8 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
|
||||
|
||||
let mut imports: Vec<Extern> = Vec::new();
|
||||
if let Some(module_name) = data.find_wasi_module_name() {
|
||||
let wasi_handle = wasmtime_wasi::instantiate_wasi(
|
||||
"",
|
||||
global_exports,
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
)?;
|
||||
let wasi_instance = Instance::from_handle(&store, wasi_handle)?;
|
||||
let wasi_instance = wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[])
|
||||
.map_err(|e| format_err!("wasm instantiation error: {:?}", e))?;
|
||||
for i in module.borrow().imports().iter() {
|
||||
if i.module().as_str() != module_name {
|
||||
bail!("unknown import module {}", i.module().as_str());
|
||||
|
||||
@@ -58,8 +58,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
||||
builder.build().context("failed to build wasi context")?,
|
||||
)
|
||||
.context("failed to instantiate wasi")?,
|
||||
)
|
||||
.context("failed to create instance from handle")?,
|
||||
),
|
||||
);
|
||||
|
||||
let module = HostRef::new(Module::new(&store, &data).context("failed to create wasm module")?);
|
||||
|
||||
@@ -11,6 +11,7 @@ readme = "README.md"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
wasmtime = { path = "../api" }
|
||||
wasmtime-runtime = { path = "../runtime" }
|
||||
wasmtime-environ = { path = "../environ" }
|
||||
wasmtime-jit = { path = "../jit" }
|
||||
|
||||
@@ -9,9 +9,23 @@ use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use target_lexicon::HOST;
|
||||
use wasi_common::{WasiCtx, WasiCtxBuilder};
|
||||
use wasmtime_api as api;
|
||||
use wasmtime_environ::{translate_signature, Export, Module};
|
||||
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMFunctionBody};
|
||||
|
||||
/// Creates `api::Instance` object implementing the "wasi" interface.
|
||||
pub fn create_wasi_instance(
|
||||
store: &api::HostRef<api::Store>,
|
||||
preopened_dirs: &[(String, File)],
|
||||
argv: &[String],
|
||||
environ: &[(String, String)],
|
||||
) -> Result<api::Instance, InstantiationError> {
|
||||
let global_exports = store.borrow().global_exports().clone();
|
||||
let wasi = instantiate_wasi("", global_exports, preopened_dirs, argv, environ)?;
|
||||
let instance = api::Instance::from_handle(&store, wasi);
|
||||
Ok(instance)
|
||||
}
|
||||
|
||||
/// Return an instance implementing the "wasi" interface.
|
||||
pub fn instantiate_wasi(
|
||||
prefix: &str,
|
||||
|
||||
@@ -5,7 +5,7 @@ extern crate alloc;
|
||||
mod instantiate;
|
||||
mod syscalls;
|
||||
|
||||
pub use instantiate::{instantiate_wasi, instantiate_wasi_with_context};
|
||||
pub use instantiate::{create_wasi_instance, instantiate_wasi, instantiate_wasi_with_context};
|
||||
|
||||
pub fn is_wasi_module(name: &str) -> bool {
|
||||
// FIXME: this should be more conservative, but while WASI is in flux and
|
||||
|
||||
Reference in New Issue
Block a user