Reimplement wasmtime-wasi on top of wasmtime (#899)
* Reimplement `wasmtime-wasi` on top of `wasmtime` This commit reimplements the `wasmtime-wasi` crate on top of the `wasmtime` API crate, instead of being placed on top of the `wasmtime-*` family of internal crates. The purpose here is to continue to exercise the API as well as avoid usage of internals wherever possible and instead use the safe API as much as possible. The `wasmtime-wasi` crate's API has been updated as part of this PR as well. The general outline of it is now: * Each module snapshot has a `WasiCtxBuilder`, `WasiCtx`, and `Wasi` type. * The `WasiCtx*` types are reexported from `wasi-common`. * The `Wasi` type is synthesized by the `wig` crate's procedural macro * The `Wasi` type exposes one constructor which takes a `Store` and a `WasiCtx`, and produces a `Wasi` * Each `Wasi` struct fields for all the exported functions in that wasi module. They're all public an they all have type `wasmtime::Func` * The `Wasi` type has a `get_export` method to fetch an struct field by name. The intention here is that we can continue to make progress on #727 by integrating WASI construction into the `Instance::new` experience, but it requires everything to be part of the same system! The main oddity required by the `wasmtime-wasi` crate is that it needs access to the caller's `memory` export, if any. This is currently done with a bit of a hack and is expected to go away once interface types are more fully baked in. * Remove now no-longer-necessary APIs from `wasmtime` * rustfmt * Rename to from_abi
This commit is contained in:
@@ -18,9 +18,7 @@ doc = false
|
||||
|
||||
[dependencies]
|
||||
wasmtime = { path = "../../api", version = "0.9.0" }
|
||||
wasmtime-environ = { path = "../../environ", version = "0.9.0" }
|
||||
wasmtime-interface-types = { path = "../../interface-types", version = "0.9.0" }
|
||||
wasmtime-runtime = { path = "../../runtime", version = "0.9.0" }
|
||||
wasmtime-wasi = { path = "../../wasi", version = "0.9.0" }
|
||||
target-lexicon = { version = "0.10.0", default-features = false }
|
||||
anyhow = "1.0.19"
|
||||
|
||||
@@ -89,9 +89,11 @@ 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 instance = wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[])
|
||||
let cx = wasmtime_wasi::WasiCtxBuilder::new()
|
||||
.build()
|
||||
.map_err(|e| err2py(e.into()))?;
|
||||
Some((module_name, instance))
|
||||
let wasi = wasmtime_wasi::Wasi::new(&store, cx);
|
||||
Some((module_name, wasi))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -111,7 +113,7 @@ pub fn instantiate(
|
||||
.ok_or_else(|| {
|
||||
PyErr::new::<Exception, _>(format!("wasi export {} is not found", i.name(),))
|
||||
})?;
|
||||
imports.push(e.clone());
|
||||
imports.push(e.clone().into());
|
||||
} else {
|
||||
return Err(PyErr::new::<Exception, _>(format!(
|
||||
"imported module {} is not found",
|
||||
|
||||
@@ -62,14 +62,14 @@ 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_instance = #root::wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[])
|
||||
.map_err(|e| format_err!("wasm instantiation error: {:?}", e))?;
|
||||
let wasi_cx = #root::wasmtime_wasi::WasiCtxBuilder::new().build()?;
|
||||
let wasi = #root::wasmtime_wasi::Wasi::new(&store, wasi_cx);
|
||||
for i in module.imports().iter() {
|
||||
if i.module() != module_name {
|
||||
bail!("unknown import module {}", i.module());
|
||||
}
|
||||
if let Some(export) = wasi_instance.get_export(i.name()) {
|
||||
imports.push(export.clone());
|
||||
if let Some(export) = wasi.get_export(i.name()) {
|
||||
imports.push(export.clone().into());
|
||||
} else {
|
||||
bail!("unknown import {}:{}", i.module(), i.name())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user