Skip to content

Commit

Permalink
Pass context into NewRuntime (#748)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
  • Loading branch information
mathetake committed Aug 18, 2022
1 parent 7d071a4 commit 076d324
Show file tree
Hide file tree
Showing 41 changed files with 73 additions and 73 deletions.
4 changes: 2 additions & 2 deletions assemblyscript/assemblyscript_example_test.go
Expand Up @@ -13,7 +13,7 @@ import (
func Example_instantiate() {
ctx := context.Background()

r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// This adds the "env" module to the runtime, with AssemblyScript's special
Expand All @@ -30,7 +30,7 @@ func Example_instantiate() {
func Example_functionExporter() {
ctx := context.Background()

r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// First construct your own module builder for "env"
Expand Down
2 changes: 1 addition & 1 deletion assemblyscript/assemblyscript_test.go
Expand Up @@ -422,7 +422,7 @@ func requireProxyModule(t *testing.T, fns FunctionExporter, config wazero.Module
// Set context to one that has an experimental listener
ctx := context.WithValue(testCtx, FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&log))

r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())

builder := r.NewModuleBuilder("env")
fns.ExportFunctions(builder)
Expand Down
4 changes: 2 additions & 2 deletions builder.go
Expand Up @@ -15,7 +15,7 @@ import (
// Ex. Below defines and instantiates a module named "env" with one function:
//
// ctx := context.Background()
// r := wazero.NewRuntime()
// r := wazero.NewRuntime(ctx)
// defer r.Close(ctx) // This closes everything this Runtime created.
//
// hello := func() {
Expand Down Expand Up @@ -198,7 +198,7 @@ type ModuleBuilder interface {
// Ex.
//
// ctx := context.Background()
// r := wazero.NewRuntime()
// r := wazero.NewRuntime(ctx)
// defer r.Close(ctx) // This closes everything this Runtime created.
//
// hello := func() {
Expand Down
8 changes: 4 additions & 4 deletions builder_test.go
Expand Up @@ -360,7 +360,7 @@ func TestNewModuleBuilder_Compile(t *testing.T) {
tc := tt

t.Run(tc.name, func(t *testing.T) {
b := tc.input(NewRuntime()).(*moduleBuilder)
b := tc.input(NewRuntime(testCtx)).(*moduleBuilder)
compiled, err := b.Compile(testCtx, NewCompileConfig())
require.NoError(t, err)
m := compiled.(*compiledModule)
Expand Down Expand Up @@ -412,15 +412,15 @@ func TestNewModuleBuilder_Compile_Errors(t *testing.T) {
tc := tt

t.Run(tc.name, func(t *testing.T) {
_, e := tc.input(NewRuntime()).Compile(testCtx, tc.config)
_, e := tc.input(NewRuntime(testCtx)).Compile(testCtx, tc.config)
require.EqualError(t, e, tc.expectedErr)
})
}
}

// TestNewModuleBuilder_Instantiate ensures Runtime.InstantiateModule is called on success.
func TestNewModuleBuilder_Instantiate(t *testing.T) {
r := NewRuntime()
r := NewRuntime(testCtx)
m, err := r.NewModuleBuilder("env").Instantiate(testCtx, r)
require.NoError(t, err)

Expand All @@ -434,7 +434,7 @@ func TestNewModuleBuilder_Instantiate(t *testing.T) {

// TestNewModuleBuilder_Instantiate_Errors ensures errors propagate from Runtime.InstantiateModule
func TestNewModuleBuilder_Instantiate_Errors(t *testing.T) {
r := NewRuntime()
r := NewRuntime(testCtx)
_, err := r.NewModuleBuilder("env").Instantiate(testCtx, r)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion config.go
Expand Up @@ -151,7 +151,7 @@ func NewRuntimeConfig() RuntimeConfig {
type runtimeConfig struct {
enabledFeatures wasm.Features
isInterpreter bool
newEngine func(wasm.Features) wasm.Engine
newEngine func(context.Context, wasm.Features) wasm.Engine
}

// engineLessConfig helps avoid copy/pasting the wrong defaults.
Expand Down
4 changes: 2 additions & 2 deletions emscripten/emscripten_example_test.go
Expand Up @@ -14,7 +14,7 @@ import (
func Example_instantiate() {
ctx := context.Background()

r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Add WASI which is typically required when using Emscripten.
Expand All @@ -35,7 +35,7 @@ func Example_instantiate() {
func Example_functionExporter() {
ctx := context.Background()

r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Add WASI which is typically required when using Emscripten.
Expand Down
2 changes: 1 addition & 1 deletion emscripten/emscripten_test.go
Expand Up @@ -29,7 +29,7 @@ func TestGrow(t *testing.T) {
// Set context to one that has an experimental listener
ctx := context.WithValue(testCtx, FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(&log))

r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
defer r.Close(ctx)

_, err := wasi_snapshot_preview1.Instantiate(ctx, r)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Expand Up @@ -22,7 +22,7 @@ func Example() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := NewRuntime()
r := NewRuntime(ctx)

// Add a module to the runtime named "wasm/math" which exports one function
// "add", implemented in WebAssembly.
Expand Down
2 changes: 1 addition & 1 deletion examples/allocation/rust/greet.go
Expand Up @@ -25,7 +25,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Instantiate a Go-defined module named "env" that exports a function to
Expand Down
2 changes: 1 addition & 1 deletion examples/allocation/tinygo/greet.go
Expand Up @@ -26,7 +26,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
// Enable WebAssembly 2.0 support, which is required for TinyGo 0.24+.
WithWasmCore2())
defer r.Close(ctx) // This closes everything this Runtime created.
Expand Down
2 changes: 1 addition & 1 deletion examples/allocation/zig/greet.go
Expand Up @@ -31,7 +31,7 @@ func run() error {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
// Enable WebAssembly 2.0 support.
WithWasmCore2(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/assemblyscript/assemblyscript.go
Expand Up @@ -28,7 +28,7 @@ func main() {

// Create a new WebAssembly Runtime.
// Use WebAssembly 2.0 because AssemblyScript uses some >1.0 features.
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
WithWasmCore2())
defer r.Close(ctx) // This closes everything this Runtime created.

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/add.go
Expand Up @@ -24,7 +24,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Add a module to the runtime named "wasm/math" which exports one function "add", implemented in WebAssembly.
Expand Down
2 changes: 1 addition & 1 deletion examples/import-go/age-calculator.go
Expand Up @@ -28,7 +28,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Instantiate a Go-defined module named "env" that exports functions to
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-results/multiple-results.go
Expand Up @@ -34,7 +34,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Add a module that uses offset parameters for multiple results defined in WebAssembly.
Expand All @@ -45,7 +45,7 @@ func main() {

// wazero enables WebAssembly 1.0 by default. Opt-in to other features:
runtimeWithMultiValue := wazero.NewRuntimeWithConfig(
wazero.NewRuntimeConfig().WithFeatureMultiValue(true),
ctx, wazero.NewRuntimeConfig().WithFeatureMultiValue(true),
// ^^ Note: WebAssembly 2.0 (WithWasmCore2) includes "multi-value".
)

Expand Down
2 changes: 1 addition & 1 deletion examples/namespace/counter.go
Expand Up @@ -25,7 +25,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

// Compile WebAssembly that requires its own "env" module.
Expand Down
2 changes: 1 addition & 1 deletion examples/wasi/cat.go
Expand Up @@ -43,7 +43,7 @@ func main() {
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
// Enable WebAssembly 2.0 support, which is required for TinyGo 0.24+.
WithWasmCore2())
defer r.Close(ctx) // This closes everything this Runtime created.
Expand Down
2 changes: 1 addition & 1 deletion experimental/fs_example_test.go
Expand Up @@ -24,7 +24,7 @@ var fsWasm []byte
func Example_withFS() {
ctx := context.Background()

r := wazero.NewRuntime()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.

if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion experimental/listener_example_test.go
Expand Up @@ -58,7 +58,7 @@ func Example_customListenerFactory() {
// Set context to one that has an experimental listener
ctx := context.WithValue(context.Background(), FunctionListenerFactoryKey{}, u)

r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
defer r.Close(ctx) // This closes everything this Runtime created.

if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions experimental/listener_test.go
Expand Up @@ -51,15 +51,15 @@ func TestFunctionListenerFactory(t *testing.T) {

if platform.CompilerSupported() {
t.Run("fails on compile if compiler", func(t *testing.T) {
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigCompiler())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler())
defer r.Close(testCtx) // This closes everything this Runtime created.
_, err := r.CompileModule(ctx, bin, wazero.NewCompileConfig())
require.EqualError(t, err,
"context includes a FunctionListenerFactoryKey, which is only supported in the interpreter")
})
}

r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
defer r.Close(ctx) // This closes everything this Runtime created.

_, err := r.CompileModule(ctx, bin, wazero.NewCompileConfig())
Expand Down
2 changes: 1 addition & 1 deletion experimental/logging/log_listener_example_test.go
Expand Up @@ -24,7 +24,7 @@ func Example_newLoggingListenerFactory() {
// Set context to one that has an experimental listener
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(os.Stdout))

r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
defer r.Close(ctx) // This closes everything this Runtime created.

if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/compiler/engine.go
Expand Up @@ -590,11 +590,11 @@ func (e *moduleEngine) Call(ctx context.Context, callCtx *wasm.CallContext, f *w
return
}

func NewEngine(enabledFeatures wasm.Features) wasm.Engine {
return newEngine(enabledFeatures)
func NewEngine(ctx context.Context, enabledFeatures wasm.Features) wasm.Engine {
return newEngine(ctx, enabledFeatures)
}

func newEngine(enabledFeatures wasm.Features) *engine {
func newEngine(_ context.Context, enabledFeatures wasm.Features) *engine {
return &engine{
enabledFeatures: enabledFeatures,
codes: map[wasm.ModuleID][]*code{},
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/compiler/engine_test.go
Expand Up @@ -35,7 +35,7 @@ func (e *engineTester) ListenerFactory() experimental.FunctionListenerFactory {

// NewEngine implements the same method as documented on enginetest.EngineTester.
func (e *engineTester) NewEngine(enabledFeatures wasm.Features) wasm.Engine {
return newEngine(enabledFeatures)
return newEngine(context.Background(), enabledFeatures)
}

// InitTables implements the same method as documented on enginetest.EngineTester.
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestCompiler_Releasecode_Panic(t *testing.T) {
// See comments on initialValueStackSize and initialCallFrameStackSize.
func TestCompiler_SliceAllocatedOnHeap(t *testing.T) {
enabledFeatures := wasm.Features20191205
e := newEngine(enabledFeatures)
e := newEngine(context.Background(), enabledFeatures)
s, ns := wasm.NewStore(enabledFeatures, e)

const hostModuleName = "env"
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestCompiler_SliceAllocatedOnHeap(t *testing.T) {

// TODO: move most of this logic to enginetest.go so that there is less drift between interpreter and compiler
func TestEngine_Cachedcodes(t *testing.T) {
e := newEngine(wasm.Features20191205)
e := newEngine(context.Background(), wasm.Features20191205)
exp := []*code{
{codeSegment: []byte{0x0}},
{codeSegment: []byte{0x0}},
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/interpreter/interpreter.go
Expand Up @@ -29,7 +29,7 @@ type engine struct {
mux sync.RWMutex
}

func NewEngine(enabledFeatures wasm.Features) wasm.Engine {
func NewEngine(_ context.Context, enabledFeatures wasm.Features) wasm.Engine {
return &engine{
enabledFeatures: enabledFeatures,
codes: map[wasm.ModuleID][]*code{},
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/interpreter/interpreter_test.go
Expand Up @@ -83,7 +83,7 @@ func (e engineTester) ListenerFactory() experimental.FunctionListenerFactory {

// NewEngine implements enginetest.EngineTester NewEngine.
func (e engineTester) NewEngine(enabledFeatures wasm.Features) wasm.Engine {
return NewEngine(enabledFeatures)
return NewEngine(context.Background(), enabledFeatures)
}

// InitTables implements enginetest.EngineTester InitTables.
Expand Down
2 changes: 1 addition & 1 deletion internal/integration_test/bench/bench_test.go
Expand Up @@ -180,7 +180,7 @@ func createRuntime(b *testing.B, config wazero.RuntimeConfig) wazero.Runtime {
m.Memory().Write(ctx, offset, b)
}

r := wazero.NewRuntimeWithConfig(config)
r := wazero.NewRuntimeWithConfig(testCtx, config)

_, err := r.NewModuleBuilder("env").
ExportFunction("get_random_string", getRandomString).
Expand Down
2 changes: 1 addition & 1 deletion internal/integration_test/bench/codec_test.go
Expand Up @@ -94,7 +94,7 @@ func TestExampleUpToDate(t *testing.T) {
})

t.Run("Executable", func(t *testing.T) {
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().WithWasmCore2())
r := wazero.NewRuntimeWithConfig(testCtx, wazero.NewRuntimeConfig().WithWasmCore2())

// Add WASI to satisfy import tests
wm, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
Expand Down
2 changes: 1 addition & 1 deletion internal/integration_test/engine/adhoc_test.go
Expand Up @@ -67,7 +67,7 @@ func runAllTests(t *testing.T, tests map[string]func(t *testing.T, r wazero.Runt
testf := testf // pin
t.Run(name, func(t *testing.T) {
t.Parallel()
testf(t, wazero.NewRuntimeWithConfig(config))
testf(t, wazero.NewRuntimeWithConfig(testCtx, config))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/integration_test/fs/fs_test.go
Expand Up @@ -149,7 +149,7 @@ func (f *wasiFile) Seek(offset int64, whence int) (int64, error) {
}

func TestReader(t *testing.T) {
r := wazero.NewRuntime()
r := wazero.NewRuntime(testCtx)
defer r.Close(testCtx)

_, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
Expand Down
4 changes: 2 additions & 2 deletions internal/integration_test/fuzzcases/fuzzcases_test.go
Expand Up @@ -28,15 +28,15 @@ func runWithCompiler(t *testing.T, runner func(t *testing.T, r wazero.Runtime))
return
}
t.Run("compiler", func(t *testing.T) {
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigCompiler().WithWasmCore2())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler().WithWasmCore2())
defer r.Close(ctx)
runner(t, r)
})
}

func runWithInterpreter(t *testing.T, runner func(t *testing.T, r wazero.Runtime)) {
t.Run("interpreter", func(t *testing.T) {
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfigInterpreter().WithWasmCore2())
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter().WithWasmCore2())
defer r.Close(ctx)
runner(t, r)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/integration_test/spectest/spectest.go
Expand Up @@ -417,7 +417,7 @@ func maybeSetMemoryCap(mod *wasm.Module) {

// Run runs all the test inside the testDataFS file system where all the cases are described
// via JSON files created from wast2json.
func Run(t *testing.T, testDataFS embed.FS, newEngine func(wasm.Features) wasm.Engine, enabledFeatures wasm.Features) {
func Run(t *testing.T, testDataFS embed.FS, newEngine func(context.Context, wasm.Features) wasm.Engine, enabledFeatures wasm.Features) {
files, err := testDataFS.ReadDir("testdata")
require.NoError(t, err)

Expand All @@ -443,7 +443,7 @@ func Run(t *testing.T, testDataFS embed.FS, newEngine func(wasm.Features) wasm.E
wastName := basename(base.SourceFile)

t.Run(wastName, func(t *testing.T) {
s, ns := wasm.NewStore(enabledFeatures, newEngine(enabledFeatures))
s, ns := wasm.NewStore(enabledFeatures, newEngine(testCtx, enabledFeatures))
addSpectestModule(t, s, ns)

var lastInstantiatedModuleName string
Expand Down

0 comments on commit 076d324

Please sign in to comment.