Skip to content

Commit

Permalink
Merge #2386
Browse files Browse the repository at this point in the history
2386: fix unwrap error when given a no export functions wasm r=syrusakbary a=chenyukang


# Description
When the wasm file contains no export functions, an unwrap crash will trigger:

```
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', lib/cli/src/commands/run.rs:344:52
```

This fix will give a more detailed output.




Co-authored-by: chenyukang <[email protected]>
  • Loading branch information
bors[bot] and chenyukang authored Jun 4, 2021
2 parents 013fca6 + c5601f0 commit cae21af
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,17 @@ impl Run {
let suggested_command = format!(
"wasmer {} -i {} {}",
self.path.display(),
suggested_functions.get(0).unwrap(),
suggested_functions.get(0).unwrap_or(&String::new()),
args.join(" ")
);
let suggestion = format!(
"Similar functions found: {}.\nTry with: {}",
names, suggested_command
);
let suggestion = if suggested_functions.len() == 0 {
String::from("Can not find any export functions.")
} else {
format!(
"Similar functions found: {}.\nTry with: {}",
names, suggested_command
)
};
match e {
ExportError::Missing(_) => {
anyhow!("No export `{}` found in the module.\n{}", name, suggestion)
Expand Down
5 changes: 5 additions & 0 deletions tests/examples/no_start.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(type (;0;) (func (param f64) (result i32)))
(func (;0;) (type 0) (param f64) (result i32)
unreachable))

4 changes: 4 additions & 0 deletions tests/integration/cli/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub const ASSET_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../test

pub const WASMER_INCLUDE_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../lib/c-api");

#[cfg(feature = "debug")]
pub const WASMER_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/debug/wasmer");

#[cfg(not(feature = "debug"))]
pub const WASMER_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../../target/release/wasmer"
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ fn test_no_imports_wat_path() -> String {
format!("{}/{}", ASSET_PATH, "fib.wat")
}

fn test_no_start_wat_path() -> String {
format!("{}/{}", ASSET_PATH, "no_start.wat")
}

#[test]
fn run_wasi_works() -> anyhow::Result<()> {
let output = Command::new(WASMER_PATH)
Expand Down Expand Up @@ -39,6 +43,7 @@ fn run_wasi_works() -> anyhow::Result<()> {
}

#[test]

fn run_no_imports_wasm_works() -> anyhow::Result<()> {
let output = Command::new(WASMER_PATH)
.arg("run")
Expand All @@ -57,3 +62,16 @@ fn run_no_imports_wasm_works() -> anyhow::Result<()> {

Ok(())
}

#[test]
fn run_no_start_wasm_report_error() -> anyhow::Result<()> {
let output = Command::new(WASMER_PATH)
.arg("run")
.arg(test_no_start_wat_path())
.output()?;

assert_eq!(output.status.success(), false);
let result = std::str::from_utf8(&output.stderr).unwrap().to_string();
assert_eq!(result.contains("Can not find any export functions."), true);
Ok(())
}

0 comments on commit cae21af

Please sign in to comment.