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

assert, util: *DeepEqual() handles ArrayBuffers #22266

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
const { compare } = process.binding('buffer');
const { isArrayBufferView } = require('internal/util/types');
const { internalBinding } = require('internal/bootstrap/loaders');
const { isDate, isMap, isRegExp, isSet } = internalBinding('types');
const {
isAnyArrayBuffer,
isDate,
isMap,
isRegExp,
isSet
} = internalBinding('types');
const { getOwnNonIndexProperties } = process.binding('util');

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

function areEqualArrayBuffers(buf1, buf2) {
return buf1.byteLength === buf2.byteLength &&
compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;
}

function isFloatTypedArrayTag(tag) {
return tag === '[object Float32Array]' || tag === '[object Float64Array]';
}
Expand Down Expand Up @@ -154,6 +165,10 @@ function strictDeepEqual(val1, val2, memos) {
return false;
}
return keyCheck(val1, val2, kStrict, memos, kIsMap);
} else if (isAnyArrayBuffer(val1)) {
if (!areEqualArrayBuffers(val1, val2)) {
return false;
}
// TODO: Make the valueOf checks safe.
} else if (typeof val1.valueOf === 'function') {
const val1Value = val1.valueOf();
Expand Down Expand Up @@ -217,6 +232,11 @@ function looseDeepEqual(val1, val2, memos) {
} else if (isSet(val2) || isMap(val2)) {
return false;
}
if (isAnyArrayBuffer(val1) && isAnyArrayBuffer(val2)) {
if (!areEqualArrayBuffers(val1, val2)) {
return false;
}
}
return keyCheck(val1, val2, kLoose, memos, kNoIterator);
}

Expand Down
21 changes: 18 additions & 3 deletions test/parallel/test-assert-typedarray-deepequal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ const equalArrayPairs = [
[new Float32Array([+0.0]), new Float32Array([+0.0])],
[new Uint8Array([1, 2, 3, 4]).subarray(1), new Uint8Array([2, 3, 4])],
[new Uint16Array([1, 2, 3, 4]).subarray(1), new Uint16Array([2, 3, 4])],
[new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])]
[new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])],
[new ArrayBuffer(3), new ArrayBuffer(3)],
[new SharedArrayBuffer(3), new SharedArrayBuffer(3)]
];

const looseEqualArrayPairs = [
[new Float64Array([+0.0]), new Float32Array([-0.0])],
[new Int16Array(256), new Uint16Array(256)],
[new Int16Array([256]), new Uint16Array([256])],
[new Float32Array([+0.0]), new Float32Array([-0.0])],
[new Float64Array([+0.0]), new Float64Array([-0.0])]
[new Float64Array([+0.0]), new Float64Array([-0.0])],
[new ArrayBuffer(3), new SharedArrayBuffer(3)]
];

const notEqualArrayPairs = [
Expand All @@ -44,7 +47,19 @@ const notEqualArrayPairs = [
[new Int16Array([-256]), new Uint16Array([0xff00])], // same bits
[new Int32Array([-256]), new Uint32Array([0xffffff00])], // ditto
[new Float32Array([0.1]), new Float32Array([0.0])],
[new Float64Array([0.1]), new Float64Array([0.0])]
[new Float64Array([0.1]), new Float64Array([0.0])],
[new Uint8Array([1, 2, 3]).buffer, new Uint8Array([4, 5, 6]).buffer],
[
new Uint8Array(new SharedArrayBuffer(3)).fill(1).buffer,
new Uint8Array(new SharedArrayBuffer(3)).fill(2).buffer
],
[new ArrayBuffer(2), new ArrayBuffer(3)],
[new SharedArrayBuffer(2), new SharedArrayBuffer(3)],
[new ArrayBuffer(2), new SharedArrayBuffer(3)],
[
new Uint8Array(new ArrayBuffer(3)).fill(1).buffer,
new Uint8Array(new SharedArrayBuffer(3)).fill(2).buffer
Copy link
Member

Choose a reason for hiding this comment

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

This should already fail because of the different type in strict mode. I would therefore change the test to fill(1) on both sides. In that case it would probably pass in loose mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is already a test for that under looseEqualArrayPairs. Here I'm testing that buffers of different types with the same length but different contents are neither loosely nor strictly equal.

]
];

equalArrayPairs.forEach((arrayPair) => {
Expand Down