If fn is not a normal function with no free variables, raise an Error.
If unpacker is not a normal function with no free variables, raise an Error.
Let packed = packer(state).
If packed is not an ArrayBuffer raise a TypeError.
Return an uncallable value that can be sent over a channel including enough information for Function.importMobile to reconstitute (fn, unpacker, packed).
Function.importMobile(mfn)
If mfn is not a valid output from Function.makeMobile raise a TypeError.
Let (fn, unpacker, packed) = the reverse the last step makeMobile did to produce mfn.
Let unpacked = Call(unpacker, null, [packed])
Let globals = the global scope for Function's Realm.
Let imported = BoundFunctionCreate(null, [globals, unpacked])
Return imported
To apply this to the blöck example, rewrite the code you want to be mobile as a lambda so that the fetch call introduces no free variable.
and for the use case of blöcks that capture and clone their environment:
const result = await worker<endpoint>{|
const res = await fetch(endpoint); // OK to use now
const json = await res.json();
return json[2].firstName;
|};
The { endpoint } provides the same cues as the <endpoint> mechanism.
One can override how cloning happens by specifying different packer/unpackers. For example, one could use a packer that calls JSON.stringify with a replacer that tries to make functions mobile.
I think the interface part of the problem can be phrased in terms of mobile functions.
"Confined mobile functions" explains the terminology.
To fit this into JavaScript, we could define two new methods on
Function
.Function.importMobile
to reconstitute (fn, unpacker, packed).Function.makeMobile
raise a TypeError.makeMobile
did to produce mfn.Function
's Realm.To apply this to the blöck example, rewrite the code you want to be mobile as a lambda so that the
fetch
call introduces no free variable.and for the use case of blöcks that capture and clone their environment:
becomes
The
{ endpoint }
provides the same cues as the<endpoint>
mechanism.One can override how cloning happens by specifying different packer/unpackers. For example, one could use a packer that calls JSON.stringify with a replacer that tries to make functions mobile.