mirror of
https://github.com/encounter/wasmtime.git
synced 2026-03-30 11:42:15 -07:00
9ce3ffe15e
* Update some CI dependencies * Update to the latest nightly toolchain * Update mdbook * Update QEMU for cross-compiled testing * Update `cargo nextest` for usage with MIRI prtest:full * Remove lots of unnecessary imports * Downgrade qemu as 8.2.1 seems to segfault * Remove more imports * Remove unused winch trait method * Fix warnings about unused trait methods * More unused imports * More unused imports
35 lines
1.2 KiB
Rust
35 lines
1.2 KiB
Rust
//! Small example of how you can interrupt the execution of a wasm module to
|
|
//! ensure that it doesn't run for too long.
|
|
|
|
// You can execute this example with `cargo run --example interrupt`
|
|
|
|
use wasmtime::*;
|
|
|
|
fn main() -> Result<()> {
|
|
// Enable epoch interruption code via `Config` which means that code will
|
|
// get interrupted when `Engine::increment_epoch` happens.
|
|
let engine = Engine::new(Config::new().epoch_interruption(true))?;
|
|
let mut store = Store::new(&engine, ());
|
|
store.set_epoch_deadline(1);
|
|
|
|
// Compile and instantiate a small example with an infinite loop.
|
|
let module = Module::from_file(&engine, "examples/interrupt.wat")?;
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
|
|
|
|
// Spin up a thread to send us an interrupt in a second
|
|
std::thread::spawn(move || {
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
println!("Interrupting!");
|
|
engine.increment_epoch();
|
|
});
|
|
|
|
println!("Entering infinite loop ...");
|
|
let err = run.call(&mut store, ()).unwrap_err();
|
|
|
|
println!("trap received...");
|
|
assert_eq!(err.downcast::<Trap>()?, Trap::Interrupt);
|
|
|
|
Ok(())
|
|
}
|