26 Managing Memory
26.1 WeakRef Objects
A
26.1.1 The WeakRef Constructor
The WeakRef
- is
%WeakRef% . -
is the initial value of the
"WeakRef" property of theglobal object . -
creates and initializes a new WeakRef when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
-
may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specifiedWeakRefbehaviour must include asupercall to theWeakRefconstructor to create and initialize the subclass instance with the internal state necessary to support theWeakRef.prototypebuilt-in methods.
26.1.1.1 WeakRef ( target )
When the WeakRef function is called with argument target, the following steps are taken:
- If NewTarget is
undefined , throw aTypeError exception. - If
Type (target) is not Object, throw aTypeError exception. - Let weakRef be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[WeakRefTarget]] »).%WeakRef.prototype% " - Perform
AddToKeptObjects (target). - Set weakRef.[[WeakRefTarget]] to target.
- Return weakRef.
26.1.2 Properties of the WeakRef Constructor
The
-
has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
26.1.2.1 WeakRef.prototype
The initial value of WeakRef.prototype is the
This property has the attributes { [[Writable]]:
26.1.3 Properties of the WeakRef Prototype Object
The WeakRef prototype object:
- is
%WeakRef.prototype% . -
has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakRefTarget]] internal slot.
26.1.3.1 WeakRef.prototype.constructor
The initial value of WeakRef.prototype.constructor is
This property has the attributes { [[Writable]]:
26.1.3.2 WeakRef.prototype.deref ( )
The following steps are taken:
- Let weakRef be the
this value. - Perform ?
RequireInternalSlot (weakRef, [[WeakRefTarget]]). - Return
WeakRefDeref (weakRef).
If the
target = { foo: function() {} };
let weakRef = new WeakRef(target);
... later ...
if (weakRef.deref()) {
weakRef.deref().foo();
}
In the above example, if the first deref does not evaluate to
26.1.3.3 WeakRef.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value
This property has the attributes { [[Writable]]:
26.1.4 WeakRef Abstract Operations
26.1.4.1 WeakRefDeref ( weakRef )
The abstract operation WeakRefDeref takes argument weakRef (a
- Let target be weakRef.[[WeakRefTarget]].
- If target is not
empty , then- Perform
AddToKeptObjects (target). - Return target.
- Perform
- Return
undefined .
This abstract operation is defined separately from
26.1.5 Properties of WeakRef Instances
26.2 FinalizationRegistry Objects
A
26.2.1 The FinalizationRegistry Constructor
The FinalizationRegistry
- is
%FinalizationRegistry% . -
is the initial value of the
"FinalizationRegistry" property of theglobal object . -
creates and initializes a new FinalizationRegistry when called as a
constructor . - is not intended to be called as a function and will throw an exception when called in that manner.
-
may be used as the value in an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specifiedFinalizationRegistrybehaviour must include asupercall to theFinalizationRegistryconstructor to create and initialize the subclass instance with the internal state necessary to support theFinalizationRegistry.prototypebuilt-in methods.
26.2.1.1 FinalizationRegistry ( cleanupCallback )
When the FinalizationRegistry function is called with argument cleanupCallback, the following steps are taken:
- If NewTarget is
undefined , throw aTypeError exception. - If
IsCallable (cleanupCallback) isfalse , throw aTypeError exception. - Let finalizationRegistry be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[Realm]], [[CleanupCallback]], [[Cells]] »).%FinalizationRegistry.prototype% " - Let fn be the
active function object . - Set finalizationRegistry.[[Realm]] to fn.[[Realm]].
- Set finalizationRegistry.[[CleanupCallback]] to
HostMakeJobCallback (cleanupCallback). - Set finalizationRegistry.[[Cells]] to a new empty
List . - Return finalizationRegistry.
26.2.2 Properties of the FinalizationRegistry Constructor
The
-
has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
26.2.2.1 FinalizationRegistry.prototype
The initial value of FinalizationRegistry.prototype is the
This property has the attributes { [[Writable]]:
26.2.3 Properties of the FinalizationRegistry Prototype Object
The FinalizationRegistry prototype object:
- is
%FinalizationRegistry.prototype% . -
has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have [[Cells]] and [[CleanupCallback]] internal slots.
26.2.3.1 FinalizationRegistry.prototype.constructor
The initial value of FinalizationRegistry.prototype.constructor is
26.2.3.2 FinalizationRegistry.prototype.register ( target, heldValue [ , unregisterToken ] )
The following steps are taken:
- Let finalizationRegistry be the
this value. - Perform ?
RequireInternalSlot (finalizationRegistry, [[Cells]]). - If
Type (target) is not Object, throw aTypeError exception. - If
SameValue (target, heldValue) istrue , throw aTypeError exception. - If
Type (unregisterToken) is not Object, then- If unregisterToken is not
undefined , throw aTypeError exception. - Set unregisterToken to
empty .
- If unregisterToken is not
- Let cell be the
Record { [[WeakRefTarget]]: target, [[HeldValue]]: heldValue, [[UnregisterToken]]: unregisterToken }. - Append cell to finalizationRegistry.[[Cells]].
- Return
undefined .
Based on the algorithms and definitions in this specification, cell.[[HeldValue]] is
26.2.3.3 FinalizationRegistry.prototype.unregister ( unregisterToken )
The following steps are taken:
- Let finalizationRegistry be the
this value. - Perform ?
RequireInternalSlot (finalizationRegistry, [[Cells]]). - If
Type (unregisterToken) is not Object, throw aTypeError exception. - Let removed be
false . - For each
Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of finalizationRegistry.[[Cells]], do- If cell.[[UnregisterToken]] is not
empty andSameValue (cell.[[UnregisterToken]], unregisterToken) istrue , then- Remove cell from finalizationRegistry.[[Cells]].
- Set removed to
true .
- If cell.[[UnregisterToken]] is not
- Return removed.
26.2.3.4 FinalizationRegistry.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value
This property has the attributes { [[Writable]]: