Skip to content

Commit a7a3f92

Browse files
gxcsoccerdead-horse
authored andcommitted
fix: support writeLong parameter is a Long object (#96)
1 parent 445d5f2 commit a7a3f92

4 files changed

Lines changed: 70 additions & 29 deletions

File tree

.autod.conf.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
module.exports = {
4+
write: true,
5+
prefix: '^',
6+
devprefix: '^',
7+
exclude: [],
8+
devdep: [
9+
'autod',
10+
'mm',
11+
'jshint',
12+
'mocha',
13+
'istanbul',
14+
],
15+
semver: [
16+
'mocha@3',
17+
],
18+
};

lib/v2/encoder.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
var debug = require('debug')('hessian:v2:encoder');
1818
var is = require('is-type-of');
1919
var util = require('util');
20-
var EncoderV1 = require('../v1/encoder');
21-
var javaObject = require('../object');
20+
var Long = require('long');
2221
var utility = require('utility');
22+
var javaObject = require('../object');
23+
var EncoderV1 = require('../v1/encoder');
2324
var supportES6Map = require('../utils').supportES6Map;
2425

2526
function Encoder(options) {
@@ -142,23 +143,35 @@ proto.writeInt = function (val) {
142143
* ```
143144
*/
144145
proto.writeLong = function (val) {
145-
if (typeof val !== 'number' && utility.isSafeNumberString(val)) {
146-
val = Number(val);
146+
if (typeof val === 'string') {
147+
if (!utility.isSafeNumberString(val)) {
148+
val = Long.fromString(val);
149+
} else {
150+
val = Number(val);
151+
}
152+
} else if (Long.isLong(val) && (val.high === 0 || val.high === -1)) {
153+
val = val.toNumber();
147154
}
148155

149-
if (val >= -8 && val <= 15) {
150-
this.byteBuffer.put(val + 0xe0);
151-
} else if (val >= -2048 && val <= 2047) {
152-
var b0 = val & 0xff;
153-
var code = (val >> 8) + 0xf8;
154-
this.byteBuffer.put(code).put(b0);
155-
} else if (val >= -262144 && val <= 262143) {
156-
var b1b0 = val & 0xffff;
157-
var code = (val >> 16) + 0x3c;
158-
this.byteBuffer.put(code).putUInt16(b1b0);
159-
} else if (val >= -0x80000000 && val <= 0x7fffffff) {
160-
// 32-bit integer cast to long
161-
this.byteBuffer.put(0x59).putInt32(val);
156+
if (typeof val === 'number') {
157+
if (val >= -8 && val <= 15) {
158+
this.byteBuffer.put(val + 0xe0);
159+
} else if (val >= -2048 && val <= 2047) {
160+
var b0 = val & 0xff;
161+
var code = (val >> 8) + 0xf8;
162+
this.byteBuffer.put(code).put(b0);
163+
} else if (val >= -262144 && val <= 262143) {
164+
var b1b0 = val & 0xffff;
165+
var code = (val >> 16) + 0x3c;
166+
this.byteBuffer.put(code).putUInt16(b1b0);
167+
} else if (val >= -0x80000000 && val <= 0x7fffffff) {
168+
// 32-bit integer cast to long
169+
this.byteBuffer.put(0x59).putInt32(val);
170+
} else {
171+
this.byteBuffer
172+
.put(0x4c) // 'L'
173+
.putLong(val);
174+
}
162175
} else {
163176
this.byteBuffer
164177
.put(0x4c) // 'L'

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"test-cov": "istanbul cover _mocha -- -t 15000 test/*.test.js",
1313
"lint": "jshint .",
1414
"ci": "npm run lint && npm run test-cov",
15-
"autod": "autod -w --prefix '~' -e benchmark",
15+
"autod": "autod",
1616
"benchmark": "node benchmark/encode.js && node benchmark/decode.js"
1717
},
1818
"repository": {
@@ -34,20 +34,21 @@
3434
},
3535
"homepage": "https://github.com/node-modules/hessian.js",
3636
"dependencies": {
37-
"byte": "^1.1.6",
38-
"debug": "^2.6.8",
39-
"is-type-of": "^1.1.0",
37+
"byte": "^1.3.0",
38+
"debug": "^3.1.0",
39+
"is-type-of": "^1.2.0",
4040
"long": "^3.2.0",
41-
"utility": "^1.12.0"
41+
"utility": "^1.13.1"
4242
},
4343
"devDependencies": {
44-
"autod": "*",
45-
"beautify-benchmark": "*",
46-
"benchmark": "*",
47-
"istanbul": "*",
48-
"js-to-java": "2",
49-
"jshint": "*",
50-
"mocha": "2"
44+
"autod": "^3.0.1",
45+
"beautify-benchmark": "^0.2.4",
46+
"benchmark": "^2.1.4",
47+
"istanbul": "^0.4.5",
48+
"js-to-java": "^2.4.0",
49+
"jshint": "^2.9.5",
50+
"mm": "^2.2.0",
51+
"mocha": "^3.5.3"
5152
},
5253
"engines": {
5354
"node": ">= 0.12.0"

test/long.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ describe('long.test.js', function () {
4949
});
5050

5151
it('should write and read equal java impl', function () {
52+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(15)), '2.0'), utils.bytes('v2/long/15'));
53+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-8)), '2.0'), utils.bytes('v2/long/-8'));
54+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-2048)), '2.0'), utils.bytes('v2/long/-2048'));
55+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(2047)), '2.0'), utils.bytes('v2/long/2047'));
56+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-262144)), '2.0'), utils.bytes('v2/long/-262144'));
57+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(262143)), '2.0'), utils.bytes('v2/long/262143'));
58+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-2147483648)), '2.0'), utils.bytes('v2/long/-2147483648'));
59+
assert.deepEqual(hessian.encode(java.long(Long.fromNumber(2147483647)), '2.0'), utils.bytes('v2/long/2147483647'));
60+
5261
assert.deepEqual(hessian.encode(java.long(0), '1.0'), utils.bytes('v1/long/0'));
5362
assert(hessian.decode(utils.bytes('v1/long/0')) === 0);
5463
assert.deepEqual(hessian.encode(java.long(-8), '1.0'), utils.bytes('v1/long/-8'));

0 commit comments

Comments
 (0)