Removal of WeakMap/WeakSet clear

Chris Toshok toshok at gmail.com
Wed Dec 3 09:20:39 PST 2014


On Thu, Nov 27, 2014 at 10:40 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

>
> This is the end of my assumed inverted WC design and why I assert that a
> clear method is incompatible with it.
>
>
Couldn't this be solved by adding a little state (a monotonically
increasing 'generation' counter) to the WC?  Then, something like this:

```js
WC.prototype.clear = function() { this.generation ++; }
WC.prototype.has = function(k) {
  var slot = getWCSlot(k, this);
  return slot && slot.generation == this.generation;
};
WC.prototype.set = function(k, v) {
  var slot = getWCSlot(k, this);
  if (slot) {
    // update the slot's information (including generation)
    slot.generation = this.generation;
    slot.value = v;
  }
  else {
    k[@@weakContainers][this] = { generation: this.generation, value: v };
  }
};
WC.prototype.get = function(k) {
  var slot = getWCSlot(k, this);
  if (!slot) return undefined;
  if (slot.generation != this.generation) {
    // purge the key's slot for this weakmap
    delete k[@@weakContainers][this];
    return undefined;
  }
  return slot.value;
};
```

Then clear()'s description can be changed to (if it wasn't this already)
simply: "There is no way to retrieve values corresponding to keys added
prior to the clear()"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141203/cd5bc82f/attachment.html>


More information about the es-discuss mailing list