Skip to content

Commit

Permalink
doc(examples): Clean and comment the early-exit example
Browse files Browse the repository at this point in the history
  • Loading branch information
jubianchi committed Nov 4, 2020
1 parent 11abe0d commit f97d3da
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! This example shows how the host can terminate execution of Wasm early from
//! inside a host function called by the Wasm.
//! There are cases where you may want to interrupt this synchronous execution of the WASM module
//! while the it is calling a host function. This can be useful for saving resources, and not
//! returning back to the guest WASM for execution, when you already know the WASM execution will
//! fail, or no longer be needed.
//!
//! In this example, we will run a WASM module that calls the imported host function
//! interrupt_execution. This host function will immediately stop executing the WebAssembly module.
//!
//! You can run the example directly by executing in Wasmer root:
//!
//! ```shell
//! cargo run --example early-exit --release --features "cranelift"
Expand Down Expand Up @@ -73,15 +80,17 @@ fn main() -> anyhow::Result<()> {

// Here we go.
//
// The Wasm module exports a function called `sum`. We'll use this function
// as our entrypoint.
let run_func: NativeFunc<(i32, i32), i32> =
instance.exports.get_native_function("run").unwrap();
// Get the `run` function which we'll use as our entrypoint.
println!("Calling `run` function...");
let run_func: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(1, 7) {
Ok(result) => {
bail!("Expected early termination with `ExitCode`, found: {}", result);
bail!(
"Expected early termination with `ExitCode`, found: {}",
result
);
}
// In case of a failure, which we expect, we attempt to downcast the error into the error
// type that we were expecting.
Expand Down

0 comments on commit f97d3da

Please sign in to comment.