Skip to content

Instantly share code, notes, and snippets.

@dherman
Last active December 27, 2015 10:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherman/7312578 to your computer and use it in GitHub Desktop.
Save dherman/7312578 to your computer and use it in GitHub Desktop.
Why (hookable) eval/Function, globals, and realms are all necessarily coupled.

Design "theorem".

The (hookable) eval/Function function objects, the global object, and the realm all have to be in 1:1 correspondence.

"Proof".

(a) same realm ==> same Function

Since Function is reachable by function literals, it is tied to a realm.

(b) same realm ==> same eval

Since the semantics of the direct eval syntax checks the object identity of the original eval function object, it is tied to a realm.

(c) same realm ==> same global object

Since Function and indirect eval are implicitly "closed over" a global object (i.e., they evaluate code in the scope of a fixed global object), it shouldn't be possible for two loaders to share a realm but have different globals, as the Function constructor would use the wrong global in one of them.

(d) same global ==> same realm

It's pretty hard to imagine writing sensible code where new Array and [] or new Object and {} have distinct prototypes. While technically we could imagine breaking this invariant, real-world JS depends on basic built-in classes like Array being there and behaving predictably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment