Skip to content

Commit 4b01143

Browse files
committed
Allow struct restarts to properly use start position
1 parent fc49397 commit 4b01143

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

struct.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
6969
const TYPE = Symbol('type');
7070
const PARENT = Symbol('parent');
7171
setWriteStructSlots(writeStruct, prepareStructures);
72-
function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
72+
function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
7373
let typedStructs = packr.typedStructs || (packr.typedStructs = []);
7474
// note that we rely on pack.js to load stored structures before we get to this point
7575
let targetView = target.dataView;
7676
let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
7777
let safeEnd = target.length - 10;
7878
let start = position;
7979
if (position > safeEnd) {
80-
let lastStart = start;
8180
target = makeRoom(position);
8281
targetView = target.dataView;
83-
position -= lastStart;
84-
refsStartPosition -= lastStart;
85-
start = 0;
82+
position -= encodingStart;
83+
start -= encodingStart;
84+
refsStartPosition -= encodingStart;
85+
encodingStart = 0;
8686
safeEnd = target.length - 10;
8787
}
8888

@@ -120,13 +120,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
120120
};
121121
}
122122
if (position > safeEnd) {
123-
let lastStart = start;
124123
target = makeRoom(position);
125124
targetView = target.dataView;
126-
position -= lastStart;
127-
refsStartPosition -= lastStart;
128-
refPosition -= lastStart;
129-
start = 0;
125+
position -= encodingStart;
126+
start -= encodingStart;
127+
refsStartPosition -= encodingStart;
128+
refPosition -= encodingStart;
129+
encodingStart = 0;
130130
safeEnd = target.length - 10
131131
}
132132
switch (typeof value) {
@@ -165,13 +165,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
165165
let strLength = value.length;
166166
refOffset = refPosition - refsStartPosition;
167167
if ((strLength << 2) + refPosition > safeEnd) {
168-
let lastStart = start;
169168
target = makeRoom((strLength << 2) + refPosition);
170169
targetView = target.dataView;
171-
position -= lastStart;
172-
refsStartPosition -= lastStart;
173-
refPosition -= lastStart;
174-
start = 0;
170+
position -= encodingStart;
171+
start -= encodingStart;
172+
refsStartPosition -= encodingStart;
173+
refPosition -= encodingStart;
174+
encodingStart = 0;
175175
safeEnd = target.length - 10
176176
}
177177
if (strLength > ((0xff00 + refOffset) >> 2)) {
@@ -330,9 +330,10 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
330330
refPosition = newPosition.position;
331331
targetView = newPosition.targetView;
332332
target = newPosition.target;
333-
refsStartPosition -= start;
334-
position -= start;
335-
start = 0;
333+
refsStartPosition -= encodingStart;
334+
position -= encodingStart;
335+
start -= encodingStart;
336+
encodingStart = 0;
336337
} else
337338
refPosition = newPosition;
338339
if (size === 2) {
@@ -406,7 +407,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
406407
if (refsStartPosition === refPosition)
407408
return position; // no refs
408409
typedStructs.lastStringStart = position - start;
409-
return writeStruct(object, target, start, structures, makeRoom, pack, packr);
410+
return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
410411
}
411412
return refPosition;
412413
}

0 commit comments

Comments
 (0)