wa-lang.org/wazero@v1.0.2/internal/wasm/binary/limits.go (about) 1 package binary 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "wa-lang.org/wazero/internal/leb128" 8 ) 9 10 // decodeLimitsType returns the `limitsType` (min, max) decoded with the WebAssembly 1.0 (20191205) Binary Format. 11 // 12 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#limits%E2%91%A6 13 func decodeLimitsType(r *bytes.Reader) (min uint32, max *uint32, err error) { 14 var flag byte 15 if flag, err = r.ReadByte(); err != nil { 16 err = fmt.Errorf("read leading byte: %v", err) 17 return 18 } 19 20 switch flag { 21 case 0x00: 22 min, _, err = leb128.DecodeUint32(r) 23 if err != nil { 24 err = fmt.Errorf("read min of limit: %v", err) 25 } 26 case 0x01: 27 min, _, err = leb128.DecodeUint32(r) 28 if err != nil { 29 err = fmt.Errorf("read min of limit: %v", err) 30 return 31 } 32 var m uint32 33 if m, _, err = leb128.DecodeUint32(r); err != nil { 34 err = fmt.Errorf("read max of limit: %v", err) 35 } else { 36 max = &m 37 } 38 default: 39 err = fmt.Errorf("%v for limits: %#x != 0x00 or 0x01", ErrInvalidByte, flag) 40 } 41 return 42 } 43 44 // encodeLimitsType returns the `limitsType` (min, max) encoded in WebAssembly 1.0 (20191205) Binary Format. 45 // 46 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#limits%E2%91%A6 47 func encodeLimitsType(min uint32, max *uint32) []byte { 48 if max == nil { 49 return append(leb128.EncodeUint32(0x00), leb128.EncodeUint32(min)...) 50 } 51 return append(leb128.EncodeUint32(0x01), append(leb128.EncodeUint32(min), leb128.EncodeUint32(*max)...)...) 52 }