Skip to content

Commit

Permalink
fixup: simplify the code and change visualization
Browse files Browse the repository at this point in the history
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
  • Loading branch information
daeyeon committed May 19, 2022
1 parent 161dbe5 commit 87a63f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
40 changes: 19 additions & 21 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,18 +724,16 @@ function getCtxStyle(value, constructor, tag) {
return getPrefix(constructor, tag, fallback);
}

function formatProxy(ctx, target, handler, recurseTimes, showProperties = true) {
function formatProxy(ctx, proxy, recurseTimes) {
if (recurseTimes > ctx.depth && ctx.depth !== null) {
return ctx.stylize('Proxy [Array]', 'special');
}
recurseTimes += 1;
ctx.indentationLvl += 2;
const res = showProperties ?
[
formatValue(ctx, target, recurseTimes),
formatValue(ctx, handler, recurseTimes),
] :
[];
const res = [
formatValue(ctx, proxy[0], recurseTimes),
formatValue(ctx, proxy[1], recurseTimes),
];
ctx.indentationLvl -= 2;
return reduceToSingleString(
ctx, res, '', ['Proxy [', ']'], kArrayExtrasType, recurseTimes);
Expand All @@ -759,15 +757,15 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
const context = value;
// Always check for proxies to prevent side effects and to prevent triggering
// any proxy handlers.
const details = getProxyDetails(value, !!ctx.showProxy);
if (details !== undefined) {
const proxy = getProxyDetails(value, !!ctx.showProxy);
if (proxy !== undefined) {
if (proxy === null || proxy[2] === true) {
return ctx.stylize('<Revoked Proxy>', 'special');
}
if (ctx.showProxy) {
return formatProxy(ctx, details[0], details[1], recurseTimes);
} else if (details === null) {
// The proxy is revoked. Both target and handler of it are null.
return formatProxy(ctx, details, null, recurseTimes, ctx.showProxy);
return formatProxy(ctx, proxy, recurseTimes);
}
value = details;
value = proxy;
}

// Provide a hook for user-specified inspect functions.
Expand All @@ -783,7 +781,7 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
// a counter internally.
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
const isCrossContext =
details !== undefined || !(context instanceof Object);
proxy !== undefined || !(context instanceof Object);
const ret = FunctionPrototypeCall(
maybeCustom,
context,
Expand Down Expand Up @@ -1939,12 +1937,9 @@ function reduceToSingleString(
braces[0].length + base.length + 10;
if (isBelowBreakLength(ctx, output, start, base)) {
const joinedOutput = join(output, ', ');
const space = joinedOutput.length > 0 ? ' ' : '';
if (!joinedOutput.includes('\n')) {
return (
`${base ? `${base} ` : ''}${braces[0]}${space}${joinedOutput}` +
`${space}${braces[1]}`
);
return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
` ${braces[1]}`;
}
}
}
Expand Down Expand Up @@ -1975,11 +1970,14 @@ function hasBuiltInToString(value) {
const getFullProxy = false;
const proxyTarget = getProxyDetails(value, getFullProxy);
if (proxyTarget !== undefined) {
if (proxyTarget === null) {
return true;
}
value = proxyTarget;
}

// Count objects that have no `toString` function as built-in.
if (typeof value?.toString !== 'function') {
if (typeof value.toString !== 'function') {
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
if (!args[0]->IsProxy())
return;

Environment* env = Environment::GetCurrent(args);
Local<Proxy> proxy = args[0].As<Proxy>();

// TODO(BridgeAR): Remove the length check as soon as we prohibit access to
Expand All @@ -125,7 +126,8 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
if (args.Length() == 1 || args[1]->IsTrue()) {
Local<Value> ret[] = {
proxy->GetTarget(),
proxy->GetHandler()
proxy->GetHandler(),
Boolean::New(env->isolate(), proxy->IsRevoked())
};

args.GetReturnValue().Set(
Expand Down
8 changes: 5 additions & 3 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ assert.strictEqual(handler, details[1]);
details = processUtil.getProxyDetails(proxyObj);
assert.strictEqual(target, details[0]);
assert.strictEqual(handler, details[1]);
assert.strictEqual(details[2], false);

details = processUtil.getProxyDetails(proxyObj, false);
assert.strictEqual(target, details);
Expand All @@ -66,17 +67,18 @@ r.revoke();
details = processUtil.getProxyDetails(r.proxy, true);
assert.strictEqual(details[0], null);
assert.strictEqual(details[1], null);
assert.strictEqual(details[2], true);

details = processUtil.getProxyDetails(r.proxy, false);
assert.strictEqual(details, null);

assert.strictEqual(util.inspect(r.proxy), 'Proxy []');
assert.strictEqual(util.inspect(r.proxy), '<Revoked Proxy>');
assert.strictEqual(
util.inspect(r, { showProxy: true }),
'{ proxy: Proxy [ null, null ], revoke: [Function (anonymous)] }',
'{ proxy: <Revoked Proxy>, revoke: [Function (anonymous)] }',
);

assert.strictEqual(util.format('%s', r.proxy), 'Proxy []');
assert.strictEqual(util.format('%s', r.proxy), '<Revoked Proxy>');

assert.strictEqual(
util.inspect(proxyObj, opts),
Expand Down

0 comments on commit 87a63f5

Please sign in to comment.