Save Object.observe()! (please) + make WeakMap/WeakSet observable.
Rick Waldron
waldron.rick at gmail.com
Wed Nov 4 22:36:42 UTC 2015
On Wed, Nov 4, 2015 at 8:16 AM Coroutines <coroutines at gmail.com> wrote:
> On Wed, Nov 4, 2015 at 4:56 AM, Romuald Quantin <romu at soundstep.com>
> wrote:
>
>> As an aside and as Coroutines,
>>
>> I never understood why there is this inability to enumerate WeakMap keys.
>>
>
> If I had it my way there would be no WeakMap or WeakSet. I'd have a
> Symbol.mode similar to Lua's __mode meta(method/field?) that would show the
> garbage collector that keys or values are weakly referenced in an object.
> Or even:
>
> obj[Symbol.weakValues] = true;
> obj[Symbol.weakProperties] = true;
>
> WeakMap isn't really all that special. Set is important for how it
> maintains only unique values are inserted - but you could do this with
> Proxy.
>
This is all very misunderstood (both Romuald and Coroutines comments).
WeakMap is extremely special, for these reasons:
1. The weakly mapped relationship between the key and value allows the
runtime to gc the value once the key becomes otherwise unreachable.
Specifically: if the _only_ reachable reference to the key is the weakmap
relationship, then that relationship can be discarded and the value can be
garbage collected at some implementation-determined time in the future.
2. For security purposes, only code that has been explicitly given access
to _both_ the weakmap and a key may gain access to the value stored within
the weakmap. This is why there is no enumeration—if there were, then any
code that could access the weakmap could then also access all of the keys
and values. If that's a desirable trait, then just use a Map.
These things cannot be done with Proxy, because anything that Proxy does
will inevitably be a strongly held reference.
Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151104/b9bf4552/attachment.html>
More information about the es-discuss
mailing list