wa-lang.org/wazero@v1.0.2/internal/wasm/binary/table.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"wa-lang.org/wazero/api"
     8  	"wa-lang.org/wazero/internal/wasm"
     9  )
    10  
    11  // decodeTable returns the wasm.Table decoded with the WebAssembly 1.0 (20191205) Binary Format.
    12  //
    13  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
    14  func decodeTable(r *bytes.Reader, enabledFeatures api.CoreFeatures) (*wasm.Table, error) {
    15  	tableType, err := r.ReadByte()
    16  	if err != nil {
    17  		return nil, fmt.Errorf("read leading byte: %v", err)
    18  	}
    19  
    20  	if tableType != wasm.RefTypeFuncref {
    21  		if err := enabledFeatures.RequireEnabled(api.CoreFeatureReferenceTypes); err != nil {
    22  			return nil, fmt.Errorf("table type funcref is invalid: %w", err)
    23  		}
    24  	}
    25  
    26  	min, max, err := decodeLimitsType(r)
    27  	if err != nil {
    28  		return nil, fmt.Errorf("read limits: %v", err)
    29  	}
    30  	if min > wasm.MaximumFunctionIndex {
    31  		return nil, fmt.Errorf("table min must be at most %d", wasm.MaximumFunctionIndex)
    32  	}
    33  	if max != nil {
    34  		if *max < min {
    35  			return nil, fmt.Errorf("table size minimum must not be greater than maximum")
    36  		}
    37  	}
    38  	return &wasm.Table{Min: min, Max: max, Type: tableType}, nil
    39  }
    40  
    41  // encodeTable returns the wasm.Table encoded in WebAssembly 1.0 (20191205) Binary Format.
    42  //
    43  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
    44  func encodeTable(i *wasm.Table) []byte {
    45  	return append([]byte{i.Type}, encodeLimitsType(i.Min, i.Max)...)
    46  }