Skip to content

Commit

Permalink
Cleanups ctxkey package (#2184)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Apr 15, 2024
1 parent 378956d commit a3d0d96
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 46 deletions.
10 changes: 5 additions & 5 deletions experimental/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package experimental
import (
"context"

"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
)

// Snapshot holds the execution state at the time of a Snapshotter.Snapshot call.
Expand All @@ -26,23 +26,23 @@ type Snapshotter interface {
// to a non-nil value, and host functions will be able to retrieve it using SnapshotterKey.
//
// Deprecated: use WithSnapshotter to enable snapshots.
type EnableSnapshotterKey = ctxkey.EnableSnapshotterKey
type EnableSnapshotterKey = expctxkeys.EnableSnapshotterKey

// WithSnapshotter enables snapshots.
// Passing the returned context to a exported function invocation enables snapshots,
// and allows host functions to retrieve the Snapshotter using GetSnapshotter.
func WithSnapshotter(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxkey.EnableSnapshotterKey{}, struct{}{})
return context.WithValue(ctx, expctxkeys.EnableSnapshotterKey{}, struct{}{})
}

// SnapshotterKey is a context key to access a Snapshotter from a host function.
// It is only present if EnableSnapshotter was set in the function invocation context.
//
// Deprecated: use GetSnapshotter to get the snapshotter.
type SnapshotterKey = ctxkey.SnapshotterKey
type SnapshotterKey = expctxkeys.SnapshotterKey

// GetSnapshotter gets the Snapshotter from a host function.
// It is only present if WithSnapshotter was called with the function invocation context.
func GetSnapshotter(ctx context.Context) Snapshotter {
return ctx.Value(ctxkey.SnapshotterKey{}).(Snapshotter)
return ctx.Value(expctxkeys.SnapshotterKey{}).(Snapshotter)
}
4 changes: 2 additions & 2 deletions experimental/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package experimental
import (
"context"

"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
)

// CloseNotifier is a notification hook, invoked when a module is closed.
Expand Down Expand Up @@ -57,7 +57,7 @@ func (f CloseNotifyFunc) CloseNotify(ctx context.Context, exitCode uint32) {
// context.Context.
func WithCloseNotifier(ctx context.Context, notifier CloseNotifier) context.Context {
if notifier != nil {
return context.WithValue(ctx, ctxkey.CloseNotifierKey{}, notifier)
return context.WithValue(ctx, expctxkeys.CloseNotifierKey{}, notifier)
}
return ctx
}
4 changes: 2 additions & 2 deletions experimental/close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
"github.com/tetratelabs/wazero/internal/testing/require"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func TestWithCloseNotifier(t *testing.T) {
tc := tt
t.Run(tc.name, func(t *testing.T) {
if decorated := experimental.WithCloseNotifier(testCtx, tc.notification); tc.expected {
require.NotNil(t, decorated.Value(ctxkey.CloseNotifierKey{}))
require.NotNil(t, decorated.Value(expctxkeys.CloseNotifierKey{}))
} else {
require.Same(t, testCtx, decorated)
}
Expand Down
6 changes: 3 additions & 3 deletions experimental/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
)

// StackIterator allows iterating on each function of the call stack, starting
Expand All @@ -28,12 +28,12 @@ type StackIterator interface {
// Its associated value should be a FunctionListenerFactory.
//
// Deprecated: use WithFunctionListenerFactory to enable snapshots.
type FunctionListenerFactoryKey = ctxkey.FunctionListenerFactoryKey
type FunctionListenerFactoryKey = expctxkeys.FunctionListenerFactoryKey

// WithFunctionListenerFactory registers a FunctionListenerFactory
// with the context.
func WithFunctionListenerFactory(ctx context.Context, factory FunctionListenerFactory) context.Context {
return context.WithValue(ctx, ctxkey.FunctionListenerFactoryKey{}, factory)
return context.WithValue(ctx, expctxkeys.FunctionListenerFactoryKey{}, factory)
}

// FunctionListenerFactory returns FunctionListeners to be notified when a
Expand Down
6 changes: 3 additions & 3 deletions experimental/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package experimental
import (
"context"

"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
)

// MemoryAllocator is a memory allocation hook which is invoked
Expand All @@ -15,7 +15,7 @@ type MemoryAllocator func(min, cap, max uint64) MemoryBuffer

// MemoryBuffer is a memory buffer that backs a Wasm memory.
type MemoryBuffer interface {
// Return the backing []byte for the memory buffer.
// Buffer returns the backing []byte for the memory buffer.
Buffer() []byte
// Grow the backing memory buffer to size bytes in length.
// To back a shared memory, Grow can't change the address
Expand All @@ -29,7 +29,7 @@ type MemoryBuffer interface {
// context.Context.
func WithMemoryAllocator(ctx context.Context, allocator MemoryAllocator) context.Context {
if allocator != nil {
return context.WithValue(ctx, ctxkey.MemoryAllocatorKey{}, allocator)
return context.WithValue(ctx, expctxkeys.MemoryAllocatorKey{}, allocator)
}
return ctx
}
13 changes: 0 additions & 13 deletions internal/ctxkey/close.go

This file was deleted.

3 changes: 0 additions & 3 deletions internal/ctxkey/memory.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/engine/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
"github.com/tetratelabs/wazero/internal/filecache"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/moremath"
Expand Down Expand Up @@ -573,8 +573,8 @@ func (ce *callEngine) call(ctx context.Context, params, results []uint64) (_ []u
}
}

if ctx.Value(ctxkey.EnableSnapshotterKey{}) != nil {
ctx = context.WithValue(ctx, ctxkey.SnapshotterKey{}, ce)
if ctx.Value(expctxkeys.EnableSnapshotterKey{}) != nil {
ctx = context.WithValue(ctx, expctxkeys.SnapshotterKey{}, ce)
}

defer func() {
Expand Down Expand Up @@ -744,7 +744,7 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
frame.pc = op.Us[v]
case wazeroir.OperationKindCall:
func() {
if ctx.Value(ctxkey.EnableSnapshotterKey{}) != nil {
if ctx.Value(expctxkeys.EnableSnapshotterKey{}) != nil {
defer func() {
if r := recover(); r != nil {
if s, ok := r.(*snapshot); ok && s.ce == ce {
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/wazevo/call_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
"github.com/tetratelabs/wazero/internal/expctxkeys"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/internal/wasmdebug"
Expand Down Expand Up @@ -194,9 +194,9 @@ func (c *callEngine) CallWithStack(ctx context.Context, paramResultStack []uint6

// CallWithStack implements api.Function.
func (c *callEngine) callWithStack(ctx context.Context, paramResultStack []uint64) (err error) {
snapshotEnabled := ctx.Value(ctxkey.EnableSnapshotterKey{}) != nil
snapshotEnabled := ctx.Value(expctxkeys.EnableSnapshotterKey{}) != nil
if snapshotEnabled {
ctx = context.WithValue(ctx, ctxkey.SnapshotterKey{}, c)
ctx = context.WithValue(ctx, expctxkeys.SnapshotterKey{}, c)
}

if wazevoapi.StackGuardCheckEnabled {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctxkey
package expctxkeys

// EnableSnapshotterKey is a context key to indicate that snapshotting should be enabled.
// The context.Context passed to a exported function invocation should have this key set
Expand Down
5 changes: 5 additions & 0 deletions internal/expctxkeys/close.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package expctxkeys

// CloseNotifierKey is a context.Context Value key. Its associated value should be a
// Notifier.
type CloseNotifierKey struct{}
2 changes: 2 additions & 0 deletions internal/expctxkeys/expctxkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package expctxkeys provides keys for the context used to store the experimental APIs.
package expctxkeys
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctxkey
package expctxkeys

// FunctionListenerFactoryKey is a context.Context Value key.
// Its associated value should be a FunctionListenerFactory.
Expand Down
4 changes: 4 additions & 0 deletions internal/expctxkeys/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package expctxkeys

// MemoryAllocatorKey is a context.Context key for the experimental memory allocator.
type MemoryAllocatorKey struct{}
6 changes: 3 additions & 3 deletions internal/wasm/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/leb128"
internalsys "github.com/tetratelabs/wazero/internal/sys"
Expand Down Expand Up @@ -125,7 +125,7 @@ type (
Source *Module

// CloseNotifier is an experimental hook called once on close.
CloseNotifier ctxkey.Notifier
CloseNotifier experimental.CloseNotifier
}

// DataInstance holds bytes corresponding to the data segment in a module.
Expand Down Expand Up @@ -365,7 +365,7 @@ func (s *Store) instantiate(

var allocator experimental.MemoryAllocator
if ctx != nil {
allocator, _ = ctx.Value(ctxkey.MemoryAllocatorKey{}).(experimental.MemoryAllocator)
allocator, _ = ctx.Value(expctxkeys.MemoryAllocatorKey{}).(experimental.MemoryAllocator)
}

m.buildGlobals(module, m.Engine.FunctionInstanceReference)
Expand Down
6 changes: 3 additions & 3 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/tetratelabs/wazero/api"
experimentalapi "github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ctxkey"
"github.com/tetratelabs/wazero/internal/expctxkeys"
internalsock "github.com/tetratelabs/wazero/internal/sock"
internalsys "github.com/tetratelabs/wazero/internal/sys"
"github.com/tetratelabs/wazero/internal/wasm"
Expand Down Expand Up @@ -242,7 +242,7 @@ func (r *runtime) CompileModule(ctx context.Context, binary []byte) (CompiledMod

func buildFunctionListeners(ctx context.Context, internal *wasm.Module) ([]experimentalapi.FunctionListener, error) {
// Test to see if internal code are using an experimental feature.
fnlf := ctx.Value(ctxkey.FunctionListenerFactoryKey{})
fnlf := ctx.Value(expctxkeys.FunctionListenerFactoryKey{})
if fnlf == nil {
return nil, nil
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (r *runtime) InstantiateModule(
return
}

if closeNotifier, ok := ctx.Value(ctxkey.CloseNotifierKey{}).(ctxkey.Notifier); ok {
if closeNotifier, ok := ctx.Value(expctxkeys.CloseNotifierKey{}).(experimentalapi.CloseNotifier); ok {
mod.(*wasm.ModuleInstance).CloseNotifier = closeNotifier
}

Expand Down

0 comments on commit a3d0d96

Please sign in to comment.