-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Conversation
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.
Can you add a test?
And perhaps we should do the same to URLSearchParams?
writable: false, | ||
enumerable: false, | ||
configurable: true | ||
}); |
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.
Wouldn’t it be a bit more customary to define this on the prototype?
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.
Web IDL requires that. The class string of the prototype should be URLPrototype
.
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.
@targos Thanks for clarifying!
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.
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?
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'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
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.
If there is no performance regression then I'm not against merging this - otherwise we should probably update them with a prototype getter.
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.
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.
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.
@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.
7881fa1
to
45d7274
Compare
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 |
AIX failure is unrelated |
Landed in f5d92f3 |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
url
Description
Add Symbol.toStringTag for URL class
Fixes: #10554