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

Fix "run --invoke [function]" to behave the same as "run" #2997

Merged
merged 4 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C
- [#3008](https://github.com/wasmerio/wasmer/pull/3008) Add a new CI check that uses cargo public-api to track changes in the API between master and the last deployed version on crates.io
- [#3003](https://github.com/wasmerio/wasmer/pull/3003) Remove RuntimeError::raise from public API
- [#2999](https://github.com/wasmerio/wasmer/pull/2999) Allow `--invoke` CLI option for Emscripten files without a `main` function
- [#2997](https://github.com/wasmerio/wasmer/pull/2997) Fix "run --invoke [function]" to behave the same as "run"
- [#2946](https://github.com/wasmerio/wasmer/pull/2946) Remove dylib,staticlib engines in favor of a single Universal engine
- [#2949](https://github.com/wasmerio/wasmer/pull/2949) Switch back to using custom LLVM builds on CI
- #2892 Renamed `get_native_function` to `get_typed_function`, marked former as deprecated.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,34 @@ impl Run {
};
#[cfg(not(feature = "wasi"))]
let ret = {
let instance = Instance::new(&mut store, &module, &imports! {})?;
self.inner_run(ctx, instance)
let instance = Instance::new(&module, &imports! {})?;

// If this module exports an _initialize function, run that first.
if let Ok(initialize) = instance.exports.get_function("_initialize") {
initialize
.call(&[])
.with_context(|| "failed to run _initialize function")?;
}

// Do we want to invoke a function?
if let Some(ref invoke) = self.invoke {
let result = self.invoke_function(&instance, invoke, &self.args)?;
println!(
"{}",
result
.iter()
.map(|val| val.to_string())
.collect::<Vec<String>>()
.join(" ")
);
} else {
let start: Function = self.try_find_function(&instance, "_start", &[])?;
let result = start.call(&[]);
#[cfg(feature = "wasi")]
self.wasi.handle_result(result)?;
#[cfg(not(feature = "wasi"))]
result?;
}
};

ret
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ repository = "https://github.com/wasmerio/wasmer"
edition = "2018"
publish = false

[dev-dependencies]
rand = "0.8.5"

Comment on lines +10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tempfile is useful for this workflow. (Don't rewrite your PR for this minor thing, it's just a suggestion for future tests)

[dependencies]
anyhow = "1"
tempfile = "3"
51 changes: 50 additions & 1 deletion tests/integration/cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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 @@ -63,6 +62,56 @@ fn run_no_imports_wasm_works() -> anyhow::Result<()> {
Ok(())
}

// This test verifies that "wasmer run --invoke _start module.wat"
// works the same as "wasmer run module.wat" (without --invoke).
#[test]
fn run_invoke_works_with_nomain_wasi() -> anyhow::Result<()> {
// In this example the function "wasi_unstable.arg_sizes_get"
// is a function that is imported from the WASI env.
let wasi_wat = "
(module
(import \"wasi_unstable\" \"args_sizes_get\"
(func $__wasi_args_sizes_get (param i32 i32) (result i32)))
(func $_start)
(memory 1)
(export \"memory\" (memory 0))
(export \"_start\" (func $_start))
)
";

let random = rand::random::<u64>();
let module_file = std::env::temp_dir().join(&format!("{random}.wat"));
std::fs::write(&module_file, wasi_wat.as_bytes()).unwrap();
let output = Command::new(WASMER_PATH)
.arg("run")
.arg(&module_file)
.output()?;

let stderr = std::str::from_utf8(&output.stderr).unwrap().to_string();
let success = output.status.success();
if !success {
println!("ERROR in 'wasmer run [module.wat]':\r\n{stderr}");
panic!();
}

let output = Command::new(WASMER_PATH)
.arg("run")
.arg("--invoke")
.arg("_start")
.arg(&module_file)
.output()?;

let stderr = std::str::from_utf8(&output.stderr).unwrap().to_string();
let success = output.status.success();
if !success {
println!("ERROR in 'wasmer run --invoke _start [module.wat]':\r\n{stderr}");
panic!();
}

std::fs::remove_file(&module_file).unwrap();
Ok(())
}

#[test]
fn run_no_start_wasm_report_error() -> anyhow::Result<()> {
let output = Command::new(WASMER_PATH)
Expand Down