Flattens Memory and Table types and backfills Table encoder (#356)

This flattens Memory and Table types, particularly making it a
compilation error to add multiple of either.

This also backfills binary encoding of Table.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-03-10 15:47:49 +08:00
committed by GitHub
parent 074a2cffbf
commit 97c759b9d8
32 changed files with 586 additions and 413 deletions

View File

@@ -0,0 +1,45 @@
package binary
import (
"bytes"
"fmt"
wasm "github.com/tetratelabs/wazero/internal/wasm"
)
// decodeTable returns the wasm.Table decoded with the WebAssembly 1.0 (20191205) Binary Format.
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
func decodeTable(r *bytes.Reader) (*wasm.Table, error) {
b, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("read leading byte: %v", err)
}
if b != wasm.ElemTypeFuncref {
return nil, fmt.Errorf("invalid element type %#x != funcref(%#x)", b, wasm.ElemTypeFuncref)
}
min, max, err := decodeLimitsType(r)
if err != nil {
return nil, fmt.Errorf("read limits: %v", err)
}
if min > wasm.MaximumFunctionIndex {
return nil, fmt.Errorf("table min must be at most %d", wasm.MaximumFunctionIndex)
}
if max != nil {
if *max < min {
return nil, fmt.Errorf("table size minimum must not be greater than maximum")
} else if *max > wasm.MaximumFunctionIndex {
return nil, fmt.Errorf("table max must be at most %d", wasm.MaximumFunctionIndex)
}
}
return &wasm.Table{Min: min, Max: max}, nil
}
// encodeTable returns the internalwasm.Table encoded in WebAssembly 1.0 (20191205) Binary Format.
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
func encodeTable(i *wasm.Table) []byte {
return append([]byte{wasm.ElemTypeFuncref}, encodeLimitsType(i.Min, i.Max)...)
}