-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: support inspecting namespaces of unevaluated modules #20782
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo nit.
lib/util.js
Outdated
@@ -993,6 +997,29 @@ function formatPromise(ctx, value, recurseTimes, keys) { | |||
return output; | |||
} | |||
|
|||
function formatNamespaceProperty(ctx, ns, recurseTimes, key, array) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you s/ns/value/
? All functions call it that except this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to see these debugging experiences made nicer.
lib/util.js
Outdated
@@ -761,8 +761,12 @@ function formatError(value) { | |||
function formatObject(ctx, value, recurseTimes, keys) { | |||
const len = keys.length; | |||
const output = new Array(len); | |||
for (var i = 0; i < len; i++) | |||
output[i] = formatProperty(ctx, value, recurseTimes, keys[i], 0); | |||
const isNs = types.isModuleNamespaceObject(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should rather go along with the value checks at line 571.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not quite sure how to do that, should i make another formatObject function like formatNamespaceObject?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be the ideal way here. That way there is no performance impact on regular objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll move it for consistency, but just for the record the isModuleNamespaceObject check still happens either way, there is no perf difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure how V8 optimizes the loop in this case. I guess it could be smart to remove the isNs
check but otherwise there would be a minor overhead.
lib/util.js
Outdated
let name; | ||
let value; | ||
try { | ||
value = formatValue(ctx, ns[key], recurseTimes, array === 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array === 0
will always be true.
lib/util.js
Outdated
value = formatValue(ctx, ns[key], recurseTimes, array === 0); | ||
} catch (err) { | ||
if (err instanceof ReferenceError) { | ||
value = ctx.stylize('<uninitialized>', 'special'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am somewhat surprised that the keys are exposed even though accessing them throws. I guess that is done by design - out of curiosity: would you be so kind and point me to the reasons why this is done like that? After looking at the test again, I see why it throws.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even though we know the export names, they aren't yet intialised (think console.log(a); const a = 1
). there is no js value that can represent this state so v8 throws a reference error, which is what would happen with that example.
lib/util.js
Outdated
name = ctx.stylize(strEscape(key), 'string'); | ||
} | ||
|
||
return `${name}: ${value}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of copy pasting this part. It could easily get out of sync if anything small changes.
@addaleax thanks for starting the benchmark. If they turn out to have a impact I would like to only show this when |
83ed8df
to
f0644e4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with my nits addressed.
lib/util.js
Outdated
@@ -589,6 +589,9 @@ function formatValue(ctx, value, recurseTimes, ln) { | |||
if (keyLength === 0) | |||
return ctx.stylize(`[${name}]`, 'special'); | |||
base = `[${name}]`; | |||
} else if (types.isModuleNamespaceObject(value)) { | |||
braces[0] = `[${tag}] {`; | |||
formatter = formatNamespaceObject; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to move this down to the last else because it is probably one of the least used values and does not need to be performant.
lib/util.js
Outdated
@@ -993,8 +1005,37 @@ function formatPromise(ctx, value, recurseTimes, keys) { | |||
return output; | |||
} | |||
|
|||
function formatKey(ctx, key, enumerable = true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use a default value and just explicitly pass through the value.
lib/util.js
Outdated
} else { | ||
name = ctx.stylize(strEscape(key), 'string'); | ||
} | ||
return name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could now be rewritten to: if (foo) return ... if (bar) return ...
f0644e4
to
d065be3
Compare
linuxone rebuild: https://ci.nodejs.org/job/node-test-commit-linuxone/1372/ failures look unrelated |
landed in 064057b |
PR-URL: #20782 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
PR-URL: #20782 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes