Implementing membranes using proxies, and [[GetInheritance]]

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Sep 11 08:51:09 PDT 2013


On Sep 11, 2013, at 7:17 AM, Tom Van Cutsem wrote:

> 2013/9/11 David Bruant <bruant.d at gmail.com>
> Le 11/09/2013 06:10, Boris Zbarsky a écrit :
> 
> Hey all,
> 
> I was looking at implementing a membrane using ES6 proxies and ran into a snag.  Consider a situation where object A has prototype B.  A' is a proxy implementing the membrane, whose target is A.
> 
> But now if Object.getPrototypeOf(A') is invoked the return value will be B (unless it just throws).  There's no way for A' to return a new proxy B' whose target is B in this situation.
> In essence yes. In practice, you can do:
>     // trap:
>     getPrototypeOf: function(target){
>         target.__proto__ = B';
>         return B';
>     }
> But of course, it changes A [[Prototype]], which is probably not desirable. And of course, although to-be-standard, __proto__ is bad taste...
> 
> Indeed, this is also the pattern I used, except it doesn't set the `__proto__` of the real target, but of a shadow target: <https://github.com/tvcutsem/harmony-reflect/blob/master/examples/membrane.js#L246>.
> 
> Setting `__proto__` may be bad taste in general, but this is a case where using this capability is necessary.

At least if the ES6 spec. is fully implemented, Object.setPrototypeOf would be preferable to assigning to __proto__.  EG,

 // trap:
    getPrototypeOf: function(target){
        Object.setPrototypeOf(target, B');
        return B';
    }

There's nothing magic about __proto__=.  Both dunder proto and Object.setPrototypeOf are defined in terms of [[SetInheritance]].  The only difference is that the functionality of  dunder protois dependent upon the current [[Prototype]][  value of target while setPrototypeOf doesn't have that dependency.

> 
> Ps: btw, wasn't "GetInheritance" supposed to be renamed "GetPrototype"?
> 
> I think we had agreement on that. Allen?

I'm willing to call them [[GetPrototypeOf]] and [[SetPrototypeOf]] to match the trap names. I prefer avoiding a direct connotation with the [[Prototype]] internal data property as an exotic object is not required to have one.

Allen


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130911/ca2766a8/attachment.html>


More information about the es-discuss mailing list