Extracts CompileConfig and consolidates code. (#533)

This performs several changes to allow compilation config to be
centralized and scoped properly. The immediate effects are that we can
now process external types during `Runtime.CompileModule` instead of
doing so later during `Runtime.InstantiateModule`. Another nice side
effect is memory size problems can err at a source line instead of
having to be handled in several places.

There are some API effects to this, and to pay for them, some less used
APIs were removed. The "easy APIs" are left alone. For example, the APIs
to compile and instantiate a module from Go or Wasm in one step are left
alone.

Here are the changes, some of which are only for consistency. Rationale
is summarized in each point.
* ModuleBuilder.Build -> ModuleBuilder.Compile
  * The result of this is similar to `CompileModule`, and pairs better
    with `ModuleBuilder.Instantiate` which is like `InstantiateModule`.
* CompiledCode -> CompiledModule
  * We punted on this name, the result is more than just code. This is
    better I think and more consistent as it introduces less terms.
* Adds CompileConfig param to Runtime.CompileModule.
  * This holds existing features and will have future ones, such as
    mapping externtypes to uint64 for wasm that doesn't yet support it.
* Merges Runtime.InstantiateModuleWithConfig with Runtime.InstantiateModule
  * This allows us to explain APIs in terms of implicit or explicit
    compilation and config, vs implicit, kindof implicit, and explicit.
* Removes Runtime.InstantiateModuleFromCodeWithConfig
  * Similar to above, this API only saves the compilation step and also
    difficult to reason with from a name POV.
* RuntimeConfig.WithMemory(CapacityPages|LimitPages) -> CompileConfig.WithMemorySizer
  * This allows all error handling to be attached to the source line
  * This also allows someone to reduce unbounded memory while knowing
    what its minimum is.
* ModuleConfig.With(Import|ImportModule) -> CompileConfig.WithImportRenamer
  * This allows more types of import manipulation, also without
    conflating functions with globals.
* Adds api.ExternType
  * Needed for ImportRenamer and will be needed later for ExportRenamer.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-05-09 11:02:32 +08:00
committed by GitHub
parent 0561190cb9
commit 8f8c9ee205
47 changed files with 784 additions and 1104 deletions

View File

@@ -344,7 +344,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
b := tc.input(NewRuntime()).(*moduleBuilder)
compiled, err := b.Build(testCtx)
compiled, err := b.Compile(testCtx, NewCompileConfig())
require.NoError(t, err)
m := compiled.(*compiledCode)
@@ -353,7 +353,7 @@ func TestNewModuleBuilder_Build(t *testing.T) {
require.Equal(t, b.r.store.Engine, m.compiledEngine)
// Built module must be instantiable by Engine.
_, err = b.r.InstantiateModule(testCtx, m)
_, err = b.r.InstantiateModule(testCtx, m, NewModuleConfig())
require.NoError(t, err)
})
}
@@ -363,25 +363,26 @@ func TestNewModuleBuilder_Build(t *testing.T) {
func TestNewModuleBuilder_Build_Errors(t *testing.T) {
tests := []struct {
name string
input func(RuntimeConfig) ModuleBuilder
input func(Runtime) ModuleBuilder
config CompileConfig
expectedErr string
}{
{
name: "memory min > limit", // only one test to avoid duplicating tests in module_test.go
input: func(cfg RuntimeConfig) ModuleBuilder {
return NewRuntimeWithConfig(cfg).NewModuleBuilder("").
ExportMemory("memory", math.MaxUint32)
input: func(rt Runtime) ModuleBuilder {
return rt.NewModuleBuilder("").ExportMemory("memory", math.MaxUint32)
},
config: NewCompileConfig(),
expectedErr: "memory[memory] min 4294967295 pages (3 Ti) over limit of 65536 pages (4 Gi)",
},
{
name: "memory cap < min", // only one test to avoid duplicating tests in module_test.go
input: func(cfg RuntimeConfig) ModuleBuilder {
cfg = cfg.WithMemoryCapacityPages(func(minPages uint32, maxPages *uint32) uint32 {
return 1
})
return NewRuntimeWithConfig(cfg).NewModuleBuilder("").ExportMemory("memory", 2)
input: func(rt Runtime) ModuleBuilder {
return rt.NewModuleBuilder("").ExportMemory("memory", 2)
},
config: NewCompileConfig().WithMemorySizer(func(minPages uint32, maxPages *uint32) (min, capacity, max uint32) {
return 2, 1, 2
}),
expectedErr: "memory[memory] capacity 1 pages (64 Ki) less than minimum 2 pages (128 Ki)",
},
}
@@ -390,7 +391,7 @@ func TestNewModuleBuilder_Build_Errors(t *testing.T) {
tc := tt
t.Run(tc.name, func(t *testing.T) {
_, e := tc.input(NewRuntimeConfig()).Build(testCtx)
_, e := tc.input(NewRuntime()).Compile(testCtx, tc.config)
require.EqualError(t, e, tc.expectedErr)
})
}