From 38c8c8afce97f6cc4b175b5517e6dd41ea6e03ea Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Tue, 24 Sep 2019 17:16:50 +0800 Subject: [PATCH 1/8] Fix bug --- lib/write-type.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/write-type.js b/lib/write-type.js index 94a10ab..c527743 100644 --- a/lib/write-type.js +++ b/lib/write-type.js @@ -51,30 +51,34 @@ function getWriteType(options) { token[type](encoder, value); } + function isInt(n){ + return Number(n) === n && n % 1 === 0; + } + function number(encoder, value) { - var ivalue = value | 0; var type; - if (value !== ivalue) { + if (isInt(value)) { + if (-0x20 <= value && value <= 0x7F) { + // positive fixint -- 0x00 - 0x7f + // negative fixint -- 0xe0 - 0xff + type = value & 0xFF; + } else if (0 <= value) { + // uint 8 -- 0xcc + // uint 16 -- 0xcd + // uint 32 -- 0xce + type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : 0xce; + } else { + // int 8 -- 0xd0 + // int 16 -- 0xd1 + // int 32 -- 0xd2 + type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : 0xd2; + } + token[type](encoder, value); + } else { // float 64 -- 0xcb type = 0xcb; token[type](encoder, value); - return; - } else if (-0x20 <= ivalue && ivalue <= 0x7F) { - // positive fixint -- 0x00 - 0x7f - // negative fixint -- 0xe0 - 0xff - type = ivalue & 0xFF; - } else if (0 <= ivalue) { - // uint 8 -- 0xcc - // uint 16 -- 0xcd - // uint 32 -- 0xce - type = (ivalue <= 0xFF) ? 0xcc : (ivalue <= 0xFFFF) ? 0xcd : 0xce; - } else { - // int 8 -- 0xd0 - // int 16 -- 0xd1 - // int 32 -- 0xd2 - type = (-0x80 <= ivalue) ? 0xd0 : (-0x8000 <= ivalue) ? 0xd1 : 0xd2; } - token[type](encoder, ivalue); } // uint 64 -- 0xcf From 5aa830e232f7e95c4e941084900bbdffebd210b9 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Wed, 25 Sep 2019 16:44:56 +0800 Subject: [PATCH 2/8] Fix negative number bug --- lib/write-type.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/write-type.js b/lib/write-type.js index c527743..3cccb0d 100644 --- a/lib/write-type.js +++ b/lib/write-type.js @@ -71,7 +71,7 @@ function getWriteType(options) { // int 8 -- 0xd0 // int 16 -- 0xd1 // int 32 -- 0xd2 - type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : 0xd2; + type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-2147483648 <= value) ? 0xd2 : 0xcb; } token[type](encoder, value); } else { From aca7b8fd7447743923098733556f43fcb0595838 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Thu, 26 Sep 2019 11:41:16 +0800 Subject: [PATCH 3/8] Use better code from marcinkowskip --- lib/write-type.js | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/write-type.js b/lib/write-type.js index 3cccb0d..c89f761 100644 --- a/lib/write-type.js +++ b/lib/write-type.js @@ -51,34 +51,29 @@ function getWriteType(options) { token[type](encoder, value); } - function isInt(n){ - return Number(n) === n && n % 1 === 0; - } - function number(encoder, value) { var type; - if (isInt(value)) { - if (-0x20 <= value && value <= 0x7F) { - // positive fixint -- 0x00 - 0x7f - // negative fixint -- 0xe0 - 0xff - type = value & 0xFF; - } else if (0 <= value) { - // uint 8 -- 0xcc - // uint 16 -- 0xcd - // uint 32 -- 0xce - type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : 0xce; - } else { - // int 8 -- 0xd0 - // int 16 -- 0xd1 - // int 32 -- 0xd2 - type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-2147483648 <= value) ? 0xd2 : 0xcb; - } - token[type](encoder, value); - } else { + if (Math.floor(value) != value) { // float 64 -- 0xcb type = 0xcb; token[type](encoder, value); + return; + } else if (-0x20 <= value && value <= 0x7F) { + // positive fixint -- 0x00 - 0x7f + // negative fixint -- 0xe0 - 0xff + type = value & 0xFF; + } else if (0 <= value) { + // uint 8 -- 0xcc + // uint 16 -- 0xcd + // uint 32 -- 0xce + type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf; + } else { + // int 8 -- 0xd0 + // int 16 -- 0xd1 + // int 32 -- 0xd2 + type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3; } + token[type](encoder, value); } // uint 64 -- 0xcf From f6884e6c3095cacc1a0bb832a6cff1e0b5561d11 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Fri, 29 Nov 2019 14:38:41 +0800 Subject: [PATCH 4/8] Downgrade zuul version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2e483ad..ba6e811 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "mocha": "^3.1.2", "msgpack.codec": "git+https://github.com/kawanet/msgpack-javascript.git#msgpack.codec", "uglify-js": "^2.7.3", - "zuul": "^3.11.1" + "zuul": "3.11.1" }, "homepage": "https://github.com/kawanet/msgpack-lite", "jshintConfig": { From aa9c702967105acd3de82ec658c8d61780687c5f Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Fri, 29 Nov 2019 14:47:14 +0800 Subject: [PATCH 5/8] Downgrade ip-regex --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ba6e811..2bb754b 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "mocha": "^3.1.2", "msgpack.codec": "git+https://github.com/kawanet/msgpack-javascript.git#msgpack.codec", "uglify-js": "^2.7.3", - "zuul": "3.11.1" + "zuul": "3.11.1", + "ip-regex": "1.0.0" }, "homepage": "https://github.com/kawanet/msgpack-lite", "jshintConfig": { From 0c44f7c0a790ef3183eb81cf0e611a8b51594f24 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Fri, 29 Nov 2019 15:06:02 +0800 Subject: [PATCH 6/8] Recovery package.json --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 2bb754b..2e483ad 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,7 @@ "mocha": "^3.1.2", "msgpack.codec": "git+https://github.com/kawanet/msgpack-javascript.git#msgpack.codec", "uglify-js": "^2.7.3", - "zuul": "3.11.1", - "ip-regex": "1.0.0" + "zuul": "^3.11.1" }, "homepage": "https://github.com/kawanet/msgpack-lite", "jshintConfig": { From 47fab6de48dbbdca9f18720f616f41dc97a44cd4 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Tue, 19 May 2020 22:33:19 +0800 Subject: [PATCH 7/8] Add testing for uint64 --- lib/write-type.js | 4 ++-- test/10.encode.js | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/write-type.js b/lib/write-type.js index c89f761..ac221ea 100644 --- a/lib/write-type.js +++ b/lib/write-type.js @@ -56,8 +56,6 @@ function getWriteType(options) { if (Math.floor(value) != value) { // float 64 -- 0xcb type = 0xcb; - token[type](encoder, value); - return; } else if (-0x20 <= value && value <= 0x7F) { // positive fixint -- 0x00 - 0x7f // negative fixint -- 0xe0 - 0xff @@ -66,11 +64,13 @@ function getWriteType(options) { // uint 8 -- 0xcc // uint 16 -- 0xcd // uint 32 -- 0xce + // uint 64 -- 0xcf type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf; } else { // int 8 -- 0xd0 // int 16 -- 0xd1 // int 32 -- 0xd2 + // int 64 -- 0xd3 type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3; } token[type](encoder, value); diff --git a/test/10.encode.js b/test/10.encode.js index cc1bfbb..a98e6fe 100755 --- a/test/10.encode.js +++ b/test/10.encode.js @@ -123,21 +123,23 @@ function run_tests(codecopt) { // uint 8 -- 0xcc // uint 16 -- 0xcd // uint 32 -- 0xce - // uint 64 -- 0xcf -- NOT SUPPORTED + // uint 64 -- 0xcf it("cc-cf: uint 8/16/32/64", function() { assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]); assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]); - assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF, options)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]); + assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFF, options)), [0xce, 0xFF, 0xFF, 0xFF, 0xFF]); + assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFFFFFFFFFF, options)), [0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); }); // int 8 -- 0xd0 // int 16 -- 0xd1 // int 32 -- 0xd2 - // int 64 -- 0xd3 -- NOT SUPPORTED + // int 64 -- 0xd3 it("d0-d3: int 8/16/32/64", function() { assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]); assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]); assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]); + assert.deepEqual(toArray(msgpack.encode(-0x8000000000000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); }); // str 8 -- 0xd9 From b9881b14e6676c40845c5b4fb7ac5de99f658954 Mon Sep 17 00:00:00 2001 From: Jimmy Huang Date: Wed, 20 May 2020 12:05:45 +0800 Subject: [PATCH 8/8] Fix testing of uint64. We can not support uint64 and int64 because the limit of javascript. --- test/10.encode.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/10.encode.js b/test/10.encode.js index a98e6fe..75373b4 100755 --- a/test/10.encode.js +++ b/test/10.encode.js @@ -123,23 +123,25 @@ function run_tests(codecopt) { // uint 8 -- 0xcc // uint 16 -- 0xcd // uint 32 -- 0xce - // uint 64 -- 0xcf + // uint 64 -- 0xcf -- NOT SUPPORTED it("cc-cf: uint 8/16/32/64", function() { assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]); assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]); assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFF, options)), [0xce, 0xFF, 0xFF, 0xFF, 0xFF]); - assert.deepEqual(toArray(msgpack.encode(0xFFFFFFFFFFFFFFFF, options)), [0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); + assert.deepEqual(toArray(msgpack.encode(0x8000000000000000, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + assert.notDeepEqual(toArray(msgpack.encode(0x8000000000000001, options)), [0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]); + assert.notDeepEqual(toArray(msgpack.encode(0xFFFFFFFFFFFFFFFF, options)), [0xcf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); }); // int 8 -- 0xd0 // int 16 -- 0xd1 // int 32 -- 0xd2 - // int 64 -- 0xd3 + // int 64 -- 0xd3 -- NOT SUPPORTED it("d0-d3: int 8/16/32/64", function() { assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]); assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]); assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]); - assert.deepEqual(toArray(msgpack.encode(-0x8000000000000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + assert.notDeepEqual(toArray(msgpack.encode(-0x80000001, options)), [0xd3, 0x80, 0x00, 0x00, 0x01]); }); // str 8 -- 0xd9