From 3a8c2bbb82b88b8b86f3f79e3d254f8aa6143150 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Fri, 8 Nov 2019 07:57:53 -0600 Subject: [PATCH] Adds WASI support to markdown rust example. (#509) * Adds WASI support to markdown rust example. * Rename has_wasi -> find_wasi_module_name --- misc/wasmtime-rust/Cargo.toml | 1 + misc/wasmtime-rust/macro/src/lib.rs | 10 ++++++++++ wasmtime-interface-types/src/lib.rs | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/misc/wasmtime-rust/Cargo.toml b/misc/wasmtime-rust/Cargo.toml index 6ce913155d..cf3ad45c2c 100644 --- a/misc/wasmtime-rust/Cargo.toml +++ b/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" diff --git a/misc/wasmtime-rust/macro/src/lib.rs b/misc/wasmtime-rust/macro/src/lib.rs index cc8861658b..902960fca3 100644 --- a/misc/wasmtime-rust/macro/src/lib.rs +++ b/misc/wasmtime-rust/macro/src/lib.rs @@ -64,6 +64,16 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result { ..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 }) diff --git a/wasmtime-interface-types/src/lib.rs b/wasmtime-interface-types/src/lib.rs index 7db0b3fb21..3b2a553c07 100644 --- a/wasmtime-interface-types/src/lib.rs +++ b/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 { + 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)