Permalink
Browse files

Fixed serialization order of fixed64, fallback to parseInt with no lo…

…ng lib, see #534
  • Loading branch information...
dcodeIO committed Dec 9, 2016
1 parent 47608dd commit 7def340833f9f1cc41f4835bd0d62e203b54d9eb

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +67 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -73,6 +73,7 @@ protobuf.parse = require("./parse");
// Serialization
protobuf.Writer = require("./writer");
protobuf.BufferWriter = protobuf.Writer.BufferWriter;
var Reader =
protobuf.Reader = require("./reader");
protobuf.BufferReader = protobuf.Reader.BufferReader;
protobuf.codegen = require("./codegen");
@@ -97,14 +98,25 @@ protobuf.Message = require("./message");
protobuf.types = require("./types");
protobuf.common = require("./common");
protobuf.rpc = require("./rpc");
var util =
protobuf.util = require("./util");
protobuf.configure = configure;
/**
* Reconfigures the library according to the environment.
* @returns {undefined}
*/
function configure() {
util._configure();
Reader._configure();
}
// Be nice to AMD
if (typeof define === 'function' && define.amd)
define(["long"], function(Long) {
if (Long) {
protobuf.util.Long = Long;
protobuf.Reader.configure();
configure();
}
return protobuf;
});
@@ -12,29 +12,6 @@ function indexOutOfRange(reader, writeLength) {
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
}
/**
* Configures the Reader interface according to the environment.
* @memberof Reader
* @returns {undefined}
*/
function configure() {
if (util.Long) {
ReaderPrototype.int64 = read_int64_long;
ReaderPrototype.uint64 = read_uint64_long;
ReaderPrototype.sint64 = read_sint64_long;
ReaderPrototype.fixed64 = read_fixed64_long;
ReaderPrototype.sfixed64 = read_sfixed64_long;
} else {
ReaderPrototype.int64 = read_int64_number;
ReaderPrototype.uint64 = read_uint64_number;
ReaderPrototype.sint64 = read_sint64_number;
ReaderPrototype.fixed64 = read_fixed64_number;
ReaderPrototype.sfixed64 = read_sfixed64_number;
}
}
Reader.configure = configure;
/**
* Constructs a new reader instance using the specified buffer.
* @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
@@ -628,4 +605,22 @@ BufferReaderPrototype.finish = function finish_buffer(buffer) {
return remain;
};
function configure() {
if (util.Long) {
ReaderPrototype.int64 = read_int64_long;
ReaderPrototype.uint64 = read_uint64_long;
ReaderPrototype.sint64 = read_sint64_long;
ReaderPrototype.fixed64 = read_fixed64_long;
ReaderPrototype.sfixed64 = read_sfixed64_long;
} else {
ReaderPrototype.int64 = read_int64_number;
ReaderPrototype.uint64 = read_uint64_number;
ReaderPrototype.sint64 = read_sint64_number;
ReaderPrototype.fixed64 = read_fixed64_number;
ReaderPrototype.sfixed64 = read_sfixed64_number;
}
}
Reader._configure = configure;
configure();
@@ -251,7 +251,13 @@ util.newBuffer = function newBuffer(size) {
: new (typeof Uint8Array !== 'undefined' && Uint8Array || Array)(size);
};
var runtime = require("./util/runtime");
util.EventEmitter = require("./util/eventemitter");
// Merge in runtime utility
util.merge(util, require("./util/runtime"));
util.merge(util, runtime);
util._configure = function configure() {
runtime.Long = util.Long;
};
@@ -78,14 +78,17 @@ LongBits.fromNumber = function fromNumber(value) {
* Constructs new long bits from a number, long or string.
* @param {Long|number|string} value Value
* @returns {util.LongBits} Instance
* @throws {TypeError} If `value` is a string and no long library is present.
*/
LongBits.from = function from(value) {
switch (typeof value) { // eslint-disable-line default-case
case 'number':
return LongBits.fromNumber(value);
case 'string':
value = util.Long.fromString(value); // throws without a long lib
if (util.Long)
value = util.Long.fromString(value);
// fallthrough
else
return LongBits.fromNumber(parseInt(value, 10));
}
return (value.low || value.high) && new LongBits(value.low >>> 0, value.high >>> 0) || zero;
};
@@ -112,7 +115,9 @@ LongBitsPrototype.toNumber = function toNumber(unsigned) {
* @returns {Long} Long
*/
LongBitsPrototype.toLong = function toLong(unsigned) {
return new util.Long(this.lo, this.hi, unsigned);
return util.Long
? new util.Long(this.lo, this.hi, unsigned)
: { low: this.lo, high: this.hi, unsigned: Boolean(unsigned) };
};
var charCodeAt = String.prototype.charCodeAt;
@@ -313,7 +313,7 @@ WriterPrototype.sfixed32 = function write_sfixed32(value) {
*/
WriterPrototype.fixed64 = function write_fixed64(value) {
var bits = LongBits.from(value);
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
return this.push(writeFixed32, 4, bits.lo).push(writeFixed32, 4, bits.hi);
};
/**
@@ -0,0 +1,39 @@
var tape = require("tape");
var protobuf = require("..");
tape.test("fixed64", function(test) {
var root = protobuf.Root.fromJSON({
nested: {
test: {
nested: {
Test: {
fields: {
int_64: {
type: 'fixed64',
id: 1
}
}
}
}
}
}
});
var Test = root.lookup("test.Test");
var buffer = Test.encode({
int_64: '314159265358979'
}).finish();
test.equal(buffer.length, 9, "should encode a total of 9 bytes");
test.equal(buffer[0], 9, "should encode id 1, wireType 1");
var decoded = Test.decode(buffer);
// decoded.int_64 is a Long here, so this implicitly calls Long#toString:
test.ok(decoded.int_64 == '314159265358979', "should decode back the original value");
test.end();
});
@@ -1,6 +1,6 @@
/*
* protobuf.js v6.1.0 TypeScript definitions
* Generated Fri, 09 Dec 2016 18:46:45 UTC
* Generated Fri, 09 Dec 2016 22:46:53 UTC
*/
declare module "protobufjs" {
@@ -435,6 +435,12 @@ declare module "protobufjs" {
*/
function loadSync(filename: (string|string[]), root?: Root): Root;
/**
* Reconfigures the library according to the environment.
* @returns {undefined}
*/
function configure(): void;
/**
* Constructs a new map field instance.
* @classdesc Reflected map field.
@@ -1004,13 +1010,6 @@ declare module "protobufjs" {
*/
constructor(buffer: Uint8Array);
/**
* Configures the Reader interface according to the environment.
* @memberof Reader
* @returns {undefined}
*/
static configure(): void;
/**
* Read buffer.
* @type {Uint8Array}
@@ -1727,7 +1726,6 @@ declare module "protobufjs" {
* Constructs new long bits from a number, long or string.
* @param {Long|number|string} value Value
* @returns {util.LongBits} Instance
* @throws {TypeError} If `value` is a string and no long library is present.
*/
static from(value: (Long|number|string)): util.LongBits;

0 comments on commit 7def340

Please sign in to comment.