One way to get truly shared proxy handlers would be to allow proxies
to have some internal instance "state" that gets passed to each trap,
just like objects have internal instance state ( [[Prototype]],
[[Class]], [[Extensible]] etc) that gets "passed" to their "traps" (
[[Get]], [[GetOwnProperty]], [[Call]] etc.).  This would add another
argument to each trap, but I think it might be better anyway to switch
to a single object argument for all traps so that the order becomes
irrelevant, of course the argument names then become relevant, so it's
a tradeoff.  There could be a "create" trap to initialize this state,
and a way to pass arguments to this trap.

It might also be useful to have a "getPrototypeOf" trap to avoid
having to explicitly (and correctly) calculate and pass the prototype
argument on each Proxy.create call.  Removing the prototype argument
could also allow Proxy.create and Proxy.createFunction to be merged
into a single API.  This trap could be called just once (to avoid
mutable prototypes) right after the "create" trap.  This trap would be
ignored for function proxies since they should always have
Function.prototype.

Putting this altogether, and utilizing destructuring, this could look
something like...

Proxy.Handler = {
  ...
  create: function(target) {
     let state = {target: target};
     return state;
  },
  ...
  getPrototypeOf: function ( { state } ) {
    return Object.getPrototypeOf ( state.target ) );
  }
  ...
  get: function(  { state, proxy, receiver, name } ) { ... },
  ...
}

In order to be able to pass the createArgs to the "create" trap,
definition of the full handler (handler + callTrap + constructTrap)
would need to be separated from actual proxy instance construction.
Here's how this might look...

//Proxy.Constructor would return a proxy constructor which constructs
proxies having the passed handler, callTrap (optional), and
constructTrap(optional)
let ProxyConstructor = Proxy.Constructor(handler, callTrap, constructTrap);

The proxy constructor when called would first forward its arguments to
the handler's "create" trap to initialize the state, then call
the handler's "getPrototypeOf" trap passing the state, then construct
and return the actual proxy using handler, callTrap, and
constructTrap.

let proxy = new ProxyConstructor(...createArgs); // would also work
without "new"

For Proxy.Handler proxies, this would translate to...

let ForwardingProxy = Proxy.Constructor(Proxy.Handler);
let proxy = new ForwardingProxy(target);


Thanks,
Sean Eagan
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to