MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / invoke_component

Method invoke_component

src/commands/run.rs:776–834  ·  view source on GitHub ↗
(
        &self,
        store: &mut Store<Host>,
        component: &wasmtime::component::Component,
        linker: &mut wasmtime::component::Linker<Host>,
    )

Source from the content-addressed store, hash-verified

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(&param_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, &params, func, &mut results)
830 .await?;
831
832 println!("{}", DisplayFuncResults(&results));
833 Ok(instance)

Callers 1

load_main_moduleMethod · 0.80

Calls 15

OkFunction · 0.85
with_contextMethod · 0.80
component_typeMethod · 0.80
call_component_funcMethod · 0.80
parseFunction · 0.50
unwrapMethod · 0.45
as_refMethod · 0.45
nameMethod · 0.45
engineMethod · 0.45
lenMethod · 0.45
expectMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected