Relationship between globals, Realms, and global environment records
allen at wirfs-brock.com
Tue Nov 25 10:06:44 PST 2014
On Nov 25, 2014, at 9:07 AM, Boris Zbarsky wrote:
> On 11/25/14, 11:45 AM, Allen Wirfs-Brock wrote:
>> I'm simply trying to understand what what is meant when HTML talks about changing the global object/realm/etc.
> Ah. That part is simple.
> Per current HTML spec (and the Gecko implementation) when document.open() is performed, a new Realm and global object (Window) is created. The WindowProxy involved is changed to point to the new global object. The new Window points to the same Document that the old Window pointed to.
>> and how that can be reconciled with a rational language semantics such as exhibited by the function I presented.
> Again per current HTML spec existing functions don't change in any way. They continue to perform bareword lookups against the global that corresponds to their Realm. At least that's how I interpret the spec and how Gecko implements it; the spec itself is not very clear on this, because it doesn't actually define how WindowProxy works.
> In any case, that's my take on the situation. ;)
Assuming that the global object is a WindowProxy then (and assuming the semantics of Window Proxy allows it) HTML can retarget the proxy however it wants. Under the ES semantics if a Realm is created is with such a proxy as its global object then its global environment record will also (immutably) reference the same proxy when doing name lookups. So by changing the target of the proxy you can change the observed global bindings of IdentifierReferences.
But there are many places (anywhere you see something like %foo% in the ES6 spec) where ES accesses built-ins directly from the current realm without doing a name resolution. That is why in the test function I provided I used [ ] to create Array instances. The prototype of such objects is %ArrayPrototype% from the function's realm, not the result of evaluating 'Array.prototype'.
More information about the es-discuss