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() ... ```
219 lines
7.4 KiB
Go
219 lines
7.4 KiB
Go
package binary
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
func TestModule_Encode(t *testing.T) {
|
|
i32, f32 := wasm.ValueTypeI32, wasm.ValueTypeF32
|
|
zero := uint32(0)
|
|
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.Module
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "empty",
|
|
input: &wasm.Module{},
|
|
expected: append(Magic, version...),
|
|
},
|
|
{
|
|
name: "only name section",
|
|
input: &wasm.Module{NameSection: &wasm.NameSection{ModuleName: "simple"}},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDCustom, 0x0e, // 14 bytes in this section
|
|
0x04, 'n', 'a', 'm', 'e',
|
|
subsectionIDModuleName, 0x07, // 7 bytes in this subsection
|
|
0x06, // the Module name simple is 6 bytes long
|
|
's', 'i', 'm', 'p', 'l', 'e'),
|
|
},
|
|
{
|
|
name: "type section",
|
|
input: &wasm.Module{
|
|
TypeSection: []*wasm.FunctionType{
|
|
{},
|
|
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
|
|
{Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}},
|
|
},
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDType, 0x12, // 18 bytes in this section
|
|
0x03, // 3 types
|
|
0x60, 0x00, 0x00, // func=0x60 no param no result
|
|
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
|
|
0x60, 0x04, i32, i32, i32, i32, 0x01, i32, // func=0x60 4 params and 1 result
|
|
),
|
|
},
|
|
{
|
|
name: "type and import section",
|
|
input: &wasm.Module{
|
|
TypeSection: []*wasm.FunctionType{
|
|
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
|
|
{Params: []wasm.ValueType{f32, f32}, Results: []wasm.ValueType{f32}},
|
|
},
|
|
ImportSection: []*wasm.Import{
|
|
{
|
|
Module: "Math", Name: "Mul",
|
|
Type: wasm.ExternTypeFunc,
|
|
DescFunc: 1,
|
|
}, {
|
|
Module: "Math", Name: "Add",
|
|
Type: wasm.ExternTypeFunc,
|
|
DescFunc: 0,
|
|
},
|
|
},
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDType, 0x0d, // 13 bytes in this section
|
|
0x02, // 2 types
|
|
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
|
|
0x60, 0x02, f32, f32, 0x01, f32, // func=0x60 2 params and 1 result
|
|
wasm.SectionIDImport, 0x17, // 23 bytes in this section
|
|
0x02, // 2 imports
|
|
0x04, 'M', 'a', 't', 'h', 0x03, 'M', 'u', 'l', wasm.ExternTypeFunc,
|
|
0x01, // type index
|
|
0x04, 'M', 'a', 't', 'h', 0x03, 'A', 'd', 'd', wasm.ExternTypeFunc,
|
|
0x00, // type index
|
|
),
|
|
},
|
|
{
|
|
name: "type function and start section",
|
|
input: &wasm.Module{
|
|
TypeSection: []*wasm.FunctionType{{}},
|
|
ImportSection: []*wasm.Import{{
|
|
Module: "", Name: "hello",
|
|
Type: wasm.ExternTypeFunc,
|
|
DescFunc: 0,
|
|
}},
|
|
StartSection: &zero,
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDType, 0x04, // 4 bytes in this section
|
|
0x01, // 1 type
|
|
0x60, 0x0, 0x0, // func=0x60 0 params and 0 result
|
|
wasm.SectionIDImport, 0x0a, // 10 bytes in this section
|
|
0x01, // 1 import
|
|
0x00, 0x05, 'h', 'e', 'l', 'l', 'o', wasm.ExternTypeFunc,
|
|
0x00, // type index
|
|
wasm.SectionIDStart, 0x01,
|
|
0x00, // start function index
|
|
),
|
|
},
|
|
{
|
|
name: "table and memory section",
|
|
input: &wasm.Module{
|
|
TableSection: &wasm.Table{Min: 3},
|
|
MemorySection: &wasm.Memory{Min: 1, Max: 1},
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDTable, 0x04, // 4 bytes in this section
|
|
0x01, // 1 table
|
|
wasm.ElemTypeFuncref, 0x0, 0x03, // func, only min: 3
|
|
wasm.SectionIDMemory, 0x04, // 4 bytes in this section
|
|
0x01, // 1 memory
|
|
0x01, 0x01, 0x01, // min and max = 1
|
|
),
|
|
},
|
|
{
|
|
name: "exported func with instructions",
|
|
input: &wasm.Module{
|
|
TypeSection: []*wasm.FunctionType{
|
|
{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
|
|
},
|
|
FunctionSection: []wasm.Index{0},
|
|
CodeSection: []*wasm.Code{
|
|
{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}},
|
|
},
|
|
ExportSection: map[string]*wasm.Export{
|
|
"AddInt": {Name: "AddInt", Type: wasm.ExternTypeFunc, Index: wasm.Index(0)},
|
|
},
|
|
NameSection: &wasm.NameSection{
|
|
FunctionNames: wasm.NameMap{{Index: wasm.Index(0), Name: "addInt"}},
|
|
LocalNames: wasm.IndirectNameMap{
|
|
{Index: wasm.Index(0), NameMap: wasm.NameMap{
|
|
{Index: wasm.Index(0), Name: "value_1"},
|
|
{Index: wasm.Index(1), Name: "value_2"},
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDType, 0x07, // 7 bytes in this section
|
|
0x01, // 1 type
|
|
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
|
|
wasm.SectionIDFunction, 0x02, // 2 bytes in this section
|
|
0x01, // 1 function
|
|
0x00, // func[0] type index 0
|
|
wasm.SectionIDExport, 0x0a, // 10 bytes in this section
|
|
0x01, // 1 export
|
|
0x06, 'A', 'd', 'd', 'I', 'n', 't', // size of "AddInt", "AddInt"
|
|
wasm.ExternTypeFunc, 0x00, // func[0]
|
|
wasm.SectionIDCode, 0x09, // 9 bytes in this section
|
|
01, // one code section
|
|
07, // length of the body + locals
|
|
00, // count of local blocks
|
|
wasm.OpcodeLocalGet, 0, // local.get 0
|
|
wasm.OpcodeLocalGet, 1, // local.get 1
|
|
wasm.OpcodeI32Add, // i32.add
|
|
wasm.OpcodeEnd, // end of instructions/code
|
|
wasm.SectionIDCustom, 0x27, // 39 bytes in this section
|
|
0x04, 'n', 'a', 'm', 'e',
|
|
subsectionIDFunctionNames, 0x09, // 9 bytes
|
|
0x01, // two function names
|
|
0x00, 0x06, 'a', 'd', 'd', 'I', 'n', 't', // index 0, size of "addInt", "addInt"
|
|
subsectionIDLocalNames, 0x15, // 21 bytes
|
|
0x01, // one function
|
|
0x00, 0x02, // index 0 has 2 locals
|
|
0x00, 0x07, 'v', 'a', 'l', 'u', 'e', '_', '1', // index 0, size of "value_1", "value_1"
|
|
0x01, 0x07, 'v', 'a', 'l', 'u', 'e', '_', '2', // index 1, size of "value_2", "value_2"
|
|
),
|
|
},
|
|
{
|
|
name: "exported global var",
|
|
input: &wasm.Module{
|
|
GlobalSection: []*wasm.Global{
|
|
{
|
|
Type: &wasm.GlobalType{ValType: i32, Mutable: true},
|
|
Init: &wasm.ConstantExpression{Opcode: wasm.OpcodeI32Const, Data: []byte{0}},
|
|
},
|
|
},
|
|
ExportSection: map[string]*wasm.Export{
|
|
"sp": {Name: "sp", Type: wasm.ExternTypeGlobal, Index: wasm.Index(0)},
|
|
},
|
|
},
|
|
expected: append(append(Magic, version...),
|
|
wasm.SectionIDGlobal, 0x06, // 6 bytes in this section
|
|
0x01, wasm.ValueTypeI32, 0x01, // 1 global i32 mutable
|
|
wasm.OpcodeI32Const, 0x00, wasm.OpcodeEnd, // arbitrary init to zero
|
|
wasm.SectionIDExport, 0x06, // 6 bytes in this section
|
|
0x01, // 1 export
|
|
0x02, 's', 'p', // size of "sp", "sp"
|
|
wasm.ExternTypeGlobal, 0x00, // global[0]
|
|
),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
bytes := EncodeModule(tc.input)
|
|
require.Equal(t, tc.expected, bytes)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestModule_Encode_HostFunctionSection_Unsupported(t *testing.T) {
|
|
// We don't currently have an approach to serialize reflect.Value pointers
|
|
fn := reflect.ValueOf(func(wasm.Module) {})
|
|
require.Panics(t, func() {
|
|
EncodeModule(&wasm.Module{HostFunctionSection: []*reflect.Value{&fn}})
|
|
})
|
|
}
|