Skip to content

Commit

Permalink
Fix coded buffer reader so all tests pass using dart2js.
Browse files Browse the repository at this point in the history
Bug: #59

BUG=
R=sgjesse@google.com

Review URL: https://codereview.chromium.org//2094533003 .
  • Loading branch information
Brian Slesinsky committed Jun 27, 2016
1 parent f7ee81e commit 6ab6e52
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## upcoming

* fix zigzag function so all coded buffer reader tests work on dart2js.

## 0.5.2

* make PbMixin constructor public for use within protoc plugin.
Expand Down
34 changes: 18 additions & 16 deletions lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ class CodedBufferReader {
final int _recursionLimit;
final int _sizeLimit;

CodedBufferReader(
List<int> buffer,
CodedBufferReader(List<int> buffer,
{int recursionLimit: DEFAULT_RECURSION_LIMIT,
int sizeLimit: DEFAULT_SIZE_LIMIT}) :
_buffer =
buffer is Uint8List
? buffer
: new Uint8List(buffer.length)..setRange(0, buffer.length, buffer),
_recursionLimit = recursionLimit,
_sizeLimit = math.min(sizeLimit, buffer.length) {
int sizeLimit: DEFAULT_SIZE_LIMIT})
: _buffer = buffer is Uint8List ? buffer : new Uint8List(buffer.length)
..setRange(0, buffer.length, buffer),
_recursionLimit = recursionLimit,
_sizeLimit = math.min(sizeLimit, buffer.length) {
_currentLimit = _sizeLimit;
}

Expand Down Expand Up @@ -62,7 +59,7 @@ class CodedBufferReader {
}

void readGroup(int fieldNumber, GeneratedMessage message,
ExtensionRegistry extensionRegistry) {
ExtensionRegistry extensionRegistry) {
if (_recursionDepth >= _recursionLimit) {
throw new InvalidProtocolBufferException.recursionLimitExceeded();
}
Expand All @@ -84,8 +81,8 @@ class CodedBufferReader {
return unknownFieldSet;
}

void readMessage(GeneratedMessage message,
ExtensionRegistry extensionRegistry) {
void readMessage(
GeneratedMessage message, ExtensionRegistry extensionRegistry) {
int length = readInt32();
if (_recursionDepth >= _recursionLimit) {
throw new InvalidProtocolBufferException.recursionLimitExceeded();
Expand Down Expand Up @@ -123,13 +120,15 @@ class CodedBufferReader {
var view = new Uint8List.view(data.buffer, data.offsetInBytes, 8);
return new Int64.fromBytes(view);
}

bool readBool() => _readRawVarint32() != 0;
List<int> readBytes() {
int length = readInt32();
_checkLimit(length);
return new Uint8List.view(_buffer.buffer,
_buffer.offsetInBytes + _bufferPos - length, length);
return new Uint8List.view(
_buffer.buffer, _buffer.offsetInBytes + _bufferPos - length, length);
}

String readString() => _UTF8.decode(readBytes());
double readFloat() =>
_readByteData(4).getFloat32(0, Endianness.LITTLE_ENDIAN);
Expand All @@ -150,8 +149,11 @@ class CodedBufferReader {
}

static int _decodeZigZag32(int value) {
if ((value & 0x1) == 1) value = -value;
return value >> 1;
if ((value & 0x1) == 1) {
return -(value >> 1) - 1;
} else {
return value >> 1;
}
}

static Int64 _decodeZigZag64(Int64 value) {
Expand Down
29 changes: 15 additions & 14 deletions test/codec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@TestOn("dart-vm")
library pb_codec_tests;

import 'dart:typed_data';
Expand Down Expand Up @@ -186,20 +185,22 @@ void main() {
expect(readFloat(bits), doubleEquals(value));
}

void _test64(List<int> hilo, double value) {
readDouble(int bits) {
var bytes = dataToBytes(
new ByteData(8)..setUint64(0, bits, Endianness.LITTLE_ENDIAN));
return new CodedBufferReader(bytes).readDouble();
}
final doubleToBytes = convertToBytes(PbFieldType.OD);

final doubleToBytes = convertToBytes(PbFieldType.OD);
doubleToBits(double value) =>
makeData(doubleToBytes(value)).getUint64(0, Endianness.LITTLE_ENDIAN);

int bits = (hilo[0] << 32) | hilo[1];
expect(doubleToBits(value), bits);
expect(readDouble(bits), doubleEquals(value));
void _test64(List<int> hilo, double value) {
// Encode a double to its wire format.
ByteData data = makeData(doubleToBytes(value));
var actualHilo = [
data.getUint32(4, Endianness.LITTLE_ENDIAN),
data.getUint32(0, Endianness.LITTLE_ENDIAN)
];
//int encoded = data.getUint64(0, Endianness.LITTLE_ENDIAN);
expect(actualHilo, hilo);

// Decode it again (round trip).
List<int> bytes = dataToBytes(data);
double reencoded = new CodedBufferReader(bytes).readDouble();
expect(reencoded, doubleEquals(value));
}

test('testFloat', () {
Expand Down

0 comments on commit 6ab6e52

Please sign in to comment.