Removal of WeakMap/WeakSet clear

Chris Toshok toshok at gmail.com
Wed Dec 3 11:28:33 PST 2014


A more compact implementation occurred to me during my commute:  just have
an internal slot in the WC that it uses when looking up values, instead of
the WC reference itself.

This has the downside of not being able to purge old slots on calls to
has/get, but removes the possibility of overflow (if 'generation' is a
uint32, e.g)

```js
WC.prototype.clear = function() { this.[[WCIdentity]] = new object(); }
WC.prototype.has = function(k) {
  var slot = GetWCSlot(k, this.[[WCIdentity]]);
  return slot !== undefined;
};
WC.prototype.set = function(k,v) {
  var slot = GetWCSlot(k, this.[[WCIdentity]]);
  if (!slot)
    slot = CreateWCSlot(k, this.[[WCIdentity]]);
  slot.value = v;
};
WC.prototype.get = function(k) {
  var slot = GetWCSlot(k, this.[[WCIdentity]]);
  return slot && slot.value;
};
```

On Wed, Dec 3, 2014 at 9:20 AM, Chris Toshok <toshok at gmail.com> wrote:

> 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/b785e268/attachment.html>


More information about the es-discuss mailing list