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:
@@ -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"
|
||||
|
||||
@@ -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 })
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user