github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/wasm/binary/table.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/wasilibs/wazerox/api"
     8  	"github.com/wasilibs/wazerox/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, ret *wasm.Table) (err error) {
    15  	ret.Type, err = r.ReadByte()
    16  	if err != nil {
    17  		return fmt.Errorf("read leading byte: %v", err)
    18  	}
    19  
    20  	if ret.Type != wasm.RefTypeFuncref {
    21  		if err = enabledFeatures.RequireEnabled(api.CoreFeatureReferenceTypes); err != nil {
    22  			return fmt.Errorf("table type funcref is invalid: %w", err)
    23  		}
    24  	}
    25  
    26  	var shared bool
    27  	ret.Min, ret.Max, shared, err = decodeLimitsType(r)
    28  	if err != nil {
    29  		return fmt.Errorf("read limits: %v", err)
    30  	}
    31  	if ret.Min > wasm.MaximumFunctionIndex {
    32  		return fmt.Errorf("table min must be at most %d", wasm.MaximumFunctionIndex)
    33  	}
    34  	if ret.Max != nil {
    35  		if *ret.Max < ret.Min {
    36  			return fmt.Errorf("table size minimum must not be greater than maximum")
    37  		}
    38  	}
    39  	if shared {
    40  		return fmt.Errorf("tables cannot be marked as shared")
    41  	}
    42  	return
    43  }