github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/testing/binaryencoding/section.go (about)

     1  package binaryencoding
     2  
     3  import (
     4  	"github.com/tetratelabs/wazero/internal/leb128"
     5  	"github.com/tetratelabs/wazero/internal/wasm"
     6  )
     7  
     8  // encodeSection encodes the sectionID, the size of its contents in bytes, followed by the contents.
     9  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#sections%E2%91%A0
    10  func encodeSection(sectionID wasm.SectionID, contents []byte) []byte {
    11  	return append([]byte{sectionID}, encodeSizePrefixed(contents)...)
    12  }
    13  
    14  // encodeTypeSection encodes a wasm.SectionIDType for the given imports in WebAssembly 1.0 (20191205) Binary
    15  // Format.
    16  //
    17  // See EncodeFunctionType
    18  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#type-section%E2%91%A0
    19  func encodeTypeSection(types []wasm.FunctionType) []byte {
    20  	contents := leb128.EncodeUint32(uint32(len(types)))
    21  	for i := range types {
    22  		t := &types[i]
    23  		contents = append(contents, EncodeFunctionType(t)...)
    24  	}
    25  	return encodeSection(wasm.SectionIDType, contents)
    26  }
    27  
    28  // encodeImportSection encodes a wasm.SectionIDImport for the given imports in WebAssembly 1.0 (20191205) Binary
    29  // Format.
    30  //
    31  // See EncodeImport
    32  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#import-section%E2%91%A0
    33  func encodeImportSection(imports []wasm.Import) []byte {
    34  	contents := leb128.EncodeUint32(uint32(len(imports)))
    35  	for i := range imports {
    36  		imp := &imports[i]
    37  		contents = append(contents, EncodeImport(imp)...)
    38  	}
    39  	return encodeSection(wasm.SectionIDImport, contents)
    40  }
    41  
    42  // EncodeFunctionSection encodes a wasm.SectionIDFunction for the type indices associated with module-defined
    43  // functions in WebAssembly 1.0 (20191205) Binary Format.
    44  //
    45  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#function-section%E2%91%A0
    46  func EncodeFunctionSection(typeIndices []wasm.Index) []byte {
    47  	contents := leb128.EncodeUint32(uint32(len(typeIndices)))
    48  	for _, index := range typeIndices {
    49  		contents = append(contents, leb128.EncodeUint32(index)...)
    50  	}
    51  	return encodeSection(wasm.SectionIDFunction, contents)
    52  }
    53  
    54  // encodeCodeSection encodes a wasm.SectionIDCode for the module-defined function in WebAssembly 1.0 (20191205)
    55  // Binary Format.
    56  //
    57  // See encodeCode
    58  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#code-section%E2%91%A0
    59  func encodeCodeSection(code []wasm.Code) []byte {
    60  	contents := leb128.EncodeUint32(uint32(len(code)))
    61  	for i := range code {
    62  		c := &code[i]
    63  		contents = append(contents, encodeCode(c)...)
    64  	}
    65  	return encodeSection(wasm.SectionIDCode, contents)
    66  }
    67  
    68  // encodeTableSection encodes a wasm.SectionIDTable for the module-defined function in WebAssembly 1.0
    69  // (20191205) Binary Format.
    70  //
    71  // See EncodeTable
    72  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#table-section%E2%91%A0
    73  func encodeTableSection(tables []wasm.Table) []byte {
    74  	var contents []byte = leb128.EncodeUint32(uint32(len(tables)))
    75  	for i := range tables {
    76  		table := &tables[i]
    77  		contents = append(contents, EncodeTable(table)...)
    78  	}
    79  	return encodeSection(wasm.SectionIDTable, contents)
    80  }
    81  
    82  // encodeMemorySection encodes a wasm.SectionIDMemory for the module-defined function in WebAssembly 1.0
    83  // (20191205) Binary Format.
    84  //
    85  // See EncodeMemory
    86  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memory-section%E2%91%A0
    87  func encodeMemorySection(memory *wasm.Memory) []byte {
    88  	contents := append([]byte{1}, EncodeMemory(memory)...)
    89  	return encodeSection(wasm.SectionIDMemory, contents)
    90  }
    91  
    92  // encodeGlobalSection encodes a wasm.SectionIDGlobal for the given globals in WebAssembly 1.0 (20191205) Binary
    93  // Format.
    94  //
    95  // See encodeGlobal
    96  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#global-section%E2%91%A0
    97  func encodeGlobalSection(globals []wasm.Global) []byte {
    98  	contents := leb128.EncodeUint32(uint32(len(globals)))
    99  	for _, g := range globals {
   100  		contents = append(contents, encodeGlobal(g)...)
   101  	}
   102  	return encodeSection(wasm.SectionIDGlobal, contents)
   103  }
   104  
   105  // encodeExportSection encodes a wasm.SectionIDExport for the given exports in WebAssembly 1.0 (20191205) Binary
   106  // Format.
   107  //
   108  // See encodeExport
   109  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#export-section%E2%91%A0
   110  func encodeExportSection(exports []wasm.Export) []byte {
   111  	contents := leb128.EncodeUint32(uint32(len(exports)))
   112  	for i := range exports {
   113  		e := &exports[i]
   114  		contents = append(contents, encodeExport(e)...)
   115  	}
   116  	return encodeSection(wasm.SectionIDExport, contents)
   117  }
   118  
   119  // EncodeStartSection encodes a wasm.SectionIDStart for the given function index in WebAssembly 1.0 (20191205)
   120  // Binary Format.
   121  //
   122  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#start-section%E2%91%A0
   123  func EncodeStartSection(funcidx wasm.Index) []byte {
   124  	return encodeSection(wasm.SectionIDStart, leb128.EncodeUint32(funcidx))
   125  }
   126  
   127  // encodeEelementSection encodes a wasm.SectionIDElement for the elements in WebAssembly 1.0 (20191205)
   128  // Binary Format.
   129  //
   130  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#element-section%E2%91%A0
   131  func encodeElementSection(elements []wasm.ElementSegment) []byte {
   132  	contents := leb128.EncodeUint32(uint32(len(elements)))
   133  	for i := range elements {
   134  		e := &elements[i]
   135  		contents = append(contents, encodeElement(e)...)
   136  	}
   137  	return encodeSection(wasm.SectionIDElement, contents)
   138  }
   139  
   140  // encodeDataSection encodes a wasm.SectionIDData for the data in WebAssembly 1.0 (20191205)
   141  // Binary Format.
   142  //
   143  // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#data-section%E2%91%A0
   144  func encodeDataSection(datum []wasm.DataSegment) []byte {
   145  	contents := leb128.EncodeUint32(uint32(len(datum)))
   146  	for i := range datum {
   147  		d := &datum[i]
   148  		contents = append(contents, encodeDataSegment(d)...)
   149  	}
   150  	return encodeSection(wasm.SectionIDData, contents)
   151  }