ecma262 icon indicating copy to clipboard operation
ecma262 copied to clipboard

Editorial: Create the realm execution context slightly later

Open jmdyck opened this issue 1 year ago • 3 comments

InitializeHostDefinedRealm (IHDR), in addition to creating a realm, also creates an execution context (EC) to house it, and pushes that EC onto the EC stack. In the status quo, the EC-related steps appear in the midst of the realm-creation steps, which is odd, and doesn't appear to be necessary. (You might think that the later realm-creation steps somehow assume that the realm EC exists and has been pushed on the EC stack, but as far as I can tell, they don't. I think it's just an artifact of how IHDR was introduced in ES6.)

This PR moves the EC-related steps to a point after the realm's fields have been finalized.

(This makes it easier to see that the early settings of [[GlobalObject]] and [[GlobalEnv]] to *undefined* are superfluous. I would have deleted those steps, but that overlaps with PR #3445.)


I would have preferred to move the EC-related steps to just before the Return step. However, that would be a change in behavior. The invocation of SetDefaultGlobalBindings can return an abrupt completion, which is immediately returned by IHDR. In such a case, the status quo says that the EC-related things have happened, which is a side-effect that can be observed by IHDR's caller. In particular, when the HTML spec invokes IHDR, it ignores the return value; it fetches the realm EC from the EC stack regardless of whether IHDR returned an abrupt completion.

(Mind you, I think the only way that SetDefaultGlobalBindings could throw is if IHDR's caller provided a global object that was specifically designed to make that happen.)


I suggested this change in https://github.com/tc39/ecma262/pull/3445#issuecomment-2489897373

This should probably have been a quick follow-up to PR #3139.

See also PR #3274, which considers changing some of the linkage between IHDR and its caller.

jmdyck avatar Nov 21 '24 15:11 jmdyck

Ah, in the sense of "the current Realm Record" = "the Realm component of the running execution context".

But if we first changed IHDR as described in https://github.com/tc39/ecma262/pull/3274#issuecomment-2495512097, that would give you a more direct way to access the realm being created, and then this PR wouldn't be a problem?

jmdyck avatar Nov 26 '24 14:11 jmdyck

Yes, that would be fine. I just need a reasonable way to get the (new) realm.

Ms2ger avatar Nov 26 '24 15:11 Ms2ger

The rendered spec for this PR is available at https://tc39.es/ecma262/pr/3497.

github-actions[bot] avatar Nov 05 '25 19:11 github-actions[bot]