custom proto arg for Proxy.createFunction?
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 . But if the user provides a prototype, the library could enforce that proto instanceof Function . 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.
>  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.)
>  Again, the Function associated with the same global as Proxy.
> es-discuss mailing list
> es-discuss at mozilla.org
More information about the es-discuss