Ensure shallow clones of typed arrays don't clone the buffer.

This commit is contained in:
John-David Dalton
2014-07-17 06:57:05 -07:00
parent fd03500786
commit c97c2fee37
2 changed files with 10 additions and 9 deletions

View File

@@ -1426,7 +1426,8 @@
if (Ctor instanceof Ctor) {
Ctor = ctorByClass[className];
}
return new Ctor(cloneBuffer(value.buffer), value.byteOffset, value.length);
var buffer = value.buffer;
return new Ctor(isDeep ? cloneBuffer(buffer) : buffer, value.byteOffset, value.length);
case numberClass:
case stringClass:

View File

@@ -616,7 +616,7 @@
if (root.Uint8Array) {
try {
var array = new Uint8Array(new ArrayBuffer(8));
actual = lodashBizarro.clone(array);
actual = lodashBizarro.cloneDeep(array);
} catch(e) {
actual = null;
}
@@ -1475,8 +1475,7 @@
_.each(['clone', 'cloneDeep'], function(methodName) {
var func = _[methodName],
isDeep = methodName == 'cloneDeep',
klass = new Klass;
isDeep = methodName == 'cloneDeep';
_.forOwn(objects, function(object, key) {
test('`_.' + methodName + '` should clone ' + key, 2, function() {
@@ -1520,14 +1519,14 @@
test('`_.' + methodName + '` should clone ' + type + ' arrays', 10, function() {
var Ctor = root[type];
if (Ctor) {
var array = new Ctor(new ArrayBuffer(24));
_.times(2, function(index) {
var actual = index ? func(array, 8, 1) : func(array);
var buffer = new ArrayBuffer(24),
array = index ? new Ctor(buffer, 8, 1) : new Ctor(buffer),
actual = func(array);
deepEqual(actual, array);
notStrictEqual(actual, array);
notStrictEqual(actual.buffer, array.buffer);
strictEqual(actual.buffer === array.buffer, !isDeep);
strictEqual(actual.byteOffset, array.byteOffset);
strictEqual(actual.length, array.length);
});
@@ -1558,7 +1557,8 @@
});
test('`_.' + methodName + '` should provide the correct `callback` arguments', 1, function() {
var argsList = [];
var argsList = [],
klass = new Klass;
func(klass, function() {
argsList.push(slice.call(arguments));