github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/testing/binaryencoding/element.go (about) 1 package binaryencoding 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/wasilibs/wazerox/internal/leb128" 8 "github.com/wasilibs/wazerox/internal/wasm" 9 ) 10 11 func ensureElementKindFuncRef(r *bytes.Reader) error { 12 elemKind, err := r.ReadByte() 13 if err != nil { 14 return fmt.Errorf("read element prefix: %w", err) 15 } 16 if elemKind != 0x0 { // ElemKind is fixed to 0x0 now: https://www.w3.org/TR/2022/WD-wasm-core-2-20220419/binary/modules.html#element-section 17 return fmt.Errorf("element kind must be zero but was 0x%x", elemKind) 18 } 19 return nil 20 } 21 22 // encodeCode returns the wasm.ElementSegment encoded in WebAssembly 1.0 (20191205) Binary Format. 23 // 24 // https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#element-section%E2%91%A0 25 func encodeElement(e *wasm.ElementSegment) (ret []byte) { 26 if e.Mode == wasm.ElementModeActive { 27 ret = append(ret, leb128.EncodeInt32(int32(e.TableIndex))...) 28 ret = append(ret, encodeConstantExpression(e.OffsetExpr)...) 29 ret = append(ret, leb128.EncodeUint32(uint32(len(e.Init)))...) 30 for _, idx := range e.Init { 31 ret = append(ret, leb128.EncodeInt32(int32(idx))...) 32 } 33 } else { 34 panic("TODO: support encoding for non-active elements in bulk-memory-operations proposal") 35 } 36 return 37 }