Skip to content

Commit d5b1bfc

Browse files
committed
Sync from Piper @310858019
PROTOBUF_SYNC_PIPER
1 parent 5353aa1 commit d5b1bfc

14 files changed

Lines changed: 1901 additions & 187 deletions

experimental/runtime/kernel/conformance/conformance_testee.js

100644100755
File mode changed.

experimental/runtime/kernel/conformance/conformance_testee_runner_node.js

100644100755
File mode changed.

experimental/runtime/kernel/indexer.js

Lines changed: 4 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
88
const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
99
const WireType = goog.require('protobuf.binary.WireType');
1010
const {Field} = goog.require('protobuf.binary.field');
11-
const {checkCriticalElementIndex, checkCriticalState} = goog.require('protobuf.internal.checks');
11+
const {checkCriticalState} = goog.require('protobuf.internal.checks');
12+
const {skipField, tagToFieldNumber, tagToWireType} = goog.require('protobuf.binary.tag');
1213

1314
/**
1415
* Appends a new entry in the index array for the given field number.
@@ -26,26 +27,6 @@ function addIndexEntry(storage, fieldNumber, wireType, startIndex) {
2627
}
2728
}
2829

29-
/**
30-
* Returns wire type stored in a tag.
31-
* Protos store the wire type as the first 3 bit of a tag.
32-
* @param {number} tag
33-
* @return {!WireType}
34-
*/
35-
function tagToWireType(tag) {
36-
return /** @type {!WireType} */ (tag & 0x07);
37-
}
38-
39-
/**
40-
* Returns the field number stored in a tag.
41-
* Protos store the field number in the upper 29 bits of a 32 bit number.
42-
* @param {number} tag
43-
* @return {number}
44-
*/
45-
function tagToFieldNumber(tag) {
46-
return tag >>> 3;
47-
}
48-
4930
/**
5031
* Creates an index of field locations in a given binary protobuf.
5132
* @param {!BufferDecoder} bufferDecoder
@@ -62,83 +43,13 @@ function buildIndex(bufferDecoder, pivot) {
6243
const wireType = tagToWireType(tag);
6344
const fieldNumber = tagToFieldNumber(tag);
6445
checkCriticalState(fieldNumber > 0, `Invalid field number ${fieldNumber}`);
65-
6646
addIndexEntry(storage, fieldNumber, wireType, bufferDecoder.cursor());
67-
68-
checkCriticalState(
69-
!skipField_(bufferDecoder, wireType, fieldNumber),
70-
'Found unmatched stop group.');
47+
skipField(bufferDecoder, wireType, fieldNumber);
7148
}
7249
return storage;
7350
}
7451

75-
/**
76-
* Skips over fields until the next field of the message.
77-
* @param {!BufferDecoder} bufferDecoder
78-
* @param {!WireType} wireType
79-
* @param {number} fieldNumber
80-
* @return {boolean} Whether the field we skipped over was a stop group.
81-
* @private
82-
*/
83-
function skipField_(bufferDecoder, wireType, fieldNumber) {
84-
switch (wireType) {
85-
case WireType.VARINT:
86-
checkCriticalElementIndex(
87-
bufferDecoder.cursor(), bufferDecoder.endIndex());
88-
bufferDecoder.skipVarint();
89-
return false;
90-
case WireType.FIXED64:
91-
bufferDecoder.skip(8);
92-
return false;
93-
case WireType.DELIMITED:
94-
checkCriticalElementIndex(
95-
bufferDecoder.cursor(), bufferDecoder.endIndex());
96-
const length = bufferDecoder.getUnsignedVarint32();
97-
bufferDecoder.skip(length);
98-
return false;
99-
case WireType.START_GROUP:
100-
checkCriticalState(
101-
skipGroup_(bufferDecoder, fieldNumber), 'No end group found.');
102-
return false;
103-
case WireType.END_GROUP:
104-
// Signal that we found a stop group to the caller
105-
return true;
106-
case WireType.FIXED32:
107-
bufferDecoder.skip(4);
108-
return false;
109-
default:
110-
throw new Error(`Invalid wire type: ${wireType}`);
111-
}
112-
}
113-
114-
/**
115-
* Skips over fields until it finds the end of a given group.
116-
* @param {!BufferDecoder} bufferDecoder
117-
* @param {number} groupFieldNumber
118-
* @return {boolean} Returns true if an end was found.
119-
* @private
120-
*/
121-
function skipGroup_(bufferDecoder, groupFieldNumber) {
122-
// On a start group we need to keep skipping fields until we find a
123-
// corresponding stop group
124-
// Note: Since we are calling skipField from here nested groups will be
125-
// handled by recursion of this method and thus we will not see a nested
126-
// STOP GROUP here unless there is something wrong with the input data.
127-
while (bufferDecoder.hasNext()) {
128-
const tag = bufferDecoder.getUnsignedVarint32();
129-
const wireType = tagToWireType(tag);
130-
const fieldNumber = tagToFieldNumber(tag);
131-
132-
if (skipField_(bufferDecoder, wireType, fieldNumber)) {
133-
checkCriticalState(
134-
groupFieldNumber === fieldNumber,
135-
`Expected stop group for fieldnumber ${groupFieldNumber} not found.`);
136-
return true;
137-
}
138-
}
139-
return false;
140-
}
141-
14252
exports = {
14353
buildIndex,
54+
tagToWireType,
14455
};

experimental/runtime/kernel/indexer_test.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ describe('Indexer does', () => {
7272

7373
it('fail for invalid wire type (6)', () => {
7474
expect(() => buildIndex(createBufferDecoder(0x0E, 0x01), PIVOT))
75-
.toThrowError('Invalid wire type: 6');
75+
.toThrowError('Unexpected wire type: 6');
7676
});
7777

7878
it('fail for invalid wire type (7)', () => {
7979
expect(() => buildIndex(createBufferDecoder(0x0F, 0x01), PIVOT))
80-
.toThrowError('Invalid wire type: 7');
80+
.toThrowError('Unexpected wire type: 7');
8181
});
8282

8383
it('index varint', () => {
@@ -269,33 +269,8 @@ describe('Indexer does', () => {
269269

270270
it('fail on unmatched stop group', () => {
271271
const data = createBufferDecoder(0x0C, 0x01);
272-
if (CHECK_CRITICAL_STATE) {
273-
expect(() => buildIndex(data, PIVOT))
274-
.toThrowError('Found unmatched stop group.');
275-
} else {
276-
// Note in unchecked mode we produce invalid output for invalid inputs.
277-
// This test just documents our behavior in those cases.
278-
// These values might change at any point and are not considered
279-
// what the implementation should be doing here.
280-
const storage = buildIndex(data, PIVOT);
281-
282-
expect(getStorageSize(storage)).toBe(1);
283-
const entryArray = storage.get(1).getIndexArray();
284-
expect(entryArray).not.toBeUndefined();
285-
expect(entryArray.length).toBe(1);
286-
const entry = entryArray[0];
287-
288-
expect(Field.getWireType(entry)).toBe(WireType.END_GROUP);
289-
expect(Field.getStartIndex(entry)).toBe(1);
290-
291-
const entryArray2 = storage.get(0).getIndexArray();
292-
expect(entryArray2).not.toBeUndefined();
293-
expect(entryArray2.length).toBe(1);
294-
const entry2 = entryArray2[0];
295-
296-
expect(Field.getWireType(entry2)).toBe(WireType.FIXED64);
297-
expect(Field.getStartIndex(entry2)).toBe(2);
298-
}
272+
expect(() => buildIndex(data, PIVOT))
273+
.toThrowError('Unexpected wire type: 4');
299274
});
300275

301276
it('fail for groups without matching stop group', () => {

0 commit comments

Comments
 (0)