Security Demands Simplicity (was: Private Slots)

Andreas Rossberg rossberg at google.com
Thu Jan 17 09:13:39 PST 2013


On 17 January 2013 18:00, Mark S. Miller <erights at google.com> wrote:
> I still have this position on classes. But I no longer buy that
> pessimistic conclusion about WeakMaps. Consider how WeakMaps would be
> used by the expansion of classes-with-private. Just 'cause it's on the
> top of my head, below I use the old representation of one WeakMap per
> class providing access to a record of all the private state. For the
> same reason, I'll use the encapsulation of the Purse example without
> any of the numeric checks.
>
> class Purse {
>     constructor(private balance) {
>     getBalance() { return balance; }
>     makePurse() { return Purse(0); }
>     deposit(amount, srcPurse) {
>         private(srcPurse).balance -= amount;
>         balance += amount;
>     }
> }

Hm, I'm afraid I don't fully understand that example. There seems to
be a missing closing brace for the constructor, and I don't know what
the free occurrences of 'balance' are referring to. Also, the second
line of the deposit function seems to be missing in the expansion.

>
> expansion
>
> let Purse = (function() {
>     let amp = WeakMap();
>     function Purse(balance) {
>         amp.set(this, Object.seal({
>             get balance() { return balance; },
>             set balance(newBalance) { balance = newBalance; }
>         }));
>     }
>     Purse.prototype = {
>         getBalance: function() { return balance; },
>         makePurse: function() { return Purse(0); },
>         deposit: function(amount, srcPurse) {
>             amp.get(srcPurse).balance -= amount;
>         }
>     }
>     return Purse;
> })();


More information about the es-discuss mailing list