Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Normative: Fix the definition of liveness for WeakMap and WeakSet #121

Merged
merged 3 commits into from May 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 31 additions & 18 deletions spec/abstract-jobs.html
Expand Up @@ -100,6 +100,33 @@ <h1>Objectives</h1>
</p>
</emu-clause>

<emu-clause id="sec-liveness">
<h1>Liveness</h1>

An object _obj_ is considered <dfn>live</dfn> if the following conditions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't an object be considered not live in these conditions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks, will need to fix this before landing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

are met:

<ul>
<li>_obj_ is not included in any agent's [[KeptAlive]] List.</li>
<li>
No possible future execution of the program would observably
reference _obj_, except through a chain of references which
includes a [[Target]] field of a FinalizationGroup's [[Cells]] List,
or the [[Target]] internal slot of a WeakRef.
</li>
</ul>
<emu-note>
The above definition implies that, if a key in a WeakMap is not live,
then its corresponding value is not necessarily live either. Presence
of an object as a key in a WeakMap or a member of a WeakSet does not
imply that the object is live.
</emu-note>
<emu-note type=editor>
The exact definition of liveness remains under discussion in
<a href="https://github.com/tc39/proposal-weakrefs/issues/115">#115</a>.
</p>
</emu-clause>

<emu-clause id="sec-weakref-execution">
<h1>Execution</h1>

Expand All @@ -111,24 +138,10 @@ <h1>Execution</h1>

<ul>
<li>
If the following conditions about an ECMAScript object are met, then
the implementation may replace each reference to the object from the
[[Target]] field of a FinalizationGroup's [[Cells]] List, or the
[[Target]] internal slot of a WeakRef, to the value ~empty~:
<ul>
<li>The object is not included in any agent's [[KeptAlive]] List.</li>
<li>
No possible future execution of the program would observably
reference the object, except through a chain of references which
includes a [[Target]] field of a FinalizationGroup's [[Cells]] List,
or the [[Target]] internal slot of a WeakRef.
</p>
<p>
NOTE: The exact guarantee remains under discussion in
<a href="https://github.com/tc39/proposal-weakrefs/issues/115">#115</a>.
</p>
</li>
</ul>
If an object is not live, then the implementation may replace any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can “live” here link to the liveness section?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it autolink due to the dfn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh hmm, probably so.

reference to the object from the [[Target]] field of a
FinalizationGroup's [[Cells]] List, or the [[Target]] internal slot
of a WeakRef, to the value ~empty~.
</li>
<li>
When there is a FinalizationGroup _fg_ such that
Expand Down
29 changes: 17 additions & 12 deletions spec/weak-ref.html
Expand Up @@ -136,19 +136,24 @@ <h1>Properties of WeakRef Instances</h1>
internal slot.
</p>
</emu-clause>
</emu-clause>

<emu-clause id="sec-collection-modifications">
<h1>Modifications to collection type definitions</h1>
<p>WeakMap and WeakSet keys are not kept alive just because a WeakRef points to them.</p>

<emu-clause id="sec-weakmap">
<h1>WeakMap Objects</h1>
<p>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. 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, <ins>or if all references to a ECMAScript object are from a [[Target]] field or internal slot,</ins> 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.</p>
</emu-clause>
<emu-clause id="sec-collection-modifications">
<h1>Modifications to collection type definitions</h1>
<emu-note type=editor>
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-live keys or members are deleted.
</emu-note>

<emu-clause id="sec-weakmap">
<h1>WeakMap Objects</h1>
<p>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. <ins>There is no way for ECMAScript programs to observe the presence of a key in a WeakMap without a reference to that key. This implies that, if an object is not live, and it is present as a WeakMap key, then the implementation may unobservably remove the key-value pair from the WeakMap.</ins> <del>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.</del></p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem to relax the existing requirement that unreachable keys and associated value must be removed:

WeakMap implementations must detect and remove such key/value pairs and any associated resources.

the implementation may unobservably remove the key-value pair from the WeakMap

Of course the removal was unobservable before Weakrefs, but a non-collecting engine may now be compliant when it wasn't before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this relaxes the requirement's wording, but the effect is observably equivalent. I don't think the previous wording was really optimal, and I am proposing this change, as long as we are cleaning up the definitions around liveness.

</emu-clause>

<emu-clause id="sec-weakset">
<h1>WeakSet Objects</h1>
<p>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. If an object that is contained by a WeakSet is only reachable by following a chain of references that start within that WeakSet, <ins>or if all references to a ECMAScript object are from a [[Target]] field or internal slot,</ins> then that object is inaccessible and is automatically removed from the WeakSet. WeakSet implementations must detect and remove such objects and any associated resources.</p>
</emu-clause>
<emu-clause id="sec-weakset">
<h1>WeakSet Objects</h1>
<p>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. <ins>There is no way for ECMAScript programs to observe the presence of an object held in a WeakSet without a reference to that object. This implies that, if an object is not live, and it is held in a WeakSet, then the implementation may unobservably remove the object from the WeakSet.</ins> <del>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.</del></p>
</emu-clause>
</emu-clause>