github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/global.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/wasm" 9 ) 10 11 // decodeGlobal returns the api.Global decoded with the WebAssembly 1.0 (20191205) Binary Format. 12 // 13 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-global 14 func decodeGlobal(r *bytes.Reader, enabledFeatures api.CoreFeatures, ret *wasm.Global) (err error) { 15 ret.Type, err = decodeGlobalType(r) 16 if err != nil { 17 return err 18 } 19 20 err = decodeConstantExpression(r, enabledFeatures, &ret.Init) 21 return 22 } 23 24 // decodeGlobalType returns the wasm.GlobalType decoded with the WebAssembly 1.0 (20191205) Binary Format. 25 // 26 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-globaltype 27 func decodeGlobalType(r *bytes.Reader) (wasm.GlobalType, error) { 28 vt, err := decodeValueTypes(r, 1) 29 if err != nil { 30 return wasm.GlobalType{}, fmt.Errorf("read value type: %w", err) 31 } 32 33 ret := wasm.GlobalType{ 34 ValType: vt[0], 35 } 36 37 b, err := r.ReadByte() 38 if err != nil { 39 return wasm.GlobalType{}, fmt.Errorf("read mutablity: %w", err) 40 } 41 42 switch mut := b; mut { 43 case 0x00: // not mutable 44 case 0x01: // mutable 45 ret.Mutable = true 46 default: 47 return wasm.GlobalType{}, fmt.Errorf("%w for mutability: %#x != 0x00 or 0x01", ErrInvalidByte, mut) 48 } 49 return ret, nil 50 }