wa-lang.org/wazero@v1.0.2/internal/wasm/binary/import.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/leb128"
     9  	"wa-lang.org/wazero/internal/wasm"
    10  )
    11  
    12  func decodeImport(
    13  	r *bytes.Reader,
    14  	idx uint32,
    15  	memorySizer func(minPages uint32, maxPages *uint32) (min, capacity, max uint32),
    16  	memoryLimitPages uint32,
    17  	enabledFeatures api.CoreFeatures,
    18  ) (i *wasm.Import, err error) {
    19  	i = &wasm.Import{}
    20  	if i.Module, _, err = decodeUTF8(r, "import module"); err != nil {
    21  		return nil, fmt.Errorf("import[%d] error decoding module: %w", idx, err)
    22  	}
    23  
    24  	if i.Name, _, err = decodeUTF8(r, "import name"); err != nil {
    25  		return nil, fmt.Errorf("import[%d] error decoding name: %w", idx, err)
    26  	}
    27  
    28  	b, err := r.ReadByte()
    29  	if err != nil {
    30  		return nil, fmt.Errorf("import[%d] error decoding type: %w", idx, err)
    31  	}
    32  	i.Type = b
    33  	switch i.Type {
    34  	case wasm.ExternTypeFunc:
    35  		i.DescFunc, _, err = leb128.DecodeUint32(r)
    36  	case wasm.ExternTypeTable:
    37  		i.DescTable, err = decodeTable(r, enabledFeatures)
    38  	case wasm.ExternTypeMemory:
    39  		i.DescMem, err = decodeMemory(r, memorySizer, memoryLimitPages)
    40  	case wasm.ExternTypeGlobal:
    41  		i.DescGlobal, err = decodeGlobalType(r)
    42  	default:
    43  		err = fmt.Errorf("%w: invalid byte for importdesc: %#x", ErrInvalidByte, b)
    44  	}
    45  	if err != nil {
    46  		return nil, fmt.Errorf("import[%d] %s[%s.%s]: %w", idx, wasm.ExternTypeName(i.Type), i.Module, i.Name, err)
    47  	}
    48  	return
    49  }
    50  
    51  // encodeImport returns the wasm.Import encoded in WebAssembly 1.0 (20191205) Binary Format.
    52  //
    53  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-import
    54  func encodeImport(i *wasm.Import) []byte {
    55  	data := encodeSizePrefixed([]byte(i.Module))
    56  	data = append(data, encodeSizePrefixed([]byte(i.Name))...)
    57  	data = append(data, i.Type)
    58  	switch i.Type {
    59  	case wasm.ExternTypeFunc:
    60  		data = append(data, leb128.EncodeUint32(i.DescFunc)...)
    61  	case wasm.ExternTypeTable:
    62  		data = append(data, wasm.RefTypeFuncref)
    63  		data = append(data, encodeLimitsType(i.DescTable.Min, i.DescTable.Max)...)
    64  	case wasm.ExternTypeMemory:
    65  		maxPtr := &i.DescMem.Max
    66  		if !i.DescMem.IsMaxEncoded {
    67  			maxPtr = nil
    68  		}
    69  		data = append(data, encodeLimitsType(i.DescMem.Min, maxPtr)...)
    70  	case wasm.ExternTypeGlobal:
    71  		g := i.DescGlobal
    72  		var mutable byte
    73  		if g.Mutable {
    74  			mutable = 1
    75  		}
    76  		data = append(data, g.ValType, mutable)
    77  	default:
    78  		panic(fmt.Errorf("invalid externtype: %s", wasm.ExternTypeName(i.Type)))
    79  	}
    80  	return data
    81  }