github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/testing/binaryencoding/import.go (about) 1 package binaryencoding 2 3 import ( 4 "fmt" 5 6 "github.com/wasilibs/wazerox/internal/leb128" 7 "github.com/wasilibs/wazerox/internal/wasm" 8 ) 9 10 // EncodeImport returns the wasm.Import encoded in WebAssembly 1.0 (20191205) Binary Format. 11 // 12 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-import 13 func EncodeImport(i *wasm.Import) []byte { 14 data := encodeSizePrefixed([]byte(i.Module)) 15 data = append(data, encodeSizePrefixed([]byte(i.Name))...) 16 data = append(data, i.Type) 17 switch i.Type { 18 case wasm.ExternTypeFunc: 19 data = append(data, leb128.EncodeUint32(i.DescFunc)...) 20 case wasm.ExternTypeTable: 21 data = append(data, wasm.RefTypeFuncref) 22 data = append(data, EncodeLimitsType(i.DescTable.Min, i.DescTable.Max, false)...) 23 case wasm.ExternTypeMemory: 24 maxPtr := &i.DescMem.Max 25 if !i.DescMem.IsMaxEncoded { 26 maxPtr = nil 27 } 28 data = append(data, EncodeLimitsType(i.DescMem.Min, maxPtr, i.DescMem.IsShared)...) 29 case wasm.ExternTypeGlobal: 30 g := i.DescGlobal 31 var mutable byte 32 if g.Mutable { 33 mutable = 1 34 } 35 data = append(data, g.ValType, mutable) 36 default: 37 panic(fmt.Errorf("invalid externtype: %s", wasm.ExternTypeName(i.Type))) 38 } 39 return data 40 }