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

Method module

crates/wasmtime/src/runtime/linker.rs:791–855  ·  view source on GitHub ↗

Define automatic instantiations of a [`Module`] in this linker. This automatically handles [Commands and Reactors] instantiation and initialization. Exported functions of a Command module may be called directly, however instead of having a single instance which is reused for each call, each call creates a new instance, which lives for the duration of the call. The imports of the Command are reso

(
        &mut self,
        mut store: impl AsContextMut<Data = T>,
        module_name: &str,
        module: &Module,
    )

Source from the content-addressed store, hash-verified

789 /// # }
790 /// ```
791 pub fn module(
792 &mut self,
793 mut store: impl AsContextMut<Data = T>,
794 module_name: &str,
795 module: &Module,
796 ) -> Result<&mut Self>
797 where
798 T: 'static,
799 {
800 // NB: this is intended to function the same as `Linker::module_async`,
801 // they should be kept in sync.
802
803 // This assert isn't strictly necessary since it'll bottom out in the
804 // `HostFunc::to_func` method anyway. This is placed earlier for this
805 // function though to prevent the functions created here from delaying
806 // the panic until they're called.
807 assert!(
808 Engine::same(&self.engine, store.as_context().engine()),
809 "different engines for this linker and the store provided"
810 );
811 match ModuleKind::categorize(module)? {
812 ModuleKind::Command => {
813 self.command(
814 store,
815 module_name,
816 module,
817 |store, func_ty, export_name, instance_pre| {
818 Func::new(
819 store,
820 func_ty.clone(),
821 move |mut caller, params, results| {
822 // Create a new instance for this command execution.
823 let instance = instance_pre.instantiate(&mut caller)?;
824
825 // `unwrap()` everything here because we know the instance contains a
826 // function export with the given name and signature because we're
827 // iterating over the module it was instantiated from.
828 instance
829 .get_export(&mut caller, &export_name)
830 .unwrap()
831 .into_func()
832 .unwrap()
833 .call(&mut caller, params, results)?;
834
835 Ok(())
836 },
837 )
838 },
839 )
840 }
841 ModuleKind::Reactor => {
842 let instance = self.instantiate(&mut store, &module)?;
843
844 if let Some(export) = instance.get_export(&mut store, "_initialize") {
845 if let Extern::Func(func) = export {
846 func.typed::<(), ()>(&store)
847 .and_then(|f| f.call(&mut store, ()))
848 .context("calling the Reactor initialization function")?;

Callers 4

_get_by_importMethod · 0.45
newMethod · 0.45

Calls 11

OkFunction · 0.85
commandMethod · 0.80
newFunction · 0.50
cloneMethod · 0.45
instantiateMethod · 0.45
callMethod · 0.45
unwrapMethod · 0.45
into_funcMethod · 0.45
get_exportMethod · 0.45
contextMethod · 0.45
instanceMethod · 0.45

Tested by

no test coverage detected