Simplifies API per feedback (#427)

During #425, @neilalexander gave constructive feedback that the API is
both moving fast, and not good enough yet. This attempts to reduce the
incidental complexity at the cost of a little conflation.

### odd presence of `wasm` and `wasi` packages -> `api` package

We had public API packages in wasm and wasi, which helped us avoid
leaking too many internals as public. That these had names that look
like there should be implementations in them cause unnecessary
confusion. This squashes both into one package "api" which has no
package collission with anything.

We've long struggled with the poorly specified and non-uniformly
implemented WASI specification. Trying to bring visibility to its
constraints knowing they are routinely invalid taints our API for no
good reason. This removes all `WASI` commands for a default to invoke
the function `_start` if it exists. In doing so, there's only one path
to start a module.

Moreover, this puts all wasi code in a top-level package "wasi" as it
isn't re-imported by any internal types.

### Reuse of Module for pre and post instantiation to `Binary` -> `Module`

Module is defined by WebAssembly in many phases, from decoded to
instantiated. However, using the same noun in multiple packages is very
confusing. We at one point tried a name "DecodedModule" or
"InstantiatedModule", but this is a fools errand. By deviating slightly
from the spec we can make it unambiguous what a module is.

This make a result of compilation a `Binary`, retaining `Module` for an
instantiated one. In doing so, there's no longer any name conflicts
whatsoever.

### Confusion about config -> `ModuleConfig`

Also caused by splitting wasm into wasm+wasi is configuration. This
conflates both into the same type `ModuleConfig` as it is simpler than
trying to explain a "will never be finished" api of wasi snapshot-01 in
routine use of WebAssembly. In other words, this further moves WASI out
of the foreground as it has been nothing but burden.

```diff
--- a/README.md
+++ b/README.md
@@ -49,8 +49,8 @@ For example, here's how you can allow WebAssembly modules to read
-wm, err := r.InstantiateModule(wazero.WASISnapshotPreview1())
-defer wm.Close()
+wm, err := wasi.InstantiateSnapshotPreview1(r)
+defer wm.Close()

-sysConfig := wazero.NewSysConfig().WithFS(os.DirFS("/work/home"))
-module, err := wazero.StartWASICommandWithConfig(r, compiled, sysConfig)
+config := wazero.ModuleConfig().WithFS(os.DirFS("/work/home"))
+module, err := r.InstantiateModule(binary, config)
 defer module.Close()
 ...
```
This commit is contained in:
Crypt Keeper
2022-04-02 06:42:36 +08:00
committed by GitHub
parent 3c3df6b836
commit 2664b1eb62
127 changed files with 3153 additions and 3230 deletions

View File

@@ -6,13 +6,13 @@ import (
"github.com/stretchr/testify/require"
internalwasm "github.com/tetratelabs/wazero/internal/wasm"
"github.com/tetratelabs/wazero/wasm"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)
// TestNewModuleBuilder_Build only covers a few scenarios to avoid duplicating tests in internal/wasm/host_test.go
func TestNewModuleBuilder_Build(t *testing.T) {
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
i32, i64 := api.ValueTypeI32, api.ValueTypeI64
uint32_uint32 := func(uint32) uint32 {
return 0
@@ -26,38 +26,38 @@ func TestNewModuleBuilder_Build(t *testing.T) {
tests := []struct {
name string
input func(Runtime) ModuleBuilder
expected *internalwasm.Module
expected *wasm.Module
}{
{
name: "empty",
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("")
},
expected: &internalwasm.Module{},
expected: &wasm.Module{},
},
{
name: "only name",
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("env")
},
expected: &internalwasm.Module{NameSection: &internalwasm.NameSection{ModuleName: "env"}},
expected: &wasm.Module{NameSection: &wasm.NameSection{ModuleName: "env"}},
},
{
name: "ExportFunction",
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("").ExportFunction("1", uint32_uint32)
},
expected: &internalwasm.Module{
TypeSection: []*internalwasm.FunctionType{
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
},
FunctionSection: []internalwasm.Index{0},
FunctionSection: []wasm.Index{0},
HostFunctionSection: []*reflect.Value{&fnUint32_uint32},
ExportSection: map[string]*internalwasm.Export{
"1": {Name: "1", Type: internalwasm.ExternTypeFunc, Index: 0},
ExportSection: map[string]*wasm.Export{
"1": {Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
},
NameSection: &internalwasm.NameSection{
FunctionNames: internalwasm.NameMap{{Index: 0, Name: "1"}},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "1"}},
},
},
},
@@ -66,17 +66,17 @@ func TestNewModuleBuilder_Build(t *testing.T) {
input: func(r Runtime) ModuleBuilder {
return r.NewModuleBuilder("").ExportFunction("1", uint32_uint32).ExportFunction("1", uint64_uint32)
},
expected: &internalwasm.Module{
TypeSection: []*internalwasm.FunctionType{
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
},
FunctionSection: []internalwasm.Index{0},
FunctionSection: []wasm.Index{0},
HostFunctionSection: []*reflect.Value{&fnUint64_uint32},
ExportSection: map[string]*internalwasm.Export{
"1": {Name: "1", Type: internalwasm.ExternTypeFunc, Index: 0},
ExportSection: map[string]*wasm.Export{
"1": {Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
},
NameSection: &internalwasm.NameSection{
FunctionNames: internalwasm.NameMap{{Index: 0, Name: "1"}},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "1"}},
},
},
},
@@ -86,19 +86,19 @@ func TestNewModuleBuilder_Build(t *testing.T) {
// Intentionally out of order
return r.NewModuleBuilder("").ExportFunction("2", uint64_uint32).ExportFunction("1", uint32_uint32)
},
expected: &internalwasm.Module{
TypeSection: []*internalwasm.FunctionType{
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
},
FunctionSection: []internalwasm.Index{0, 1},
FunctionSection: []wasm.Index{0, 1},
HostFunctionSection: []*reflect.Value{&fnUint32_uint32, &fnUint64_uint32},
ExportSection: map[string]*internalwasm.Export{
"1": {Name: "1", Type: internalwasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: internalwasm.ExternTypeFunc, Index: 1},
ExportSection: map[string]*wasm.Export{
"1": {Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: wasm.ExternTypeFunc, Index: 1},
},
NameSection: &internalwasm.NameSection{
FunctionNames: internalwasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
},
},
},
@@ -110,19 +110,19 @@ func TestNewModuleBuilder_Build(t *testing.T) {
"2": uint64_uint32,
})
},
expected: &internalwasm.Module{
TypeSection: []*internalwasm.FunctionType{
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
},
FunctionSection: []internalwasm.Index{0, 1},
FunctionSection: []wasm.Index{0, 1},
HostFunctionSection: []*reflect.Value{&fnUint32_uint32, &fnUint64_uint32},
ExportSection: map[string]*internalwasm.Export{
"1": {Name: "1", Type: internalwasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: internalwasm.ExternTypeFunc, Index: 1},
ExportSection: map[string]*wasm.Export{
"1": {Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: wasm.ExternTypeFunc, Index: 1},
},
NameSection: &internalwasm.NameSection{
FunctionNames: internalwasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
},
},
},
@@ -135,19 +135,19 @@ func TestNewModuleBuilder_Build(t *testing.T) {
"2": uint64_uint32,
})
},
expected: &internalwasm.Module{
TypeSection: []*internalwasm.FunctionType{
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
},
FunctionSection: []internalwasm.Index{0, 1},
FunctionSection: []wasm.Index{0, 1},
HostFunctionSection: []*reflect.Value{&fnUint32_uint32, &fnUint64_uint32},
ExportSection: map[string]*internalwasm.Export{
"1": {Name: "1", Type: internalwasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: internalwasm.ExternTypeFunc, Index: 1},
ExportSection: map[string]*wasm.Export{
"1": {Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
"2": {Name: "2", Type: wasm.ExternTypeFunc, Index: 1},
},
NameSection: &internalwasm.NameSection{
FunctionNames: internalwasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "1"}, {Index: 1, Name: "2"}},
},
},
},
@@ -185,7 +185,7 @@ func TestNewModuleBuilder_InstantiateModule_Errors(t *testing.T) {
}
// requireHostModuleEquals is redefined from internal/wasm/host_test.go to avoid an import cycle extracting it.
func requireHostModuleEquals(t *testing.T, expected, actual *internalwasm.Module) {
func requireHostModuleEquals(t *testing.T, expected, actual *wasm.Module) {
// `require.Equal(t, expected, actual)` fails reflect pointers don't match, so brute compare:
require.Equal(t, expected.TypeSection, actual.TypeSection)
require.Equal(t, expected.ImportSection, actual.ImportSection)