github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/table.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/bananabytelabs/wazero/api"
     8  	"github.com/bananabytelabs/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, 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  	ret.Min, ret.Max, err = decodeLimitsType(r)
    27  	if err != nil {
    28  		return fmt.Errorf("read limits: %v", err)
    29  	}
    30  	if ret.Min > wasm.MaximumFunctionIndex {
    31  		return fmt.Errorf("table min must be at most %d", wasm.MaximumFunctionIndex)
    32  	}
    33  	if ret.Max != nil {
    34  		if *ret.Max < ret.Min {
    35  			return fmt.Errorf("table size minimum must not be greater than maximum")
    36  		}
    37  	}
    38  	return
    39  }