github.com/tetratelabs/wazero@v1.7.1/experimental/checkpoint.go (about)

     1  package experimental
     2  
     3  import (
     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.
    10  type 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.
    19  type Snapshotter interface {
    20  	// Snapshot captures the current execution state.
    21  	Snapshot() Snapshot
    22  }
    23  
    24  // EnableSnapshotterKey is a context key to indicate that snapshotting should be enabled.
    25  // The context.Context passed to a exported function invocation should have this key set
    26  // to a non-nil value, and host functions will be able to retrieve it using SnapshotterKey.
    27  //
    28  // Deprecated: use WithSnapshotter to enable snapshots.
    29  type EnableSnapshotterKey = expctxkeys.EnableSnapshotterKey
    30  
    31  // WithSnapshotter enables snapshots.
    32  // Passing the returned context to a exported function invocation enables snapshots,
    33  // and allows host functions to retrieve the Snapshotter using GetSnapshotter.
    34  func WithSnapshotter(ctx context.Context) context.Context {
    35  	return context.WithValue(ctx, expctxkeys.EnableSnapshotterKey{}, struct{}{})
    36  }
    37  
    38  // SnapshotterKey is a context key to access a Snapshotter from a host function.
    39  // It is only present if EnableSnapshotter was set in the function invocation context.
    40  //
    41  // Deprecated: use GetSnapshotter to get the snapshotter.
    42  type SnapshotterKey = expctxkeys.SnapshotterKey
    43  
    44  // GetSnapshotter gets the Snapshotter from a host function.
    45  // It is only present if WithSnapshotter was called with the function invocation context.
    46  func GetSnapshotter(ctx context.Context) Snapshotter {
    47  	return ctx.Value(expctxkeys.SnapshotterKey{}).(Snapshotter)
    48  }