Skip to content

Commit 439b75b

Browse files
calebsanderBridgeAR
authored andcommitted
assert, util: *DeepEqual() handles ArrayBuffers
Previously, all ArrayBuffers were considered equal in assert.deepEqual() and assert.deepStrictEqual(). Now, ArrayBuffers and SharedArrayBuffers must have the same byte lengths and contents to be considered equal. In loose mode, an ArrayBuffer is considered equal to a SharedArrayBuffer if they have the same contents, whereas in strict mode, the buffers must be both ArrayBuffers or both SharedArrayBuffers. PR-URL: #22266 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6bc43ae commit 439b75b

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/internal/util/comparisons.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
const { compare } = process.binding('buffer');
44
const { isArrayBufferView } = require('internal/util/types');
55
const { internalBinding } = require('internal/bootstrap/loaders');
6-
const { isDate, isMap, isRegExp, isSet } = internalBinding('types');
6+
const {
7+
isAnyArrayBuffer,
8+
isDate,
9+
isMap,
10+
isRegExp,
11+
isSet
12+
} = internalBinding('types');
713
const { getOwnNonIndexProperties } = process.binding('util');
814

915
const ReflectApply = Reflect.apply;
@@ -55,6 +61,11 @@ function areSimilarTypedArrays(a, b) {
5561
new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
5662
}
5763

64+
function areEqualArrayBuffers(buf1, buf2) {
65+
return buf1.byteLength === buf2.byteLength &&
66+
compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;
67+
}
68+
5869
function isFloatTypedArrayTag(tag) {
5970
return tag === '[object Float32Array]' || tag === '[object Float64Array]';
6071
}
@@ -154,6 +165,10 @@ function strictDeepEqual(val1, val2, memos) {
154165
return false;
155166
}
156167
return keyCheck(val1, val2, kStrict, memos, kIsMap);
168+
} else if (isAnyArrayBuffer(val1)) {
169+
if (!areEqualArrayBuffers(val1, val2)) {
170+
return false;
171+
}
157172
// TODO: Make the valueOf checks safe.
158173
} else if (typeof val1.valueOf === 'function') {
159174
const val1Value = val1.valueOf();
@@ -217,6 +232,11 @@ function looseDeepEqual(val1, val2, memos) {
217232
} else if (isSet(val2) || isMap(val2)) {
218233
return false;
219234
}
235+
if (isAnyArrayBuffer(val1) && isAnyArrayBuffer(val2)) {
236+
if (!areEqualArrayBuffers(val1, val2)) {
237+
return false;
238+
}
239+
}
220240
return keyCheck(val1, val2, kLoose, memos, kNoIterator);
221241
}
222242

test/parallel/test-assert-typedarray-deepequal.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ const equalArrayPairs = [
2323
[new Float32Array([+0.0]), new Float32Array([+0.0])],
2424
[new Uint8Array([1, 2, 3, 4]).subarray(1), new Uint8Array([2, 3, 4])],
2525
[new Uint16Array([1, 2, 3, 4]).subarray(1), new Uint16Array([2, 3, 4])],
26-
[new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])]
26+
[new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])],
27+
[new ArrayBuffer(3), new ArrayBuffer(3)],
28+
[new SharedArrayBuffer(3), new SharedArrayBuffer(3)]
2729
];
2830

2931
const looseEqualArrayPairs = [
3032
[new Float64Array([+0.0]), new Float32Array([-0.0])],
3133
[new Int16Array(256), new Uint16Array(256)],
3234
[new Int16Array([256]), new Uint16Array([256])],
3335
[new Float32Array([+0.0]), new Float32Array([-0.0])],
34-
[new Float64Array([+0.0]), new Float64Array([-0.0])]
36+
[new Float64Array([+0.0]), new Float64Array([-0.0])],
37+
[new ArrayBuffer(3), new SharedArrayBuffer(3)]
3538
];
3639

3740
const notEqualArrayPairs = [
@@ -44,7 +47,19 @@ const notEqualArrayPairs = [
4447
[new Int16Array([-256]), new Uint16Array([0xff00])], // same bits
4548
[new Int32Array([-256]), new Uint32Array([0xffffff00])], // ditto
4649
[new Float32Array([0.1]), new Float32Array([0.0])],
47-
[new Float64Array([0.1]), new Float64Array([0.0])]
50+
[new Float64Array([0.1]), new Float64Array([0.0])],
51+
[new Uint8Array([1, 2, 3]).buffer, new Uint8Array([4, 5, 6]).buffer],
52+
[
53+
new Uint8Array(new SharedArrayBuffer(3)).fill(1).buffer,
54+
new Uint8Array(new SharedArrayBuffer(3)).fill(2).buffer
55+
],
56+
[new ArrayBuffer(2), new ArrayBuffer(3)],
57+
[new SharedArrayBuffer(2), new SharedArrayBuffer(3)],
58+
[new ArrayBuffer(2), new SharedArrayBuffer(3)],
59+
[
60+
new Uint8Array(new ArrayBuffer(3)).fill(1).buffer,
61+
new Uint8Array(new SharedArrayBuffer(3)).fill(2).buffer
62+
]
4863
];
4964

5065
equalArrayPairs.forEach((arrayPair) => {

0 commit comments

Comments
 (0)