@@ -493,30 +493,13 @@ impl RunCommand {
493493 #[ cfg( feature = "component-model" ) ]
494494 CliLinker :: Component ( linker) => {
495495 let component = main_target. unwrap_component ( ) ;
496- if self . invoke . is_some ( ) {
496+ let result = if self . invoke . is_some ( ) {
497497 self . invoke_component ( & mut * store, component, linker) . await
498498 } else {
499- let command = wasmtime_wasi:: p2:: bindings:: Command :: instantiate_async (
500- & mut * store,
501- component,
502- linker,
503- )
504- . await ?;
505-
506- let result = command
507- . wasi_cli_run ( )
508- . call_run ( & mut * store)
499+ self . run_command_component ( & mut * store, component, linker)
509500 . await
510- . context ( "failed to invoke `run` function" )
511- . map_err ( |e| self . handle_core_dump ( & mut * store, e) ) ;
512-
513- // Translate the `Result<(),()>` produced by wasm into a feigned
514- // explicit exit here with status 1 if `Err(())` is returned.
515- result. and_then ( |wasm_result| match wasm_result {
516- Ok ( ( ) ) => Ok ( ( ) ) ,
517- Err ( ( ) ) => Err ( wasmtime_wasi:: I32Exit ( 1 ) . into ( ) ) ,
518- } )
519- }
501+ } ;
502+ result. map_err ( |e| self . handle_core_dump ( & mut * store, e) )
520503 }
521504 } ;
522505 finish_epoch_handler ( store) ;
@@ -596,6 +579,56 @@ impl RunCommand {
596579 Ok ( ( ) )
597580 }
598581
582+ /// Execute the default behavior for components on the CLI, looking for
583+ /// `wasi:cli`-based commands and running their exported `run` function.
584+ #[ cfg( feature = "component-model" ) ]
585+ async fn run_command_component (
586+ & self ,
587+ store : & mut Store < Host > ,
588+ component : & wasmtime:: component:: Component ,
589+ linker : & wasmtime:: component:: Linker < Host > ,
590+ ) -> Result < ( ) > {
591+ let instance = linker. instantiate_async ( & mut * store, component) . await ?;
592+
593+ let mut result = None ;
594+ let _ = & mut result;
595+
596+ // If WASIp3 is enabled at compile time, enabled at runtime, and found
597+ // in this component then use that to generate the result.
598+ #[ cfg( feature = "component-model-async" ) ]
599+ if self . run . common . wasi . p3 . unwrap_or ( crate :: common:: P3_DEFAULT ) {
600+ if let Ok ( command) = wasmtime_wasi:: p3:: bindings:: Command :: new ( & mut * store, & instance) {
601+ result = Some (
602+ instance
603+ . run_concurrent ( & mut * store, async |store| {
604+ command. wasi_cli_run ( ) . call_run ( store) . await
605+ } )
606+ . await ?,
607+ ) ;
608+ }
609+ }
610+
611+ let result = match result {
612+ Some ( result) => result,
613+ // If WASIp3 wasn't found then fall back to requiring WASIp2 and
614+ // this'll report an error if the right export doesn't exist.
615+ None => {
616+ wasmtime_wasi:: p2:: bindings:: Command :: new ( & mut * store, & instance) ?
617+ . wasi_cli_run ( )
618+ . call_run ( & mut * store)
619+ . await
620+ }
621+ } ;
622+ let wasm_result = result. context ( "failed to invoke `run` function" ) ?;
623+
624+ // Translate the `Result<(),()>` produced by wasm into a feigned
625+ // explicit exit here with status 1 if `Err(())` is returned.
626+ match wasm_result {
627+ Ok ( ( ) ) => Ok ( ( ) ) ,
628+ Err ( ( ) ) => Err ( wasmtime_wasi:: I32Exit ( 1 ) . into ( ) ) ,
629+ }
630+ }
631+
599632 #[ cfg( feature = "component-model" ) ]
600633 fn search_component (
601634 engine : & Engine ,
@@ -756,20 +789,8 @@ impl RunCommand {
756789 store : & mut Store < Host > ,
757790 module : & RunTarget ,
758791 ) -> Result < ( ) > {
759- let mut cli = self . run . common . wasi . cli ;
760-
761- // Accept -Scommon as a deprecated alias for -Scli.
762- if let Some ( common) = self . run . common . wasi . common {
763- if cli. is_some ( ) {
764- bail ! (
765- "The -Scommon option should not be use with -Scli as it is a deprecated alias"
766- ) ;
767- } else {
768- // In the future, we may add a warning here to tell users to use
769- // `-S cli` instead of `-S common`.
770- cli = Some ( common) ;
771- }
772- }
792+ self . run . validate_p3_option ( ) ?;
793+ let cli = self . run . validate_cli_enabled ( ) ?;
773794
774795 if cli != Some ( false ) {
775796 match linker {
@@ -801,8 +822,7 @@ impl RunCommand {
801822 }
802823 #[ cfg( feature = "component-model" ) ]
803824 CliLinker :: Component ( linker) => {
804- let link_options = self . run . compute_wasi_features ( ) ;
805- wasmtime_wasi:: p2:: add_to_linker_with_options_async ( linker, & link_options) ?;
825+ self . run . add_wasmtime_wasi_to_linker ( linker) ?;
806826 self . set_wasi_ctx ( store) ?;
807827 }
808828 }
0 commit comments