(
&self,
store: &mut Store<Host>,
component: &wasmtime::component::Component,
linker: &mut wasmtime::component::Linker<Host>,
)
| 774 | |
| 775 | #[cfg(feature = "component-model")] |
| 776 | async fn invoke_component( |
| 777 | &self, |
| 778 | store: &mut Store<Host>, |
| 779 | component: &wasmtime::component::Component, |
| 780 | linker: &mut wasmtime::component::Linker<Host>, |
| 781 | ) -> Result<wasmtime::component::Instance> { |
| 782 | use wasmtime::component::{ |
| 783 | Val, |
| 784 | wasm_wave::{ |
| 785 | untyped::UntypedFuncCall, |
| 786 | wasm::{DisplayFuncResults, WasmFunc}, |
| 787 | }, |
| 788 | }; |
| 789 | |
| 790 | // Check if the invoke string is present |
| 791 | let invoke: &String = self.invoke.as_ref().unwrap(); |
| 792 | |
| 793 | let untyped_call = UntypedFuncCall::parse(invoke).with_context(|| { |
| 794 | format!( |
| 795 | "Failed to parse invoke '{invoke}': See https://docs.wasmtime.dev/cli-options.html#run for syntax", |
| 796 | ) |
| 797 | })?; |
| 798 | |
| 799 | let name = untyped_call.name(); |
| 800 | let matches = |
| 801 | Self::search_component_funcs(store.engine(), component.component_type(), name); |
| 802 | let (names, func_type) = match matches.len() { |
| 803 | 0 => bail!("No exported func named `{name}` in component."), |
| 804 | 1 => &matches[0], |
| 805 | _ => bail!( |
| 806 | "Multiple exports named `{name}`: {matches:?}. FIXME: support some way to disambiguate names" |
| 807 | ), |
| 808 | }; |
| 809 | |
| 810 | let param_types = WasmFunc::params(func_type).collect::<Vec<_>>(); |
| 811 | let params = untyped_call |
| 812 | .to_wasm_params(¶m_types) |
| 813 | .with_context(|| format!("while interpreting parameters in invoke \"{invoke}\""))?; |
| 814 | |
| 815 | let export = names |
| 816 | .iter() |
| 817 | .fold(None, |instance, name| { |
| 818 | component.get_export_index(instance.as_ref(), name) |
| 819 | }) |
| 820 | .expect("export has at least one name"); |
| 821 | |
| 822 | let instance = linker.instantiate_async(&mut *store, component).await?; |
| 823 | |
| 824 | let func = instance |
| 825 | .get_func(&mut *store, export) |
| 826 | .expect("found export index"); |
| 827 | |
| 828 | let mut results = vec![Val::Bool(false); func_type.results().len()]; |
| 829 | self.call_component_func(store, ¶ms, func, &mut results) |
| 830 | .await?; |
| 831 | |
| 832 | println!("{}", DisplayFuncResults(&results)); |
| 833 | Ok(instance) |
no test coverage detected