checkpoint.go

 1package experimental
 2
 3import (
 4	"context"
 5
 6	"github.com/tetratelabs/wazero/internal/expctxkeys"
 7)
 8
 9// Snapshot holds the execution state at the time of a Snapshotter.Snapshot call.
10type Snapshot interface {
11	// Restore sets the Wasm execution state to the capture. Because a host function
12	// calling this is resetting the pointer to the executation stack, the host function
13	// will not be able to return values in the normal way. ret is a slice of values the
14	// host function intends to return from the restored function.
15	Restore(ret []uint64)
16}
17
18// Snapshotter allows host functions to snapshot the WebAssembly execution environment.
19type Snapshotter interface {
20	// Snapshot captures the current execution state.
21	Snapshot() Snapshot
22}
23
24// WithSnapshotter enables snapshots.
25// Passing the returned context to a exported function invocation enables snapshots,
26// and allows host functions to retrieve the Snapshotter using GetSnapshotter.
27func WithSnapshotter(ctx context.Context) context.Context {
28	return context.WithValue(ctx, expctxkeys.EnableSnapshotterKey{}, struct{}{})
29}
30
31// GetSnapshotter gets the Snapshotter from a host function.
32// It is only present if WithSnapshotter was called with the function invocation context.
33func GetSnapshotter(ctx context.Context) Snapshotter {
34	return ctx.Value(expctxkeys.SnapshotterKey{}).(Snapshotter)
35}