Skip to content

Commit 7def340

Browse files
committed
Fixed serialization order of fixed64, fallback to parseInt with no long lib, see #534
1 parent 47608dd commit 7def340

File tree

12 files changed

+146
-73
lines changed

12 files changed

+146
-73
lines changed

dist/protobuf.js

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

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

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

dist/protobuf.min.js.gz

67 Bytes
Binary file not shown.

dist/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protobuf.parse = require("./parse");
7373
// Serialization
7474
protobuf.Writer = require("./writer");
7575
protobuf.BufferWriter = protobuf.Writer.BufferWriter;
76+
var Reader =
7677
protobuf.Reader = require("./reader");
7778
protobuf.BufferReader = protobuf.Reader.BufferReader;
7879
protobuf.codegen = require("./codegen");
@@ -97,14 +98,25 @@ protobuf.Message = require("./message");
9798
protobuf.types = require("./types");
9899
protobuf.common = require("./common");
99100
protobuf.rpc = require("./rpc");
101+
var util =
100102
protobuf.util = require("./util");
103+
protobuf.configure = configure;
104+
105+
/**
106+
* Reconfigures the library according to the environment.
107+
* @returns {undefined}
108+
*/
109+
function configure() {
110+
util._configure();
111+
Reader._configure();
112+
}
101113

102114
// Be nice to AMD
103115
if (typeof define === 'function' && define.amd)
104116
define(["long"], function(Long) {
105117
if (Long) {
106118
protobuf.util.Long = Long;
107-
protobuf.Reader.configure();
119+
configure();
108120
}
109121
return protobuf;
110122
});

src/reader.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,6 @@ function indexOutOfRange(reader, writeLength) {
1212
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
1313
}
1414

15-
/**
16-
* Configures the Reader interface according to the environment.
17-
* @memberof Reader
18-
* @returns {undefined}
19-
*/
20-
function configure() {
21-
if (util.Long) {
22-
ReaderPrototype.int64 = read_int64_long;
23-
ReaderPrototype.uint64 = read_uint64_long;
24-
ReaderPrototype.sint64 = read_sint64_long;
25-
ReaderPrototype.fixed64 = read_fixed64_long;
26-
ReaderPrototype.sfixed64 = read_sfixed64_long;
27-
} else {
28-
ReaderPrototype.int64 = read_int64_number;
29-
ReaderPrototype.uint64 = read_uint64_number;
30-
ReaderPrototype.sint64 = read_sint64_number;
31-
ReaderPrototype.fixed64 = read_fixed64_number;
32-
ReaderPrototype.sfixed64 = read_sfixed64_number;
33-
}
34-
}
35-
36-
Reader.configure = configure;
37-
3815
/**
3916
* Constructs a new reader instance using the specified buffer.
4017
* @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
@@ -628,4 +605,22 @@ BufferReaderPrototype.finish = function finish_buffer(buffer) {
628605
return remain;
629606
};
630607

608+
function configure() {
609+
if (util.Long) {
610+
ReaderPrototype.int64 = read_int64_long;
611+
ReaderPrototype.uint64 = read_uint64_long;
612+
ReaderPrototype.sint64 = read_sint64_long;
613+
ReaderPrototype.fixed64 = read_fixed64_long;
614+
ReaderPrototype.sfixed64 = read_sfixed64_long;
615+
} else {
616+
ReaderPrototype.int64 = read_int64_number;
617+
ReaderPrototype.uint64 = read_uint64_number;
618+
ReaderPrototype.sint64 = read_sint64_number;
619+
ReaderPrototype.fixed64 = read_fixed64_number;
620+
ReaderPrototype.sfixed64 = read_sfixed64_number;
621+
}
622+
}
623+
624+
Reader._configure = configure;
625+
631626
configure();

src/util.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ util.newBuffer = function newBuffer(size) {
251251
: new (typeof Uint8Array !== 'undefined' && Uint8Array || Array)(size);
252252
};
253253

254+
var runtime = require("./util/runtime");
255+
254256
util.EventEmitter = require("./util/eventemitter");
255257

256258
// Merge in runtime utility
257-
util.merge(util, require("./util/runtime"));
259+
util.merge(util, runtime);
260+
261+
util._configure = function configure() {
262+
runtime.Long = util.Long;
263+
};

src/util/longbits.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,17 @@ LongBits.fromNumber = function fromNumber(value) {
7878
* Constructs new long bits from a number, long or string.
7979
* @param {Long|number|string} value Value
8080
* @returns {util.LongBits} Instance
81-
* @throws {TypeError} If `value` is a string and no long library is present.
8281
*/
8382
LongBits.from = function from(value) {
8483
switch (typeof value) { // eslint-disable-line default-case
8584
case 'number':
8685
return LongBits.fromNumber(value);
8786
case 'string':
88-
value = util.Long.fromString(value); // throws without a long lib
87+
if (util.Long)
88+
value = util.Long.fromString(value);
89+
// fallthrough
90+
else
91+
return LongBits.fromNumber(parseInt(value, 10));
8992
}
9093
return (value.low || value.high) && new LongBits(value.low >>> 0, value.high >>> 0) || zero;
9194
};
@@ -112,7 +115,9 @@ LongBitsPrototype.toNumber = function toNumber(unsigned) {
112115
* @returns {Long} Long
113116
*/
114117
LongBitsPrototype.toLong = function toLong(unsigned) {
115-
return new util.Long(this.lo, this.hi, unsigned);
118+
return util.Long
119+
? new util.Long(this.lo, this.hi, unsigned)
120+
: { low: this.lo, high: this.hi, unsigned: Boolean(unsigned) };
116121
};
117122

118123
var charCodeAt = String.prototype.charCodeAt;

src/writer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ WriterPrototype.sfixed32 = function write_sfixed32(value) {
313313
*/
314314
WriterPrototype.fixed64 = function write_fixed64(value) {
315315
var bits = LongBits.from(value);
316-
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
316+
return this.push(writeFixed32, 4, bits.lo).push(writeFixed32, 4, bits.hi);
317317
};
318318

319319
/**

0 commit comments

Comments
 (0)