Proxying built-ins (Was: [[Invoke]] and implicit method calls)

David Bruant bruant.d at gmail.com
Wed Sep 25 11:17:26 PDT 2013


Le 25/09/2013 17:59, Allen Wirfs-Brock a écrit :
> On Sep 25, 2013, at 3:01 AM, David Bruant wrote:
>> I think it's important to have a generic solution to avoid having 
>> magic (non-self-hostable) built-ins (but I don't have this solution).
>
> I don't think there is one, based upon Direct Proxies.
There probably is one. Something like for a given class, register a 
bunch of functions (typically the ones on the prototype) which receive 
the unwrapped target if this target was the output of the class 
constructor. It could look like something like (self-hosting Date):

     // don't worry too much about the syntax
     class Date(){
         constructor(){
             this.datetime = readDatetimeFromSystem();
             setInterval(()=>{ this.datetime++; }, 1);
         }

         private datetime;

         public getMonth(){
             return someComputation(this.datetime);
         }
     }

     // only the class creator should be able to do that (maybe "class" 
should be an expression returning the capability or something)
     Date.functionsWhereThisShouldBeUnwrapped = [Date.prototype.getMonth];

     var d = new Date();
     var p = new Proxy(d, {});
     d.getMonth(); // works
     // the method sees c because 1) it's "whitelisted", 2) the target 
is a Date instance

     var p2 = new Proxy({}, {}); // random target
     p2.getMonth = p.getMonth;
     p2.getMonth(); // throws because p2 wasn't created by the Date 
constructor and the private state can't be found

Edges are a bit hand-wavy, but I hope you get my point that in a way or 
another, it's possible to maintain the information of which constructor 
created what function and tell with fine-grain which function should 
unwrap automatically to the target (leading to private state access).

> That's why if we want to have Proxy as a primitive that supports 
> creating membranes we need to stop thinking about it as a more 
> universal primitive that also supports things like transparent forwarding
Why would the 2 goals be contradictory?

David


More information about the es-discuss mailing list