Relationship between globals, Realms, and global environment records
Allen Wirfs-Brock
allen at wirfs-brock.com
Tue Nov 25 08:11:33 PST 2014
On Nov 25, 2014, at 2:14 AM, Anne van Kesteren wrote:
> On Tue, Nov 25, 2014 at 1:51 AM, Allen Wirfs-Brock
> <allen at wirfs-brock.com> wrote:
>> There is definite a 1:1:1 relationship between a global object, realmRec,
>> global environment record. And ES views those relationships are immutable.
>> You can't just change the global object of a realmRec or give it a different
>> global environment record after it has been initialized.
>
> How would you test whether all three of them have changed?
Changed in what sense and from what perspective?
Every ES function object is associated with a realm when the function object is created. That association can't be changed as it would violated internal invariants of the function. For example:
function f() {
let gPO = Object.getPrototypeOf;
let a = gPO( [ ] );
doSomething();
let b = gPO( [ ] );
assert(a===b);
}
The "current realm" dynamically tracks the realm of the actively executing ES function. The only way to change the "current realm" is to transfer control (via call, return, throw) to a function that was created with a different realm association.
regarding
var document = someDocument.open(...);
perhaps you can model this by saying that every Document object is created in a new Realm. Then the 'open' call is a cross-realm call that changes the current Realm to someDocument's realm. When 'open' returns the current Realm is restored to the original caller's realm. 'document' is just a property of the caller realm's global object so the assignment just changes the value of that property. Any subsequent method invocations on 'document' will be cross-realm calls into someDocument's realm.
Allen
More information about the es-discuss
mailing list