@@ -32,7 +32,9 @@ const { isBuffer } = require('buffer').Buffer;
3232
3333const {
3434 previewMapIterator,
35- previewSetIterator
35+ previewSetIterator,
36+ previewWeakMap,
37+ previewWeakSet
3638} = require ( 'internal/v8' ) ;
3739
3840const {
@@ -54,6 +56,8 @@ const {
5456 isPromise,
5557 isSet,
5658 isSetIterator,
59+ isWeakMap,
60+ isWeakSet,
5761 isRegExp,
5862 isDate,
5963 isTypedArray
@@ -291,6 +295,8 @@ function inspect(value, opts) {
291295 colors : inspectDefaultOptions . colors ,
292296 customInspect : inspectDefaultOptions . customInspect ,
293297 showProxy : inspectDefaultOptions . showProxy ,
298+ // TODO(BridgeAR): Deprecate `maxArrayLength` and replace it with
299+ // `maxEntries`.
294300 maxArrayLength : inspectDefaultOptions . maxArrayLength ,
295301 breakLength : inspectDefaultOptions . breakLength ,
296302 indentationLvl : 0 ,
@@ -328,6 +334,8 @@ Object.defineProperty(inspect, 'defaultOptions', {
328334 if ( options === null || typeof options !== 'object' ) {
329335 throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' ) ;
330336 }
337+ // TODO(BridgeAR): Add input validation and make sure `defaultOptions` are
338+ // not configurable.
331339 return _extend ( inspectDefaultOptions , options ) ;
332340 }
333341} ) ;
@@ -465,6 +473,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
465473 let braces ;
466474 let noIterator = true ;
467475 let raw ;
476+ let extra ;
468477
469478 // Iterators and the rest are split to reduce checks
470479 if ( value [ Symbol . iterator ] ) {
@@ -562,6 +571,20 @@ function formatValue(ctx, value, recurseTimes, ln) {
562571 } else if ( isPromise ( value ) ) {
563572 braces [ 0 ] = `${ prefix } {` ;
564573 formatter = formatPromise ;
574+ } else if ( isWeakSet ( value ) ) {
575+ braces [ 0 ] = `${ prefix } {` ;
576+ if ( ctx . showHidden ) {
577+ formatter = formatWeakSet ;
578+ } else {
579+ extra = '[items unknown]' ;
580+ }
581+ } else if ( isWeakMap ( value ) ) {
582+ braces [ 0 ] = `${ prefix } {` ;
583+ if ( ctx . showHidden ) {
584+ formatter = formatWeakMap ;
585+ } else {
586+ extra = '[items unknown]' ;
587+ }
565588 } else {
566589 // Check boxed primitives other than string with valueOf()
567590 // NOTE: `Date` has to be checked first!
@@ -616,6 +639,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
616639 ctx . seen . push ( value ) ;
617640 const output = formatter ( ctx , value , recurseTimes , keys ) ;
618641
642+ if ( extra !== undefined )
643+ output . unshift ( extra ) ;
644+
619645 for ( var i = 0 ; i < symbols . length ; i ++ ) {
620646 output . push ( formatProperty ( ctx , value , recurseTimes , symbols [ i ] , 0 ) ) ;
621647 }
@@ -839,6 +865,46 @@ function formatMap(ctx, value, recurseTimes, keys) {
839865 return output ;
840866}
841867
868+ function formatWeakSet ( ctx , value , recurseTimes , keys ) {
869+ const maxArrayLength = Math . max ( ctx . maxArrayLength , 0 ) ;
870+ const entries = previewWeakSet ( value , maxArrayLength + 1 ) ;
871+ const maxLength = Math . min ( maxArrayLength , entries . length ) ;
872+ let output = new Array ( maxLength ) ;
873+ for ( var i = 0 ; i < maxLength ; ++ i )
874+ output [ i ] = formatValue ( ctx , entries [ i ] , recurseTimes ) ;
875+ // Sort all entries to have a halfway reliable output (if more entries than
876+ // retrieved ones exist, we can not reliably return the same output).
877+ output = output . sort ( ) ;
878+ if ( entries . length > maxArrayLength )
879+ output . push ( '... more items' ) ;
880+ for ( i = 0 ; i < keys . length ; i ++ )
881+ output . push ( formatProperty ( ctx , value , recurseTimes , keys [ i ] , 0 ) ) ;
882+ return output ;
883+ }
884+
885+ function formatWeakMap ( ctx , value , recurseTimes , keys ) {
886+ const maxArrayLength = Math . max ( ctx . maxArrayLength , 0 ) ;
887+ const entries = previewWeakMap ( value , maxArrayLength + 1 ) ;
888+ // Entries exist as [key1, val1, key2, val2, ...]
889+ const remainder = entries . length / 2 > maxArrayLength ;
890+ const len = entries . length / 2 - ( remainder ? 1 : 0 ) ;
891+ const maxLength = Math . min ( maxArrayLength , len ) ;
892+ let output = new Array ( maxLength ) ;
893+ for ( var i = 0 ; i < len ; i ++ ) {
894+ const pos = i * 2 ;
895+ output [ i ] = `${ formatValue ( ctx , entries [ pos ] , recurseTimes ) } => ` +
896+ formatValue ( ctx , entries [ pos + 1 ] , recurseTimes ) ;
897+ }
898+ // Sort all entries to have a halfway reliable output (if more entries than
899+ // retrieved ones exist, we can not reliably return the same output).
900+ output = output . sort ( ) ;
901+ if ( remainder > 0 )
902+ output . push ( '... more items' ) ;
903+ for ( i = 0 ; i < keys . length ; i ++ )
904+ output . push ( formatProperty ( ctx , value , recurseTimes , keys [ i ] , 0 ) ) ;
905+ return output ;
906+ }
907+
842908function formatCollectionIterator ( preview , ctx , value , recurseTimes , keys ) {
843909 const output = [ ] ;
844910 for ( const entry of preview ( value ) ) {
0 commit comments