@@ -8,11 +8,35 @@ const { internalBinding } = require('internal/test/binding');
8
8
const processUtil = internalBinding ( 'util' ) ;
9
9
const opts = { showProxy : true } ;
10
10
11
- const target = { } ;
11
+ let proxyObj ;
12
+ let called = false ;
13
+ const target = {
14
+ [ util . inspect . custom ] ( depth , { showProxy } ) {
15
+ if ( showProxy === false ) {
16
+ called = true ;
17
+ if ( proxyObj !== this ) {
18
+ throw new Error ( 'Failed' ) ;
19
+ }
20
+ }
21
+ return [ 1 , 2 , 3 ] ;
22
+ }
23
+ } ;
12
24
const handler = {
13
- get : function ( ) { throw new Error ( 'Getter should not be called' ) ; }
25
+ getPrototypeOf ( ) { throw new Error ( 'getPrototypeOf' ) ; } ,
26
+ setPrototypeOf ( ) { throw new Error ( 'setPrototypeOf' ) ; } ,
27
+ isExtensible ( ) { throw new Error ( 'isExtensible' ) ; } ,
28
+ preventExtensions ( ) { throw new Error ( 'preventExtensions' ) ; } ,
29
+ getOwnPropertyDescriptor ( ) { throw new Error ( 'getOwnPropertyDescriptor' ) ; } ,
30
+ defineProperty ( ) { throw new Error ( 'defineProperty' ) ; } ,
31
+ has ( ) { throw new Error ( 'has' ) ; } ,
32
+ get ( ) { throw new Error ( 'get' ) ; } ,
33
+ set ( ) { throw new Error ( 'set' ) ; } ,
34
+ deleteProperty ( ) { throw new Error ( 'deleteProperty' ) ; } ,
35
+ ownKeys ( ) { throw new Error ( 'ownKeys' ) ; } ,
36
+ apply ( ) { throw new Error ( 'apply' ) ; } ,
37
+ construct ( ) { throw new Error ( 'construct' ) ; }
14
38
} ;
15
- const proxyObj = new Proxy ( target , handler ) ;
39
+ proxyObj = new Proxy ( target , handler ) ;
16
40
17
41
// Inspecting the proxy should not actually walk it's properties
18
42
util . inspect ( proxyObj , opts ) ;
@@ -23,19 +47,31 @@ const details = processUtil.getProxyDetails(proxyObj);
23
47
assert . strictEqual ( target , details [ 0 ] ) ;
24
48
assert . strictEqual ( handler , details [ 1 ] ) ;
25
49
26
- assert . strictEqual ( util . inspect ( proxyObj , opts ) ,
27
- 'Proxy [ {}, { get: [Function: get] } ]' ) ;
50
+ assert . strictEqual (
51
+ util . inspect ( proxyObj , opts ) ,
52
+ 'Proxy [ [ 1, 2, 3 ],\n' +
53
+ ' { getPrototypeOf: [Function: getPrototypeOf],\n' +
54
+ ' setPrototypeOf: [Function: setPrototypeOf],\n' +
55
+ ' isExtensible: [Function: isExtensible],\n' +
56
+ ' preventExtensions: [Function: preventExtensions],\n' +
57
+ ' getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor],\n' +
58
+ ' defineProperty: [Function: defineProperty],\n' +
59
+ ' has: [Function: has],\n' +
60
+ ' get: [Function: get],\n' +
61
+ ' set: [Function: set],\n' +
62
+ ' deleteProperty: [Function: deleteProperty],\n' +
63
+ ' ownKeys: [Function: ownKeys],\n' +
64
+ ' apply: [Function: apply],\n' +
65
+ ' construct: [Function: construct] } ]'
66
+ ) ;
28
67
29
68
// Using getProxyDetails with non-proxy returns undefined
30
69
assert . strictEqual ( processUtil . getProxyDetails ( { } ) , undefined ) ;
31
70
32
- // This will throw because the showProxy option is not used
33
- // and the get function on the handler object defined above
34
- // is actually invoked.
35
- assert . throws (
36
- ( ) => util . inspect ( proxyObj ) ,
37
- / ^ E r r o r : G e t t e r s h o u l d n o t b e c a l l e d $ /
38
- ) ;
71
+ // Inspecting a proxy without the showProxy option set to true should not
72
+ // trigger any proxy handlers.
73
+ assert . strictEqual ( util . inspect ( proxyObj ) , '[ 1, 2, 3 ]' ) ;
74
+ assert ( called ) ;
39
75
40
76
// Yo dawg, I heard you liked Proxy so I put a Proxy
41
77
// inside your Proxy that proxies your Proxy's Proxy.
0 commit comments