2121'use strict' ;
2222
2323const {
24+ ArrayBufferIsView,
25+ ArrayBufferPrototypeGetByteLength,
2426 ArrayFrom,
2527 ArrayIsArray,
2628 ArrayPrototypeIndexOf,
2729 ArrayPrototypeJoin,
2830 ArrayPrototypePush,
2931 ArrayPrototypeSlice,
32+ DataViewPrototypeGetBuffer,
33+ DataViewPrototypeGetByteLength,
34+ DataViewPrototypeGetByteOffset,
3035 Error,
3136 FunctionPrototypeCall,
3237 MapPrototypeDelete,
@@ -38,6 +43,7 @@ const {
3843 ObjectIs,
3944 ObjectKeys,
4045 ObjectPrototypeIsPrototypeOf,
46+ ObjectPrototypeToString,
4147 ReflectApply,
4248 ReflectHas,
4349 ReflectOwnKeys,
@@ -50,6 +56,8 @@ const {
5056 StringPrototypeSlice,
5157 StringPrototypeSplit,
5258 SymbolIterator,
59+ TypedArrayPrototypeGetLength,
60+ Uint8Array,
5361} = primordials ;
5462
5563const {
@@ -65,6 +73,8 @@ const AssertionError = require('internal/assert/assertion_error');
6573const { inspect } = require ( 'internal/util/inspect' ) ;
6674const { Buffer } = require ( 'buffer' ) ;
6775const {
76+ isArrayBuffer,
77+ isDataView,
6878 isKeyObject,
6979 isPromise,
7080 isRegExp,
@@ -73,6 +83,7 @@ const {
7383 isDate,
7484 isWeakSet,
7585 isWeakMap,
86+ isSharedArrayBuffer,
7687} = require ( 'internal/util/types' ) ;
7788const { isError, deprecate, emitExperimentalWarning } = require ( 'internal/util' ) ;
7889const { innerOk } = require ( 'internal/assert/utils' ) ;
@@ -369,9 +380,143 @@ function isSpecial(obj) {
369380}
370381
371382const typesToCallDeepStrictEqualWith = [
372- isKeyObject , isWeakSet , isWeakMap , Buffer . isBuffer ,
383+ isKeyObject , isWeakSet , isWeakMap , Buffer . isBuffer , isSharedArrayBuffer ,
373384] ;
374385
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+
375520/**
376521 * Compares two objects or values recursively to check if they are equal.
377522 * @param {any } actual - The actual value to compare.
@@ -388,22 +533,14 @@ function compareBranch(
388533) {
389534 // Check for Map object equality
390535 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+ }
397538
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 ) ;
407544 }
408545
409546 for ( const type of typesToCallDeepStrictEqualWith ) {
@@ -415,68 +552,12 @@ function compareBranch(
415552
416553 // Check for Set object equality
417554 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 ) ;
439556 }
440557
441558 // Check if expected array is a subset of actual array
442559 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 ) ;
480561 }
481562
482563 // Comparison done when at least one of the values is not an object
0 commit comments