Skip to content

Commit 23d6643

Browse files
committed
Refactor runtime util into separate file, reader/writer uses runtime util, fix packable float/double see #513
1 parent ad5abe7 commit 23d6643

File tree

11 files changed

+203
-253
lines changed

11 files changed

+203
-253
lines changed

dist/protobuf.js

Lines changed: 116 additions & 147 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

-8 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.

runtime.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
/**
4-
* Minimal static code generator runtime.
4+
* Minimal static codegen runtime.
55
* @namespace
66
*/
77
var runtime = exports;
@@ -20,34 +20,7 @@ runtime.Writer = require("./src/writer");
2020
* Runtime utility.
2121
* @memberof runtime
2222
*/
23-
var util = runtime.util = {};
24-
25-
/**
26-
* Converts a number or long to an 8 characters long hash string.
27-
* @param {Long|number} value Value to convert
28-
* @returns {string} Hash
29-
*/
30-
util.longToHash = function longToHash(value) {
31-
return value
32-
? LongBits.from(value).toHash()
33-
: '\0\0\0\0\0\0\0\0';
34-
};
35-
36-
/**
37-
* Tests if two possibly long values are not equal.
38-
* @param {number|Long} a First value
39-
* @param {number|Long} b Second value
40-
* @returns {boolean} `true` if not equal
41-
*/
42-
util.longNeq = function longNeq(a, b) {
43-
return typeof a === 'number'
44-
? typeof b === 'number'
45-
? a !== b
46-
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
47-
: typeof b === 'number'
48-
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
49-
: a.low !== b.low || a.high !== b.high;
50-
};
23+
runtime.util = require("./src/util/runtime");
5124

5225
/**
5326
* Resolves lazy type references.

src/reader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = Reader;
33

44
Reader.BufferReader = BufferReader;
55

6-
var util = require("./util"),
6+
var util = require("./util/runtime"),
77
ieee754 = require("../lib/ieee754");
88
var LongBits = util.LongBits,
99
Long = util.Long;

src/types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ types.mapKey = bake([
115115
* @type {Object.<string,number>}
116116
*/
117117
types.packed = bake([
118+
/* double */ 1,
119+
/* float */ 5,
118120
/* int32 */ 0,
119121
/* uint32 */ 0,
120122
/* sint32 */ 0,
@@ -126,4 +128,4 @@ types.packed = bake([
126128
/* fixed64 */ 1,
127129
/* sfixed64 */ 1,
128130
/* bool */ 0
129-
], 2);
131+
]);

src/util.js

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,8 @@
66
*/
77
var util = exports;
88

9-
var LongBits =
10-
util.LongBits = require("./util/longbits");
119
util.codegen = require("./util/codegen");
1210

13-
/**
14-
* Whether running within node or not.
15-
* @memberof util
16-
* @type {boolean}
17-
*/
18-
var isNode = util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node);
19-
20-
/**
21-
* Optional buffer class to use.
22-
* If you assign any compatible buffer implementation to this property, the library will use it.
23-
* @type {?Function}
24-
*/
25-
util.Buffer = null;
26-
27-
if (isNode)
28-
try { util.Buffer = require("buffer").Buffer; } catch (e) {} // eslint-disable-line no-empty
29-
30-
/**
31-
* Optional Long class to use.
32-
* If you assign any compatible long implementation to this property, the library will use it.
33-
* @type {?Function}
34-
*/
35-
util.Long = global.dcodeIO && global.dcodeIO.Long || null;
36-
37-
if (!util.Long)
38-
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty
39-
4011
/**
4112
* Tests if the specified value is a string.
4213
* @memberof util
@@ -213,46 +184,6 @@ util.resolvePath = function resolvePath(originPath, importPath, alreadyNormalize
213184
return originPath.length ? normalizePath(originPath + '/' + importPath) : importPath;
214185
};
215186

216-
/**
217-
* Converts a number or long to an 8 characters long hash string.
218-
* @param {Long|number} value Value to convert
219-
* @returns {string} Hash
220-
*/
221-
util.longToHash = function longToHash(value) {
222-
return value
223-
? LongBits.from(value).toHash()
224-
: '\0\0\0\0\0\0\0\0';
225-
};
226-
227-
/**
228-
* Converts an 8 characters long hash string to a long or number.
229-
* @param {string} hash Hash
230-
* @param {boolean} [unsigned=false] Whether unsigned or not
231-
* @returns {Long|number} Original value
232-
*/
233-
util.longFromHash = function longFromHash(hash, unsigned) {
234-
var bits = LongBits.fromHash(hash);
235-
if (util.Long)
236-
return util.Long.fromBits(bits.lo, bits.hi, unsigned);
237-
return bits.toNumber(Boolean(unsigned));
238-
};
239-
240-
/**
241-
* Tests if two possibly long values are not equal.
242-
* @param {number|Long} a First value
243-
* @param {number|Long} b Second value
244-
* @returns {boolean} `true` if not equal
245-
*/
246-
util.longNeq = function longNeq(a, b) {
247-
return typeof a === 'number'
248-
? typeof b === 'number'
249-
? a !== b
250-
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
251-
: typeof b === 'number'
252-
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
253-
: a.low !== b.low || a.high !== b.high;
254-
};
255-
256187
/**
257188
* Merges the properties of the source object into the destination object.
258189
* @param {Object} dst Destination object
@@ -292,3 +223,6 @@ util.safeProp = function safeProp(prop) {
292223
util.newBuffer = function newBuffer(size) {
293224
return new (util.Buffer || typeof Uint8Array !== 'undefined' && Uint8Array || Array)(size || 0);
294225
};
226+
227+
// Merge in runtime utility
228+
util.merge(util, require("./util/runtime"));

src/util/runtime.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use strict";
2+
3+
var util = exports;
4+
5+
var LongBits = util.LongBits = require("./longbits");
6+
7+
/**
8+
* Whether running within node or not.
9+
* @memberof util
10+
* @type {boolean}
11+
*/
12+
var isNode = util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node);
13+
14+
/**
15+
* Optional buffer class to use.
16+
* If you assign any compatible buffer implementation to this property, the library will use it.
17+
* @type {?Function}
18+
*/
19+
util.Buffer = null;
20+
21+
if (isNode)
22+
try { util.Buffer = require("buffer").Buffer; } catch (e) {} // eslint-disable-line no-empty
23+
24+
/**
25+
* Optional Long class to use.
26+
* If you assign any compatible long implementation to this property, the library will use it.
27+
* @type {?Function}
28+
*/
29+
util.Long = global.dcodeIO && global.dcodeIO.Long || null;
30+
31+
if (!util.Long && isNode)
32+
try { util.Long = require("long"); } catch (e) {} // eslint-disable-line no-empty
33+
34+
/**
35+
* Converts a number or long to an 8 characters long hash string.
36+
* @param {Long|number} value Value to convert
37+
* @returns {string} Hash
38+
*/
39+
util.longToHash = function longToHash(value) {
40+
return value
41+
? LongBits.from(value).toHash()
42+
: '\0\0\0\0\0\0\0\0';
43+
};
44+
45+
/**
46+
* Converts an 8 characters long hash string to a long or number.
47+
* @param {string} hash Hash
48+
* @param {boolean} [unsigned=false] Whether unsigned or not
49+
* @returns {Long|number} Original value
50+
*/
51+
util.longFromHash = function longFromHash(hash, unsigned) {
52+
var bits = LongBits.fromHash(hash);
53+
if (util.Long)
54+
return util.Long.fromBits(bits.lo, bits.hi, unsigned);
55+
return bits.toNumber(Boolean(unsigned));
56+
};
57+
58+
/**
59+
* Tests if two possibly long values are not equal.
60+
* @param {number|Long} a First value
61+
* @param {number|Long} b Second value
62+
* @returns {boolean} `true` if not equal
63+
*/
64+
util.longNeq = function longNeq(a, b) {
65+
return typeof a === 'number'
66+
? typeof b === 'number'
67+
? a !== b
68+
: (a = LongBits.fromNumber(a)).lo !== b.low || a.hi !== b.high
69+
: typeof b === 'number'
70+
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
71+
: a.low !== b.low || a.high !== b.high;
72+
};

0 commit comments

Comments
 (0)