Browse Source

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

* Adds WASI support to markdown rust example.

* Rename has_wasi -> find_wasi_module_name
pull/522/head
Yury Delendik 5 years ago
committed by GitHub
parent
commit
3a8c2bbb82
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      misc/wasmtime-rust/Cargo.toml
  2. 10
      misc/wasmtime-rust/macro/src/lib.rs
  3. 14
      wasmtime-interface-types/src/lib.rs

1
misc/wasmtime-rust/Cargo.toml

@ -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"

10
misc/wasmtime-rust/macro/src/lib.rs

@ -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 })

14
wasmtime-interface-types/src/lib.rs

@ -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)

Loading…
Cancel
Save