@@ -107,10 +107,7 @@ void WasmBinaryWriter::writeHeader() {
107
107
}
108
108
109
109
int32_t WasmBinaryWriter::writeU32LEBPlaceholder () {
110
- int32_t ret = o.size ();
111
- o << int32_t (0 );
112
- o << int8_t (0 );
113
- return ret;
110
+ return o.writeU32LEBPlaceholder ();
114
111
}
115
112
116
113
void WasmBinaryWriter::writeResizableLimits (
@@ -142,26 +139,12 @@ template<typename T> int32_t WasmBinaryWriter::startSection(T code) {
142
139
}
143
140
144
141
void WasmBinaryWriter::finishSection (int32_t start) {
145
- // section size does not include the reserved bytes of the size field itself
146
- int32_t size = o.size () - start - MaxLEB32Bytes;
147
- auto sizeFieldSize = o.writeAt (start, U32LEB (size));
148
- // We can move things back if the actual LEB for the size doesn't use the
149
- // maximum 5 bytes. In that case we need to adjust offsets after we move
150
- // things backwards.
151
- auto adjustmentForLEBShrinking = MaxLEB32Bytes - sizeFieldSize;
152
- if (adjustmentForLEBShrinking) {
153
- // we can save some room, nice
154
- assert (sizeFieldSize < MaxLEB32Bytes);
155
- std::move (&o[start] + MaxLEB32Bytes,
156
- &o[start] + MaxLEB32Bytes + size,
157
- &o[start] + sizeFieldSize);
158
- o.resize (o.size () - adjustmentForLEBShrinking);
159
- if (sourceMap) {
160
- for (auto i = sourceMapLocationsSizeAtSectionStart;
161
- i < sourceMapLocations.size ();
162
- ++i) {
163
- sourceMapLocations[i].first -= adjustmentForLEBShrinking;
164
- }
142
+ auto adjustmentForLEBShrinking = o.emitRetroactiveSectionSizeLEB (start);
143
+ if (adjustmentForLEBShrinking && sourceMap) {
144
+ for (auto i = sourceMapLocationsSizeAtSectionStart;
145
+ i < sourceMapLocations.size ();
146
+ ++i) {
147
+ sourceMapLocations[i].first -= adjustmentForLEBShrinking;
165
148
}
166
149
}
167
150
@@ -172,6 +155,10 @@ void WasmBinaryWriter::finishSection(int32_t start) {
172
155
// The section type byte is right before the LEB for the size; we want
173
156
// offsets that are relative to the body, which is after that section type
174
157
// byte and the the size LEB.
158
+ //
159
+ // We can compute the size of the size field LEB by considering the original
160
+ // size of the maximal LEB, and the adjustment due to shrinking.
161
+ auto sizeFieldSize = MaxLEB32Bytes - adjustmentForLEBShrinking;
175
162
auto body = start + sizeFieldSize;
176
163
// Offsets are relative to the body of the code section: after the
177
164
// section type byte and the size.
@@ -1538,8 +1525,7 @@ void WasmBinaryWriter::writeData(const char* data, size_t size) {
1538
1525
}
1539
1526
1540
1527
void WasmBinaryWriter::writeInlineString (std::string_view name) {
1541
- o << U32LEB (name.size ());
1542
- writeData (name.data (), name.size ());
1528
+ o.writeInlineString (name);
1543
1529
}
1544
1530
1545
1531
static bool isHexDigit (char ch) {
0 commit comments