Jelajahi Sumber

Project import generated by Copybara

PiperOrigin-RevId: 299970447
Rafi Kamal 5 tahun lalu
induk
melakukan
4fa3c8e883

+ 4 - 5
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
    * @package
    */
    */
-  skipVarint(index) {
-    this.cursor_ = index;
+  skipVarint() {
+    const startIndex = this.cursor_;
     while (this.dataView_.getUint8(this.cursor_++) & 0x80) {
     while (this.dataView_.getUint8(this.cursor_++) & 0x80) {
     }
     }
-    checkCriticalPositionIndex(this.cursor_, index + 10);
+    checkCriticalPositionIndex(this.cursor_, startIndex + 10);
   }
   }
 
 
   /**
   /**

+ 4 - 4
js/experimental/runtime/kernel/buffer_decoder_test.js

@@ -41,7 +41,7 @@ describe('Skip varint does', () => {
   it('skip a varint', () => {
   it('skip a varint', () => {
     const bufferDecoder =
     const bufferDecoder =
         BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
         BufferDecoder.fromArrayBuffer(createArrayBuffer(0x01));
-    bufferDecoder.skipVarint(0);
+    bufferDecoder.skipVarint();
     expect(bufferDecoder.cursor()).toBe(1);
     expect(bufferDecoder.cursor()).toBe(1);
   });
   });
 
 
@@ -50,13 +50,13 @@ describe('Skip varint does', () => {
         0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
         0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
 
 
     if (CHECK_CRITICAL_STATE) {
     if (CHECK_CRITICAL_STATE) {
-      expect(() => bufferDecoder.skipVarint(0)).toThrow();
+      expect(() => bufferDecoder.skipVarint()).toThrow();
     } else {
     } else {
       // Note in unchecked mode we produce invalid output for invalid inputs.
       // Note in unchecked mode we produce invalid output for invalid inputs.
       // This test just documents our behavior in those cases.
       // This test just documents our behavior in those cases.
       // These values might change at any point and are not considered
       // These values might change at any point and are not considered
       // what the implementation should be doing here.
       // what the implementation should be doing here.
-      bufferDecoder.skipVarint(0);
+      bufferDecoder.skipVarint();
       expect(bufferDecoder.cursor()).toBe(11);
       expect(bufferDecoder.cursor()).toBe(11);
     }
     }
   });
   });
@@ -64,7 +64,7 @@ describe('Skip varint does', () => {
   it('fail when varint is beyond end of underlying array', () => {
   it('fail when varint is beyond end of underlying array', () => {
     const bufferDecoder =
     const bufferDecoder =
         BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x80));
         BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x80));
-    expect(() => bufferDecoder.skipVarint(0)).toThrow();
+    expect(() => bufferDecoder.skipVarint()).toThrow();
   });
   });
 });
 });
 
 

+ 1 - 1
js/experimental/runtime/kernel/indexer.js

@@ -85,7 +85,7 @@ function skipField_(bufferDecoder, wireType, fieldNumber) {
     case WireType.VARINT:
     case WireType.VARINT:
       checkCriticalElementIndex(
       checkCriticalElementIndex(
           bufferDecoder.cursor(), bufferDecoder.endIndex());
           bufferDecoder.cursor(), bufferDecoder.endIndex());
-      bufferDecoder.skipVarint(bufferDecoder.cursor());
+      bufferDecoder.skipVarint();
       return false;
       return false;
     case WireType.FIXED64:
     case WireType.FIXED64:
       bufferDecoder.skip(8);
       bufferDecoder.skip(8);

+ 2 - 1
js/experimental/runtime/kernel/writer.js

@@ -451,7 +451,8 @@ class Writer {
   getLength_(bufferDecoder, start, wireType) {
   getLength_(bufferDecoder, start, wireType) {
     switch (wireType) {
     switch (wireType) {
       case WireType.VARINT:
       case WireType.VARINT:
-        bufferDecoder.skipVarint(start);
+        bufferDecoder.setCursor(start);
+        bufferDecoder.skipVarint();
         return bufferDecoder.cursor() - start;
         return bufferDecoder.cursor() - start;
       case WireType.FIXED64:
       case WireType.FIXED64:
         return 8;
         return 8;