...
1 package binaryencoding
2
3 import (
4 "github.com/tetratelabs/wazero/internal/leb128"
5 "github.com/tetratelabs/wazero/internal/wasm"
6 )
7
8
9
10 func encodeSection(sectionID wasm.SectionID, contents []byte) []byte {
11 return append([]byte{sectionID}, encodeSizePrefixed(contents)...)
12 }
13
14
15
16
17
18
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
29
30
31
32
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
43
44
45
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
55
56
57
58
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
69
70
71
72
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
83
84
85
86
87 func encodeMemorySection(memory *wasm.Memory) []byte {
88 contents := append([]byte{1}, EncodeMemory(memory)...)
89 return encodeSection(wasm.SectionIDMemory, contents)
90 }
91
92
93
94
95
96
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
106
107
108
109
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
120
121
122
123 func EncodeStartSection(funcidx wasm.Index) []byte {
124 return encodeSection(wasm.SectionIDStart, leb128.EncodeUint32(funcidx))
125 }
126
127
128
129
130
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
141
142
143
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 }
152
View as plain text