From a84745c76d4dca580a89f00599db4259c202ba4d Mon Sep 17 00:00:00 2001 From: shaoshuai0102 Date: Tue, 27 Oct 2015 12:09:00 +0800 Subject: [PATCH 1/5] decode long with type --- lib/v2/decoder.js | 30 ++++++++++++++---------------- test/long.test.js | 35 +++++++++++++++++++++++++++++++++++ test/object.test.js | 10 ++++++++-- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 0029556..f6571a1 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -164,38 +164,36 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Number} * @api public */ -proto.readLong = function () { +proto.readLong = function (withType) { + var result; var code = this.byteBuffer.get(); // Compact long if (code >= 0xd8 && code <= 0xef) { // Longs between -8 and 15 are represented by a single octet in the range xd8 to xef. // value = (code - 0xe0) - return code - 0xe0; - } - if (code >= 0xf0 && code <= 0xff) { + result = code - 0xe0; + } else if (code >= 0xf0 && code <= 0xff) { // Longs between -2048 and 2047 are encoded in two octets with the leading byte in the range xf0 to xff. // value = ((code - 0xf8) << 8) + b0 - return ((code - 0xf8) << 8) + this.byteBuffer.get(); - } - if (code >= 0x38 && code <= 0x3f) { + result = ((code - 0xf8) << 8) + this.byteBuffer.get(); + } else if (code >= 0x38 && code <= 0x3f) { // Longs between -262144 and 262143 are encoded in three octets with the leading byte in the range x38 to x3f. // value = ((code - 0x3c) << 16) + (b1 << 8) + b0 var b1 = this.byteBuffer.get(); var b0 = this.byteBuffer.get(); - return ((code - 0x3c) << 16) + (b1 << 8) + b0; - } + result = ((code - 0x3c) << 16) + (b1 << 8) + b0; // ::= x77 b3 b2 b1 b0 # 32-bit integer cast to long - if (code === 0x77) { + } else if (code === 0x77) { // Longs between which fit into 32-bits are encoded in five octets with the leading byte x59. // value = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0 - return this.byteBuffer.getInt32(); - } - - if (code === 0x4c) { - return utils.handleLong(this.byteBuffer.getLong()); + result = this.byteBuffer.getInt32(); + } else if (code === 0x4c) { + result = utils.handleLong(this.byteBuffer.getLong()); + } else { + this.throwError('readLong', code); } - this.throwError('readLong', code); + return this.handleType('long', result, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/long.test.js b/test/long.test.js index 097bcbf..f2b5405 100644 --- a/test/long.test.js +++ b/test/long.test.js @@ -91,6 +91,23 @@ describe('long.test.js', function () { hessian.decode(utils.bytes('v1/long/2147483648')).should.equal(2147483648); }); + it('should decode with type', function () { + hessian.decode(utils.bytes('v1/long/-7'), true).should.eql({ + $class: 'long', + $: -7, + }); + + hessian.decode(utils.bytes('v1/long/262143'), true).should.eql({ + $class: 'long', + $: 262143, + }); + + hessian.decode(utils.bytes('v1/long/2147483648'), true).should.eql({ + $class: 'long', + $: 2147483648, + }); + }); + describe('v2.0', function () { it('should read compact long', function () { hessian.decode(new Buffer([0xe0]), '2.0').should.equal(0); @@ -295,4 +312,22 @@ describe('long.test.js', function () { hessian.decode(utils.bytes('v1/long/2147483648'), '2.0').should.equal(2147483648); }); }); + + it('should decode with type', function () { + hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0', true).should.eql({ + $class: 'long', + $: -262144, + }); + + hessian.decode(utils.bytes('v2/long/-2048'), '2.0', true).should.eql({ + $class: 'long', + $: -2048, + }); + + hessian.decode(utils.bytes('v2/long/2147483646'), '2.0', true).should.eql({ + $class: 'long', + $: 2147483646, + }); + }); + }); diff --git a/test/object.test.js b/test/object.test.js index 079d036..8c6f355 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -810,7 +810,10 @@ describe('object.test.js', function () { a0.should.eql({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { - value: 0 + value: { + $class: 'long', + $: 0, + }, } }); hessian.encode({ @@ -830,7 +833,10 @@ describe('object.test.js', function () { a1.should.eql({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { - value: 1 + value: { + $class: 'long', + $: 1, + }, } }); hessian.encode({ From 70184e09b6bb8ed080c5d660d4fe628712096072 Mon Sep 17 00:00:00 2001 From: shaoshuai0102 Date: Tue, 27 Oct 2015 15:44:18 +0800 Subject: [PATCH 2/5] decode double with type --- lib/v2/decoder.js | 36 +++++++++++++++++------------------- test/double.test.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index f6571a1..1f150a6 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -212,29 +212,27 @@ utils.addByteCodes(BYTE_CODES, [ * @return {Number} * @api public */ -proto.readDouble = function () { +proto.readDouble = function (withType) { + var result; var code = this.byteBuffer.get(); if (code === 0x44) { - return this.byteBuffer.getDouble(); - } - + result = this.byteBuffer.getDouble(); // Compact double - if (code === 0x67) { - return 0.0; - } - if (code === 0x68) { - return 1.0; - } - if (code === 0x69) { - return this.byteBuffer.getInt8(); - } - if (code === 0x6a) { - return this.byteBuffer.getInt16(); - } - if (code === 0x6b) { - return this.byteBuffer.getFloat(); + } else if (code === 0x67) { + result = 0.0; + } else if (code === 0x68) { + result = 1.0; + } else if (code === 0x69) { + result = this.byteBuffer.getInt8(); + } else if (code === 0x6a) { + result = this.byteBuffer.getInt16(); + } else if (code === 0x6b) { + result = this.byteBuffer.getFloat(); + } else { + this.throwError('readDouble', code); } - this.throwError('readDouble', code); + + return this.handleType('double', result, withType); }; utils.addByteCodes(BYTE_CODES, [ diff --git a/test/double.test.js b/test/double.test.js index 55fd815..7cfdc4e 100644 --- a/test/double.test.js +++ b/test/double.test.js @@ -102,6 +102,23 @@ describe('double.test.js', function () { hessian.decode(utils.bytes('v1/double/2147483646.456'), '1.0').should.equal(2147483646.456); }); + it('should decode with type', function () { + hessian.decode(utils.bytes('v1/double/0'), '1.0', true).should.eql({ + $class: 'double', + $: 0, + }); + + hessian.decode(utils.bytes('v1/double/-127.9999'), '1.0', true).should.eql({ + $class: 'double', + $: -127.9999, + }); + + hessian.decode(utils.bytes('v1/double/-2147483647'), '1.0', true).should.eql({ + $class: 'double', + $: -2147483647, + }); + }); + describe('v2.0', function () { it('should read 0.0 and 1.0', function () { hessian.decode(new Buffer([0x67]), '2.0').should.equal(0.0); @@ -133,6 +150,23 @@ describe('double.test.js', function () { hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0').should.equal(10.0); }); + it('should decode with type', function () { + hessian.decode(new Buffer([0x69, 0x01]), '2.0', true).should.eql({ + $class: 'double', + $: 1.0, + }); + + hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true).should.eql({ + $class: 'double', + $: 10.0, + }); + + hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0', true).should.eql({ + $class: 'double', + $: 128.0, + }); + }); + it('should write 0.0 and 1.0', function () { hessian.encode(java.double(0), '2.0').should.eql(new Buffer([0x67])); hessian.encode(java.double(0.0), '2.0').should.eql(new Buffer([0x67])); From 48f263be2bcbb0d8f80c54b22f9bca4018330fa6 Mon Sep 17 00:00:00 2001 From: shaoshuai0102 Date: Tue, 27 Oct 2015 15:44:58 +0800 Subject: [PATCH 3/5] decode map with type --- lib/v2/decoder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 1f150a6..2741ed3 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -752,7 +752,7 @@ proto.readMap = function (withType) { if (!type) { var map = {}; this._addRef(map); - this._readMap(map); + this._readMap(map, withType); return map; } From d9fa33a04dea4b2e3bf550a1711454f5a1d6d6bd Mon Sep 17 00:00:00 2001 From: shaoshuai0102 Date: Thu, 29 Oct 2015 17:49:04 +0800 Subject: [PATCH 4/5] chore: update .travis.yml --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ee8c329..9956901 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,10 @@ +sudo: false language: node_js node_js: - - 'iojs-1' + - '4' + - '3' + - '2' + - '1' - '0.12' - '0.11' - '0.10' From 4d8d8c027f8c9aa895240b5a00fd85216dac3e74 Mon Sep 17 00:00:00 2001 From: shaoshuai0102 Date: Fri, 30 Oct 2015 15:08:33 +0800 Subject: [PATCH 5/5] test: disable timeout of mocha in string.test because large file processing in node4 takes too long --- test/string.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/string.test.js b/test/string.test.js index c2e80ba..cdd5a5f 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -19,6 +19,8 @@ var hessian = require('../'); var utils = require('./utils'); describe('string.test.js', function () { + this.timeout(0); + var helloBuffer = Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), new Buffer('hello')]);