github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/import.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/leb128"
     9  	"github.com/bananabytelabs/wazero/internal/wasm"
    10  )
    11  
    12  func decodeImport(
    13  	r *bytes.Reader,
    14  	idx uint32,
    15  	memorySizer memorySizer,
    16  	memoryLimitPages uint32,
    17  	enabledFeatures api.CoreFeatures,
    18  	ret *wasm.Import,
    19  ) (err error) {
    20  	if ret.Module, _, err = decodeUTF8(r, "import module"); err != nil {
    21  		err = fmt.Errorf("import[%d] error decoding module: %w", idx, err)
    22  		return
    23  	}
    24  
    25  	if ret.Name, _, err = decodeUTF8(r, "import name"); err != nil {
    26  		err = fmt.Errorf("import[%d] error decoding name: %w", idx, err)
    27  		return
    28  	}
    29  
    30  	b, err := r.ReadByte()
    31  	if err != nil {
    32  		err = fmt.Errorf("import[%d] error decoding type: %w", idx, err)
    33  		return
    34  	}
    35  	ret.Type = b
    36  	switch ret.Type {
    37  	case wasm.ExternTypeFunc:
    38  		ret.DescFunc, _, err = leb128.DecodeUint32(r)
    39  	case wasm.ExternTypeTable:
    40  		err = decodeTable(r, enabledFeatures, &ret.DescTable)
    41  	case wasm.ExternTypeMemory:
    42  		ret.DescMem, err = decodeMemory(r, memorySizer, memoryLimitPages)
    43  	case wasm.ExternTypeGlobal:
    44  		ret.DescGlobal, err = decodeGlobalType(r)
    45  	default:
    46  		err = fmt.Errorf("%w: invalid byte for importdesc: %#x", ErrInvalidByte, b)
    47  	}
    48  	if err != nil {
    49  		err = fmt.Errorf("import[%d] %s[%s.%s]: %w", idx, wasm.ExternTypeName(ret.Type), ret.Module, ret.Name, err)
    50  	}
    51  	return
    52  }