You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Handle the FinalizationRegistry's context in HostEnqueueFinalizationRegistryCleanupJob (#111)
Each FinalizationRegistry object has a context associated with it, which was the
context at its construction. This context is used for calling its callback when
any registered object is GC'd, which takes place in the
`CleanupFinalizationRegistry` AO.
In the current spec, `CleanupFinalizationRegistry` handles the swapping of this
context, and restores it back at the end. This, however, causes a problem for
hosts that report uncaught JS exceptions by running JS code (such as the
`"error"` event on `window` in the web, or the `"unhandledException"` event on
`process` in Node.js), since they should run this event in the
FinalizationRegistry's creation context, but `CleanupFinalizationRegistry` exits
that context before the end of the function.
Since `CleanupFinalizationRegistry` (and
`HostEnqueueFinalizationRegistryCleanupJob`) only clean up a single
FinalizationRegitry, however, the context swapping can be pushed to the host
hook instead, with the step about host-defined error reporting being inside that
context. This ensures that such error reporting events are fired in the right
context.
): either a normal completion containing ~unused~ or a throw completion
138
-
</h1>
139
-
<dlclass="header">
140
-
</dl>
141
-
<emu-alg>
142
-
1. <del>Assert: _finalizationRegistry_ has [[Cells]] and [[CleanupCallback]] internal slots.</del>
143
-
1. <ins>Assert: _finalizationRegistry_ has [[Cells]], [[CleanupCallback]], and [[FinalizationRegistryAsyncContextMapping]] internal slots.</ins>
144
-
1. Let _callback_ be _finalizationRegistry_.[[CleanupCallback]].
145
-
1. While _finalizationRegistry_.[[Cells]] contains a Record _cell_ such that _cell_.[[WeakRefTarget]] is ~empty~, an implementation may perform the following steps:
146
-
1. Choose any such _cell_.
147
-
1. Remove _cell_ from _finalizationRegistry_.[[Cells]].
<p>Let _cleanupJob_ be a new Job Abstract Closure with no parameters that captures _finalizationRegistry_ and performs the following steps when called:</p>
148
+
<emu-alg>
149
+
1. <ins>Let _previousContextMapping_ be AsyncContextSwap(_finalizationRegistry_.[[FinalizationRegistryAsyncContextMapping]]).</ins>
150
+
1. Let _cleanupResult_ be Completion(CleanupFinalizationRegistry(_finalizationRegistry_)).
151
+
1. If _cleanupResult_ is an abrupt completion, perform any host-defined steps for reporting the error.
<p>An implementation of HostEnqueueFinalizationRegistryCleanupJob schedules _cleanupJob_ to be performed at some future time, if possible. It must also conform to the requirements in <emu-xrefhref="#sec-jobs"></emu-xref>.</p>
0 commit comments