LibWeb: Fix entry_realm() to correctly find the realm execution context

entry_realm() was using the topmost execution context, but the spec
defines the entry execution context as the most recently pushed *realm*
execution context — the one owned by the environment settings object.

In a synchronous cross-window call, JS function calls push additional
execution contexts above the entry realm, causing the wrong realm to
be returned. Fix this by walking the stack to find the context that
matches its environment settings object's realm execution context.
This commit is contained in:
Shannon Booth
2026-03-22 20:37:29 +01:00
committed by Jelle Raaijmakers
parent 406a32c366
commit 9d7b18f517
Notes: github-actions[bot] 2026-03-23 08:01:58 +00:00

View File

@@ -520,8 +520,14 @@ JS::Realm& entry_realm()
// With this in hand, we define the entry execution context to be the most recently pushed item in the JavaScript execution context stack that is a realm execution context.
// The entry realm is the principal realm of the entry execution context's Realm component.
// NOTE: Currently all execution contexts in LibJS are realm execution contexts
return principal_realm(*vm.running_execution_context().realm);
auto entry_execution_context = vm.execution_context_stack().last_matching([](JS::ExecutionContext* context) {
if (!context->realm)
return false;
return &principal_realm_settings_object(*context->realm).realm_execution_context() == context;
});
VERIFY(entry_execution_context.has_value());
return *entry_execution_context.value()->realm;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#entry-settings-object