Skip to content

Commit

Permalink
Fixed serialization order of fixed64, fallback to parseInt with no lo…
Browse files Browse the repository at this point in the history
…ng lib, see #534
  • Loading branch information
dcodeIO committed Dec 9, 2016
1 parent 47608dd commit 7def340
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 73 deletions.
78 changes: 48 additions & 30 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.

6 changes: 3 additions & 3 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.

14 changes: 13 additions & 1 deletion src/index.js
Expand Up @@ -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");
Expand All @@ -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;
});
41 changes: 18 additions & 23 deletions src/reader.js
Expand Up @@ -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`.
Expand Down Expand Up @@ -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();
8 changes: 7 additions & 1 deletion src/util.js
Expand Up @@ -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;
};
11 changes: 8 additions & 3 deletions src/util/longbits.js
Expand Up @@ -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;
};
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/writer.js
Expand Up @@ -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);
};

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/fixed64.js
@@ -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();

});
16 changes: 7 additions & 9 deletions types/protobuf.js.d.ts
@@ -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" {

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 7def340

Please sign in to comment.