From f0cbd7f3ca49c984f5f395c5cf076c38a9e81381 Mon Sep 17 00:00:00 2001 From: officeyutong Date: Wed, 1 Mar 2023 08:51:07 +0000 Subject: [PATCH] update: [rust runtime]support passing arguments to Wasm program --- runtime/rust/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/runtime/rust/src/main.rs b/runtime/rust/src/main.rs index 3f21360..32f1cad 100644 --- a/runtime/rust/src/main.rs +++ b/runtime/rust/src/main.rs @@ -37,11 +37,12 @@ struct CommandArgs { wrapper_module_name: String, #[arg(short = 'c', long, help = "Callback export name", default_value_t = String::from("go-callback"))] callback_export_name: String, + #[arg(help = "Arguments that will be passed to the Wasm program")] + args_to_wasm: Vec, } fn main() -> anyhow::Result<()> { let args = CommandArgs::parse(); - Logger::try_with_str(if args.verbose { "debug" } else { "info" })? .format(my_log_format) .start()?; @@ -50,10 +51,15 @@ fn main() -> anyhow::Result<()> { let mut linker = Linker::new(&engine); wasmtime_wasi::add_to_linker(&mut linker, |s: &mut AppState| &mut s.wasi) .with_context(|| anyhow!("Failed to add wasmtime_wasi to linker"))?; + let mut args_to_wasm = args.args_to_wasm; + args_to_wasm.insert(0, args.wasm_module_file.clone()); + let wasi = WasiCtxBuilder::new() .inherit_stdio() .inherit_args() .with_context(|| anyhow!("Failed to build Wasi Context"))? + .args(&args_to_wasm[..]) + .with_context(|| anyhow!("Failed to pass arguments to Wasm program"))? .build(); let mut store = Store::new(&engine, AppState::new(wasi, args.callback_export_name)); let main_module = Module::from_file(&engine, args.wasm_module_file)