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

testharness.js: fix shadow realm detection #33668

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cookie-store/serviceworker_cookieStore_cross_origin.js
@@ -1,6 +1,7 @@
self.GLOBAL = {
isWindow: () => false,
isWorker: () => false,
isShadowRealm: () => false,
};

self.addEventListener('message', async event => {
Expand Down
10 changes: 9 additions & 1 deletion resources/idlharness-shadowrealm.js
Expand Up @@ -29,7 +29,15 @@ function idl_test_shadowrealm(srcs, deps) {
promise_setup(async t => {
const realm = new ShadowRealm();
// https://github.com/web-platform-tests/wpt/issues/31996
realm.evaluate("globalThis.self = globalThis; undefined");
realm.evaluate("globalThis.self = globalThis; undefined;");

realm.evaluate(`
globalThis.self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
};
`);

const ss = await Promise.all(script_urls.map(url => fetch_text(url)));
for (const s of ss) {
Expand Down
10 changes: 4 additions & 6 deletions resources/testharness.js
Expand Up @@ -537,13 +537,11 @@
}
/* Shadow realm global objects are _ordinary_ objects (i.e. their prototype is
* Object) so we don't have a nice `instanceof` test to use; instead, we
* can look for the presence of web APIs that wouldn't be available in
* environments not listed above:
*
* As long as, within the shadow realm, we load the testharness before
* other libraries, this won't have any false positives, even in e.g. node
* check if the there is a GLOBAL.isShadowRealm() property
* on the global object. that was set by the test harness when it
* created the ShadowRealm.
*/
if ('AbortController' in global_scope) {
if (global_scope.GLOBAL.isShadowRealm()) {
return new ShadowRealmTestEnvironment();
}

Expand Down
8 changes: 6 additions & 2 deletions tools/serve/serve.py
Expand Up @@ -259,6 +259,7 @@ class AnyHtmlHandler(HtmlWrapperHandler):
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="/resources/testharness.js"></script>
Expand Down Expand Up @@ -360,12 +361,13 @@ class ShadowRealmHandler(HtmlWrapperHandler):
await new Promise(r.evaluate(`
(resolve, reject) => {
(async () => {
await import("/resources/testharness.js");
%(script)s
globalThis.self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
};
await import("/resources/testharness.js");
%(script)s
await import("%(path)s");
})().then(resolve, (e) => reject(e.toString()));
}
Expand Down Expand Up @@ -411,6 +413,7 @@ class ClassicWorkerHandler(BaseWorkerHandler):
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
importScripts("/resources/testharness.js");
%(script)s
Expand All @@ -428,6 +431,7 @@ class ModuleWorkerHandler(BaseWorkerHandler):
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
import "/resources/testharness.js";
%(script)s
Expand Down
1 change: 1 addition & 0 deletions tools/wptserve/tests/functional/docroot/bar.any.worker.js
Expand Up @@ -2,6 +2,7 @@
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
importScripts("/resources/testharness.js");

Expand Down
1 change: 1 addition & 0 deletions tools/wptserve/tests/functional/docroot/foo.any.html
Expand Up @@ -5,6 +5,7 @@
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="/resources/testharness.js"></script>
Expand Down