Skip to content

Commit

Permalink
Post-merge, properly encode negative int32's as 10 bytes [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Nov 28, 2016
1 parent ae6f474 commit 8fdebf8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 44 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ branches:
matrix:
include:
- node_js: 6
script: npm run zuul
script: sleep 3
script:
- npm run zuul
- sleep 3
36 changes: 18 additions & 18 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint": "eslint src",
"test": "tape tests/*.js | tap-spec",
"zuul": "zuul --ui tape --no-coverage --concurrency 1 -- tests/*.js",
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 -- tests/*.js",
"bench": "node bench",
"all": "npm run lint && npm run test && npm run build && npm run docs && npm run types && npm run bench"
},
Expand Down
32 changes: 16 additions & 16 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var LongBits = util.LongBits,
Long = util.Long;

function indexOutOfRange(reader, writeLength) {
return "index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len;
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
}

/**
Expand Down Expand Up @@ -68,7 +68,7 @@ function Tag(id, wireType) {
*/
ReaderPrototype.tag = function read_tag() {
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
return new Tag(this.buf[this.pos] >>> 3, this.buf[this.pos++] & 7);
};

Expand All @@ -82,7 +82,7 @@ ReaderPrototype.int32 = function read_int32() {
octet = 0;
do {
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
octet = this.buf[this.pos++];
if (shift < 32)
value |= (octet & 127) << shift;
Expand Down Expand Up @@ -139,22 +139,22 @@ function readLongVarint() {
} else {
for (i = 0; i < 4; ++i) {
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
b = this.buf[this.pos++];
lo |= (b & 127) << i * 7;
if (b < 128)
return new LongBits(lo >>> 0, hi >>> 0);
}
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
b = this.buf[this.pos++];
lo |= (b & 127) << 28;
hi |= (b & 127) >> 4;
if (b < 128)
return new LongBits(lo >>> 0, hi >>> 0);
for (i = 0; i < 5; ++i) {
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
b = this.buf[this.pos++];
hi |= (b & 127) << i * 7 + 3;
if (b < 128)
Expand Down Expand Up @@ -223,7 +223,7 @@ ReaderPrototype.bool = function read_bool() {
*/
ReaderPrototype.fixed32 = function read_fixed32() {
if (this.pos + 4 > this.len)
throw RangeError(indexOutOfRange(this, 4));
throw indexOutOfRange(this, 4);
this.pos += 4;
return this.buf[this.pos - 4]
| this.buf[this.pos - 3] << 8
Expand All @@ -249,7 +249,7 @@ ReaderPrototype.sfixed32 = function read_sfixed32() {
*/
function readLongFixed() {
if (this.pos + 8 > this.len)
throw RangeError(indexOutOfRange(this, 8));
throw indexOutOfRange(this, 8);
return new LongBits(
( this.buf[this.pos++]
| this.buf[this.pos++] << 8
Expand Down Expand Up @@ -299,7 +299,7 @@ ReaderPrototype.sfixed64 = Long && read_sfixed64_long || read_sfixed64_number;
*/
ReaderPrototype.float = function read_float() {
if (this.pos + 4 > this.len)
throw RangeError(indexOutOfRange(this, 4));
throw indexOutOfRange(this, 4);
var value = ieee754.read(this.buf, this.pos, false, 23, 4);
this.pos += 4;
return value;
Expand All @@ -312,7 +312,7 @@ ReaderPrototype.float = function read_float() {
*/
ReaderPrototype.double = function read_double() {
if (this.pos + 8 > this.len)
throw RangeError(indexOutOfRange(this, 4));
throw indexOutOfRange(this, 4);
var value = ieee754.read(this.buf, this.pos, false, 52, 8);
this.pos += 8;
return value;
Expand All @@ -327,7 +327,7 @@ ReaderPrototype.bytes = function read_bytes() {
start = this.pos,
end = this.pos + length;
if (end > this.len)
throw RangeError(indexOutOfRange(this, length));
throw indexOutOfRange(this, length);
this.pos += length;
return start === end // fix for IE 10/Win8 and others' subarray returning array of size 1
? new this.buf.constructor(0)
Expand Down Expand Up @@ -371,11 +371,11 @@ ReaderPrototype.skip = function skip(length) {
if (length === undefined) {
do {
if (this.pos >= this.len)
throw RangeError(indexOutOfRange(this));
throw indexOutOfRange(this);
} while (this.buf[this.pos++] & 128);
} else {
if (this.pos + length > this.len)
throw RangeError(indexOutOfRange(this, length));
throw indexOutOfRange(this, length);
this.pos += length;
}
return this;
Expand Down Expand Up @@ -476,7 +476,7 @@ BufferReaderPrototype.constructor = BufferReader;
*/
BufferReaderPrototype.float = function read_float_buffer() {
if (this.pos + 4 > this.len)
throw RangeError(indexOutOfRange(this, 4));
throw indexOutOfRange(this, 4);
var value = this.buf.readFloatLE(this.pos, true);
this.pos += 4;
return value;
Expand All @@ -488,7 +488,7 @@ BufferReaderPrototype.float = function read_float_buffer() {
*/
BufferReaderPrototype.double = function read_double_buffer() {
if (this.pos + 8 > this.len)
throw RangeError(indexOutOfRange(this, 8));
throw indexOutOfRange(this, 8);
var value = this.buf.readDoubleLE(this.pos, true);
this.pos += 8;
return value;
Expand All @@ -503,7 +503,7 @@ BufferReaderPrototype.string = function read_string_buffer() {
start = this.pos,
end = this.pos + length;
if (end > this.len)
throw RangeError(indexOutOfRange(this, length));
throw indexOutOfRange(this, length);
this.pos += length;
return this.buf.toString("utf8", start, end);
};
Expand Down
6 changes: 5 additions & 1 deletion src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ WriterPrototype.uint32 = function write_uint32(value) {
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.int32 = WriterPrototype.uint32;
WriterPrototype.int32 = function write_int32(value) {
return value < 0
? this.push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
: this.uint32(value);
};

/**
* Writes a 32 bit value as a varint, zig-zag encoded.
Expand Down
19 changes: 19 additions & 0 deletions tests/negative-int32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var tape = require("tape");

var protobuf = require("..");

tape.test("negative int32 values", function(test) {
var writer = protobuf.Writer();
writer.int32(-5615122);
var buf = writer.finish();

test.equal(buf.length, 10, "should encode to 10 bytes");

var reader = protobuf.Reader(buf);

test.equal(reader.int32(), -5615122, "should decode from 10 bytes");

test.equal(reader.pos, 10, "should have consumed the entire test buffer");

test.end();
});

0 comments on commit 8fdebf8

Please sign in to comment.