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

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/bananabytelabs/wazero/internal/leb128"
     8  	"github.com/bananabytelabs/wazero/internal/wasm"
     9  )
    10  
    11  func decodeExport(r *bytes.Reader, ret *wasm.Export) (err error) {
    12  	if ret.Name, _, err = decodeUTF8(r, "export name"); err != nil {
    13  		return
    14  	}
    15  
    16  	b, err := r.ReadByte()
    17  	if err != nil {
    18  		err = fmt.Errorf("error decoding export kind: %w", err)
    19  		return
    20  	}
    21  
    22  	ret.Type = b
    23  	switch ret.Type {
    24  	case wasm.ExternTypeFunc, wasm.ExternTypeTable, wasm.ExternTypeMemory, wasm.ExternTypeGlobal:
    25  		if ret.Index, _, err = leb128.DecodeUint32(r); err != nil {
    26  			err = fmt.Errorf("error decoding export index: %w", err)
    27  		}
    28  	default:
    29  		err = fmt.Errorf("%w: invalid byte for exportdesc: %#x", ErrInvalidByte, b)
    30  	}
    31  	return
    32  }