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: call toString before valueOf when stringifying #11737

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const IteratorPrototype = Object.getPrototypeOf(
const unpairedSurrogateRe =
/([^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
function toUSVString(val) {
const str = '' + val;
const str = `${val}`;
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
// slower than `unpairedSurrogateRe.exec()`.
const match = unpairedSurrogateRe.exec(str);
Expand Down Expand Up @@ -218,7 +218,7 @@ function onParseHashComplete(flags, protocol, username, password,
class URL {
constructor(input, base) {
// toUSVString is not needed.
input = '' + input;
input = `${input}`;
if (base !== undefined && !(base instanceof URL))
base = new URL(base);
parse(this, input, base);
Expand Down Expand Up @@ -329,7 +329,7 @@ Object.defineProperties(URL.prototype, {
},
set(input) {
// toUSVString is not needed.
input = '' + input;
input = `${input}`;
parse(this, input);
}
},
Expand All @@ -348,7 +348,7 @@ Object.defineProperties(URL.prototype, {
},
set(scheme) {
// toUSVString is not needed.
scheme = '' + scheme;
scheme = `${scheme}`;
if (scheme.length === 0)
return;
binding.parse(scheme, binding.kSchemeStart, null, this[context],
Expand All @@ -363,7 +363,7 @@ Object.defineProperties(URL.prototype, {
},
set(username) {
// toUSVString is not needed.
username = '' + username;
username = `${username}`;
if (!this.hostname)
return;
const ctx = this[context];
Expand All @@ -384,7 +384,7 @@ Object.defineProperties(URL.prototype, {
},
set(password) {
// toUSVString is not needed.
password = '' + password;
password = `${password}`;
if (!this.hostname)
return;
const ctx = this[context];
Expand All @@ -410,7 +410,7 @@ Object.defineProperties(URL.prototype, {
set(host) {
const ctx = this[context];
// toUSVString is not needed.
host = '' + host;
host = `${host}`;
if (this[cannotBeBase] ||
(this[special] && host.length === 0)) {
// Cannot set the host if cannot-be-base is set or
Expand All @@ -435,7 +435,7 @@ Object.defineProperties(URL.prototype, {
set(host) {
const ctx = this[context];
// toUSVString is not needed.
host = '' + host;
host = `${host}`;
if (this[cannotBeBase] ||
(this[special] && host.length === 0)) {
// Cannot set the host if cannot-be-base is set or
Expand All @@ -460,7 +460,7 @@ Object.defineProperties(URL.prototype, {
},
set(port) {
// toUSVString is not needed.
port = '' + port;
port = `${port}`;
const ctx = this[context];
if (!ctx.host || this[cannotBeBase] ||
this.protocol === 'file:')
Expand All @@ -484,7 +484,7 @@ Object.defineProperties(URL.prototype, {
},
set(path) {
// toUSVString is not needed.
path = '' + path;
path = `${path}`;
if (this[cannotBeBase])
return;
binding.parse(path, binding.kPathStart, null, this[context],
Expand Down Expand Up @@ -533,7 +533,7 @@ Object.defineProperties(URL.prototype, {
set(hash) {
const ctx = this[context];
// toUSVString is not needed.
hash = '' + hash;
hash = `${hash}`;
if (this.protocol === 'javascript:')
return;
if (!hash) {
Expand Down Expand Up @@ -1125,12 +1125,12 @@ function originFor(url, base) {

function domainToASCII(domain) {
// toUSVString is not needed.
return binding.domainToASCII('' + domain);
return binding.domainToASCII(`${domain}`);
}

function domainToUnicode(domain) {
// toUSVString is not needed.
return binding.domainToUnicode('' + domain);
return binding.domainToUnicode(`${domain}`);
}

// Utility function that converts a URL object into an ordinary
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ test(function() {
params.set('a');
}, /^TypeError: "name" and "value" arguments must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.set(obj, 'b'), /^Error: toString$/);
assert.throws(() => params.set('a', obj), /^Error: toString$/);
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ test(() => {
}

{
const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();

assert.throws(() => new URLSearchParams({ a: obj }), /^Error: toString$/);
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ test(function() {
params.delete();
}, /^TypeError: "name" argument must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.delete(obj), /^Error: toString$/);
assert.throws(() => params.delete(sym),
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ test(function() {
params.get();
}, /^TypeError: "name" argument must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.get(obj), /^Error: toString$/);
assert.throws(() => params.get(sym),
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-getall.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ test(function() {
params.getAll();
}, /^TypeError: "name" argument must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.getAll(obj), /^Error: toString$/);
assert.throws(() => params.getAll(sym),
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ test(function() {
params.has();
}, /^TypeError: "name" argument must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.has(obj), /^Error: toString$/);
assert.throws(() => params.has(sym),
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ test(function() {
params.set('a');
}, /^TypeError: "name" and "value" arguments must be specified$/);

const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
assert.throws(() => params.append(obj, 'b'), /^Error: toString$/);
assert.throws(() => params.append('a', obj), /^Error: toString$/);
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-whatwg-url-setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ startURLSettersTests()

{
const url = new URL('http://example.com/');
const obj = { toString() { throw new Error('toString'); } };
const obj = {
toString() { throw new Error('toString'); },
valueOf() { throw new Error('valueOf'); }
};
const sym = Symbol();
const props = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(url));
for (const [name, { set }] of Object.entries(props)) {
Expand Down