| 639 | } |
| 640 | |
| 641 | fn execute(&mut self) -> Result<()> { |
| 642 | if let Some((mut store, instance)) = self.component_store_and_instance.take() { |
| 643 | let command = wasmtime_wasi::p2::bindings::sync::Command::new(&mut store, &instance)?; |
| 644 | match command.wasi_cli_run().call_run(&mut store)? { |
| 645 | Ok(()) => Ok(()), |
| 646 | Err(()) => Err(format_err!("calling `run` failed")), |
| 647 | } |
| 648 | } else { |
| 649 | let (mut store, instance) = self |
| 650 | .store_and_instance |
| 651 | .take() |
| 652 | .expect("instantiate the module before executing it"); |
| 653 | |
| 654 | let start_func = instance.get_typed_func::<(), ()>(&mut store, "_start")?; |
| 655 | match start_func.call(&mut store, ()) { |
| 656 | Ok(_) => Ok(()), |
| 657 | Err(trap) => { |
| 658 | // Since _start will likely return by using the system `exit` call, we must |
| 659 | // check the trap code to see if it actually represents a successful exit. |
| 660 | if let Some(exit) = trap.downcast_ref::<I32Exit>() { |
| 661 | if exit.0 == 0 { |
| 662 | return Ok(()); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | Err(trap) |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |