Skip to content

Commit

Permalink
Merge #1036
Browse files Browse the repository at this point in the history
1036: Add experimental invoke support to WASI in wasmer cli r=MarkMcCaskey a=MarkMcCaskey

Resolves #1024 

This PR also does a bit of code clean up

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2019
2 parents 59d2b8b + d2f5690 commit b336726
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,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 @@ -455,28 +472,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 @@ -786,16 +813,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

0 comments on commit b336726

Please sign in to comment.