9.10 Processing Model of WeakRef and FinalizationRegistry Targets
9.10.1 Objectives
This specification does not make any guarantees that any object or symbol will be garbage collected. Objects or symbols which are not
The semantics of
-
When
WeakRef.prototype.deref
is called, the referent (ifundefined is not returned) is kept alive so that subsequent, synchronous accesses also return the same value. This list is reset when synchronous work is done using theClearKeptObjects abstract operation. -
When an object or symbol which is registered with a
FinalizationRegistry becomes unreachable, a call of theFinalizationRegistry 's cleanup callback may eventually be made, after synchronous ECMAScript execution completes. TheFinalizationRegistry cleanup is performed with theCleanupFinalizationRegistry abstract operation.
Neither of these actions (
Some ECMAScript implementations include garbage collector implementations which run in the background, including when ECMAScript is idle. Letting the
9.10.2 Liveness
For some set of objects and/or symbols S a hypothetical WeakRef-oblivious execution with respect to S is an execution whereby the abstract operation
At any point during evaluation, a set of objects and/or symbols S is considered live if either of the following conditions is met:
-
Any element in S is included in any
agent 's [[KeptAlive]]List . - There exists a valid future hypothetical WeakRef-oblivious execution with respect to S that observes the identity of any value in S.
Presence of an object or a symbol in a field, an internal slot, or a property does not imply that the value is live. For example if the value in question is never passed back to the program, then it cannot be observed.
This is the case for keys in a WeakMap, members of a WeakSet, as well as the [[WeakRefTarget]] and [[UnregisterToken]] fields of a
The above definition implies that, if a key in a WeakMap is not live, then its corresponding value is not necessarily live either.
9.10.3 Execution
At any time, if a set of objects and/or symbols S is not
- For each element value of S, do
- For each
WeakRef ref such that ref.[[WeakRefTarget]] is value, do- Set ref.[[WeakRefTarget]] to
empty .
- Set ref.[[WeakRefTarget]] to
- For each
FinalizationRegistry fg such that fg.[[Cells]] contains aRecord cell such that cell.[[WeakRefTarget]] is value, do- Set cell.[[WeakRefTarget]] to
empty . - Optionally, perform
HostEnqueueFinalizationRegistryCleanupJob (fg).
- Set cell.[[WeakRefTarget]] to
- For each WeakMap map such that map.[[WeakMapData]] contains a
Record r such that r.[[Key]] is value, do- Set r.[[Key]] to
empty . - Set r.[[Value]] to
empty .
- Set r.[[Key]] to
- For each WeakSet set such that set.[[WeakSetData]] contains value, do
- Replace the element of set.[[WeakSetData]] whose value is value with an element whose value is
empty .
- Replace the element of set.[[WeakSetData]] whose value is value with an element whose value is
- For each
Together with the definition of liveness, this clause prescribes optimizations that an implementation may apply regarding
It is possible to access an object without observing its identity. Optimizations such as dead variable elimination and scalar replacement on properties of non-escaping objects whose identity is not observed are allowed. These optimizations are thus allowed to observably empty
On the other hand, if an object's identity is observable, and that object is in the [[WeakRefTarget]] internal slot of a
Because calling
Implementations are not obligated to empty
If an implementation chooses a non-
9.10.4 Host Hooks
9.10.4.1 HostEnqueueFinalizationRegistryCleanupJob ( finalizationRegistry )
The
Let cleanupJob be a new
- Let cleanupResult be
Completion (CleanupFinalizationRegistry (finalizationRegistry)). - If cleanupResult is an
abrupt completion , perform anyhost-defined steps for reporting the error. - Return
unused .
An implementation of HostEnqueueFinalizationRegistryCleanupJob schedules cleanupJob to be performed at some future time, if possible. It must also conform to the requirements in