Skip to content

Commit c5609bc

Browse files
authored
Update documentation of enter/exit hooks (bytecodealliance#3041)
Clarify that they're executed not only around imports but also around function calls. Additionally spell out the semantics around traps a bit more clearly too.
1 parent 1acb40b commit c5609bc

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

crates/wasmtime/src/store.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,51 @@ impl<T> Store<T> {
327327
inner.limiter = Some(Box::new(limiter));
328328
}
329329

330-
/// Configure a function that runs each time WebAssembly code running on this [`Store`] calls
331-
/// into native code.
330+
/// Configure a function that runs each time the host resumes execution from
331+
/// WebAssembly.
332332
///
333-
/// This function may return a [`Trap`], which terminates execution.
333+
/// This hook is called in two circumstances:
334+
///
335+
/// * When WebAssembly calls a function defined by the host, this hook is
336+
/// called before other host code runs.
337+
/// * When WebAssembly returns back to the host after being called, this
338+
/// hook is called.
339+
///
340+
/// This method can be used with [`Store::exiting_native_code_hook`] to track
341+
/// execution time of WebAssembly, for example, by starting/stopping timers
342+
/// in the enter/exit hooks.
343+
///
344+
/// This function may return a [`Trap`]. If a trap is returned when an
345+
/// import was called, it is immediately raised as-if the host import had
346+
/// returned the trap. If a trap is returned after wasm returns to the host
347+
/// then the wasm function's result is ignored and this trap is returned
348+
/// instead.
334349
pub fn entering_native_code_hook(
335350
&mut self,
336351
hook: impl FnMut(&mut T) -> Result<(), Trap> + Send + Sync + 'static,
337352
) {
338353
self.inner.entering_native_hook = Some(Box::new(hook));
339354
}
340355

341-
/// Configure a function that runs before native code running on this [`Store`] returns to
342-
/// WebAssembly code.
356+
/// Configure a function that runs just before WebAssembly code starts
357+
/// executing.
358+
///
359+
/// The closure provided is called in two circumstances:
360+
///
361+
/// * When the host calls a WebAssembly function, the hook is called just
362+
/// before WebAssembly starts executing.
363+
/// * When a host function returns back to WebAssembly this hook is called
364+
/// just before the return.
365+
///
366+
/// This method can be used with [`Store::entering_native_code_hook`] to track
367+
/// execution time of WebAssembly, for example, by starting/stopping timers
368+
/// in the enter/exit hooks.
343369
///
344-
/// This function may return a [`Trap`], which terminates execution.
370+
/// This function may return a [`Trap`]. If a trap is returned when an
371+
/// imported host function is returning, then the imported host function's
372+
/// result is ignored and the trap is raised. If a trap is returned when
373+
/// the host is about to start executing WebAssembly, then no WebAssembly
374+
/// code is run and the trap is returned instead.
345375
pub fn exiting_native_code_hook(
346376
&mut self,
347377
hook: impl FnMut(&mut T) -> Result<(), Trap> + Send + Sync + 'static,

0 commit comments

Comments
 (0)