Adds WASI support to markdown rust example. (#509)

* Adds WASI support to markdown rust example.

* Rename has_wasi -> find_wasi_module_name
This commit is contained in:
Yury Delendik
2019-11-08 07:57:53 -06:00
committed by GitHub
parent 43b761ef5f
commit 3a8c2bbb82
3 changed files with 25 additions and 0 deletions

View File

@@ -17,4 +17,5 @@ cranelift-native = { version = "0.49" }
wasmtime-interface-types = { path = "../../wasmtime-interface-types" }
wasmtime-jit = { path = "../../wasmtime-jit" }
wasmtime-rust-macro = { path = "./macro" }
wasmtime-wasi = { path = "../../wasmtime-wasi" }
anyhow = "1.0.19"

View File

@@ -64,6 +64,16 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
..Default::default()
});
let data = #root::wasmtime_interface_types::ModuleData::new(&bytes)?;
if let Some(module_name) = data.find_wasi_module_name() {
let wasi_handle = wasmtime_wasi::instantiate_wasi(
"",
cx.get_global_exports(),
&[],
&[],
&[],
)?;
cx.name_instance(module_name, wasi_handle);
}
let handle = cx.instantiate_module(None, &bytes)?;
Ok(#name { cx, handle, data })

View File

@@ -100,6 +100,20 @@ impl ModuleData {
})
}
/// Detects if WASI support is needed: returns module name that is requested.
pub fn find_wasi_module_name(&self) -> Option<String> {
self.inner.as_ref().and_then(|Inner { module }| {
module
.imports
.iter()
.find(|walrus::Import { module, .. }| match module.as_str() {
"wasi" | "wasi_unstable" => true,
_ => false,
})
.map(|walrus::Import { module, .. }| module.clone())
})
}
/// Same as `Context::invoke` except that this works with a `&[Value]` list
/// instead of a `&[RuntimeValue]` list. (in this case `Value` is the set of
/// wasm interface types)