zcorpan wrote:cheezy wrote:Why do web workers always have to be defined in a separate script? (...) One possible reason I can think of is the separation between the worker's environment and the host environment,
Yep.
Thank you for confirming my guess. Is this noted in a FAQ or some similar place? Or is it so obvious that I am the only one thinking about it?
zcorpan wrote:cheezy wrote: but aren't there other ways to accomplish this?
How?
I am not a Javascript VM developer, so if the following does not make sense, please don't be too hard on me. A reply of "Sorry, we thought about this longer than you did, and there are still cases where this is impossible" is perfectly valid, but the more I can learn from this conversation, the better.
Would it be possible to do a deep copy of the function (object) you pass to the the constructor? So copy everything (or mark it for copy-on-write), but remove references to DOM elements if they exist. This way, I think you can create a parallel data structure, so the original one remains untouched (avoiding concurrency issues).
The important difference between this and the usual JSON-serializing of objects that the examples talk about, is that functions can be passed through too in an easy manner. If you have to simulate this using only Javascript, you have to somehow bind the free variables, which requires some introspection, and thus is not easy (if even possible?) to simulate in "user space".
The Google Gears API seems to provide both
createWorker(scriptText) and
createWorkerFromUrl(scriptUrl). Why was only the URL variant retained in the Web Workers spec? With the script variant, there would have been at least a little basis for more dynamic programming.