GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: support inspecting Map, Set, and Promise by monsanto · Pull Request #1471 · nodejs/node

Conversation

@monsanto

@monsanto monsanto commented Apr 20, 2015

Copy link
Copy Markdown
Contributor

Among other things, this makes working with ES6 containers bearable on the REPL.

Before:

> new Map([[1, "foo"], [null, "bar"], [{quux: 42}, undefined]])
{}

After:

> new Map([[1, "foo"], [null, "bar"], [{quux: 42}, undefined]])
Map { 1 => 'foo', null => 'bar', { quux: 42 } => undefined }

The output format is inspired by Chrome DevTools.

I would like to support inspecting promises but I do not know of a way to synchronously obtain the status and fulfillment value. If someone would like to point me in the right direction I can provide a followup PR. (see below)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@monsanto
util: support inspecting Map and Set
613a63b
The output format is inspired by Chrome DevTools.
@domenic

domenic commented Apr 20, 2015

Copy link
Copy Markdown
Contributor

I would like to support inspecting promises but I do not know of a way to synchronously obtain the status and fulfillment value.

Probably only possible via the V8 API... would be worth looking at the dev tools code to see how it's done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

domenic
domenic reviewed Apr 20, 2015
View reviewed changes
Comment thread lib/util.js

@domenic domenic Apr 20, 2015

Copy link
Copy Markdown
Contributor

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.

Seems like this case needs tests

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@brendanashworth brendanashworth added the util Issues and PRs related to the built-in util module. label Apr 20, 2015
brendanashworth
brendanashworth reviewed Apr 20, 2015
View reviewed changes
Comment thread test/parallel/test-util-inspect.js Outdated

@brendanashworth brendanashworth Apr 20, 2015

Copy link
Copy Markdown
Contributor

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.

style: semicolons here and below

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@monsanto monsanto Apr 20, 2015

Copy link
Copy Markdown
Contributor Author

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 have that in a new commit yet to be pushed. Is it normal that make jshint doesn't touch tests?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brendanashworth brendanashworth Apr 20, 2015

Copy link
Copy Markdown
Contributor

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.

Yeah, for some reason that choice was made a long time ago. Wouldn't call it "normal" though :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@monsanto
(rebase) add semicolons to test
21e4e92
@monsanto

monsanto commented Apr 20, 2015

Copy link
Copy Markdown
Contributor Author

@domenic I have added a test covering the case you commented on, thanks.

There were no tests of alignment at all, so I took the luxury of adding tests for the rest of the containers as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

monsanto added 2 commits April 19, 2015 23:37
@monsanto
(rebase) check alignment of objects
87e36a6
@monsanto
(rebase) these semicolons are going to be the end of me
10d88e5
@monsanto

monsanto commented Apr 20, 2015

Copy link
Copy Markdown
Contributor Author

I just pushed a proof-of-concept implementation of util.inspect for Promises. It uses the mirror debugger to synchronously inspect Promises. (Alternatively, this could be done if I could have a module with compiler intrinsics enabled). There are bits I don't like about my implementation but I wanted to get this out there before I went to bed in case people think my course of action totally sucks and I should abandon ship 😛

Output looks like

> Promise.resolve(3)
Promise { 3 }
> Promise.reject(3)
Promise { <rejected> 3 }
> new Promise(function(){})
Promise { <pending> }

which is a change from Chrome DevTools, as they are ridiculously verbose:

> Promise.resolve(3)
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 3}
> Promise.reject(3)
Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: 3}
> new Promise(function(){})
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto monsanto changed the title util: support inspecting Map and Set util: support inspecting Map, Set, and Promise Apr 20, 2015
@monsanto
util: support inspecting Promises
1185ff3
@bnoordhuis

bnoordhuis commented Apr 20, 2015

Copy link
Copy Markdown
Member

Left some comments on the promise inspection code. I don't disagree with the Debug.MakeMirror approach (in fact I think it's the only way to crack open a promise) but I think the current implementation is creating mirrors too eagerly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto
(rebase) promise fixes
c4d2cd0
@monsanto

monsanto commented Apr 20, 2015

Copy link
Copy Markdown
Contributor Author

@bnoordhuis Thank you for your comments.

runInDebugContext isn't cheap

I've hoisted it to the top of the file for simplicity; however if this is too expensive or otherwise unsatisfactory I can create it on demand and cache.

only way to crack open a promise

If we could have compiler intrinsics enabled for the lib/internals folder, the Promise internals are at the properties %CreateGlobalPrivateOwnSymbol("Promise#status") and ...("Promise#value"). I don't know if this is more or less desirable than using runInDebugContext. My guess is less, but perhaps this functionality would be useful for other things in the future..

Mirrors being cached

you are correct, I should have noticed this. They are now created as "transient" so they will not be cached.

creating so many mirrors

I've guarded the mirror creation with an instanceof, so the common case should not be penalized. The code still calls inspectPromise twice if it is indeed a native promise, but I can't eliminate this without a more involved refactor (or creating a closure, which seems just as bad as creating an extra mirror). I have tried to keep my changes as minimal as possible as this is my first io.js PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto
(rebase) fix showHidden on empty set/map
ba53e97
we may mutate keys in the typecase, so don't check keys.length until no
more mutations.
chrisdickinson
chrisdickinson reviewed Apr 22, 2015
View reviewed changes
Comment thread lib/util.js

@chrisdickinson chrisdickinson Apr 22, 2015

Copy link
Copy Markdown
Contributor

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.

@domenic: Should we add support for Weak{Set,Map} inspection through Debug as a follow-on to this PR?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@domenic domenic Apr 22, 2015

Copy link
Copy Markdown
Contributor

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.

Noooo that seems very bad, basically exposes iteration to JS.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chrisdickinson chrisdickinson Apr 22, 2015

Copy link
Copy Markdown
Contributor

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.

Yeah – I thought it might be a bit fraught with peril; was surprised to see the chrome dev tools expose that info.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chrisdickinson

chrisdickinson commented Apr 22, 2015

Copy link
Copy Markdown
Contributor

The nits can probably be disregarded – just looked and saw that we were using map before, anyway :)

This LGTM. Unless @bnoordhuis or @domenic objects, I'll merge this tomorrow afternoon.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bnoordhuis

bnoordhuis commented Apr 23, 2015

Copy link
Copy Markdown
Member

If we could have compiler intrinsics enabled for the lib/internals folder, the Promise internals are at the properties %CreateGlobalPrivateOwnSymbol("Promise#status") and ...("Promise#value").

It's possible to do that but it's kind of a hack:

const v8 = require('v8');
v8.setFlagsFromString('--allow_natives_syntax');
const promiseStatus = eval('%CreateGlobalPrivateOwnSymbol("Promise#status")');
const promiseValue = eval('%CreateGlobalPrivateOwnSymbol("Promise#value")');
v8.setFlagsFromString('--noallow_natives_syntax');
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bnoordhuis
bnoordhuis reviewed Apr 23, 2015
View reviewed changes
Comment thread lib/util.js Outdated

@bnoordhuis bnoordhuis Apr 23, 2015

Copy link
Copy Markdown
Member

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.

Preferably assign empty = false and use strict checks below (i.e. empty = empty === true && keys.length === 0 and if (empty === true) {.)

It's a minor thing but V8 emits marginally tighter code for strict boolean comparisons.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@monsanto
nits n stuff
79627ae
@monsanto

monsanto commented Apr 23, 2015

Copy link
Copy Markdown
Contributor Author

@bnoordhuis @chrisdickinson OK, I addressed these style nits.

Just curious, is there a reason to keep the instanceof check out of inspectPromise?

It saves an instanceof check. I'm hoping to do a larger scale cleanup of util.inspect when I next have free time, which should fix the double inspectPromise calls and make this issue moot.

Aside: am I the only one who finds it moronic that recurseTimes is sometimes a number and sometimes not?

Yes, another thing I would like to clean up. (Or at least move the subtraction logic to a central place)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

util Issues and PRs related to the built-in util module.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@monsanto @domenic @bnoordhuis @chrisdickinson @brendanashworth
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.