| 865 | /// details on Wasmtime's out-of-memory handling. |
| 866 | #[cfg(feature = "async")] |
| 867 | pub async fn module_async( |
| 868 | &mut self, |
| 869 | mut store: impl AsContextMut<Data = T>, |
| 870 | module_name: &str, |
| 871 | module: &Module, |
| 872 | ) -> Result<&mut Self> |
| 873 | where |
| 874 | T: Send + 'static, |
| 875 | { |
| 876 | // NB: this is intended to function the same as `Linker::module`, they |
| 877 | // should be kept in sync. |
| 878 | assert!( |
| 879 | Engine::same(&self.engine, store.as_context().engine()), |
| 880 | "different engines for this linker and the store provided" |
| 881 | ); |
| 882 | match ModuleKind::categorize(module)? { |
| 883 | ModuleKind::Command => self.command( |
| 884 | store, |
| 885 | module_name, |
| 886 | module, |
| 887 | |store, func_ty, export_name, instance_pre| { |
| 888 | let upvars = Arc::new((instance_pre, export_name)); |
| 889 | Func::new_async( |
| 890 | store, |
| 891 | func_ty.clone(), |
| 892 | move |mut caller, params, results| { |
| 893 | let upvars = upvars.clone(); |
| 894 | Box::new(async move { |
| 895 | let (instance_pre, export_name) = &*upvars; |
| 896 | let instance = instance_pre.instantiate_async(&mut caller).await?; |
| 897 | |
| 898 | instance |
| 899 | .get_export(&mut caller, &export_name) |
| 900 | .unwrap() |
| 901 | .into_func() |
| 902 | .unwrap() |
| 903 | .call_async(&mut caller, params, results) |
| 904 | .await?; |
| 905 | Ok(()) |
| 906 | }) |
| 907 | }, |
| 908 | ) |
| 909 | }, |
| 910 | ), |
| 911 | ModuleKind::Reactor => { |
| 912 | let instance = self.instantiate_async(&mut store, &module).await?; |
| 913 | |
| 914 | if let Some(export) = instance.get_export(&mut store, "_initialize") { |
| 915 | if let Extern::Func(func) = export { |
| 916 | let func = func |
| 917 | .typed::<(), ()>(&store) |
| 918 | .context("loading the Reactor initialization function")?; |
| 919 | func.call_async(&mut store, ()) |
| 920 | .await |
| 921 | .context("calling the Reactor initialization function")?; |
| 922 | } |
| 923 | } |
| 924 | |