Recursive AI

WindowProxy and navigation: design note

All oneecma docs

A browsing context has one WindowProxy for its whole life and, over time, several Window objects (each in its own realm): navigating to a new document creates a new Window and the proxy starts pointing at it. Script keeps references to the proxy (window, self, frames[0], opener), so the proxy has to keep its identity while everything behind it changes. HTML 7.2.2 defines it as an exotic object; this note says how the engine's pieces fit.

What exists

PieceWhereBehavior
JsRealm.NewWindowProxy(window)Hosting/Platform/JsWindowProxy.csAn exotic object forwarding [[GetPrototypeOf]], [[GetOwnProperty]], [[DefineOwnProperty]], [[HasProperty]], [[Get]], [[Set]], [[Delete]] and [[OwnPropertyKeys]] to its window. [[PreventExtensions]] fails, [[IsExtensible]] is true and the prototype is immutable (HTML 7.2.2.2 to 7.2.2.4).
JsRealm.SetWindowProxyTarget(proxy, window)sameSwaps the window. The proxy object, its identity and its [[Extensible]] state stay.
Realm.GlobalThis / JsRealm.SetGlobalThisValue(proxy)Runtime/Realm.csECMA-262 9.3 keeps the global object (where bindings live) apart from the global this value (what script sees as this and globalThis). The proxy is installed as the latter; name lookup and var declarations still use the Window.
Brand checksJsInterface.ReceiveA receiver that is a window proxy is replaced by its window before the brand test, so window.alert() and alert.call(window) both reach the Window (HTML 3.7.6 "if esValue is a WindowProxy, set it to its [[Window]]").
ScriptEngine.CreateRealm(JsGlobalFactory)Hosting/ScriptEngine.Hosting.csThe new Window is created like the first one; the host then points the proxy at it.

A navigation, in host code:

C#
JsRealm next = engine.CreateRealm(r => MiniDomRuntime.Create(r).WindowObject);   // the new document's realm and Window
JsRealm.SetWindowProxyTarget(proxy, next.Global);                                  // the proxy now answers for it
next.SetGlobalThisValue(proxy);                                                    // and is its `this` and `globalThis`

What a browser still adds

  • Cross-origin access. HTML 7.2.1 (cross-origin objects) gives a cross-origin Window only a short list of properties (location, postMessage, frames, ...) and answers everything else with a SecurityError. The proxy needs an IsPlatformObjectSameOrigin predicate supplied by the host, with the two property tables; the forwarding methods already have one place each to check it. Nothing here changes the public surface: it is a delegate on a new overload of NewWindowProxy.
  • The security check of Web IDL 3.7.6 and 3.7.7 ("perform a security check") on attributes and operations of a platform object that is cross-origin. The receiver check is the one place to add it; JsInterface.Receive would call the host's predicate with the receiver and member.
  • Child browsing contexts as indexed and named properties (window[0], window.frameName): [[GetOwnProperty]] of the proxy consults the host before forwarding. The legacy platform handler machinery (JsLegacyPlatformHandler) already expresses this; the proxy needs a handler slot.
  • Document and realm lifetime. Replaced Windows stay alive as long as script holds objects from them (same as a browser). The engine has one job queue and one limit set for all realms, so a discarded realm costs nothing once nothing refers to it.
  • The initial about:blank Window is reused for the first navigation when same-origin: create the proxy over the old Window, then swap only when the new Window is created, as HTML 7.4.2.2 "initialise the Document object" says.

A realm made with a global factory is part of the record log (CreateRealm(JsGlobalFactory): see record-log.md), so a page runtime that navigates still gets explained reports.