Skip to content
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: improve inspect for (Async|Generator)Function #11210

Closed
wants to merge 2 commits into from

Conversation

targos
Copy link
Member

@targos targos commented Feb 7, 2017

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

util

This allows us to use async functions.
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. util Issues and PRs related to the built-in util module. labels Feb 7, 2017
@targos targos changed the title Util async func util: emphasize inspected async functions Feb 7, 2017
@targos targos added tools Issues and PRs related to the tools directory. dont-land-on-v4.x labels Feb 7, 2017
lib/util.js Outdated
'special');
const asyncPrefix = binding.isAsyncFunction(value) ? 'async ' : '';
return ctx.stylize(
`[${asyncPrefix}Function${value.name ? `: ${value.name}` : ''}]`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(async function() {}).constructor.name === 'AsyncFunction', so I’d probably want to go with that? Or just generally actually use the constructor name, if it’s available?

Copy link
Member Author

@targos targos Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I didn't think about that. We don't do it for GeneratorFunction though. Should I extend the PR to support it too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if I change the code to use constructor.name, this will be semver-major. I'm OK with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: indent by four spaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be semver-major

You mean, because it changes the output for GeneratorFunction? Yeah, that’s probably for the best. And yes, I think extending it to GeneratorFunctions would be a good idea, too.

@@ -9,6 +9,7 @@ assert.strictEqual(util.inspect(false), 'false');
assert.strictEqual(util.inspect(''), "''");
assert.strictEqual(util.inspect('hello'), "'hello'");
assert.strictEqual(util.inspect(function() {}), '[Function]');
assert.strictEqual(util.inspect(async function() {}), '[async Function]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original uses the capitalized constructor name (Function). How about using the same format, AsyncFunction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit redundant: async AsyncFunction

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a style nit.

lib/util.js Outdated
'special');
const asyncPrefix = binding.isAsyncFunction(value) ? 'async ' : '';
return ctx.stylize(
`[${asyncPrefix}Function${value.name ? `: ${value.name}` : ''}]`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: indent by four spaces.

@@ -9,6 +9,7 @@ assert.strictEqual(util.inspect(false), 'false');
assert.strictEqual(util.inspect(''), "''");
assert.strictEqual(util.inspect('hello'), "'hello'");
assert.strictEqual(util.inspect(function() {}), '[Function]');
assert.strictEqual(util.inspect(async function() {}), '[async Function]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit redundant: async AsyncFunction

@targos
Copy link
Member Author

targos commented Feb 7, 2017

@bnoordhuis I'm changing the PR to get this output:

$ ./node 
> function*a(){}
undefined
> a
[GeneratorFunction: a]
> async function b(){}
undefined
> b
[AsyncFunction: b]

What do you think?

@bnoordhuis
Copy link
Member

Seems fine to me.

@targos targos added semver-major PRs that contain breaking changes and should be released in the next major version. and removed c++ Issues and PRs that require attention from people who are familiar with C++. dont-land-on-v4.x labels Feb 7, 2017
@targos
Copy link
Member Author

targos commented Feb 7, 2017

OK, updated. Marking semver-major because the output changes for generator functions.

@targos targos changed the title util: emphasize inspected async functions util: improve inspect for (Async|Generator)Function Feb 7, 2017
lib/util.js Outdated
@@ -403,8 +403,9 @@ function formatValue(ctx, value, recurseTimes) {
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (typeof value === 'function') {
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
'special');
const cName = value.constructor ? value.constructor.name : 'Function';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s a getConstructorOf utility inside this file for dealing with some edge cases :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! Thanks, updated.

lib/util.js Outdated
@@ -403,8 +403,9 @@ function formatValue(ctx, value, recurseTimes) {
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (typeof value === 'function') {
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
'special');
const cName = getConstructorOf(value).name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to comment again 😄 I’d just move the constructor variable up before the keys.length block and do the same constructor && constructor.name … check that is used elsewhere in this file. I doubt there are many people who set their functions’ prototypes to null, but I’d prefer handling these consistently…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem :) Updated again.

Copy link
Contributor

@evanlucas evanlucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with @addaleax's suggestion

@evanlucas
Copy link
Contributor

@targos with this being a semver-major change, do you have plans to open a PR to do this for async functions in v7 as well (without the generator function change)?

@targos
Copy link
Member Author

targos commented Feb 7, 2017

@evanlucas I can do that

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bearing with me. LGTM! :)

@targos
Copy link
Member Author

targos commented Feb 7, 2017

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with style nits.

@@ -400,11 +400,14 @@ function formatValue(ctx, value, recurseTimes) {
});
}

var constructor = getConstructorOf(value);
Copy link
Member

@bnoordhuis bnoordhuis Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe const it while you're here. (EDIT: nevermind, I see it's being reassigned in some places.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't make it const because the value may be changed later.

lib/util.js Outdated
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (typeof value === 'function') {
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
'special');
const cName = constructor ? constructor.name : 'Function';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an irrational dislike for the name cName. constructorName or ctorName if the former is too long?

(Applies to line 541 as well.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to ctorName

lib/util.js Outdated
'special');
const cName = constructor ? constructor.name : 'Function';
return ctx.stylize(
`[${cName}${value.name ? `: ${value.name}` : ''}]`, 'special');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Four spaces, please. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Use the constructor name in the output, if present.
targos added a commit to targos/node that referenced this pull request Feb 7, 2017
Use the constructor name in the output, if present.
This is a backport of nodejs#11210 without
the semver-major change to GeneratorFunction output.
@targos
Copy link
Member Author

targos commented Feb 7, 2017

PR to v7.x-staging: #11211

'special');
const ctorName = constructor ? constructor.name : 'Function';
return ctx.stylize(
`[${ctorName}${value.name ? `: ${value.name}` : ''}]`, 'special');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested templates can be a little bit hard to read, maybe change it to use if-else? (though this LTGM too with this unchanged, now that there is a backport PR already)

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. LGTM.

jasnell pushed a commit that referenced this pull request Feb 11, 2017
This allows us to use async functions.

PR-URL: #11210
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
jasnell pushed a commit that referenced this pull request Feb 11, 2017
Use the constructor name in the output, if present.

PR-URL: #11210
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
@jasnell
Copy link
Member

jasnell commented Feb 11, 2017

Landed in 615789b...f65aa08

@jasnell jasnell closed this Feb 11, 2017
jasnell pushed a commit that referenced this pull request Feb 13, 2017
Use the constructor name in the output, if present.
This is a backport of #11210 without
the semver-major change to GeneratorFunction output.

PR-URL: #11211
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 14, 2017
Use the constructor name in the output, if present.
This is a backport of nodejs#11210 without
the semver-major change to GeneratorFunction output.

PR-URL: nodejs#11211
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
krydos pushed a commit to krydos/node that referenced this pull request Feb 25, 2017
This allows us to use async functions.

PR-URL: nodejs#11210
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
krydos pushed a commit to krydos/node that referenced this pull request Feb 25, 2017
Use the constructor name in the output, if present.

PR-URL: nodejs#11210
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
@jasnell jasnell mentioned this pull request Apr 4, 2017
@targos targos deleted the util-async-func branch June 1, 2017 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-major PRs that contain breaking changes and should be released in the next major version. tools Issues and PRs related to the tools directory. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants