Skip to content

Commit 77a3c0b

Browse files
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.
1 parent 67e7074 commit 77a3c0b

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

spec.html

+25-22
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,31 @@ <h1>Agents</h1>
130130
</emu-table>
131131
</emu-clause>
132132

133-
<emu-clause id="sec-cleanup-finalization-registry" type="abstract operation">
134-
<h1>
135-
CleanupFinalizationRegistry (
136-
_finalizationRegistry_: a FinalizationRegistry,
137-
): either a normal completion containing ~unused~ or a throw completion
138-
</h1>
139-
<dl class="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]].
148-
1. <del>Perform ? HostCallJobCallback(_callback_, *undefined*, « _cell_.[[HeldValue]] »).</del>
149-
1. <ins>Let _previousContextMapping_ be AsyncContextSwap(_finalizationRegistry_.[[FinalizationRegistryAsyncContextMapping]]).</ins>
150-
1. <ins>Let _result_ be Completion(HostCallJobCallback(_callback_, *undefined*, « _cell_.[[HeldValue]] »)).</ins>
151-
1. <ins>AsyncContextSwap(_previousContextMapping_).</ins>
152-
1. <ins>Perform ? _result_.</ins>
153-
1. Return ~unused~.
154-
</emu-alg>
133+
<emu-clause id="sec-weakref-processing-model">
134+
<h1>Processing Model of WeakRef and FinalizationRegistry Targets</h1>
135+
136+
<emu-clause id="sec-weakref-host-hooks">
137+
<h1>Host Hooks</h1>
138+
139+
<emu-clause id="sec-host-cleanup-finalization-registry" type="host-defined abstract operation">
140+
<h1>
141+
HostEnqueueFinalizationRegistryCleanupJob (
142+
_finalizationRegistry_: a FinalizationRegistry,
143+
): ~unused~
144+
</h1>
145+
<dl class="header">
146+
</dl>
147+
<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.
152+
1. <ins>AsyncContextSwap(_previousContextMapping_).</ins>
153+
1. Return ~unused~.
154+
</emu-alg>
155+
<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-xref href="#sec-jobs"></emu-xref>.</p>
156+
</emu-clause>
157+
</emu-clause>
155158
</emu-clause>
156159
</emu-clause>
157160

0 commit comments

Comments
 (0)