Permalink
Browse files

Refactor runtime util into separate file, reader/writer uses runtime …

…util, fix packable float/double see #513
  • Loading branch information...
dcodeIO committed Dec 2, 2016
1 parent ad5abe7 commit 23d664384900eb65e44910def45f04be996fbba1
Showing with 203 additions and 253 deletions.
  1. +116 −147 dist/protobuf.js
  2. +1 −1 dist/protobuf.js.map
  3. +3 −3 dist/protobuf.min.js
  4. BIN dist/protobuf.min.js.gz
  5. +1 −1 dist/protobuf.min.js.map
  6. +2 −29 runtime.js
  7. +1 −1 src/reader.js
  8. +3 −1 src/types.js
  9. +3 −69 src/util.js
  10. +72 −0 src/util/runtime.js
  11. +1 −1 src/writer.js

Large diffs are not rendered by default.

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 -8 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,7 +1,7 @@
"use strict";

/**
* Minimal static code generator runtime.
* Minimal static codegen runtime.
* @namespace
*/
var runtime = exports;
@@ -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.
@@ -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;
@@ -115,6 +115,8 @@ types.mapKey = bake([
* @type {Object.<string,number>}
*/
types.packed = bake([
/* double */ 1,
/* float */ 5,
/* int32 */ 0,
/* uint32 */ 0,
/* sint32 */ 0,
@@ -126,4 +128,4 @@ types.packed = bake([
/* fixed64 */ 1,
/* sfixed64 */ 1,
/* bool */ 0
], 2);
]);
@@ -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
@@ -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
@@ -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"));
@@ -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;
};
@@ -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;

0 comments on commit 23d6643

Please sign in to comment.