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

Conversation

littledan
Copy link
Member

This patch 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.

It separates out a definition of "live" that can be referenced from
various places. This defintion is based on earlier spec text, in
terms of whether something may be "observably referenced".

This patch 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.

It separates out a definition of "live" that can be referenced from
various places. This defintion is based on earlier spec text, in
terms of whether something may be "observably referenced".
@littledan
Copy link
Member Author

cc @mhofman @erights

</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.

<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


<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.

@mhofman
Copy link
Member

mhofman commented May 29, 2019

I'm wondering about the timing between removing WeakMap/WeakSet entries and clearing out [[Target]] field/slots.
I don't see anything preventing an implementation from removing a WeakMap entry separately from clearing a WeakRef's [[Target]] slot. If it also allows some user JavaScript to be interspersed between the 2, the object may become reachable again causing weird issues in user code.

One way to solve this may be to include the removal of WeakMap/WeakSet entries in the same execution section that clears out [[Target]] field/slots?

Edit: For example the IterableWeakMap example makes that assumption in the iterator:

  *[Symbol.iterator]() {
    for (const ref of this.#refSet) {
      const key = ref.deref();
      if (!key) continue;
      const { value } = this.#weakMap.get(key);
      yield [key, value];
    }
  }

If #weakMap is cleared before ref is, then const { value } = this.#weakMap.get(key); will cause a TypeError

@littledan
Copy link
Member Author

@mhofman This is a good question. I'm not sure how to word banning this revival. Isn't this an issue just with bunches of WeakRefs, not necessarily involving WeakMaps?

@littledan
Copy link
Member Author

I think the issue @mhofman is serious, and we should address it in our resolution to #105, when we figure out how to explain what guarantees we're providing. I'm landing this patch as it's really a bug fix/clarification over the text, which has this issue both before and after the patch.

@littledan littledan merged commit cc09b9d into tc39:master May 30, 2019
littledan added a commit to littledan/proposal-weakrefs that referenced this pull request May 30, 2019
This patch rephrases that logic path into an imperative algorithm,
which is described to behave atomically. The PR attempts to resolve
the issue documented in
tc39#121 (comment)
Related issue: tc39#105
littledan added a commit that referenced this pull request May 30, 2019
This patch rephrases that logic path into an imperative algorithm,
which is described to behave atomically. The PR attempts to resolve
the issue documented in
#121 (comment)
Related issue: #105
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants