Skip to content

Commit

Permalink
Allow struct restarts to properly use start position
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Sep 8, 2023
1 parent fc49397 commit 4b01143
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
const TYPE = Symbol('type');
const PARENT = Symbol('parent');
setWriteStructSlots(writeStruct, prepareStructures);
function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
let typedStructs = packr.typedStructs || (packr.typedStructs = []);
// note that we rely on pack.js to load stored structures before we get to this point
let targetView = target.dataView;
let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
let safeEnd = target.length - 10;
let start = position;
if (position > safeEnd) {
let lastStart = start;
target = makeRoom(position);
targetView = target.dataView;
position -= lastStart;
refsStartPosition -= lastStart;
start = 0;
position -= encodingStart;
start -= encodingStart;
refsStartPosition -= encodingStart;
encodingStart = 0;
safeEnd = target.length - 10;
}

Expand Down Expand Up @@ -120,13 +120,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
};
}
if (position > safeEnd) {
let lastStart = start;
target = makeRoom(position);
targetView = target.dataView;
position -= lastStart;
refsStartPosition -= lastStart;
refPosition -= lastStart;
start = 0;
position -= encodingStart;
start -= encodingStart;
refsStartPosition -= encodingStart;
refPosition -= encodingStart;
encodingStart = 0;
safeEnd = target.length - 10
}
switch (typeof value) {
Expand Down Expand Up @@ -165,13 +165,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
let strLength = value.length;
refOffset = refPosition - refsStartPosition;
if ((strLength << 2) + refPosition > safeEnd) {
let lastStart = start;
target = makeRoom((strLength << 2) + refPosition);
targetView = target.dataView;
position -= lastStart;
refsStartPosition -= lastStart;
refPosition -= lastStart;
start = 0;
position -= encodingStart;
start -= encodingStart;
refsStartPosition -= encodingStart;
refPosition -= encodingStart;
encodingStart = 0;
safeEnd = target.length - 10
}
if (strLength > ((0xff00 + refOffset) >> 2)) {
Expand Down Expand Up @@ -330,9 +330,10 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
refPosition = newPosition.position;
targetView = newPosition.targetView;
target = newPosition.target;
refsStartPosition -= start;
position -= start;
start = 0;
refsStartPosition -= encodingStart;
position -= encodingStart;
start -= encodingStart;
encodingStart = 0;
} else
refPosition = newPosition;
if (size === 2) {
Expand Down Expand Up @@ -406,7 +407,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
if (refsStartPosition === refPosition)
return position; // no refs
typedStructs.lastStringStart = position - start;
return writeStruct(object, target, start, structures, makeRoom, pack, packr);
return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
}
return refPosition;
}
Expand Down

0 comments on commit 4b01143

Please sign in to comment.