Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-10-12 Thread #!/JoePea
Gotcha. So basically that tells the consumer "No, you can't use Proxies". What I want to tell them is "Go ahead, use Proxies to your heart's content", but I don't want to tell them to write all the overly-complicated code required for their Proxies to work properly as soon as I introduce a single

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-27 Thread Jordan Harband
oh oops, i meant `if (o !== this)` not `if (o.foo !== this)` :-) On Mon, Jul 27, 2020 at 3:06 PM #!/JoePea wrote: > > `o.foo(); // works` > > I think there is something missing from that example as that line > throws before it can get to the `new Proxy` line. > > #!/JoePea > > On Wed, Jul 15,

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-27 Thread #!/JoePea
> `o.foo(); // works` I think there is something missing from that example as that line throws before it can get to the `new Proxy` line. #!/JoePea On Wed, Jul 15, 2020 at 10:47 PM Jordan Harband wrote: > > So can: > ```jsx > const o = { foo() { if (o.foo !== this) { throw 'detected'; } } }; >

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-15 Thread Jordan Harband
So can: ```jsx const o = { foo() { if (o.foo !== this) { throw 'detected'; } } }; o.foo(); // works new Proxy(o, {}).foo(); // throws ``` (as would a class that used a closed-over WeakMap for each "private field") Private fields do not introduce any new hazards here. On Tue, Jul 14, 2020 at

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-14 Thread #!/JoePea
> private members (safely) allow classes with internal slots. I'd say that they aren't safe, if they can break 3rd-party code on the external public side. #!/JoePea On Sun, Jul 12, 2020 at 3:09 PM Michael Theriot wrote: > > I assume OP wants to use proxies and private members together. They

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-14 Thread #!/JoePea
>TC39 choose (correctly, I think) in favor of secure field privacy. The lesser >evil choice.” The PrivateField tunneling that was talked about in that issue would not expose any privacy. The fact that external proxies break when internal code introduces a private field is _actually_ leaking

RE: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread Michael Haufe
To quote from a short conversation I had with Allen Wirfs-Brock: “Proxies have a similar issue WRT the "internal slots" of built-ins. The alternatives were the non-othogonality with Proxy or private fields whose privacy was insecure. TC39 choose (correctly, I think) in favor of secure field

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread Michael Theriot
I assume OP wants to use proxies and private members together. They are not designed to be compatible. Proxies and private members are a UX goal primarily for developers. Proxies easily allow observation of another object or creation of exotic objects (e.g. Array), and private members (safely)

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread kai zhu
as product-developer, can i ask what ux-objective you ultimately want achieved? ```js const sub = new Sub() // i'm a noob on proxies. what is this thing (with proxied-private-fields) ultimately used for? const proxy = new Proxy(sub, ...) ``` On Sun, Jul 12, 2020 at 4:34 PM Michael Theriot <

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread Michael Theriot
This does require you to have both the key and the weakmap though, so it actually does succeed in hiding the data so long as the weakmap is out of scope. I guess the issue I can foresee is that the key could be modified after the object is created. e.g. ```js var a = new A(); var key =

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread Michael Theriot
It nearly works, but the issue is that the key will be leaked by `Object.getOwnPropertySymbols(new A())`, so it's not truly private. There have been ideas proposing "private symbols" but I am not familiar with their issues, and I would guess with Class Fields they are unlikely to materialize

RE: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread François REMY
At the risk of pointing out the obvious: ```js const privkey = Symbol(); const stores = new WeakMap(); class A { [privkey] = {}; constructor() { const priv = {}; priv.hidden = Math.random(); stores.set(this[privkey], priv); } get hidden() { const priv =

Re: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-07-12 Thread Michael Theriot
I experienced this issue prior to this proposal, using weakmaps for private access. e.g. ```js const stores = new WeakMap(); class A { constructor() { const priv = {}; priv.hidden = 0; stores.set(this, priv); } get hidden() { const priv = stores.get(this); return

RE: Why does a JavaScript class getter for a private field fail using a Proxy?

2020-06-04 Thread Michael Haufe
This is a known issue and very painful for me as well. You can see a long ugly discussion here: I suggest the following guide to assist you: Another possible approach is to have your