Skip to content

Commit 163ef7c

Browse files
committed
assert,util: change WeakMap and WeakSet comparison handling
1 parent 1872167 commit 163ef7c

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/api/assert.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ are also recursively evaluated by the following rules.
634634
* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
635635
objects.
636636
* [`Symbol`][] properties are not compared.
637-
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
637+
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values
638+
but only on their instances.
638639
* [`RegExp`][] lastIndex, flags, and source are always compared, even if these
639640
are not enumerable properties.
640641

lib/internal/util/comparisons.js

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const {
4646
isFloat64Array,
4747
isKeyObject,
4848
isCryptoKey,
49+
isWeakMap,
50+
isWeakSet,
4951
} = types;
5052
const {
5153
constants: {
@@ -283,7 +285,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
283285
) {
284286
return false;
285287
}
288+
} else if (isWeakMap(val1) || isWeakSet(val1)) {
289+
return false;
286290
}
291+
287292
return keyCheck(val1, val2, strict, memos, kNoIterator);
288293
}
289294

test/parallel/test-assert-deep.js

+32
Original file line numberDiff line numberDiff line change
@@ -1300,3 +1300,35 @@ if (common.hasCrypto) {
13001300
}
13011301
})().then(common.mustCall());
13021302
}
1303+
1304+
// Comparing two identical WeakMap instances
1305+
{
1306+
const weakMap = new WeakMap();
1307+
assertDeepAndStrictEqual(weakMap, weakMap);
1308+
}
1309+
1310+
// Comparing two different WeakMap instances
1311+
{
1312+
const weakMap1 = new WeakMap();
1313+
const objA = {};
1314+
weakMap1.set(objA, 'ok');
1315+
1316+
const weakMap2 = new WeakMap();
1317+
const objB = {};
1318+
weakMap2.set(objB, 'ok');
1319+
1320+
assertNotDeepOrStrict(weakMap1, weakMap2);
1321+
}
1322+
1323+
// Comparing two identical WeakSet instances
1324+
{
1325+
const weakSet = new WeakSet();
1326+
assertDeepAndStrictEqual(weakSet, weakSet);
1327+
}
1328+
1329+
// Comparing two different WeakSet instances
1330+
{
1331+
const weakSet1 = new WeakSet();
1332+
const weakSet2 = new WeakSet();
1333+
assertNotDeepOrStrict(weakSet1, weakSet2);
1334+
}

0 commit comments

Comments
 (0)