2

Background

When a browser opens a new window or tab, a new browsing context is created.

As we can see from the HTML spec, the operation that creates a new browsing context calls another operation that creates a new realm, which in turn calls the operation InitializeHostDefinedRealm defined in the ECMAScript spec.

The HTML spec operation that creates the browsing context states that when a new realm is created it should be done with a new Window object as the global object, and a WindowProxy object as the global this value.

Within InitializeHostDefinedRealm, we see that a new realm is created in step 1. In step 7, the global object is created in a host-defined manner.

If we take a look at the Web IDL spec, we can see how platform objects (instances of interface objects) are created.

For it to be possible for a platform object (like the global object window) to be created, the corresponding interface object (which is a function object) must have been created previously via the operation for creating interface objects. This is because the latter operation creates the interface prototype object for the interface.

Taking it back to the global object, creating the interface object Window will create Window.prototype, which will be used when the platform object window (an instance of interface object Window) is created.

Lastly, we also know from the operation that creates platform objects that the operation requires realm.[[GlobalObject]] to be present, for instance to check if an interface member is Exposed on a realm: Create platform object (see step 10.1) > Add Regular operations that are exposed on Window (see step 1.1) > exposed definition

Questions

  1. In InitializeHostDefinedRealm, realm.[[GlobalObject]] is not set until after the host has created the new Window object (step 9 vs step 7). realm.[[GlobalObject]] cannot be set without a global object present, and the global object window cannot be created without realm.[[GlobalObject]] present. How is this apparent deadlock solved?

  2. Step 9 of the operation that creates a browsing context states that a new Window object should be created as the global object, without any further details on how that is accomplished. Does it imply that the browser first creates the Window interface object and then the corresponding window platform object?

Magnus
  • 6,791
  • 8
  • 53
  • 84
  • You seem to be conflating the WebIDL interfaces, that are accessible only internally by the browser/specs and the exposed interface objects, which are accessible from JS. These aren't the same. Many interfaces aren't exposed anywhere (e.g TypedArray, or DOMMatrixInit etc.). When the specs ask to create a Window object they don't ask to create a JS object, but an instance of the WebIDL interface. – Kaiido Jan 06 '23 at 06:58
  • @Kaiido Agreed, but when the HTML spec does "*Perform [InitializeHostDefinedRealm()](https://tc39.es/ecma262/#sec-initializehostdefinedrealm) with the provided customizations for creating the global object and the global this binding.*", then those customisations would need to be the exposed JS objects, no? Or does this happen implicitly via the ECMAScript WebIDL binding? – Bergi Jan 06 '23 at 07:38
  • @Bergi yes I think that when ES says "let global be such an object created in a host-defined manner." there is everything needed to produce that JS object through WebIDL. – Kaiido Jan 06 '23 at 08:08
  • @Kaiido The Web IDL spec had a whole section on how interfaces translate to ECMAScript objects. It tells us the operations for creating *interface objects* and *platform objects*, both ECMAScript objects . If we are thinking about a browser's use of the spec to create the global object accessible in a JavaScript script, then surely *InitializeHostDefinedRealm*'s step 7 is asking the host to create an ECMAScript object out of the Window object, no? The Web IDL spec shows how that's done, and it isn't possible without a `realm.[[GlobalObject]]` already present. What am I missing? – Magnus Jan 06 '23 at 10:01
  • We have a chat here, if helpful to debate a bit: https://chat.stackoverflow.com/rooms/250856/room-for-magnus-and-bergi-web-idl – Magnus Jan 06 '23 at 10:51
  • @Kaiido The chain of operations showing the `realm.[[GlobalObject]]` requirement when creating a *platform object*: https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface (e.g. step 10.1) > https://webidl.spec.whatwg.org/#define-the-operations (step 1.1) > https://webidl.spec.whatwg.org/#dfn-exposed (**exposed** definition) – Magnus Jan 06 '23 at 11:01
  • 2
    ... You're right there is something fishy in here. I'm starting to doubt if we really call WebIDL's *new* here though. ES apparently has [its own way of creating the global object](https://tc39.es/ecma262/#sec-global-object). Maybe there is some allowed magic around here. I'll poke around on WHATWG's chat to try to get the attention of someone that might be more familiar. – Kaiido Jan 07 '23 at 10:19
  • 1
    @Domenic answered on whatwg's matrix: *It's essentially atomic. This is what is intended by https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context step 9 being a single step instead of something like "1. create the Window; 2. create the realm and supply the Window to it."* It's a bit late for me right now to do the follow up, and I hope he'll chime in here, but to me that kinda sounds like the "magic" supposition was close. – Kaiido Jan 07 '23 at 15:45

0 Answers0