LibWeb: Ensure principal realm returned for nested Shadow Realms

Recently reported against the shadow realm proposal after running into
issues with WPT tests.

In a nested shadow realm, the associated realm is a shadow realm, not
the principal realm. One such issue this fixes is a crash when a nested
shadow realm performs an operation which requires the principal settings
object.
This commit is contained in:
Shannon Booth 2024-11-28 05:10:30 +13:00 committed by Andreas Kling
commit 91007eb476
Notes: github-actions[bot] 2024-11-30 11:07:04 +00:00
3 changed files with 44 additions and 2 deletions

View file

@ -580,8 +580,8 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
// 4. Set settings's execution context to context.
.execution_context = move(context),
// 5. Set settings's principal realm to O's associated realm
.principal_realm = object.shape().realm(),
// 5. Set settings's principal realm to O's associated realm's principal realm
.principal_realm = HTML::principal_realm(object.shape().realm()),
// 6. Set settings's module map to a new module map, initially empty.
.module_map = realm.create<HTML::ModuleMap>(),

View file

@ -0,0 +1 @@
Some event...

View file

@ -0,0 +1,41 @@
<script src="../include.js"></script>
<script>
function shadowRealmEvalAsync(realm, asyncBody) {
return new Promise(realm.evaluate(`
(resolve, reject) => {
(async () => {
${asyncBody}
})().then(resolve, (e) => reject(e.toString()));
}
`));
};
asyncTest(async done => {
const outerShadowRealm = new ShadowRealm();
outerShadowRealm.evaluate(`
var innerShadowRealm = new ShadowRealm();
`);
const outerResult = await shadowRealmEvalAsync(outerShadowRealm, `
function shadowRealmEvalAsync(realm, asyncBody) {
return new Promise(realm.evaluate(\`
(resolve, reject) => {
(async () => {
\${asyncBody}
})().then(resolve, (e) => reject(e.toString()));
}
\`));
};
const innerResult = await shadowRealmEvalAsync(innerShadowRealm, \`
return new Event("Some event...").type;
\`);
return innerResult;
`);
println(outerResult);
done();
});
</script>