url: set toStringTag for the URL class #10562

Closed
wants to merge 1 commit into
from
@jasnell
Member
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

@targos

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

lib/internal/url.js
+ writable: false,
+ enumerable: false,
+ configurable: true
+ });
@addaleax
addaleax Jan 1, 2017 Member

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

@targos
targos Jan 2, 2017 Member

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

@addaleax
addaleax Jan 2, 2017 Member

@targos Thanks for clarifying!

@benjamingr
benjamingr Jan 2, 2017 Member

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?

@jasnell
jasnell Jan 3, 2017 Member

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

@benjamingr
benjamingr Jan 3, 2017 Member

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

@TimothyGu
TimothyGu Jan 3, 2017 Member

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.

@benjamingr
benjamingr Jan 3, 2017 Member

@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 jasnell url: set toStringTag for the URL class
45d7274
@jasnell
Member
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.

@targos
targos approved these changes Jan 3, 2017 View changes
@cjihrig
cjihrig approved these changes Jan 3, 2017 View changes
@jasnell
Member
jasnell commented Jan 3, 2017

AIX failure is unrelated

@jasnell jasnell added a commit that referenced this pull request Jan 3, 2017
@jasnell jasnell url: set toStringTag for the URL class
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>
f5d92f3
@jasnell
Member
jasnell commented Jan 3, 2017

Landed in f5d92f3

@jasnell jasnell closed this Jan 3, 2017
@evanlucas evanlucas added a commit that referenced this pull request Jan 4, 2017
@jasnell @evanlucas jasnell + evanlucas url: set toStringTag for the URL class
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>
3195fb4
@evanlucas evanlucas referenced this pull request Jan 4, 2017
Merged

v7.4.0 release proposal #10589

@TimothyGu TimothyGu referenced this pull request Jan 20, 2017
Open

url: use symbol properties correctly in URL class #10906

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