Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ModuleConfig.Validate() to allow pre-flight checks on source #281

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ type ModuleConfig struct {
Source []byte
}

// Validate errs if the source is invalid. This is used to pre-flight check the Source when instantiation is deferred.
func (m *ModuleConfig) Validate() error {
_, _, err := decodeModule(m)
return err
}

// InstantiateModule instantiates the module namespace or errs if the configuration was invalid.
//
// Ex.
Expand Down
12 changes: 10 additions & 2 deletions wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func TestDecodeModule(t *testing.T) {
tc := tt

t.Run(tc.name, func(t *testing.T) {
_, name, err := decodeModule(&ModuleConfig{Name: tc.moduleName, Source: tc.source})
config := &ModuleConfig{Name: tc.moduleName, Source: tc.source}
_, name, err := decodeModule(config)
require.NoError(t, err)
if tc.moduleName == "" {
require.Equal(t, "test" /* from the text format */, name)
} else {
require.Equal(t, tc.moduleName, name)
}

// Avoid adding another test just to check Validate works
require.NoError(t, config.Validate())
})
}
}
Expand Down Expand Up @@ -71,8 +75,12 @@ func TestDecodeModule_Errors(t *testing.T) {
tc := tt

t.Run(tc.name, func(t *testing.T) {
_, _, err := decodeModule(&ModuleConfig{Source: tc.source})
config := &ModuleConfig{Source: tc.source}
_, _, err := decodeModule(config)
require.EqualError(t, err, tc.expectedErr)

// Avoid adding another test just to check Validate works
require.EqualError(t, config.Validate(), tc.expectedErr)
})
}
}
Expand Down