21
21
'use strict' ;
22
22
23
23
const {
24
+ ArrayBufferIsView,
25
+ ArrayBufferPrototypeGetByteLength,
24
26
ArrayFrom,
25
27
ArrayIsArray,
26
28
ArrayPrototypeIndexOf,
27
29
ArrayPrototypeJoin,
28
30
ArrayPrototypePush,
29
31
ArrayPrototypeSlice,
32
+ DataViewPrototypeGetBuffer,
33
+ DataViewPrototypeGetByteLength,
34
+ DataViewPrototypeGetByteOffset,
30
35
Error,
31
36
FunctionPrototypeCall,
32
37
MapPrototypeDelete,
@@ -38,6 +43,7 @@ const {
38
43
ObjectIs,
39
44
ObjectKeys,
40
45
ObjectPrototypeIsPrototypeOf,
46
+ ObjectPrototypeToString,
41
47
ReflectApply,
42
48
ReflectHas,
43
49
ReflectOwnKeys,
@@ -50,6 +56,8 @@ const {
50
56
StringPrototypeSlice,
51
57
StringPrototypeSplit,
52
58
SymbolIterator,
59
+ TypedArrayPrototypeGetLength,
60
+ Uint8Array,
53
61
} = primordials ;
54
62
55
63
const {
@@ -65,6 +73,8 @@ const AssertionError = require('internal/assert/assertion_error');
65
73
const { inspect } = require ( 'internal/util/inspect' ) ;
66
74
const { Buffer } = require ( 'buffer' ) ;
67
75
const {
76
+ isArrayBuffer,
77
+ isDataView,
68
78
isKeyObject,
69
79
isPromise,
70
80
isRegExp,
@@ -73,6 +83,7 @@ const {
73
83
isDate,
74
84
isWeakSet,
75
85
isWeakMap,
86
+ isSharedArrayBuffer,
76
87
} = require ( 'internal/util/types' ) ;
77
88
const { isError, deprecate, emitExperimentalWarning } = require ( 'internal/util' ) ;
78
89
const { innerOk } = require ( 'internal/assert/utils' ) ;
@@ -369,9 +380,143 @@ function isSpecial(obj) {
369
380
}
370
381
371
382
const typesToCallDeepStrictEqualWith = [
372
- isKeyObject , isWeakSet , isWeakMap , Buffer . isBuffer ,
383
+ isKeyObject , isWeakSet , isWeakMap , Buffer . isBuffer , isSharedArrayBuffer ,
373
384
] ;
374
385
386
+ function compareMaps ( actual , expected , comparedObjects ) {
387
+ if ( actual . size !== expected . size ) {
388
+ return false ;
389
+ }
390
+ const safeIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , actual ) ;
391
+
392
+ comparedObjects ??= new SafeWeakSet ( ) ;
393
+
394
+ for ( const { 0 : key , 1 : val } of safeIterator ) {
395
+ if ( ! MapPrototypeHas ( expected , key ) ) {
396
+ return false ;
397
+ }
398
+ if ( ! compareBranch ( val , MapPrototypeGet ( expected , key ) , comparedObjects ) ) {
399
+ return false ;
400
+ }
401
+ }
402
+ return true ;
403
+ }
404
+
405
+ function compareArrayBuffers ( actual , expected ) {
406
+ let actualView , expectedView , expectedViewLength ;
407
+
408
+ if ( ! ArrayBufferIsView ( actual ) ) {
409
+ expectedViewLength = ArrayBufferPrototypeGetByteLength ( expected ) ;
410
+
411
+ if ( expectedViewLength > ArrayBufferPrototypeGetByteLength ( actual ) ) {
412
+ return false ;
413
+ }
414
+ actualView = new Uint8Array ( actual ) ;
415
+ expectedView = new Uint8Array ( expected ) ;
416
+
417
+ } else if ( isDataView ( actual ) ) {
418
+ const actualByteLength = DataViewPrototypeGetByteLength ( actual ) ;
419
+ expectedViewLength = DataViewPrototypeGetByteLength ( expected ) ;
420
+ if ( expectedViewLength > actualByteLength ) {
421
+ return false ;
422
+ }
423
+
424
+ actualView = new Uint8Array (
425
+ DataViewPrototypeGetBuffer ( actual ) ,
426
+ DataViewPrototypeGetByteOffset ( actual ) ,
427
+ actualByteLength ,
428
+ ) ;
429
+ expectedView = new Uint8Array (
430
+ DataViewPrototypeGetBuffer ( expected ) ,
431
+ DataViewPrototypeGetByteOffset ( expected ) ,
432
+ expectedViewLength ,
433
+ ) ;
434
+ } else {
435
+ if ( ObjectPrototypeToString ( actual ) !== ObjectPrototypeToString ( expected ) ) {
436
+ return false ;
437
+ }
438
+ actualView = actual ;
439
+ expectedView = expected ;
440
+ expectedViewLength = TypedArrayPrototypeGetLength ( expected ) ;
441
+
442
+ if ( expectedViewLength > TypedArrayPrototypeGetLength ( actual ) ) {
443
+ return false ;
444
+ }
445
+ }
446
+
447
+ for ( let i = 0 ; i < expectedViewLength ; i ++ ) {
448
+ if ( actualView [ i ] !== expectedView [ i ] ) {
449
+ return false ;
450
+ }
451
+ }
452
+
453
+ return true ;
454
+ }
455
+
456
+ function compareSets ( actual , expected , comparedObjects ) {
457
+ if ( expected . size > actual . size ) {
458
+ return false ; // `expected` can't be a subset if it has more elements
459
+ }
460
+
461
+ if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
462
+
463
+ const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
464
+ const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
465
+ const usedIndices = new SafeSet ( ) ;
466
+
467
+ expectedIteration: for ( const expectedItem of expectedIterator ) {
468
+ for ( let actualIdx = 0 ; actualIdx < actualArray . length ; actualIdx ++ ) {
469
+ if ( ! usedIndices . has ( actualIdx ) && isDeepStrictEqual ( actualArray [ actualIdx ] , expectedItem ) ) {
470
+ usedIndices . add ( actualIdx ) ;
471
+ continue expectedIteration;
472
+ }
473
+ }
474
+ return false ;
475
+ }
476
+
477
+ return true ;
478
+ }
479
+
480
+ function compareArrays ( actual , expected , comparedObjects ) {
481
+ if ( expected . length > actual . length ) {
482
+ return false ;
483
+ }
484
+
485
+ if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
486
+
487
+ // Create a map to count occurrences of each element in the expected array
488
+ const expectedCounts = new SafeMap ( ) ;
489
+ for ( const expectedItem of expected ) {
490
+ let found = false ;
491
+ for ( const { 0 : key , 1 : count } of expectedCounts ) {
492
+ if ( isDeepStrictEqual ( key , expectedItem ) ) {
493
+ MapPrototypeSet ( expectedCounts , key , count + 1 ) ;
494
+ found = true ;
495
+ break ;
496
+ }
497
+ }
498
+ if ( ! found ) {
499
+ MapPrototypeSet ( expectedCounts , expectedItem , 1 ) ;
500
+ }
501
+ }
502
+
503
+ // Create a map to count occurrences of relevant elements in the actual array
504
+ for ( const actualItem of actual ) {
505
+ for ( const { 0 : key , 1 : count } of expectedCounts ) {
506
+ if ( isDeepStrictEqual ( key , actualItem ) ) {
507
+ if ( count === 1 ) {
508
+ MapPrototypeDelete ( expectedCounts , key ) ;
509
+ } else {
510
+ MapPrototypeSet ( expectedCounts , key , count - 1 ) ;
511
+ }
512
+ break ;
513
+ }
514
+ }
515
+ }
516
+
517
+ return ! expectedCounts . size ;
518
+ }
519
+
375
520
/**
376
521
* Compares two objects or values recursively to check if they are equal.
377
522
* @param {any } actual - The actual value to compare.
@@ -388,22 +533,14 @@ function compareBranch(
388
533
) {
389
534
// Check for Map object equality
390
535
if ( isMap ( actual ) && isMap ( expected ) ) {
391
- if ( actual . size !== expected . size ) {
392
- return false ;
393
- }
394
- const safeIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , actual ) ;
395
-
396
- comparedObjects ??= new SafeWeakSet ( ) ;
536
+ return compareMaps ( actual , expected , comparedObjects ) ;
537
+ }
397
538
398
- for ( const { 0 : key , 1 : val } of safeIterator ) {
399
- if ( ! MapPrototypeHas ( expected , key ) ) {
400
- return false ;
401
- }
402
- if ( ! compareBranch ( val , MapPrototypeGet ( expected , key ) , comparedObjects ) ) {
403
- return false ;
404
- }
405
- }
406
- return true ;
539
+ if (
540
+ ( ArrayBufferIsView ( actual ) && ArrayBufferIsView ( expected ) ) ||
541
+ ( isArrayBuffer ( actual ) && isArrayBuffer ( expected ) )
542
+ ) {
543
+ return compareArrayBuffers ( actual , expected ) ;
407
544
}
408
545
409
546
for ( const type of typesToCallDeepStrictEqualWith ) {
@@ -415,68 +552,12 @@ function compareBranch(
415
552
416
553
// Check for Set object equality
417
554
if ( isSet ( actual ) && isSet ( expected ) ) {
418
- if ( expected . size > actual . size ) {
419
- return false ; // `expected` can't be a subset if it has more elements
420
- }
421
-
422
- if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
423
-
424
- const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
425
- const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
426
- const usedIndices = new SafeSet ( ) ;
427
-
428
- expectedIteration: for ( const expectedItem of expectedIterator ) {
429
- for ( let actualIdx = 0 ; actualIdx < actualArray . length ; actualIdx ++ ) {
430
- if ( ! usedIndices . has ( actualIdx ) && isDeepStrictEqual ( actualArray [ actualIdx ] , expectedItem ) ) {
431
- usedIndices . add ( actualIdx ) ;
432
- continue expectedIteration;
433
- }
434
- }
435
- return false ;
436
- }
437
-
438
- return true ;
555
+ return compareSets ( actual , expected , comparedObjects ) ;
439
556
}
440
557
441
558
// Check if expected array is a subset of actual array
442
559
if ( ArrayIsArray ( actual ) && ArrayIsArray ( expected ) ) {
443
- if ( expected . length > actual . length ) {
444
- return false ;
445
- }
446
-
447
- if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
448
-
449
- // Create a map to count occurrences of each element in the expected array
450
- const expectedCounts = new SafeMap ( ) ;
451
- for ( const expectedItem of expected ) {
452
- let found = false ;
453
- for ( const { 0 : key , 1 : count } of expectedCounts ) {
454
- if ( isDeepStrictEqual ( key , expectedItem ) ) {
455
- MapPrototypeSet ( expectedCounts , key , count + 1 ) ;
456
- found = true ;
457
- break ;
458
- }
459
- }
460
- if ( ! found ) {
461
- MapPrototypeSet ( expectedCounts , expectedItem , 1 ) ;
462
- }
463
- }
464
-
465
- // Create a map to count occurrences of relevant elements in the actual array
466
- for ( const actualItem of actual ) {
467
- for ( const { 0 : key , 1 : count } of expectedCounts ) {
468
- if ( isDeepStrictEqual ( key , actualItem ) ) {
469
- if ( count === 1 ) {
470
- MapPrototypeDelete ( expectedCounts , key ) ;
471
- } else {
472
- MapPrototypeSet ( expectedCounts , key , count - 1 ) ;
473
- }
474
- break ;
475
- }
476
- }
477
- }
478
-
479
- return ! expectedCounts . size ;
560
+ return compareArrays ( actual , expected , comparedObjects ) ;
480
561
}
481
562
482
563
// Comparison done when at least one of the values is not an object
0 commit comments