A WeakRef is an object that is used to refer to a target object without preserving it from garbage collection. WeakRefs can dereference to allow access to the target object, if the target object hasn't been reclaimed by garbage collection.
The WeakRef constructor:
WeakRef
property of the extends
clause of a class definition. Subclass
constructors that intend to inherit the specified WeakRef
behaviour must include a super
call to the WeakRef
constructor
to create and initialize the subclass instance with the internal
state necessary to support the WeakRef.prototype
built-in
methods.
When the WeakRef
function is called with argument target,
the following steps are taken:
"%WeakRefPrototype%"
, « [[WeakRefTarget]] »).The WeakRef constructor:
The initial value of WeakRef.prototype
is the intrinsic
This property has the attributes { [[Writable]]:
The WeakRef prototype object:
The initial value of WeakRef.prototype.constructor
is the intrinsic object %WeakRef%.
This property has the attributes { [[Writable]]:
The following steps are taken:
target = { foo: function() {} }; let weakRef = new WeakRef(target); ... later ... if (weakRef.deref()) { weakRef.deref().foo(); }In the above example, if the first deref evaluates to true then the second deref can not fail.
The initial value of the @@toStringTag property is the String value "WeakRef"
.
This property has the attributes { [[Writable]]:
WeakRef instances are ordinary objects that inherit properties from the WeakRef prototype. WeakRef instances also have a [[WeakRefTarget]] internal slot.
WeakMap and WeakSet keys are not kept alive just because a WeakRef points
to them. This proposal rephrases the definition of WeakMaps and WeakSets
to explain their observable effects on garbage collection, rather than
specifying operationally that non-
WeakMap objects are collections of key/value pairs where the keys are objects and values may be arbitrary ECMAScript language values. A WeakMap may be queried to see if it contains a key/value pair with a specific key, but no mechanism is provided for enumerating the objects it holds as keys. In certain conditions, objects which are not If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references that start within that WeakMap, then that key/value pair is inaccessible and is automatically removed from the WeakMap. WeakMap implementations must detect and remove such key/value pairs and any associated resources.
WeakSet objects are collections of objects. A distinct object may only occur once as an element of a WeakSet's collection. A WeakSet may be queried to see if it contains a specific object, but no mechanism is provided for enumerating the objects it holds. In certain conditions, objects which are not If an object that is contained by a WeakSet is only reachable by following a chain of references that start within that WeakSet, then that object is inaccessible and is automatically removed from the WeakSet. WeakSet implementations must detect and remove such objects and any associated resources.
A FinalizationRegistry is an object that manages registration and unregistration of cleanup operations that are performed when target objects are garbage collected.
The FinalizationRegistry constructor:
FinalizationRegistry
property of
the extends
clause of a class definition. Subclass
constructors that intend to inherit the specified
FinalizationRegistry
behaviour must include a super
call to
the FinalizationRegistry
constructor to create and initialize
the subclass instance with the internal state necessary to
support the FinalizationRegistry.prototype
built-in methods.
When the FinalizationRegistry
function is called with argument cleanupCallback,
the following steps are taken:
"%FinalizationRegistryPrototype%"
, « [[Realm]], [[CleanupCallback]],
[[Cells]] »).The FinalizationRegistry constructor:
The initial value of FinalizationRegistry.prototype
is the
intrinsic
This property has the attributes { [[Writable]]:
The FinalizationRegistry prototype object:
The initial value of
FinalizationRegistry.prototype.constructor
is the intrinsic
object
The following steps are taken:
The following steps are taken:
The initial value of the @@toStringTag property is the String value "FinalizationRegistry"
.
This property has the attributes { [[Writable]]:
FinalizationRegistry instances are ordinary objects that inherit properties from the FinalizationRegistry prototype. FinalizationRegistry instances also have [[Cells]] and [[CleanupCallback]] internal slots.
Field Name | Value | Meaning |
---|---|---|
[[LittleEndian]] | Boolean | The default value computed for the isLittleEndian parameter when it is needed by the algorithms |
[[CanBlock]] | Boolean | Determines whether the agent can block or not. |
[[Signifier]] | Any globally-unique value | Uniquely identifies the agent within its agent cluster. |
[[IsLockFree1]] | Boolean | |
[[IsLockFree2]] | Boolean | |
[[CandidateExecution]] | A candidate execution |
See the memory model. |
[[KeptAlive]] | Initially a new empty |
This specification does not make any guarantees about garbage collection. Objects which are unreachable from ECMAScript may be released after long periods of time, or never at all. For this reason, this specification uses the term "may" when describing behavior triggered by garbage collection.
The semantics of WeakRef and FinalizationRegistry objects is based on two operations which happen at particular points in time:
Neither of these actions (
Some ECMAScript implementations include garbage collector implementations
which run in the background, including when ECMAScript is idle. Letting the
embedding environment schedule
For some set of objects S, a hypothetical WeakRef-oblivious
execution with respect to S is an execution whereby
WeakRef.prototype.deref being called on a WeakRef whose referent is an
element of S always returns
At any point during evaluation, a set of objects S is considered live if either of the following conditions is met:
At any time, if a set of objects S is not
Together with the definition of liveness, this clause prescribes legal optimizations that an implementation may apply regarding WeakRefs.
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 WeakRefs that point to such objects.
On the other hand, if an object's identity is observable, and that object is in the [[WeakRefTarget]] internal slot of a WeakRef, optimizations such as rematerialization that observably empty the WeakRef are prohibited.
Because calling
Implementations are not obligated to empty WeakRefs for maximal sets
of non-
If an implementation chooses a non-
HostCleanupFinalizationRegistry is an implementation-defined abstract
operation that is expected to call
ECMAScript implementations are expected to call ClearKeptObjects when a synchronous sequence of ECMAScript execution completes.
The following steps are performed:
The following steps are performed:
window.onerror
.© 2020 Dean Tribble, Till Schneidereit, Sathya Gunasekaran
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.