custom proto arg for Proxy.createFunction?

David Herman dherman at mozilla.com
Wed Feb 23 14:33:02 PST 2011


PS Correction: it's actually a non-standard extension that regexps are callable in SpiderMonkey. So the invariant is that the only callable non-host objects are descendants of Function, or possibly host objects. This doesn't change my overall point, though.

On Feb 23, 2011, at 2:26 PM, David Herman wrote:

> I've been working on a prototype implementation of the binary data spec in pure JS (implemented via typed arrays) and I've been bitten by the lack of a standard mechanism for subclassing Function.
> 
> I'm using proxies for the implementation, and Proxy.createFunction doesn't let me specify a custom prototype. Now, I can understand that this preserves the existing property of the language that the only callable things are either regexps or descendants of Function. But we could extend the proxy API to allow custom functions with user-specified prototypes and still preserve this property:
> 
>    Proxy.createFunction(handler, callTrap[, constructTrap[, proto]])
> 
> The proto argument would default to the original value of Function.prototype [1]. But if the user provides a prototype, the library could enforce that proto instanceof Function [2]. This way we would preserve the invariant that for any callable value v, either v is a regexp or (typeof v === "function" && v instanceof Function).
> 
> Maciej has also suggested a Function.create(...) API for more lightweight creation of function subtypes. This would strengthen the argument for allowing Proxy.createFunction to specify a prototype, since Proxy.createFunction() ought to be able to do anything Function.create() can do.
> 
> I'm curious to know if there are reasons I've missed why Proxy.createFunction() doesn't support a custom prototype. It seems to me like a nice additional expressiveness without any great loss of language invariants. But I may have missed something.
> 
> Thanks,
> Dave
> 
> [1] In the face of multiple globals, this would be the Function associated with the same global as Proxy. (BTW, I intend to work with Andreas on speccing out multiple globals.)
> 
> [2] Again, the Function associated with the same global as Proxy.
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list