Skip to content

Commit d9741b3

Browse files
committed
Merge branch 'fix-2875' (close PR #2876)
2 parents f5a9774 + 00d64f4 commit d9741b3

File tree

7 files changed

+52
-22
lines changed

7 files changed

+52
-22
lines changed

modules/_toDataView.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import getByteLength from './_getByteLength.js';
2+
3+
// Internal function to wrap or shallow-copy an ArrayBuffer,
4+
// typed array or DataView to a new DataView, reusing the buffer.
5+
export default function toDataView(bufferSource) {
6+
return new DataView(
7+
bufferSource.buffer || bufferSource,
8+
bufferSource.byteOffset,
9+
getByteLength(bufferSource)
10+
);
11+
}

modules/isEqual.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import isTypedArray from './isTypedArray.js';
55
import isFunction from './isFunction.js';
66
import keys from './keys.js';
77
import has from './_has.js';
8+
import toDataView from './_toDataView.js';
89

910
// Internal recursive comparison function for `_.isEqual`.
1011
function eq(a, b, aStack, bStack) {
@@ -53,12 +54,11 @@ function deepEq(a, b, aStack, bStack) {
5354
return SymbolProto.valueOf.call(a) === SymbolProto.valueOf.call(b);
5455
case '[object ArrayBuffer]':
5556
// Coerce to `DataView` so we can fall through to the next case.
56-
return deepEq(new DataView(a), new DataView(b), aStack, bStack);
57+
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
5758
case '[object DataView]':
5859
var byteLength = getByteLength(a);
59-
if (byteLength !== getByteLength(b)) {
60-
return false;
61-
}
60+
if (byteLength !== getByteLength(b)) return false;
61+
if (a.buffer === b.buffer && a.byteOffset === b.byteOffset) return true;
6262
while (byteLength--) {
6363
if (a.getUint8(byteLength) !== b.getUint8(byteLength)) {
6464
return false;
@@ -69,7 +69,7 @@ function deepEq(a, b, aStack, bStack) {
6969

7070
if (isTypedArray(a)) {
7171
// Coerce typed arrays to `DataView`.
72-
return deepEq(new DataView(a.buffer), new DataView(b.buffer), aStack, bStack);
72+
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
7373
}
7474

7575
var areArrays = className === '[object Array]';

test/objects.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,6 @@
583583
assert.strictEqual(_.isEqual(symbol, sameStringSymbol), false, 'Different symbols of same string are not equal');
584584
}
585585

586-
587-
588586
// typed arrays
589587
if (typeof ArrayBuffer !== 'undefined') {
590588
var u8 = new Uint8Array([1, 2]);
@@ -608,10 +606,13 @@
608606
assert.notOk(_.isEqual(new DataView(u8.buffer), new DataView(u16one.buffer)), 'Different DataViews with different byte data are not equal');
609607
assert.notOk(_.isEqual(u8.buffer, u16.buffer), 'Different ArrayBuffers with different length are not equal');
610608
assert.notOk(_.isEqual(u8.buffer, u16one.buffer), 'Different ArrayBuffers with different byte data are not equal');
611-
}
612609

613-
//assert.ok(_.isEqual(new DataView(u8.buffer)), new DataView(u8b.buffer))
614-
//assert.notOk(_.isEqual(new DataView((new Uint8Array([1,2])).buffer), new DataView((new Uint8Array([5,6,10])).buffer));
610+
// Regression tests for #2875.
611+
var shared = new Uint8Array([1, 2, 3, 4]);
612+
var view1 = new Uint8Array(shared.buffer, 0, 2);
613+
var view2 = new Uint8Array(shared.buffer, 2, 2);
614+
assert.notOk(_.isEqual(view1, view2), 'same buffer with different offset is not equal');
615+
}
615616
});
616617

617618
QUnit.test('isEmpty', function(assert) {

underscore-esm.js

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore-esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore.js

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)