[whatwg] Passing more than JSON data to workers

Oliver Hunt oliver at apple.com
Thu Dec 17 02:06:06 PST 2009


On Dec 17, 2009, at 10:03 PM, Boris Zbarsky wrote:

> On 12/17/09 12:48 AM, Boris Zbarsky wrote:
>> It seems very difficult to me to come up with a "function cloning"
>> solution that won't break in subtle ways when such functions are passed
>> to it...
> 
> I should clarify this.  It seems to me eminently possible to clone functions that only reference local (declared with var) variables and their arguments.  And maybe explicit |this| access; not sure.
> 
> As soon as you're talking anything else, the situation gets very complicated, it seems to me.  That includes implicit property access on the global object.
> 
> To make that clearer, consider these two functions, defined at global scope:
> 
>  var x = 1;
>  function f() {
>    return x;
>  }
>  function g() {
>    return Math;
>  }
> 
> If I understand your proposal correctly, passing f to a worker would pass across a function which always returns 1.  Passing g to a worker would do what?  Pass across a function that always returns the Math object from the web page scope?  If not, then how is Math different from x, exactly?  If yes, then why are we baking anything at all in at pass time?
> 
> How is the f() example above affected if x is bound to an object, not to a number?

I think a more interesting case is the relatively common idiom of closures for access protection, eg.

function MyObject() {
    var x;
    this.setX = function(_x) { x = _x };
    this.getX = function() { return x }
}

What should worker.postMessage(new MyObject) do if we were to try and serialise the functions? obviously you don't want them each to have (effectively) separate closures, and you can't just substitute their containing scope with the global object.

> -Boris


--Oliver




More information about the whatwg mailing list