diff --git a/cookie-store/serviceworker_cookieStore_cross_origin.js b/cookie-store/serviceworker_cookieStore_cross_origin.js
index ac59b703dd7565..fa1c4084fd45f1 100644
--- a/cookie-store/serviceworker_cookieStore_cross_origin.js
+++ b/cookie-store/serviceworker_cookieStore_cross_origin.js
@@ -1,6 +1,7 @@
self.GLOBAL = {
isWindow: () => false,
isWorker: () => false,
+ isShadowRealm: () => false,
};
self.addEventListener('message', async event => {
diff --git a/resources/idlharness-shadowrealm.js b/resources/idlharness-shadowrealm.js
index f9c1e37c1c427a..631278db22d8fb 100644
--- a/resources/idlharness-shadowrealm.js
+++ b/resources/idlharness-shadowrealm.js
@@ -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) {
diff --git a/resources/testharness.js b/resources/testharness.js
index 2c5df32b8c73c8..74124265923d7e 100644
--- a/resources/testharness.js
+++ b/resources/testharness.js
@@ -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();
}
diff --git a/tools/serve/serve.py b/tools/serve/serve.py
index 6e6f2c1c466f9b..55a4704faac2ca 100644
--- a/tools/serve/serve.py
+++ b/tools/serve/serve.py
@@ -259,6 +259,7 @@ class AnyHtmlHandler(HtmlWrapperHandler):
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
};
@@ -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()));
}
@@ -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
@@ -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
diff --git a/tools/wptserve/tests/functional/docroot/bar.any.worker.js b/tools/wptserve/tests/functional/docroot/bar.any.worker.js
index 66b7be39049eed..baecd2ac54f2dc 100644
--- a/tools/wptserve/tests/functional/docroot/bar.any.worker.js
+++ b/tools/wptserve/tests/functional/docroot/bar.any.worker.js
@@ -2,6 +2,7 @@
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
+ isShadowRealm: function() { return false; },
};
importScripts("/resources/testharness.js");
diff --git a/tools/wptserve/tests/functional/docroot/foo.any.html b/tools/wptserve/tests/functional/docroot/foo.any.html
index 88e2665c669042..8d64adc136bb29 100644
--- a/tools/wptserve/tests/functional/docroot/foo.any.html
+++ b/tools/wptserve/tests/functional/docroot/foo.any.html
@@ -5,6 +5,7 @@
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
};