buffer: remove duplicated code in fromObject
Add fromArrayLike() to handle logic of copying in values from array-like argument. PR-URL: https://github.com/nodejs/node/pull/4948 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
25751bedfe
commit
c0bfac6ba9
@ -122,6 +122,13 @@ function fromString(string, encoding) {
|
|||||||
return b;
|
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) {
|
function fromObject(obj) {
|
||||||
if (obj instanceof Buffer) {
|
if (obj instanceof Buffer) {
|
||||||
@ -134,14 +141,6 @@ function fromObject(obj) {
|
|||||||
return b;
|
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) {
|
if (obj == null) {
|
||||||
throw new TypeError('Must start with number, buffer, array or string');
|
throw new TypeError('Must start with number, buffer, array or string');
|
||||||
}
|
}
|
||||||
@ -150,25 +149,15 @@ function fromObject(obj) {
|
|||||||
return binding.createFromArrayBuffer(obj);
|
return binding.createFromArrayBuffer(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.buffer instanceof ArrayBuffer || obj.length) {
|
if (obj.buffer instanceof ArrayBuffer || 'length' in obj) {
|
||||||
let length;
|
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
|
||||||
if (typeof obj.length !== 'number' || obj.length !== obj.length)
|
return allocate(0);
|
||||||
length = 0;
|
|
||||||
else
|
|
||||||
length = obj.length;
|
|
||||||
const b = allocate(length);
|
|
||||||
for (let i = 0; i < length; i++) {
|
|
||||||
b[i] = obj[i] & 255;
|
|
||||||
}
|
}
|
||||||
return b;
|
return fromArrayLike(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
||||||
var array = obj.data;
|
return fromArrayLike(obj.data);
|
||||||
const b = allocate(array.length);
|
|
||||||
for (let i = 0; i < array.length; i++)
|
|
||||||
b[i] = array[i] & 255;
|
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new TypeError('Must start with number, buffer, array or string');
|
throw new TypeError('Must start with number, buffer, array or string');
|
||||||
|
@ -28,6 +28,13 @@ var c = new Buffer(512);
|
|||||||
console.log('c.length == %d', c.length);
|
console.log('c.length == %d', c.length);
|
||||||
assert.strictEqual(512, c.length);
|
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.
|
// First check Buffer#fill() works as expected.
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user