Editorial: InitializeHostDefinedRealm-related refactorings #3139
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In 9.6 InitializeHostDefinedRealm, there are two
If
-steps that allow the host to customize the global object and global*this*
binding. If the host just wants the default (of either), that's handled by passing*undefined*
to (the relevant parameter of)SetRealmGlobalObject
, which takes care of setting the realm's[[GlobalObject]]
and[[GlobalEnv]]
appropriately.This division of labor always struck me as a bit odd. E.g., why set
_global_
to*undefined*
and then have prose to explain the significance of doing so? Why not just set_global_
appropriately right there? That was the impetus for this PR. (My guess was thatSetRealmGlobalObject
was used by the HTML spec, but it isn't, and as far as I can tell it never was. Perhaps that was briefly someone's plan.)This PR is split into 6 commits for ease of review. If approved, I'll do some squashing.
I start by moving
InitializeHostDefinedRealm
to a better location.It's odd for
InitializeHostDefinedRealm
(clearly a realm-centric operation) to be out on its own in 9.6, when there's a "Realms" section at 9.3.(When Realms were introduced in ES6, the relative location of InitializeHostDefinedRealm was somewhat different and made more sense, but things have changed since then.)
Line-break the two "If the host requires" steps for readability.
Inline
SetRealmGlobalObject
intoInitializeHostDefinedRealm
.(Originally, I only hoisted out the
SetRealmGlobalObject
steps that assigned 'meanings' to the*undefined*
parameters, but that left so little inSetRealmGlobalObject
that I didn't see any reason for it to continue to exist.)Reorganize/simplify the result of the previous commit.
Tweak the return of
SetDefaultGlobalBindings
.In
SetDefaultGlobalBindings
,_global_
is an alias for_realmRec_.[[GlobalObject]]
. InInitializeHostDefinedRealm
, this is_realm_.[[GlobalObject]]
, which is just_global_
. ButSetDefaultGlobalBindings
returns that object, whichInitializeHostDefinedRealm
then assigns to_globalObj_
, meaning that we now have two aliases for the same object, which is unnecessarily confusing.So this commit tweaks
SetDefaultGlobalBindings
to return~unused~
, and tweaksInitializeHostDefinedRealm
to refer to_global_
rather than introducing_globalObj_
.Inline
CreateRealm
intoInitializeHostDefinedRealm
.This wasn't part of my original plan, but I like it because it surfaces both the early and later settings of
_realm_.[[GlobalObject]]
and_realm_.[[GlobalEnv]]
. This makes it really obvious that the early settings are transient. (See Editorial: More info in 'PromiseCapability Record' table #2380 (comment))Checking the downstream specs, nobody references
SetRealmGlobalObject
,CreateRealm
, orSetDefaultGlobalBindings
.