Makes ModuleConfig an interface and fixes mutability bug (#520)
This makes wazero.ModuleConfig an interface instead of a struct to prevent it from being used incorrectly. For example, even though the fields are not exported, someone can mistakenly instantiate this when it is a struct, and in doing so violate internal assumptions. This also fixes a mutability bug in the implementation. Follow-up from #519 and the last in this series Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
23
wasm.go
23
wasm.go
@@ -76,7 +76,7 @@ type Runtime interface {
|
||||
// defer wasm.Close()
|
||||
//
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
InstantiateModuleFromCodeWithConfig(ctx context.Context, source []byte, config *ModuleConfig) (api.Module, error)
|
||||
InstantiateModuleFromCodeWithConfig(ctx context.Context, source []byte, config ModuleConfig) (api.Module, error)
|
||||
|
||||
// InstantiateModule instantiates the module namespace or errs if the configuration was invalid.
|
||||
//
|
||||
@@ -114,7 +114,7 @@ type Runtime interface {
|
||||
//
|
||||
// Note: When the context is nil, it defaults to context.Background.
|
||||
// Note: Config is copied during instantiation: Later changes to config do not affect the instantiated result.
|
||||
InstantiateModuleWithConfig(ctx context.Context, compiled CompiledCode, config *ModuleConfig) (mod api.Module, err error)
|
||||
InstantiateModuleWithConfig(ctx context.Context, compiled CompiledCode, config ModuleConfig) (api.Module, error)
|
||||
}
|
||||
|
||||
func NewRuntime() Runtime {
|
||||
@@ -217,7 +217,7 @@ func (r *runtime) InstantiateModuleFromCode(ctx context.Context, source []byte)
|
||||
}
|
||||
|
||||
// InstantiateModuleFromCodeWithConfig implements Runtime.InstantiateModuleFromCodeWithConfig
|
||||
func (r *runtime) InstantiateModuleFromCodeWithConfig(ctx context.Context, source []byte, config *ModuleConfig) (api.Module, error) {
|
||||
func (r *runtime) InstantiateModuleFromCodeWithConfig(ctx context.Context, source []byte, config ModuleConfig) (api.Module, error) {
|
||||
if compiled, err := r.CompileModule(ctx, source); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
@@ -233,17 +233,22 @@ func (r *runtime) InstantiateModule(ctx context.Context, compiled CompiledCode)
|
||||
}
|
||||
|
||||
// InstantiateModuleWithConfig implements Runtime.InstantiateModuleWithConfig
|
||||
func (r *runtime) InstantiateModuleWithConfig(ctx context.Context, compiled CompiledCode, config *ModuleConfig) (mod api.Module, err error) {
|
||||
var sysCtx *wasm.SysContext
|
||||
if sysCtx, err = config.toSysContext(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
func (r *runtime) InstantiateModuleWithConfig(ctx context.Context, compiled CompiledCode, mConfig ModuleConfig) (mod api.Module, err error) {
|
||||
code, ok := compiled.(*compiledCode)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("unsupported wazero.CompiledCode implementation: %#v", compiled))
|
||||
}
|
||||
|
||||
config, ok := mConfig.(*moduleConfig)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("unsupported wazero.ModuleConfig implementation: %#v", mConfig))
|
||||
}
|
||||
|
||||
var sysCtx *wasm.SysContext
|
||||
if sysCtx, err = config.toSysContext(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
name := config.name
|
||||
if name == "" && code.module.NameSection != nil && code.module.NameSection.ModuleName != "" {
|
||||
name = code.module.NameSection.ModuleName
|
||||
|
||||
Reference in New Issue
Block a user