Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: refactor to remove some duplicated code in fromObject. #4948

Closed
wants to merge 8 commits into from
36 changes: 13 additions & 23 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -134,14 +141,6 @@ function fromObject(obj) {
return b;
}

if (Array.isArray(obj)) {
const length = obj.length;
const b = allocate(length);
for (let i = 0; i < length; i++)
b[i] = obj[i] & 255;
return b;
}

if (obj == null) {
throw new TypeError('Must start with number, buffer, array or string');
}
Expand All @@ -150,25 +149,16 @@ function fromObject(obj) {
return binding.createFromArrayBuffer(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 (obj.buffer instanceof ArrayBuffer || 'length' in obj) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return allocate(0);
} else {
return fromArrayLike(obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just an aside, all typed arrays could be more quickly handled in C++, but that doesn't concern this PR. at least now it's centralized and will be easier to make the change. :)

}
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');
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var c = new Buffer(512);
console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);

var d = new Buffer([]);
assert.strictEqual(0, d.length);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add the following:

var ui32 = new Uint32Array(4).fill(42);
var b = Buffer(ui32);
assert.deepEqual(ui32, b);

This is a test that should have been added previously, so is not technically part of this PR, but might as well add it since we're here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trevnorris , test is added.

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() {
Expand Down