From 356720efd2068c8883e0b12f9b532d6ceaaf0a4c Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 2 Dec 2019 16:53:15 -0800 Subject: [PATCH 1/2] Add experimental invoke support to WASI in wasmer cli --- src/bin/wasmer.rs | 72 +++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 457b6f51060..2cf9108d9fa 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -230,6 +230,23 @@ struct Run { args: Vec, } +impl Run { + /// Used with the `invoke` argument + fn parse_args(&self) -> Result, String> { + let mut args: Vec = Vec::new(); + for arg in self.args.iter() { + let x = arg.as_str().parse().map_err(|_| { + format!( + "Can't parse the provided argument {:?} as a integer", + arg.as_str() + ) + })?; + args.push(Value::I32(x)); + } + Ok(args) + } +} + #[allow(dead_code)] #[derive(Debug, Copy, Clone)] enum LoaderName { @@ -448,28 +465,38 @@ fn execute_wasi( let result; #[cfg(unix)] - { - let cv_pushed = - if let Some(msm) = instance.module.runnable_module.get_module_state_map() { - push_code_version(CodeVersion { - baseline: true, - msm: msm, - base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize, - backend: options.backend, - }); - true - } else { - false - }; + let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() { + push_code_version(CodeVersion { + baseline: true, + msm: msm, + base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize, + backend: options.backend, + }); + true + } else { + false + }; + + if let Some(invoke_fn) = options.invoke.as_ref() { + eprintln!("WARNING: Invoking a function with WASI is not officially supported in the WASI standard yet. Use this feature at your own risk, things may not work!"); + let args = options.parse_args()?; + let invoke_result = instance + .dyn_func(invoke_fn) + .map_err(|e| format!("Invoke failed: {:?}", e))? + .call(&args) + .map_err(|e| format!("Calling invoke fn failed: {:?}", e))?; + println!("{} returned {:?}", invoke_fn, invoke_result); + return Ok(()); + } else { result = start.call(); + } + + #[cfg(unix)] + { if cv_pushed { pop_code_version().unwrap(); } } - #[cfg(not(unix))] - { - result = start.call(); - } if let Err(ref err) = result { match err { @@ -755,16 +782,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> { .instantiate(&import_object) .map_err(|e| format!("Can't instantiate module: {:?}", e))?; - let mut args: Vec = Vec::new(); - for arg in options.args.iter() { - let x = arg.as_str().parse().map_err(|_| { - format!( - "Can't parse the provided argument {:?} as a integer", - arg.as_str() - ) - })?; - args.push(Value::I32(x)); - } + let args = options.parse_args()?; let invoke_fn = match options.invoke.as_ref() { Some(fun) => fun, From d2f5690cae747afd59218ef9f482d339e4e21365 Mon Sep 17 00:00:00 2001 From: Mark McCaskey <5770194+MarkMcCaskey@users.noreply.github.com> Date: Fri, 6 Dec 2019 10:36:45 -0800 Subject: [PATCH 2/2] Improve warning message when invoking WASI functions --- src/bin/wasmer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 2cf9108d9fa..5dedb3f1138 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -478,7 +478,7 @@ fn execute_wasi( }; if let Some(invoke_fn) = options.invoke.as_ref() { - eprintln!("WARNING: Invoking a function with WASI is not officially supported in the WASI standard yet. Use this feature at your own risk, things may not work!"); + eprintln!("WARNING: Invoking aribtrary functions with WASI is not officially supported in the WASI standard yet. Use this feature at your own risk!"); let args = options.parse_args()?; let invoke_result = instance .dyn_func(invoke_fn)