Skip to content

Commit

Permalink
#52 Provide access to wazero ModuleConfig for WASI support (#53)
Browse files Browse the repository at this point in the history
* #52 Provide access to wazero ModuleConfig for WASI support

* Update engines/wazero/wazero.go

Co-authored-by: Crypt Keeper <[email protected]>

* updating golangci-lint version to 1.52.2

---------

Co-authored-by: Crypt Keeper <[email protected]>
Co-authored-by: Phil Kedy <[email protected]>
  • Loading branch information
3 people authored Apr 18, 2023
1 parent aa02334 commit 70a4115
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.1
version: v1.52.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
8 changes: 8 additions & 0 deletions engines/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ func (m *Module) UnwrapRuntime() *wazero.Runtime {
return &m.runtime
}

// WithConfig allows you to override or replace wazero.ModuleConfig used to instantiate each guest.
// This can be used to configure clocks or filesystem access.
//
// The default (function input) conflgures WASI and waPC init functions as well as stdout and stderr.
func (m *Module) WithConfig(callback func(wazero.ModuleConfig) wazero.ModuleConfig) {
m.config = callback(m.config)
}

// wapcHost implements all required waPC host function exports.
//
// See https://wapc.io/docs/spec/#required-host-exports
Expand Down
28 changes: 28 additions & 0 deletions engines/wazero/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ func TestModule_UnwrapRuntime(t *testing.T) {
}
}

// TestModule_WithConfig ensures the module config can be extended
func TestModule_WithConfig(t *testing.T) {
m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
if err != nil {
t.Errorf("Error creating module - %v", err)
}
defer m.Close(testCtx)

var mock = &mockModuleConfig{}
m.(*Module).config = mock
m.(*Module).WithConfig(func(config wazero.ModuleConfig) wazero.ModuleConfig {
return config.WithSysWalltime()
})
if !mock.calledWithSysWalltime {
t.Errorf(`Expected call to WithSysWalltime`)
}
}

type mockModuleConfig struct {
wazero.ModuleConfig
calledWithSysWalltime bool
}

func (m *mockModuleConfig) WithSysWalltime() wazero.ModuleConfig {
m.calledWithSysWalltime = true
return m
}

// TestInstance_UnwrapModule ensures the Unwrap returns the correct api.Module interface
func TestInstance_UnwrapModule(t *testing.T) {
m, err := EngineWithRuntime(DefaultRuntime).New(testCtx, wapc.NoOpHostCallHandler, guest, mc)
Expand Down

0 comments on commit 70a4115

Please sign in to comment.