Skip to content

Commit

Permalink
Refactor runtime util into separate file, reader/writer uses runtime …
Browse files Browse the repository at this point in the history
…util, fix packable float/double see #513
  • Loading branch information
dcodeIO committed Dec 2, 2016
1 parent ad5abe7 commit 23d6643
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 253 deletions.
263 changes: 116 additions & 147 deletions dist/protobuf.js

Large diffs are not rendered by default.

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.

31 changes: 2 additions & 29 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

/**
* Minimal static code generator runtime.
* Minimal static codegen runtime.
* @namespace
*/
var runtime = exports;
Expand All @@ -20,34 +20,7 @@ runtime.Writer = require("./src/writer");
* Runtime utility.
* @memberof runtime
*/
var util = runtime.util = {};

/**
* Converts a number or long to an 8 characters long hash string.
* @param {Long|number} value Value to convert
* @returns {string} Hash
*/
util.longToHash = function longToHash(value) {
return value
? LongBits.from(value).toHash()
: '\0\0\0\0\0\0\0\0';
};

/**
* Tests if two possibly long values are not equal.
* @param {number|Long} a First value
* @param {number|Long} b Second value
* @returns {boolean} `true` if not equal
*/
util.longNeq = function longNeq(a, b) {
return typeof a === 'number'
? typeof b === 'number'
? a !== b
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
: typeof b === 'number'
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
: a.low !== b.low || a.high !== b.high;
};
runtime.util = require("./src/util/runtime");

/**
* Resolves lazy type references.
Expand Down
2 changes: 1 addition & 1 deletion src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = Reader;

Reader.BufferReader = BufferReader;

var util = require("./util"),
var util = require("./util/runtime"),
ieee754 = require("../lib/ieee754");
var LongBits = util.LongBits,
Long = util.Long;
Expand Down
4 changes: 3 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ types.mapKey = bake([
* @type {Object.<string,number>}
*/
types.packed = bake([
/* double */ 1,
/* float */ 5,
/* int32 */ 0,
/* uint32 */ 0,
/* sint32 */ 0,
Expand All @@ -126,4 +128,4 @@ types.packed = bake([
/* fixed64 */ 1,
/* sfixed64 */ 1,
/* bool */ 0
], 2);
]);
72 changes: 3 additions & 69 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,8 @@
*/
var util = exports;

var LongBits =
util.LongBits = require("./util/longbits");
util.codegen = require("./util/codegen");

/**
* Whether running within node or not.
* @memberof util
* @type {boolean}
*/
var isNode = util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node);

/**
* Optional buffer class to use.
* If you assign any compatible buffer implementation to this property, the library will use it.
* @type {?Function}
*/
util.Buffer = null;

if (isNode)
try { util.Buffer = require("buffer").Buffer; } catch (e) {} // eslint-disable-line no-empty

/**
* Optional Long class to use.
* If you assign any compatible long implementation to this property, the library will use it.
* @type {?Function}
*/
util.Long = global.dcodeIO && global.dcodeIO.Long || null;

if (!util.Long)
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty

/**
* Tests if the specified value is a string.
* @memberof util
Expand Down Expand Up @@ -213,46 +184,6 @@ util.resolvePath = function resolvePath(originPath, importPath, alreadyNormalize
return originPath.length ? normalizePath(originPath + '/' + importPath) : importPath;
};

/**
* Converts a number or long to an 8 characters long hash string.
* @param {Long|number} value Value to convert
* @returns {string} Hash
*/
util.longToHash = function longToHash(value) {
return value
? LongBits.from(value).toHash()
: '\0\0\0\0\0\0\0\0';
};

/**
* Converts an 8 characters long hash string to a long or number.
* @param {string} hash Hash
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long|number} Original value
*/
util.longFromHash = function longFromHash(hash, unsigned) {
var bits = LongBits.fromHash(hash);
if (util.Long)
return util.Long.fromBits(bits.lo, bits.hi, unsigned);
return bits.toNumber(Boolean(unsigned));
};

/**
* Tests if two possibly long values are not equal.
* @param {number|Long} a First value
* @param {number|Long} b Second value
* @returns {boolean} `true` if not equal
*/
util.longNeq = function longNeq(a, b) {
return typeof a === 'number'
? typeof b === 'number'
? a !== b
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
: typeof b === 'number'
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
: a.low !== b.low || a.high !== b.high;
};

/**
* Merges the properties of the source object into the destination object.
* @param {Object} dst Destination object
Expand Down Expand Up @@ -292,3 +223,6 @@ util.safeProp = function safeProp(prop) {
util.newBuffer = function newBuffer(size) {
return new (util.Buffer || typeof Uint8Array !== 'undefined' && Uint8Array || Array)(size || 0);
};

// Merge in runtime utility
util.merge(util, require("./util/runtime"));
72 changes: 72 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";

var util = exports;

var LongBits = util.LongBits = require("./longbits");

/**
* Whether running within node or not.
* @memberof util
* @type {boolean}
*/
var isNode = util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node);

/**
* Optional buffer class to use.
* If you assign any compatible buffer implementation to this property, the library will use it.
* @type {?Function}
*/
util.Buffer = null;

if (isNode)
try { util.Buffer = require("buffer").Buffer; } catch (e) {} // eslint-disable-line no-empty

/**
* Optional Long class to use.
* If you assign any compatible long implementation to this property, the library will use it.
* @type {?Function}
*/
util.Long = global.dcodeIO && global.dcodeIO.Long || null;

if (!util.Long && isNode)
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty

/**
* Converts a number or long to an 8 characters long hash string.
* @param {Long|number} value Value to convert
* @returns {string} Hash
*/
util.longToHash = function longToHash(value) {
return value
? LongBits.from(value).toHash()
: '\0\0\0\0\0\0\0\0';
};

/**
* Converts an 8 characters long hash string to a long or number.
* @param {string} hash Hash
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long|number} Original value
*/
util.longFromHash = function longFromHash(hash, unsigned) {
var bits = LongBits.fromHash(hash);
if (util.Long)
return util.Long.fromBits(bits.lo, bits.hi, unsigned);
return bits.toNumber(Boolean(unsigned));
};

/**
* Tests if two possibly long values are not equal.
* @param {number|Long} a First value
* @param {number|Long} b Second value
* @returns {boolean} `true` if not equal
*/
util.longNeq = function longNeq(a, b) {
return typeof a === 'number'
? typeof b === 'number'
? a !== b
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
: typeof b === 'number'
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
: a.low !== b.low || a.high !== b.high;
};
2 changes: 1 addition & 1 deletion src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = Writer;

Writer.BufferWriter = BufferWriter;

var util = require("./util"),
var util = require("./util/runtime"),
ieee754 = require("../lib/ieee754");
var LongBits = util.LongBits;

Expand Down

0 comments on commit 23d6643

Please sign in to comment.