From 37daaf2834e06b033e71bf8bdcbeb0e032ed83b4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 21 Aug 2023 07:23:39 -0700 Subject: [PATCH 1/2] Use generated bindings to run components in the CLI This commit updates the component support in the `wasmtime` CLI from #6836 to use the generated bindings for the "command" style of components rather than having that custom-written in the CLI. --- src/commands/run.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index f500844b9d8b..89a050e1b105 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -620,26 +620,27 @@ impl RunCommand { } #[cfg(feature = "component-model")] CliLinker::Component(linker) => { - let component = module.unwrap_component(); - let instance = linker.instantiate(&mut *store, component)?; - if self.invoke.is_some() { bail!("using `--invoke` with components is not supported"); } - // TODO: use the actual world - let func = instance - .get_typed_func::<(), (Result<(), ()>,)>(&mut *store, "run") - .context("failed to load `run` function")?; + let component = module.unwrap_component(); + + let (command, _instance) = preview2::command::sync::Command::instantiate( + &mut *store, + &component, + &linker, + )?; - let result = func - .call(&mut *store, ()) + let result = command + .wasi_cli_run() + .call_run(&mut *store) .context("failed to invoke `run` function") .map_err(|e| self.handle_coredump(e)); // Translate the `Result<(),()>` produced by wasm into a feigned // explicit exit here with status 1 if `Err(())` is returned. - result.and_then(|(wasm_result,)| match wasm_result { + result.and_then(|wasm_result| match wasm_result { Ok(()) => Ok(()), Err(()) => Err(wasmtime_wasi::I32Exit(1).into()), }) From f50ff3b5083ea9f8ebc68933a669bb261689ad5a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 21 Aug 2023 08:16:30 -0700 Subject: [PATCH 2/2] Fix tests --- tests/all/cli_tests/component-basic.wat | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/all/cli_tests/component-basic.wat b/tests/all/cli_tests/component-basic.wat index 2b7f54a7af75..19a5fb2c6f50 100644 --- a/tests/all/cli_tests/component-basic.wat +++ b/tests/all/cli_tests/component-basic.wat @@ -4,7 +4,9 @@ i32.const 0) ) (core instance $i (instantiate $m)) - (func (export "run") (result (result)) + (func $run (result (result)) (canon lift (core func $i "run"))) + (instance (export (interface "wasi:cli/run")) + (export "run" (func $run))) )