Files
wazero/internal/wasm/binary/table.go
Takeshi Yoneda 72f16d21eb Adds support for multi tables (#517)
This commit adds support for multiple tables per module.
Notably, if the WithFeatureReferenceTypes is enabled,
call_indirect, table.init and table.copy instructions
can reference non-zero indexed tables.

part of #484

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-05-03 11:38:51 +09:00

48 lines
1.4 KiB
Go

package binary
import (
"bytes"
"fmt"
"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 wasm.Features) (*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.Require(wasm.FeatureReferenceTypes); 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")
} 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, 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)...)
}