11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst util = require('util');
71cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
81cb0ef41Sopenharmony_ciconst processUtil = internalBinding('util');
91cb0ef41Sopenharmony_ciconst opts = { showProxy: true };
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cilet proxyObj;
121cb0ef41Sopenharmony_cilet called = false;
131cb0ef41Sopenharmony_ciconst target = {
141cb0ef41Sopenharmony_ci  [util.inspect.custom](depth, { showProxy }) {
151cb0ef41Sopenharmony_ci    if (showProxy === false) {
161cb0ef41Sopenharmony_ci      called = true;
171cb0ef41Sopenharmony_ci      if (proxyObj !== this) {
181cb0ef41Sopenharmony_ci        throw new Error('Failed');
191cb0ef41Sopenharmony_ci      }
201cb0ef41Sopenharmony_ci    }
211cb0ef41Sopenharmony_ci    return [1, 2, 3];
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci};
241cb0ef41Sopenharmony_ciconst handler = {
251cb0ef41Sopenharmony_ci  getPrototypeOf() { throw new Error('getPrototypeOf'); },
261cb0ef41Sopenharmony_ci  setPrototypeOf() { throw new Error('setPrototypeOf'); },
271cb0ef41Sopenharmony_ci  isExtensible() { throw new Error('isExtensible'); },
281cb0ef41Sopenharmony_ci  preventExtensions() { throw new Error('preventExtensions'); },
291cb0ef41Sopenharmony_ci  getOwnPropertyDescriptor() { throw new Error('getOwnPropertyDescriptor'); },
301cb0ef41Sopenharmony_ci  defineProperty() { throw new Error('defineProperty'); },
311cb0ef41Sopenharmony_ci  has() { throw new Error('has'); },
321cb0ef41Sopenharmony_ci  get() { throw new Error('get'); },
331cb0ef41Sopenharmony_ci  set() { throw new Error('set'); },
341cb0ef41Sopenharmony_ci  deleteProperty() { throw new Error('deleteProperty'); },
351cb0ef41Sopenharmony_ci  ownKeys() { throw new Error('ownKeys'); },
361cb0ef41Sopenharmony_ci  apply() { throw new Error('apply'); },
371cb0ef41Sopenharmony_ci  construct() { throw new Error('construct'); }
381cb0ef41Sopenharmony_ci};
391cb0ef41Sopenharmony_ciproxyObj = new Proxy(target, handler);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci// Inspecting the proxy should not actually walk it's properties
421cb0ef41Sopenharmony_ciutil.inspect(proxyObj, opts);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// Make sure inspecting object does not trigger any proxy traps.
451cb0ef41Sopenharmony_ciutil.format('%s', proxyObj);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// getProxyDetails is an internal method, not intended for public use.
481cb0ef41Sopenharmony_ci// This is here to test that the internals are working correctly.
491cb0ef41Sopenharmony_cilet details = processUtil.getProxyDetails(proxyObj, true);
501cb0ef41Sopenharmony_ciassert.strictEqual(target, details[0]);
511cb0ef41Sopenharmony_ciassert.strictEqual(handler, details[1]);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cidetails = processUtil.getProxyDetails(proxyObj);
541cb0ef41Sopenharmony_ciassert.strictEqual(target, details[0]);
551cb0ef41Sopenharmony_ciassert.strictEqual(handler, details[1]);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cidetails = processUtil.getProxyDetails(proxyObj, false);
581cb0ef41Sopenharmony_ciassert.strictEqual(target, details);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cidetails = processUtil.getProxyDetails({}, true);
611cb0ef41Sopenharmony_ciassert.strictEqual(details, undefined);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciconst r = Proxy.revocable({}, {});
641cb0ef41Sopenharmony_cir.revoke();
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cidetails = processUtil.getProxyDetails(r.proxy, true);
671cb0ef41Sopenharmony_ciassert.strictEqual(details[0], null);
681cb0ef41Sopenharmony_ciassert.strictEqual(details[1], null);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_cidetails = processUtil.getProxyDetails(r.proxy, false);
711cb0ef41Sopenharmony_ciassert.strictEqual(details, null);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(r.proxy), '<Revoked Proxy>');
741cb0ef41Sopenharmony_ciassert.strictEqual(
751cb0ef41Sopenharmony_ci  util.inspect(r, { showProxy: true }),
761cb0ef41Sopenharmony_ci  '{ proxy: <Revoked Proxy>, revoke: [Function (anonymous)] }',
771cb0ef41Sopenharmony_ci);
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', r.proxy), '<Revoked Proxy>');
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciassert.strictEqual(
821cb0ef41Sopenharmony_ci  util.inspect(proxyObj, opts),
831cb0ef41Sopenharmony_ci  'Proxy [\n' +
841cb0ef41Sopenharmony_ci  '  [ 1, 2, 3 ],\n' +
851cb0ef41Sopenharmony_ci  '  {\n' +
861cb0ef41Sopenharmony_ci  '    getPrototypeOf: [Function: getPrototypeOf],\n' +
871cb0ef41Sopenharmony_ci  '    setPrototypeOf: [Function: setPrototypeOf],\n' +
881cb0ef41Sopenharmony_ci  '    isExtensible: [Function: isExtensible],\n' +
891cb0ef41Sopenharmony_ci  '    preventExtensions: [Function: preventExtensions],\n' +
901cb0ef41Sopenharmony_ci  '    getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor],\n' +
911cb0ef41Sopenharmony_ci  '    defineProperty: [Function: defineProperty],\n' +
921cb0ef41Sopenharmony_ci  '    has: [Function: has],\n' +
931cb0ef41Sopenharmony_ci  '    get: [Function: get],\n' +
941cb0ef41Sopenharmony_ci  '    set: [Function: set],\n' +
951cb0ef41Sopenharmony_ci  '    deleteProperty: [Function: deleteProperty],\n' +
961cb0ef41Sopenharmony_ci  '    ownKeys: [Function: ownKeys],\n' +
971cb0ef41Sopenharmony_ci  '    apply: [Function: apply],\n' +
981cb0ef41Sopenharmony_ci  '    construct: [Function: construct]\n' +
991cb0ef41Sopenharmony_ci  '  }\n' +
1001cb0ef41Sopenharmony_ci  ']'
1011cb0ef41Sopenharmony_ci);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci// Using getProxyDetails with non-proxy returns undefined
1041cb0ef41Sopenharmony_ciassert.strictEqual(processUtil.getProxyDetails({}), undefined);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci// Inspecting a proxy without the showProxy option set to true should not
1071cb0ef41Sopenharmony_ci// trigger any proxy handlers.
1081cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxyObj), '[ 1, 2, 3 ]');
1091cb0ef41Sopenharmony_ciassert(called);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci// Yo dawg, I heard you liked Proxy so I put a Proxy
1121cb0ef41Sopenharmony_ci// inside your Proxy that proxies your Proxy's Proxy.
1131cb0ef41Sopenharmony_ciconst proxy1 = new Proxy({}, {});
1141cb0ef41Sopenharmony_ciconst proxy2 = new Proxy(proxy1, {});
1151cb0ef41Sopenharmony_ciconst proxy3 = new Proxy(proxy2, proxy1);
1161cb0ef41Sopenharmony_ciconst proxy4 = new Proxy(proxy1, proxy2);
1171cb0ef41Sopenharmony_ciconst proxy5 = new Proxy(proxy3, proxy4);
1181cb0ef41Sopenharmony_ciconst proxy6 = new Proxy(proxy5, proxy5);
1191cb0ef41Sopenharmony_ciconst expected0 = '{}';
1201cb0ef41Sopenharmony_ciconst expected1 = 'Proxy [ {}, {} ]';
1211cb0ef41Sopenharmony_ciconst expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
1221cb0ef41Sopenharmony_ciconst expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
1231cb0ef41Sopenharmony_ciconst expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
1241cb0ef41Sopenharmony_ciconst expected5 = 'Proxy [\n  ' +
1251cb0ef41Sopenharmony_ci                  'Proxy [ Proxy [ Proxy [Array], {} ], Proxy [ {}, {} ] ],\n' +
1261cb0ef41Sopenharmony_ci                  '  Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [Array], {} ] ]' +
1271cb0ef41Sopenharmony_ci                  '\n]';
1281cb0ef41Sopenharmony_ciconst expected6 = 'Proxy [\n' +
1291cb0ef41Sopenharmony_ci                  '  Proxy [\n' +
1301cb0ef41Sopenharmony_ci                  '    Proxy [ Proxy [Array], Proxy [Array] ],\n' +
1311cb0ef41Sopenharmony_ci                  '    Proxy [ Proxy [Array], Proxy [Array] ]\n' +
1321cb0ef41Sopenharmony_ci                  '  ],\n' +
1331cb0ef41Sopenharmony_ci                  '  Proxy [\n' +
1341cb0ef41Sopenharmony_ci                  '    Proxy [ Proxy [Array], Proxy [Array] ],\n' +
1351cb0ef41Sopenharmony_ci                  '    Proxy [ Proxy [Array], Proxy [Array] ]\n' +
1361cb0ef41Sopenharmony_ci                  '  ]\n' +
1371cb0ef41Sopenharmony_ci                  ']';
1381cb0ef41Sopenharmony_ciassert.strictEqual(
1391cb0ef41Sopenharmony_ci  util.inspect(proxy1, { showProxy: 1, depth: null }),
1401cb0ef41Sopenharmony_ci  expected1);
1411cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy2, opts), expected2);
1421cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy3, opts), expected3);
1431cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy4, opts), expected4);
1441cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy5, opts), expected5);
1451cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy6, opts), expected6);
1461cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy1), expected0);
1471cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy2), expected0);
1481cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy3), expected0);
1491cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy4), expected0);
1501cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy5), expected0);
1511cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy6), expected0);
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci// Just for fun, let's create a Proxy using Arrays.
1541cb0ef41Sopenharmony_ciconst proxy7 = new Proxy([], []);
1551cb0ef41Sopenharmony_ciconst expected7 = 'Proxy [ [], [] ]';
1561cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy7, opts), expected7);
1571cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy7), '[]');
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci// Now we're just getting silly, right?
1601cb0ef41Sopenharmony_ciconst proxy8 = new Proxy(Date, []);
1611cb0ef41Sopenharmony_ciconst proxy9 = new Proxy(Date, String);
1621cb0ef41Sopenharmony_ciconst expected8 = 'Proxy [ [Function: Date], [] ]';
1631cb0ef41Sopenharmony_ciconst expected9 = 'Proxy [ [Function: Date], [Function: String] ]';
1641cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy8, opts), expected8);
1651cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy9, opts), expected9);
1661cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy8), '[Function: Date]');
1671cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy9), '[Function: Date]');
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ciconst proxy10 = new Proxy(() => {}, {});
1701cb0ef41Sopenharmony_ciconst proxy11 = new Proxy(() => {}, {
1711cb0ef41Sopenharmony_ci  get() {
1721cb0ef41Sopenharmony_ci    return proxy11;
1731cb0ef41Sopenharmony_ci  },
1741cb0ef41Sopenharmony_ci  apply() {
1751cb0ef41Sopenharmony_ci    return proxy11;
1761cb0ef41Sopenharmony_ci  }
1771cb0ef41Sopenharmony_ci});
1781cb0ef41Sopenharmony_ciconst expected10 = '[Function (anonymous)]';
1791cb0ef41Sopenharmony_ciconst expected11 = '[Function (anonymous)]';
1801cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy10), expected10);
1811cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(proxy11), expected11);
182