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

url: set toStringTag for the URL class #10562

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jan 1, 2017

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-bot nodejs-github-bot added dont-land-on-v4.x url Issues and PRs related to the legacy built-in url module. labels Jan 1, 2017
Copy link
Member

@targos targos left a comment

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?

writable: false,
enumerable: false,
configurable: true
});
Copy link
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
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
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
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
Member Author

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
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
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
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
Member Author

jasnell commented Jan 3, 2017

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:

const u = new url.URL('http://example.org');
const toString = Object.prototype.toString;
assert.strictEqual(toString.call(u), '[object URL]');
assert.strictEqual(Object.getPrototypeOf(u), '[object URLPrototype]');

const sp = 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
Member Author

jasnell commented Jan 3, 2017

@jasnell
Copy link
Member Author

jasnell commented Jan 3, 2017

AIX failure is unrelated

jasnell added a commit that referenced this pull request Jan 3, 2017
PR-URL: #10562
Fixes: #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
Member Author

jasnell commented Jan 3, 2017

Landed in f5d92f3

@jasnell jasnell closed this Jan 3, 2017
evanlucas pushed a commit that referenced this pull request Jan 4, 2017
PR-URL: #10562
Fixes: #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>
@joyeecheung joyeecheung added the whatwg-url Issues and PRs related to the WHATWG URL implementation. label Jan 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
url Issues and PRs related to the legacy built-in url module. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants