Files
wazero/internal/wasm/binary/table.go
Crypt Keeper b01effc8a9 Top-levels CoreFeatures and defaults to 2.0 (#800)
While compilers should be conservative when targeting WebAssembly Core
features, runtimes should be lenient as otherwise people need to
constantly turn on all features. Currently, most examples have to turn
on 2.0 features because compilers such as AssemblyScript and TinyGo use
them by default. This matches the policy with the reality, and should
make first time use easier.

This top-levels an internal type as `api.CoreFeatures` and defaults to
2.0 as opposed to 1.0, our previous default. This is less cluttered than
the excess of `WithXXX` methods we had prior to implementing all
planned WebAssembly Core Specification 1.0 features.

Finally, this backfills rationale as flat config types were a distinct
decision even if feature set selection muddied the topic.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-06 15:14:36 +08:00

47 lines
1.4 KiB
Go

package binary
import (
"bytes"
"fmt"
"github.com/tetratelabs/wazero/api"
"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, enabledFeatures api.CoreFeatures) (*wasm.Table, error) {
tableType, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("read leading byte: %v", err)
}
if tableType != wasm.RefTypeFuncref {
if err := enabledFeatures.RequireEnabled(api.CoreFeatureReferenceTypes); err != nil {
return nil, fmt.Errorf("table type funcref is invalid: %w", err)
}
}
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")
}
}
return &wasm.Table{Min: min, Max: max, Type: tableType}, nil
}
// encodeTable returns the wasm.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{i.Type}, encodeLimitsType(i.Min, i.Max)...)
}