diff --git a/js/experimental/runtime/kernel/buffer_decoder.js b/js/experimental/runtime/kernel/buffer_decoder.js index 2b4157e952..b0c4535426 100644 --- a/js/experimental/runtime/kernel/buffer_decoder.js +++ b/js/experimental/runtime/kernel/buffer_decoder.js @@ -279,15 +279,14 @@ class BufferDecoder { } /** - * Skips over a varint at a given index. - * @param {number} index Start of the data. + * Skips over a varint from the current cursor position. * @package */ - skipVarint(index) { - this.cursor_ = index; + skipVarint() { + const startIndex = this.cursor_; while (this.dataView_.getUint8(this.cursor_++) & 0x80) { } - checkCriticalPositionIndex(this.cursor_, index + 10); + checkCriticalPositionIndex(this.cursor_, startIndex + 10); } /** diff --git a/js/experimental/runtime/kernel/buffer_decoder_test.js b/js/experimental/runtime/kernel/buffer_decoder_test.js index 7c549f8891..17f28966b8 100644 --- a/js/experimental/runtime/kernel/buffer_decoder_test.js +++ b/js/experimental/runtime/kernel/buffer_decoder_test.js @@ -41,7 +41,7 @@ describe('Skip varint does', () => { it('skip a varint', () => { const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01)); - bufferDecoder.skipVarint(0); + bufferDecoder.skipVarint(); expect(bufferDecoder.cursor()).toBe(1); }); @@ -50,13 +50,13 @@ describe('Skip varint does', () => { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00)); if (CHECK_CRITICAL_STATE) { - expect(() => bufferDecoder.skipVarint(0)).toThrow(); + expect(() => bufferDecoder.skipVarint()).toThrow(); } else { // Note in unchecked mode we produce invalid output for invalid inputs. // This test just documents our behavior in those cases. // These values might change at any point and are not considered // what the implementation should be doing here. - bufferDecoder.skipVarint(0); + bufferDecoder.skipVarint(); expect(bufferDecoder.cursor()).toBe(11); } }); @@ -64,7 +64,7 @@ describe('Skip varint does', () => { it('fail when varint is beyond end of underlying array', () => { const bufferDecoder = BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x80)); - expect(() => bufferDecoder.skipVarint(0)).toThrow(); + expect(() => bufferDecoder.skipVarint()).toThrow(); }); }); diff --git a/js/experimental/runtime/kernel/indexer.js b/js/experimental/runtime/kernel/indexer.js index 16da735611..ae418227e7 100644 --- a/js/experimental/runtime/kernel/indexer.js +++ b/js/experimental/runtime/kernel/indexer.js @@ -85,7 +85,7 @@ function skipField_(bufferDecoder, wireType, fieldNumber) { case WireType.VARINT: checkCriticalElementIndex( bufferDecoder.cursor(), bufferDecoder.endIndex()); - bufferDecoder.skipVarint(bufferDecoder.cursor()); + bufferDecoder.skipVarint(); return false; case WireType.FIXED64: bufferDecoder.skip(8); diff --git a/js/experimental/runtime/kernel/writer.js b/js/experimental/runtime/kernel/writer.js index 8b830105ef..8af7a06d0d 100644 --- a/js/experimental/runtime/kernel/writer.js +++ b/js/experimental/runtime/kernel/writer.js @@ -451,7 +451,8 @@ class Writer { getLength_(bufferDecoder, start, wireType) { switch (wireType) { case WireType.VARINT: - bufferDecoder.skipVarint(start); + bufferDecoder.setCursor(start); + bufferDecoder.skipVarint(); return bufferDecoder.cursor() - start; case WireType.FIXED64: return 8;