Skip to content

url: set toStringTag for the URL class - #10562

Closed
jasnell wants to merge 1 commit into
nodejs:masterfrom
jasnell:url-tostringtag
Closed

url: set toStringTag for the URL class#10562
jasnell wants to merge 1 commit into
nodejs:masterfrom
jasnell:url-tostringtag

Conversation

@jasnell

Copy link
Copy Markdown
Member
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

url

Description

Add Symbol.toStringTag for URL class

Fixes: #10554

@nodejs-github-botnodejs-github-bot added dont-land-on-v4.x url Issues and PRs related to the legacy built-in url module. labels Jan 1, 2017

@targostargos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a test?
And perhaps we should do the same to URLSearchParams?

Comment threadlib/internal/url.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn’t it be a bit more customary to define this on the prototype?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Web IDL requires that. The class string of the prototype should be URLPrototype.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@targos Thanks for clarifying!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can't it still be defined on the prototype and just make that decision as a getter when invoked? I'm not sure it's great to add another property on every URL object just for this?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yeah, I'd considered doing that but just went with the style already implemented for the other uses of toStringTag already in the file. We should update all of them if we're going to switch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If there is no performance regression then I'm not against merging this - otherwise we should probably update them with a prototype getter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The IDL spec says

If an object has a class string, then the object must, at the time it is created, have a property whose name is the @@toStringTag symbol and whose value is the specified string.

It specifically asked for the value of the property to be the class string (URL or URLSearchParams), which was why I used this form for URLSearchParams in the first place.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@TimothyGu that would still work when defining a getter on the prototype but without causing an extra slot allocation for every single URL/URLSearchParams object.

This is not that big of a deal, but a user might have hundreds of thousands of URL objects at once and not allocating the extra memory would be nice.

@jasnell

Copy link
Copy Markdown
MemberAuthor

PR has been updated to add a test and to move to using the getter on the class. The behavior now matches what is currently implemented by Firefox. Specifically:

constu=newurl.URL('http://example.org');consttoString=Object.prototype.toString;assert.strictEqual(toString.call(u),'[object URL]');assert.strictEqual(Object.getPrototypeOf(u),'[object URLPrototype]');constsp=u.searchParams;assert.strictEqual(toString.call(sp),'[object URLSearchParams]');assert.strictEqual(toString.call(Object.getPrototypeOf(sp)),'[object URLSearchParamsPrototype]');

Note that Chrome returns [object URL] for the getPrototypeOf check above.

@jasnell

Copy link
Copy Markdown
MemberAuthor

@jasnell

Copy link
Copy Markdown
MemberAuthor

AIX failure is unrelated

jasnell added a commit that referenced this pull request Jan 3, 2017
PR-URL: #10562Fixes: #10554
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@jasnell

Copy link
Copy Markdown
MemberAuthor

Landed in f5d92f3

@jasnelljasnell closed this Jan 3, 2017
evanlucas pushed a commit that referenced this pull request Jan 4, 2017
PR-URL: #10562Fixes: #10554
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@evanlucasevanlucas mentioned this pull request Jan 4, 2017
@joyeecheungjoyeecheung added the whatwg-url Issues and PRs related to the WHATWG URL implementation. label Jan 5, 2017
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

urlIssues and PRs related to the legacy built-in url module.whatwg-urlIssues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@jasnell@addaleax@benjamingr@TimothyGu@targos@cjihrig@joyeecheung@nodejs-github-bot