Safe, Closure-free, Serializable functions

Mark S. Miller erights at google.com
Wed Sep 25 18:15:20 PDT 2013


A concrete example of using SES to serialize and unserialize safe closure
free functions is Bob, serializing the escrowExchange function on line 22
of <
https://code.google.com/p/es-lab/source/browse/trunk/src/ses/contract/makeBob.js#22
>:

    var escrowSrc = ''+escrowExchange;

This works because of <
http://wiki.ecmascript.org/doku.php?id=harmony:function_to_string>. Bob
then sends it to the contractHost, who doesn't trust Bob, on line 59 of <
https://code.google.com/p/es-lab/source/browse/trunk/src/ses/contract/makeBob.js#59
>:

        var tokensP = Q(contractHostP).send('setup', escrowSrc);

in ES5 syntax, or

        var tokensP = contractHostP ! setup(escrowSrc);

in strawman ES7 syntax. The contractHost received this string it its setup
method at line 89 and 90 of <
https://code.google.com/p/es-lab/source/browse/trunk/src/ses/contract/makeContractHost.js#89
>:

      setup: function(contractSrc) {
        contractSrc = ''+contractSrc;

which also coerces it to a string. It then evaluates it on line 95, by
calling the previously described confine function from the SES library:

        var contract = confine(contractSrc, {Q: Q});

The contractHost can now treat the unserialized contract as, in your
terminology, safe and closure-free.

Bob could of course have serialized any function, or made up any string he
wanted. But if the contractSrc expression has any free variables other than
the safe globals[1], i.e., if it is not closure-free[1] in your
terminology, then these variable references will only result in
ReferenceErrors when read or TypeErrors when written. As far as the
contractHost is concerned, it can safely assume that this object is closure
free[1].


[1] Except for Q, which the contractHost explicitly authorizes the
contractSrc to access, in the second argument to confine above.

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


More information about the es-discuss mailing list