From 88f2a9f7abe9eb14131ad1349a4b365ec50f4b54 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Fri, 29 Jan 2016 18:15:32 +0800 Subject: [PATCH 1/8] buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 33bea88d9b6fdd..d11ec4f59f759c 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -134,7 +134,11 @@ function fromObject(obj) { return b; } - if (Array.isArray(obj)) { + if (obj == null) { + throw new TypeError('Must start with number, buffer, array or string'); + } + + function fromArrayLike(obj) { const length = obj.length; const b = allocate(length); for (let i = 0; i < length; i++) @@ -142,8 +146,8 @@ function fromObject(obj) { return b; } - if (obj == null) { - throw new TypeError('Must start with number, buffer, array or string'); + if (Array.isArray(obj)) { + return fromArrayLike(obj); } if (obj instanceof ArrayBuffer) { @@ -151,24 +155,15 @@ function fromObject(obj) { } if (obj.buffer instanceof ArrayBuffer || obj.length) { - let length; - if (typeof obj.length !== 'number' || obj.length !== obj.length) - length = 0; - else - length = obj.length; - const b = allocate(length); - for (let i = 0; i < length; i++) { - b[i] = obj[i] & 255; + if (typeof obj.length !== 'number' || obj.length !== obj.length) { + return allocate(0); + } else { + return fromArrayLike(obj); } - return b; } if (obj.type === 'Buffer' && Array.isArray(obj.data)) { - var array = obj.data; - const b = allocate(array.length); - for (let i = 0; i < array.length; i++) - b[i] = array[i] & 255; - return b; + return fromArrayLike(obj.data); } throw new TypeError('Must start with number, buffer, array or string'); From cdaa4ea4093c0c1d47df323e7062af5eabe21d96 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Sun, 31 Jan 2016 14:41:24 +0800 Subject: [PATCH 2/8] fixup! buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index d11ec4f59f759c..e8d692f2586209 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -122,6 +122,13 @@ function fromString(string, encoding) { return b; } +function fromArrayLike(obj) { + const length = obj.length; + const b = allocate(length); + for (let i = 0; i < length; i++) + b[i] = obj[i] & 255; + return b; +} function fromObject(obj) { if (obj instanceof Buffer) { @@ -138,14 +145,6 @@ function fromObject(obj) { throw new TypeError('Must start with number, buffer, array or string'); } - function fromArrayLike(obj) { - const length = obj.length; - const b = allocate(length); - for (let i = 0; i < length; i++) - b[i] = obj[i] & 255; - return b; - } - if (Array.isArray(obj)) { return fromArrayLike(obj); } From c5a886b04b198feee41abccd8e6e3e59cc8901c7 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Mon, 1 Feb 2016 14:29:16 +0800 Subject: [PATCH 3/8] fixup! buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index e8d692f2586209..f2b4d01179edfc 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -141,13 +141,14 @@ function fromObject(obj) { return b; } + if (Array.isArray(obj)) { + return fromArrayLike(obj); + } + if (obj == null) { throw new TypeError('Must start with number, buffer, array or string'); } - if (Array.isArray(obj)) { - return fromArrayLike(obj); - } if (obj instanceof ArrayBuffer) { return binding.createFromArrayBuffer(obj); From e97323d380d3b46145e95758dc70bb182357722a Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Mon, 1 Feb 2016 16:52:08 +0800 Subject: [PATCH 4/8] fixup! buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index f2b4d01179edfc..db488a4c8b3c83 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -141,10 +141,6 @@ function fromObject(obj) { return b; } - if (Array.isArray(obj)) { - return fromArrayLike(obj); - } - if (obj == null) { throw new TypeError('Must start with number, buffer, array or string'); } @@ -169,7 +165,6 @@ function fromObject(obj) { throw new TypeError('Must start with number, buffer, array or string'); } - // Static methods Buffer.isBuffer = function isBuffer(b) { From 980f20800baec9033385e1c89a8f332146eb4000 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Mon, 1 Feb 2016 16:54:35 +0800 Subject: [PATCH 5/8] fixup! fixup! buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index db488a4c8b3c83..fedcf759c07ec1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -145,7 +145,6 @@ function fromObject(obj) { throw new TypeError('Must start with number, buffer, array or string'); } - if (obj instanceof ArrayBuffer) { return binding.createFromArrayBuffer(obj); } From ab15735a59f5fc5971d15982e8b21f373e6c3fd9 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Mon, 1 Feb 2016 17:11:08 +0800 Subject: [PATCH 6/8] fixup! fixup! fixup! buffer: refactor to remove some duplicated code in fromObject. --- lib/buffer.js | 3 ++- test/parallel/test-buffer.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index fedcf759c07ec1..ab49fa2a6b7f01 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -149,7 +149,7 @@ function fromObject(obj) { return binding.createFromArrayBuffer(obj); } - if (obj.buffer instanceof ArrayBuffer || obj.length) { + if (obj.buffer instanceof ArrayBuffer || 'length' in obj) { if (typeof obj.length !== 'number' || obj.length !== obj.length) { return allocate(0); } else { @@ -164,6 +164,7 @@ function fromObject(obj) { throw new TypeError('Must start with number, buffer, array or string'); } + // Static methods Buffer.isBuffer = function isBuffer(b) { diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 6ab84fd8809289..d20dbff51f9e95 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -28,6 +28,10 @@ var c = new Buffer(512); console.log('c.length == %d', c.length); assert.strictEqual(512, c.length); +var d = new Buffer([]); +console.log('d.length == %d', d.length); +assert.strictEqual(0, d.length); + // First check Buffer#fill() works as expected. assert.throws(function() { From e16e88bd8308b520be24763a60321236451ebdd2 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Mon, 1 Feb 2016 17:25:04 +0800 Subject: [PATCH 7/8] fixup! fixup! fixup! fixup! buffer: refactor to remove some duplicated code in fromObject. --- test/parallel/test-buffer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index d20dbff51f9e95..9d79dd3158c5f0 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -29,7 +29,6 @@ console.log('c.length == %d', c.length); assert.strictEqual(512, c.length); var d = new Buffer([]); -console.log('d.length == %d', d.length); assert.strictEqual(0, d.length); // First check Buffer#fill() works as expected. From b274fe16066bc18e84b07e7e5b67a13e1ea8b31e Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Tue, 2 Feb 2016 09:46:49 +0800 Subject: [PATCH 8/8] fixup! fixup! fixup! fixup! fixup! buffer: refactor to remove some duplicated code in fromObject. --- test/parallel/test-buffer.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 9d79dd3158c5f0..2e85fe556d78d1 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -31,6 +31,10 @@ assert.strictEqual(512, c.length); var d = new Buffer([]); assert.strictEqual(0, d.length); +var ui32 = new Uint32Array(4).fill(42); +var e = Buffer(ui32); +assert.deepEqual(ui32, e); + // First check Buffer#fill() works as expected. assert.throws(function() {