Stage 2 Draft / June 7, 2022

Symbol as WeakMap Keys Proposal

1 Executable Code and Execution Contexts

1.1 Processing Model of WeakRef and FinalizationRegistry Objects

Note
Full set of proposed changes here: https://arai-a.github.io/ecma262-compare/?pr=2777

1.2 AddToKeptObjects ( object: an Object, value: an Object or a Symbol, ): unused

  1. Let agentRecord be the surrounding agent's Agent Record.
  2. Append object to agentRecord.[[KeptAlive]].
  3. Append value to agentRecord.[[KeptAlive]].
  4. Return unused.
Note
When the abstract operation AddToKeptObjects is called with a target object , or symbol, reference, it adds the target to a list that will point strongly at the target until ClearKeptObjects is called.

2 CanBeHeldWeakly ( v: unknown ): a Boolean

  1. If Type(v) is Object, return true.
  2. If Type(v) is Symbol, then
    1. For each element e of the GlobalSymbolRegistry List (see 19.4.2.2), do
      1. If SameValue(e.[[Symbol]], v) is true, return false.
    2. Return true.
  3. Return false.
Editor's Note

This abstract operation determines if v is considered to have an identity suitable for a weak reference. i.e. Is v a valid candidate key of a WeakMap, or entry in a WeakSet, or target of a WeakRef or FinalizationRegistry.

'Registered symbols' are not considered to have a suitable identity because their identity is similar to strings. Implementations may attempt to collect them when they are unreachable.

'Well-Known symbols' 6.1.5.1 return true - this should not be interpreted as evidence that using them as a key in a WeakMap will not result in a memory leak.

3 Modifications to the properties of WeakMap.prototype

3.1 WeakMap.prototype.delete ( key )

The following steps are taken:

  1. Let M be the this value.
  2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
  3. Let entries be the List that is M.[[WeakMapData]].
  4. If Type(key) is not Object, return false.
  5. If CanBeHeldWeakly(key) is false, return false.
  6. For each Record { [[Key]], [[Value]] } p of entries, do
    1. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
      1. Set p.[[Key]] to empty.
      2. Set p.[[Value]] to empty.
      3. Return true.
  7. Return false.

3.2 WeakMap.prototype.get ( key )

The following steps are taken:

  1. Let M be the this value.
  2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
  3. Let entries be the List that is M.[[WeakMapData]].
  4. If Type(key) is not Object, return undefined.
  5. If CanBeHeldWeakly(key) is false, return undefined.
  6. For each Record { [[Key]], [[Value]] } p of entries, do
    1. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
  7. Return undefined.

3.3 WeakMap.prototype.has ( key )

The following steps are taken:

  1. Let M be the this value.
  2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
  3. Let entries be the List that is M.[[WeakMapData]].
  4. If Type(key) is not Object, return false.
  5. If CanBeHeldWeakly(key) is false, return false.
  6. For each Record { [[Key]], [[Value]] } p of entries, do
    1. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return true.
  7. Return false.

3.4 WeakMap.prototype.set ( key, value )

The following steps are taken:

  1. Let M be the this value.
  2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
  3. Let entries be the List that is M.[[WeakMapData]].
  4. If Type(key) is not Object, throw a TypeError exception.
  5. If CanBeHeldWeakly(key) is false, throw a TypeError exception.
  6. For each Record { [[Key]], [[Value]] } p of entries, do
    1. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
      1. Set p.[[Value]] to value.
      2. Return M.
  7. Let p be the Record { [[Key]]: key, [[Value]]: value }.
  8. Append p as the last element of entries.
  9. Return M.

4 Modifications to the properties of WeakSet.prototype

4.1 WeakSet.prototype.add ( value )

The following steps are taken:

  1. Let S be the this value.
  2. Perform ? RequireInternalSlot(S, [[WeakSetData]]).
  3. If Type(value) is not Object, throw a TypeError exception.
  4. If CanBeHeldWeakly(value) is false, throw a TypeError exception.
  5. Let entries be the List that is S.[[WeakSetData]].
  6. For each element e of entries, do
    1. If e is not empty and SameValue(e, value) is true, then
      1. Return S.
  7. Append value as the last element of entries.
  8. Return S.

4.2 WeakSet.prototype.delete ( value )

The following steps are taken:

  1. Let S be the this value.
  2. Perform ? RequireInternalSlot(S, [[WeakSetData]]).
  3. If Type(value) is not Object, return false.
  4. If CanBeHeldWeakly(value) is false, return false.
  5. Let entries be the List that is S.[[WeakSetData]].
  6. For each element e of entries, do
    1. If e is not empty and SameValue(e, value) is true, then
      1. Replace the element of entries whose value is e with an element whose value is empty.
      2. Return true.
  7. Return false.

4.3 WeakSet.prototype.has ( value )

The following steps are taken:

  1. Let S be the this value.
  2. Perform ? RequireInternalSlot(S, [[WeakSetData]]).
  3. Let entries be the List that is S.[[WeakSetData]].
  4. If Type(value) is not Object, return false.
  5. If CanBeHeldWeakly(value) is false, return false.
  6. For each element e of entries, do
    1. If e is not empty and SameValue(e, value) is true, return true.
  7. Return false.

5 Modifications to WeakRef and FinalizationRegistry

5.1 WeakRef ( target )

When the WeakRef function is called with argument target, the following steps are taken:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. If Type(target) is not Object, throw a TypeError exception.
  3. If CanBeHeldWeakly(target) is false, throw a TypeError exception.
  4. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »).
  5. Perform ! AddToKeptObjects(target).
  6. Set weakRef.[[WeakRefTarget]] to target.
  7. Return weakRef.

5.2 FinalizationRegistry.prototype.register ( target, heldValue [ , unregisterToken ] )

The following steps are taken:

  1. Let finalizationRegistry be the this value.
  2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]).
  3. If Type(target) is not Object, throw a TypeError exception.
  4. If CanBeHeldWeakly(target) is false, throw a TypeError exception.
  5. If SameValue(target, heldValue) is true, throw a TypeError exception.
  6. If Type(unregisterToken) is not Object, then
  7. If CanBeHeldWeakly(unregisterToken) is false, then
    1. If unregisterToken is not undefined, throw a TypeError exception.
    2. Set unregisterToken to empty.
  8. Let cell be the Record { [[WeakRefTarget]]: target, [[HeldValue]]: heldValue, [[UnregisterToken]]: unregisterToken }.
  9. Append cell to finalizationRegistry.[[Cells]].
  10. Return undefined.

5.3 FinalizationRegistry.prototype.unregister ( unregisterToken )

The following steps are taken:

  1. Let finalizationRegistry be the this value.
  2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]).
  3. If Type(unregisterToken) is not Object, throw a TypeError exception.
  4. If CanBeHeldWeakly(unregisterToken) is false, throw a TypeError exception.
  5. Let removed be false.
  6. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of finalizationRegistry.[[Cells]], do
    1. If cell.[[UnregisterToken]] is not empty and SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then
      1. Remove cell from finalizationRegistry.[[Cells]].
      2. Set removed to true.
  7. Return removed.

A Copyright & Software License

Copyright Notice

© 2022 Robin Ricard,Rick Button,Daniel Ehrenberg,Leo Balter,Caridy Patiño,Rick Waldron,Ashley Claymore

Software License

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:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

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.