Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental invoke support to WASI in wasmer cli #1036

Merged
merged 2 commits into from
Dec 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ struct Run {
args: Vec<String>,
}

impl Run {
/// Used with the `invoke` argument
fn parse_args(&self) -> Result<Vec<Value>, String> {
let mut args: Vec<Value> = 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 {
Expand Down Expand Up @@ -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 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)
.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 {
Expand Down Expand Up @@ -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<Value> = 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,
Expand Down