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,149 @@ 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
+ if ( ArrayBufferIsView ( expected ) ) {
410
+ return false ;
411
+ }
412
+ expectedViewLength = ArrayBufferPrototypeGetByteLength ( expected ) ;
413
+
414
+ if ( expectedViewLength > ArrayBufferPrototypeGetByteLength ( actual ) ) {
415
+ return false ;
416
+ }
417
+ actualView = new Uint8Array ( actual ) ;
418
+ expectedView = new Uint8Array ( expected ) ;
419
+
420
+ } else if ( isDataView ( actual ) ) {
421
+ if ( ! isDataView ( expected ) ) {
422
+ return false ;
423
+ }
424
+ const actualByteLength = DataViewPrototypeGetByteLength ( actual ) ;
425
+ expectedViewLength = DataViewPrototypeGetByteLength ( expected ) ;
426
+ if ( expectedViewLength > actualByteLength ) {
427
+ return false ;
428
+ }
429
+
430
+ actualView = new Uint8Array (
431
+ DataViewPrototypeGetBuffer ( actual ) ,
432
+ DataViewPrototypeGetByteOffset ( actual ) ,
433
+ actualByteLength ,
434
+ ) ;
435
+ expectedView = new Uint8Array (
436
+ DataViewPrototypeGetBuffer ( expected ) ,
437
+ DataViewPrototypeGetByteOffset ( expected ) ,
438
+ expectedViewLength ,
439
+ ) ;
440
+ } else {
441
+ if ( ObjectPrototypeToString ( actual ) !== ObjectPrototypeToString ( expected ) ) {
442
+ return false ;
443
+ }
444
+ actualView = actual ;
445
+ expectedView = expected ;
446
+ expectedViewLength = TypedArrayPrototypeGetLength ( expected ) ;
447
+
448
+ if ( expectedViewLength > TypedArrayPrototypeGetLength ( actual ) ) {
449
+ return false ;
450
+ }
451
+ }
452
+
453
+ for ( let i = 0 ; i < expectedViewLength ; i ++ ) {
454
+ if ( actualView [ i ] !== expectedView [ i ] ) {
455
+ return false ;
456
+ }
457
+ }
458
+
459
+ return true ;
460
+ }
461
+
462
+ function compareSets ( actual , expected , comparedObjects ) {
463
+ if ( expected . size > actual . size ) {
464
+ return false ; // `expected` can't be a subset if it has more elements
465
+ }
466
+
467
+ if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
468
+
469
+ const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
470
+ const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
471
+ const usedIndices = new SafeSet ( ) ;
472
+
473
+ expectedIteration: for ( const expectedItem of expectedIterator ) {
474
+ for ( let actualIdx = 0 ; actualIdx < actualArray . length ; actualIdx ++ ) {
475
+ if ( ! usedIndices . has ( actualIdx ) && isDeepStrictEqual ( actualArray [ actualIdx ] , expectedItem ) ) {
476
+ usedIndices . add ( actualIdx ) ;
477
+ continue expectedIteration;
478
+ }
479
+ }
480
+ return false ;
481
+ }
482
+
483
+ return true ;
484
+ }
485
+
486
+ function compareArrays ( actual , expected , comparedObjects ) {
487
+ if ( expected . length > actual . length ) {
488
+ return false ;
489
+ }
490
+
491
+ if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
492
+
493
+ // Create a map to count occurrences of each element in the expected array
494
+ const expectedCounts = new SafeMap ( ) ;
495
+ for ( const expectedItem of expected ) {
496
+ let found = false ;
497
+ for ( const { 0 : key , 1 : count } of expectedCounts ) {
498
+ if ( isDeepStrictEqual ( key , expectedItem ) ) {
499
+ MapPrototypeSet ( expectedCounts , key , count + 1 ) ;
500
+ found = true ;
501
+ break ;
502
+ }
503
+ }
504
+ if ( ! found ) {
505
+ MapPrototypeSet ( expectedCounts , expectedItem , 1 ) ;
506
+ }
507
+ }
508
+
509
+ // Create a map to count occurrences of relevant elements in the actual array
510
+ for ( const actualItem of actual ) {
511
+ for ( const { 0 : key , 1 : count } of expectedCounts ) {
512
+ if ( isDeepStrictEqual ( key , actualItem ) ) {
513
+ if ( count === 1 ) {
514
+ MapPrototypeDelete ( expectedCounts , key ) ;
515
+ } else {
516
+ MapPrototypeSet ( expectedCounts , key , count - 1 ) ;
517
+ }
518
+ break ;
519
+ }
520
+ }
521
+ }
522
+
523
+ return ! expectedCounts . size ;
524
+ }
525
+
375
526
/**
376
527
* Compares two objects or values recursively to check if they are equal.
377
528
* @param {any } actual - The actual value to compare.
@@ -388,22 +539,14 @@ function compareBranch(
388
539
) {
389
540
// Check for Map object equality
390
541
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 ( ) ;
542
+ return compareMaps ( actual , expected , comparedObjects ) ;
543
+ }
397
544
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 ;
545
+ if (
546
+ ( ArrayBufferIsView ( actual ) && ArrayBufferIsView ( expected ) ) ||
547
+ ( isArrayBuffer ( actual ) && isArrayBuffer ( expected ) )
548
+ ) {
549
+ return compareArrayBuffers ( actual , expected ) ;
407
550
}
408
551
409
552
for ( const type of typesToCallDeepStrictEqualWith ) {
@@ -415,68 +558,12 @@ function compareBranch(
415
558
416
559
// Check for Set object equality
417
560
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 ;
561
+ return compareSets ( actual , expected , comparedObjects ) ;
439
562
}
440
563
441
564
// Check if expected array is a subset of actual array
442
565
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 ;
566
+ return compareArrays ( actual , expected , comparedObjects ) ;
480
567
}
481
568
482
569
// Comparison done when at least one of the values is not an object
0 commit comments