Instantiates the `module` provided and registers the instance under the `name` provided if successful.
(&mut self, name: Option<&str>, module: &ModuleKind)
| 357 | /// Instantiates the `module` provided and registers the instance under the |
| 358 | /// `name` provided if successful. |
| 359 | fn module(&mut self, name: Option<&str>, module: &ModuleKind) -> Result<()> { |
| 360 | match module { |
| 361 | ModuleKind::Core(module) => { |
| 362 | let instance = match self.instantiate_module(&module)? { |
| 363 | Outcome::Ok(i) => i, |
| 364 | Outcome::Trap(e) => return Err(e).context("instantiation failed"), |
| 365 | }; |
| 366 | if let Some(name) = name { |
| 367 | self.core_linker |
| 368 | .instance(&mut self.core_store, name, instance)?; |
| 369 | } |
| 370 | self.current = Some(InstanceKind::Core(instance)); |
| 371 | } |
| 372 | #[cfg(feature = "component-model")] |
| 373 | ModuleKind::Component(module) => { |
| 374 | let (component, mut store, instance) = match self.instantiate_component(&module)? { |
| 375 | Outcome::Ok(i) => i, |
| 376 | Outcome::Trap(e) => return Err(e).context("instantiation failed"), |
| 377 | }; |
| 378 | if let Some(name) = name { |
| 379 | let ty = component.component_type(); |
| 380 | let engine = self.engine().clone(); |
| 381 | let mut linker = self.component_linker.instance(name)?; |
| 382 | for (name, item) in ty.exports(&engine) { |
| 383 | match item.ty { |
| 384 | component::types::ComponentItem::Module(_) => { |
| 385 | let module = instance.get_module(&mut store, name).unwrap(); |
| 386 | linker.module(name, &module)?; |
| 387 | } |
| 388 | component::types::ComponentItem::Resource(_) => { |
| 389 | let resource = instance.get_resource(&mut store, name).unwrap(); |
| 390 | linker.resource(name, resource, |_, _| Ok(()))?; |
| 391 | } |
| 392 | // TODO: should ideally reflect more than just |
| 393 | // modules/resources into the linker's namespace |
| 394 | // but that's not easily supported today for host |
| 395 | // functions due to the inability to take a |
| 396 | // function from one instance and put it into the |
| 397 | // linker (must go through the host right now). |
| 398 | _ => {} |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | self.current = Some(InstanceKind::Component(store, instance)); |
| 403 | } |
| 404 | } |
| 405 | Ok(()) |
| 406 | } |
| 407 | |
| 408 | /// Compiles the module `wat` into binary and returns the name found within |
| 409 | /// it, if any. |