24.3 WeakMap Objects
WeakMaps are collections of key/value pairs where the keys are objects and/or symbols and values may be arbitrary
An implementation may impose an arbitrarily determined latency between the time a key/value pair of a WeakMap becomes inaccessible and the time when the key/value pair is removed from the WeakMap. If this latency was observable to ECMAScript program, it would be a source of indeterminacy that could impact program execution. For that reason, an ECMAScript implementation must not provide any means to observe a key of a WeakMap that does not require the observer to present the observed key.
WeakMaps must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of key/value pairs in the collection. The data structure used in this specification is only intended to describe the required observable semantics of WeakMaps. It is not intended to be a viable implementation model.
WeakMap and WeakSet are intended to provide mechanisms for dynamically associating state with an object or symbol in a manner that does not “leak” memory resources if, in the absence of the WeakMap or WeakSet instance, the object or symbol otherwise became inaccessible and subject to resource reclamation by the implementation's garbage collection mechanisms. This characteristic can be achieved by using an inverted per-object/symbol mapping of WeakMap or WeakSet instances to keys. Alternatively, each WeakMap or WeakSet instance may internally store its key and value data, but this approach requires coordination between the WeakMap or WeakSet implementation and the garbage collector. The following references describe mechanism that may be useful to implementations of WeakMap and WeakSet:
Barry Hayes. 1997. Ephemerons: a new finalization mechanism. In Proceedings of the 12th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA '97), A. Michael Berman (Ed.). ACM, New York, NY, USA, 176-183, http://doi.acm.org/10.1145/263698.263733.
Alexandra Barros, Roberto Ierusalimschy, Eliminating Cycles in Weak Tables. Journal of Universal Computer Science - J.UCS, vol. 14, no. 21, pp. 3481-3497, 2008, http://www.jucs.org/jucs_14_21/eliminating_cycles_in_weak
24.3.1 The WeakMap Constructor
The WeakMap
- is %WeakMap%.
- is the initial value of the
"WeakMap" property of theglobal object . - creates and initializes a new WeakMap 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
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified WeakMap behaviour must include asuper
call to the WeakMapconstructor to create and initialize the subclass instance with the internal state necessary to support theWeakMap.prototype
built-in methods.
24.3.1.1 WeakMap ( [ iterable ] )
This function performs the following steps when called:
- If NewTarget is
undefined , throw aTypeError exception. - Let map be ?
OrdinaryCreateFromConstructor (NewTarget,"%WeakMap.prototype%" , « [[WeakMapData]] »). - Set map.[[WeakMapData]] to a new empty
List . - If iterable is either
undefined ornull , return map. - Let adder be ?
Get (map,"set" ). - If
IsCallable (adder) isfalse , throw aTypeError exception. - Return ?
AddEntriesFromIterable (map, iterable, adder).
If the parameter iterable is present, it is expected to be an object that implements an
24.3.2 Properties of the WeakMap Constructor
The WeakMap
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
24.3.2.1 WeakMap.prototype
The initial value of WeakMap.prototype
is the
This property has the attributes { [[Writable]]:
24.3.3 Properties of the WeakMap Prototype Object
The WeakMap prototype object:
- is %WeakMap.prototype%.
- has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is an
ordinary object . - does not have a [[WeakMapData]] internal slot.
24.3.3.1 WeakMap.prototype.constructor
The initial value of WeakMap.prototype.constructor
is
24.3.3.2 WeakMap.prototype.delete ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnfalse . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Key]] to
empty . - Set p.[[Value]] to
empty . - Return
true .
- Set p.[[Key]] to
- If p.[[Key]] is not
- Return
false .
The value
24.3.3.3 WeakMap.prototype.get ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnundefined . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , return p.[[Value]].
- If p.[[Key]] is not
- Return
undefined .
24.3.3.4 WeakMap.prototype.has ( key )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , returnfalse . - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , returntrue .
- If p.[[Key]] is not
- Return
false .
24.3.3.5 WeakMap.prototype.set ( key, value )
This method performs the following steps when called:
- Let M be the
this value. - Perform ?
RequireInternalSlot (M, [[WeakMapData]]). - If
CanBeHeldWeakly (key) isfalse , throw aTypeError exception. - For each
Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do- If p.[[Key]] is not
empty andSameValue (p.[[Key]], key) istrue , then- Set p.[[Value]] to value.
- Return M.
- If p.[[Key]] is not
- Let p be the
Record { [[Key]]: key, [[Value]]: value }. - Append p to M.[[WeakMapData]].
- Return M.
24.3.3.6 WeakMap.prototype [ @@toStringTag ]
The initial value of the
This property has the attributes { [[Writable]]:
24.3.4 Properties of WeakMap Instances
WeakMap instances are