From 0a8c33123ef61e29d8e9e9f75360802ce8cb15ca Mon Sep 17 00:00:00 2001 From: XadillaX Date: Mon, 23 Aug 2021 17:58:47 +0800 Subject: [PATCH] lib,url: correct URL's argument to pass idlharness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `url.idl` defines URL's constructor as: ``` constructor(USVString url, optional USVString base); ``` `idlharness.any.js` checks its length as `1`. So we should remove constructor's second argument and use `arguments[1]` in constructor's logic. Refs: https://url.spec.whatwg.org/#idl-index PR-URL: https://github.com/nodejs/node/pull/39848 Backport-PR-URL: https://github.com/nodejs/node/pull/40383 Reviewed-By: Tobias Nießen Reviewed-By: James M Snell --- lib/internal/url.js | 2 +- test/common/wpt.js | 40 +++++++++++++++++++++++++++++++++++++++- test/wpt/status/url.json | 6 +++--- test/wpt/test-url.js | 1 + 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 4d9e978f8305ce..3035c9c1979ed1 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -617,7 +617,7 @@ function onParseHashComplete(flags, protocol, username, password, } class URL { - constructor(input, base) { + constructor(input, base = undefined) { // toUSVString is not needed. input = `${input}`; let base_context; diff --git a/test/common/wpt.js b/test/common/wpt.js index 3e2f8151a6c20b..52f48685f315e3 100644 --- a/test/common/wpt.js +++ b/test/common/wpt.js @@ -288,6 +288,7 @@ class WPTRunner { this.resource = new ResourceLoader(path); this.flags = []; + this.dummyGlobalThisScript = null; this.initScript = null; this.status = new StatusLoader(path); @@ -318,6 +319,43 @@ class WPTRunner { this.initScript = script; } + get fullInitScript() { + if (this.initScript === null && this.dummyGlobalThisScript === null) { + return null; + } + + if (this.initScript === null) { + return this.dummyGlobalThisScript; + } else if (this.dummyGlobalThisScript === null) { + return this.initScript; + } + + return `${this.fullInitScript}\n\n//===\n${this.initScript}`; + } + + /** + * Pretend the runner is run in `name`'s environment (globalThis). + * @param {'Window'} name + * @see {@link https://github.com/nodejs/node/blob/24673ace8ae196bd1c6d4676507d6e8c94cf0b90/test/fixtures/wpt/resources/idlharness.js#L654-L671} + */ + pretendGlobalThisAs(name) { + switch (name) { + case 'Window': { + this.dummyGlobalThisScript = + 'global.Window = Object.getPrototypeOf(globalThis).constructor;'; + break; + } + + // TODO(XadillaX): implement `ServiceWorkerGlobalScope`, + // `DedicateWorkerGlobalScope`, etc. + // + // e.g. `ServiceWorkerGlobalScope` should implement dummy + // `addEventListener` and so on. + + default: throw new Error(`Invalid globalThis type ${name}.`); + } + } + // TODO(joyeecheung): work with the upstream to port more tests in .html // to .js. runJsTests() { @@ -368,7 +406,7 @@ class WPTRunner { testRelativePath: relativePath, wptRunner: __filename, wptPath: this.path, - initScript: this.initScript, + initScript: this.fullInitScript, harness: { code: fs.readFileSync(harnessPath, 'utf8'), filename: harnessPath, diff --git a/test/wpt/status/url.json b/test/wpt/status/url.json index ab89356f82c805..36ff89e8e07d85 100644 --- a/test/wpt/status/url.json +++ b/test/wpt/status/url.json @@ -13,9 +13,6 @@ "urlencoded-parser.any.js": { "fail": "missing Request and Response" }, - "idlharness.any.js": { - "fail": "getter/setter names are wrong, etc." - }, "urlsearchparams-constructor.any.js": { "fail": "FormData is not defined" }, @@ -30,5 +27,8 @@ }, "url-setters-a-area.window.js": { "skip": "already tested in url-setters.any.js" + }, + "idlharness.any.js": { + "fail": "Fixed in a semver-major change: https://github.com/nodejs/node/pull/39752" } } diff --git a/test/wpt/test-url.js b/test/wpt/test-url.js index 4652bfd880cd76..0b731dbe89c608 100644 --- a/test/wpt/test-url.js +++ b/test/wpt/test-url.js @@ -15,4 +15,5 @@ runner.setInitScript(` global.DOMException = DOMException; `); +runner.pretendGlobalThisAs('Window'); runner.runJsTests();