Can we have Function.isPure(f)

Mark S. Miller erights at google.com
Mon Nov 5 14:03:45 PST 2012


On Mon, Nov 5, 2012 at 11:52 AM, Irakli Gozalishvili <rfobic at gmail.com>wrote:

>  Hi,
>
> I keep running into cases where I would like to know if function is pure.
> Although my interpretation of pure is not quite right but I don't know any
> better name. By pure in this context I would refer to functions that don't
> access an out scope variables and don't
> do any mutations of itself or it's properties no references to itself
> could be an option too. My intended use case for such a feature is to
> allow transfer (by which I meant just coping) of such functions across
> workers / processes. For example in Mozilla add-on
> we have a context-menu API
> https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/context-menu.html
> which has limited declarative syntax for context definitions where
> specific context item should appear. There is also programable API through
> content-script files where one could write a predicate function that
> is invoked in the page scope with a node reference on which event
> has occurred. In most though pure function would have being enough but
> there is no way for us to know weather given function refers to outer scope
> variables. Same use cases arise with web workers and node processes too, it
> would be great if we had something like Function.isPure(f). Also as far as
> I know jits already capture this info for optimisation purposes maybe
> it could be exposed ? Another alternative could be pure(function() { ….  })
> that would throw compile error if
> function followed is not pure.
>

Hi Irakli, because of <
http://wiki.ecmascript.org/doku.php?id=harmony:function_to_string>, you can
now code this yourself in JS, but only by writing a full JS parser and
scope analyzer. Alternatively, you can use SES <
http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/ses/>,
which evaluates the function in a scope preventing it from accessing any
non-whitelisted external variables, without needing to parse it, and
therefore without needing to determine whether it actually does so.

We use this ability to safely copy closed functions at
http://wiki.ecmascript.org/doku.php?id=strawman:concurrency#vats (last
paragraph)
and
starting at
http://wiki.ecmascript.org/doku.php?id=strawman:concurrency#vat.evallater_as_async-pgas

-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121105/a9d7b6c2/attachment-0001.html>


More information about the es-discuss mailing list