Skip to content

Commit

Permalink
url,lib: pass urlsearchparams-constructor.any.js
Browse files Browse the repository at this point in the history
According to WPT:

1. `URLSearchParams` constructor should throw exactly `TypeError` if any
   Error occurrs.
2. When a record passed to `URLSearchParams` constructor, two different
   key may result same after `toUVString()`. We should leave only the
   later one.

PR-URL: #41197
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
  • Loading branch information
XadillaX authored and targos committed Jul 31, 2022
1 parent b5eb556 commit 69f9a3a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
33 changes: 24 additions & 9 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@ const {
TypeError,
} = primordials;

class ERR_INVALID_THIS extends TypeError {
constructor(type) {
super('Value of "this" must be of ' + type);
}

get code() { return 'ERR_INVALID_THIS'; }
function throwInvalidThisError(Base, type) {
const err = new Base();
const key = 'ERR_INVALID_THIS';
ObjectDefineProperties(err, {
message: {
value: `Value of "this" must be of ${type}`,
enumerable: false,
writable: true,
configurable: true,
},
toString: {
value() {
return `${this.name} [${key}]: ${this.message}`;
},
enumerable: false,
writable: true,
configurable: true,
},
});
err.code = key;
throw err;
}

let internalsMap;
Expand Down Expand Up @@ -51,7 +66,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
return internals.name;
}
Expand All @@ -60,7 +75,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
return internals.message;
}
Expand All @@ -69,7 +84,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
const code = nameToCodeMap.get(internals.name);
return code === undefined ? 0 : code;
Expand Down
12 changes: 11 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class URLSearchParams {
} else {
// Record<USVString, USVString>
// Need to use reflection APIs for full spec compliance.
const visited = {};
this[searchParams] = [];
const keys = ReflectOwnKeys(init);
for (let i = 0; i < keys.length; i++) {
Expand All @@ -228,7 +229,16 @@ class URLSearchParams {
if (desc !== undefined && desc.enumerable) {
const typedKey = toUSVString(key);
const typedValue = toUSVString(init[key]);
this[searchParams].push(typedKey, typedValue);

// Two different key may result same after `toUSVString()`, we only
// leave the later one. Refers to WPT.
if (visited[typedKey] !== undefined) {
this[searchParams][visited[typedKey]] = typedValue;
} else {
visited[typedKey] = ArrayPrototypePush(this[searchParams],
typedKey,
typedValue) - 1;
}
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/wpt/status/url.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"urlencoded-parser.any.js": {
"fail": "missing Request and Response"
},
"urlsearchparams-constructor.any.js": {
"fail": "FormData is not defined"
},
"url-constructor.any.js": {
"requires": ["small-icu"]
},
Expand Down

0 comments on commit 69f9a3a

Please sign in to comment.