From 254cf40b4365aacb5ff10b3055c17282de3f7595 Mon Sep 17 00:00:00 2001 From: jubianchi Date: Mon, 2 Nov 2020 23:37:30 +0100 Subject: [PATCH] doc(examples): Clean and comment the early-exit example --- examples/early_exit.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/early_exit.rs b/examples/early_exit.rs index ccbabb110b7..43638f06bad 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -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" @@ -9,7 +16,7 @@ use anyhow::bail; use std::fmt; -use wasmer::{imports, wat2wasm, Function, Instance, Module, NativeFunc, RuntimeError, Store}; +use wasmer::{imports, wat2wasm, Function, Instance, Module, NativeFunc, Store, RuntimeError}; use wasmer_compiler_cranelift::Cranelift; use wasmer_engine_jit::JIT; @@ -73,10 +80,9 @@ 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) {