Skip to content

Commit 34a7ce0

Browse files
author
Daniel Wirtz
committed
varint64 support
1 parent 31d3890 commit 34a7ce0

File tree

8 files changed

+148
-49
lines changed

8 files changed

+148
-49
lines changed

ByteBuffer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@
12821282
* @param {number} value Value to write
12831283
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
12841284
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
1285+
* @throws {Error} If long support is not available
12851286
* @expose
12861287
*/
12871288
ByteBuffer.prototype.writeZigZagVarint64 = function(value, offset) {
@@ -1291,8 +1292,8 @@
12911292
/**
12921293
* Reads a zigzag encoded 64bit base 128 variable-length integer as used in protobuf.
12931294
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
1294-
* @return {number|{value: number, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1295-
* @throws {Error} If it's not a valid varint
1295+
* @return {Long|{value: Long, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1296+
* @throws {Error} If it's not a valid varint or long support is not available
12961297
* @expose
12971298
*/
12981299
ByteBuffer.prototype.readZigZagVarint64 = function(offset) {
@@ -1426,9 +1427,13 @@
14261427
* Encodes a signed 64bit integer so that it can be effectively used with varint encoding.
14271428
* @param {number|Long} n Signed long
14281429
* @return {Long} Unsigned zigzag encoded long
1430+
* @throws {Error} If long support is not available
14291431
* @expose
14301432
*/
14311433
ByteBuffer.zigZagEncode64 = function(n) {
1434+
if (!Long) {
1435+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1436+
}
14321437
// ref: src/google/protobuf/wire_format_lite.h
14331438
if (typeof n == 'object' && n instanceof Long) {
14341439
if (n.unsigned) n = n.toSigned();
@@ -1442,9 +1447,13 @@
14421447
* Decodes a zigzag encoded signed 64bit integer.
14431448
* @param {Long} n Unsigned zigzag encoded long
14441449
* @return {Long} Signed long
1450+
* @throws {Error} If long support is not available
14451451
* @expose
14461452
*/
14471453
ByteBuffer.zigZagDecode64 = function(n) {
1454+
if (!Long) {
1455+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1456+
}
14481457
// ref: src/google/protobuf/wire_format_lite.h
14491458
if (typeof n == 'object' && n instanceof Long) {
14501459
if (!n.unsigned) n = n.toUnsigned();

ByteBuffer.min.js

Lines changed: 38 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,7 @@
12021202
* @param {number} value Value to write
12031203
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
12041204
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
1205+
* @throws {Error} If long support is not available
12051206
*/
12061207
ByteBuffer.prototype.writeZigZagVarint64 = function(value, offset) {
12071208
return this.writeVarint64(ByteBuffer.zigZagEncode64(value), offset);
@@ -1210,8 +1211,8 @@
12101211
/**
12111212
* Reads a zigzag encoded 64bit base 128 variable-length integer as used in protobuf.
12121213
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
1213-
* @return {number|{value: number, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1214-
* @throws {Error} If it's not a valid varint
1214+
* @return {Long|{value: Long, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1215+
* @throws {Error} If it's not a valid varint or long support is not available
12151216
*/
12161217
ByteBuffer.prototype.readZigZagVarint64 = function(offset) {
12171218
var dec = this.readVarint64(offset);
@@ -1336,8 +1337,12 @@
13361337
* Encodes a signed 64bit integer so that it can be effectively used with varint encoding.
13371338
* @param {number|Long} n Signed long
13381339
* @return {Long} Unsigned zigzag encoded long
1340+
* @throws {Error} If long support is not available
13391341
*/
13401342
ByteBuffer.zigZagEncode64 = function(n) {
1343+
if (!Long) {
1344+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1345+
}
13411346
// ref: src/google/protobuf/wire_format_lite.h
13421347
if (typeof n == 'object' && n instanceof Long) {
13431348
if (n.unsigned) n = n.toSigned();
@@ -1351,8 +1356,12 @@
13511356
* Decodes a zigzag encoded signed 64bit integer.
13521357
* @param {Long} n Unsigned zigzag encoded long
13531358
* @return {Long} Signed long
1359+
* @throws {Error} If long support is not available
13541360
*/
13551361
ByteBuffer.zigZagDecode64 = function(n) {
1362+
if (!Long) {
1363+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1364+
}
13561365
// ref: src/google/protobuf/wire_format_lite.h
13571366
if (typeof n == 'object' && n instanceof Long) {
13581367
if (!n.unsigned) n = n.toUnsigned();

docs/ByteBuffer.html

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,28 @@ <h5>Parameters:</h5>
30443044

30453045

30463046

3047+
<h5>Throws:</h5>
3048+
3049+
3050+
<div class="param-desc">
3051+
If long support is not available
3052+
</div>
3053+
3054+
3055+
3056+
<dl>
3057+
<dt>
3058+
Type
3059+
</dt>
3060+
<dd>
3061+
3062+
<span class="param-type">Error</span>
3063+
3064+
3065+
</dd>
3066+
</dl>
3067+
3068+
30473069

30483070

30493071
<h5>Returns:</h5>
@@ -3303,6 +3325,28 @@ <h5>Parameters:</h5>
33033325

33043326

33053327

3328+
<h5>Throws:</h5>
3329+
3330+
3331+
<div class="param-desc">
3332+
If long support is not available
3333+
</div>
3334+
3335+
3336+
3337+
<dl>
3338+
<dt>
3339+
Type
3340+
</dt>
3341+
<dd>
3342+
3343+
<span class="param-type">Error</span>
3344+
3345+
3346+
</dd>
3347+
</dl>
3348+
3349+
33063350

33073351

33083352
<h5>Returns:</h5>
@@ -8975,7 +9019,7 @@ <h5>Returns:</h5>
89759019

89769020

89779021
<dt>
8978-
<h4 class="name" id="readZigZagVarint64"><span class="type-signature"></span>readZigZagVarint64<span class="signature">(<span class="optional">offset</span>)</span><span class="type-signature"> &rarr; {number|{value: number, length: number}}</span></h4>
9022+
<h4 class="name" id="readZigZagVarint64"><span class="type-signature"></span>readZigZagVarint64<span class="signature">(<span class="optional">offset</span>)</span><span class="type-signature"> &rarr; {Long|{value: Long, length: number}}</span></h4>
89799023

89809024

89819025
</dt>
@@ -9086,7 +9130,7 @@ <h5>Throws:</h5>
90869130

90879131

90889132
<div class="param-desc">
9089-
If it's not a valid varint
9133+
If it's not a valid varint or long support is not available
90909134
</div>
90919135

90929136

@@ -9121,10 +9165,10 @@ <h5>Returns:</h5>
91219165
</dt>
91229166
<dd>
91239167

9124-
<span class="param-type">number</span>
9168+
<span class="param-type">Long</span>
91259169
|
91269170

9127-
<span class="param-type">{value: number, length: number}</span>
9171+
<span class="param-type">{value: Long, length: number}</span>
91289172

91299173

91309174
</dd>
@@ -14918,6 +14962,28 @@ <h5>Parameters:</h5>
1491814962

1491914963

1492014964

14965+
<h5>Throws:</h5>
14966+
14967+
14968+
<div class="param-desc">
14969+
If long support is not available
14970+
</div>
14971+
14972+
14973+
14974+
<dl>
14975+
<dt>
14976+
Type
14977+
</dt>
14978+
<dd>
14979+
14980+
<span class="param-type">Error</span>
14981+
14982+
14983+
</dd>
14984+
</dl>
14985+
14986+
1492114987

1492214988

1492314989
<h5>Returns:</h5>

externs/ByteBuffer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ ByteBuffer.prototype.readZigZagVarint32 = function(offset) {};
518518
* @param {number|Long} value
519519
* @param {number=} offset
520520
* @return {!ByteBuffer|number}
521+
* @throws {Error}
521522
*/
522523
ByteBuffer.prototype.writeVarint64 = function(value, offset) {};
523524

@@ -532,6 +533,7 @@ ByteBuffer.prototype.readVarint64 = function(offset) {};
532533
* @param {number|Long} value
533534
* @param {number=} offset
534535
* @return {!ByteBuffer|number}
536+
* @throws {Error}
535537
*/
536538
ByteBuffer.prototype.writeZigZagVarint64 = function(value, offset) {};
537539

@@ -573,13 +575,15 @@ ByteBuffer.prototype.readZigZagVarint = function(offset) {};
573575
/**
574576
* @param {number} value
575577
* @return {number}
578+
* @throws {Error}
576579
* @nosideeffects
577580
*/
578581
ByteBuffer.calculateVarint32 = function(value) {};
579582

580583
/**
581584
* @param {number} value
582585
* @return {number}
586+
* @throws {Error}
583587
* @nosideeffects
584588
*/
585589
ByteBuffer.calculateVarint64 = function(value) {};

src/ByteBuffer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@
12821282
* @param {number} value Value to write
12831283
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
12841284
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
1285+
* @throws {Error} If long support is not available
12851286
* @expose
12861287
*/
12871288
ByteBuffer.prototype.writeZigZagVarint64 = function(value, offset) {
@@ -1291,8 +1292,8 @@
12911292
/**
12921293
* Reads a zigzag encoded 64bit base 128 variable-length integer as used in protobuf.
12931294
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
1294-
* @return {number|{value: number, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1295-
* @throws {Error} If it's not a valid varint
1295+
* @return {Long|{value: Long, length: number}} The value read if offset is omitted, else the value read and the actual number of bytes read.
1296+
* @throws {Error} If it's not a valid varint or long support is not available
12961297
* @expose
12971298
*/
12981299
ByteBuffer.prototype.readZigZagVarint64 = function(offset) {
@@ -1426,9 +1427,13 @@
14261427
* Encodes a signed 64bit integer so that it can be effectively used with varint encoding.
14271428
* @param {number|Long} n Signed long
14281429
* @return {Long} Unsigned zigzag encoded long
1430+
* @throws {Error} If long support is not available
14291431
* @expose
14301432
*/
14311433
ByteBuffer.zigZagEncode64 = function(n) {
1434+
if (!Long) {
1435+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1436+
}
14321437
// ref: src/google/protobuf/wire_format_lite.h
14331438
if (typeof n == 'object' && n instanceof Long) {
14341439
if (n.unsigned) n = n.toSigned();
@@ -1442,9 +1447,13 @@
14421447
* Decodes a zigzag encoded signed 64bit integer.
14431448
* @param {Long} n Unsigned zigzag encoded long
14441449
* @return {Long} Signed long
1450+
* @throws {Error} If long support is not available
14451451
* @expose
14461452
*/
14471453
ByteBuffer.zigZagDecode64 = function(n) {
1454+
if (!Long) {
1455+
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1456+
}
14481457
// ref: src/google/protobuf/wire_format_lite.h
14491458
if (typeof n == 'object' && n instanceof Long) {
14501459
if (!n.unsigned) n = n.toUnsigned();

tests/suite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ var suite = {
566566
var Long = ByteBuffer.Long;
567567
var values = [
568568
Long.ONE, 1,
569+
Long.fromNumber(-3),
569570
Long.fromNumber(300),
570571
Long.fromNumber(-300),
571572
Long.fromNumber(0x7FFFFFFF),

0 commit comments

Comments
 (0)