Monday, February 19th, 2007

Threading in JavaScript 1.7

Category: Editorial

Neil Mix wrote a post before Brendan discussing how we can do threading in JavaScript 1.7:

Amongst the many new features contained in Firefox 2 you’ll find JavaScript 1.7 support, a small but significant language enhancement. While there are several interesting features, the python style generators are of particular interest to me.

A generator allows you to suspend a function’s execution and resume it at a later time. While typically used for advanced looping constructs, a generator can also be flipped inside-out and used as a rudimentary coroutine.

At first glance, this may not look very impressive, especially since generators are unable to yield across multiple frames in the callstack. However, with the help of a technique called trampolining generators can be used to create advanced threading as powerful as that found in Narrative JavaScript.

You end up with code like this:


  1. function myThreadedCall() {
  2.     while (!ready) {
  3.       yield sleep(100);
  4.     }
  5.     yield waitForInput();
  6.     if ((yield post(getInput())) != null ) {
  7.       yield animateUI();
  8.     }
  9.   }

Not the most intuitive to many.

Brendan himself commented:

Hey Neil, I wanted to add that the winning argument against Lua-style coroutines in es4-discuss, IMHO, came from Lars T Hansen, who wrote:

“ECMAScript already has potential script-native-script call stacks in library functions that accept callbacks, like Array.prototype.sort and String.prototype.replace. So [Lua-style coroutines] would be a restriction in all implementations that are not prepared to write their libraries in ECMAScript. So it would be implementation dependent on whether you could yield from a callback to these methods. This does not seem like a feature.”

Posted by Dion Almaer at 12:34 am

3.7 rating from 30 votes


Comments feed TrackBack URI

For those who are interested, I played around with this new JS 1.7 feature for a school project:

Basically a little interface for showing how different sorting algorithms work visually. In order to allow for two of those running at the same time I use the new yield / .next() / Generator stuff available in JS 1.7.

-> Demo only works in FF 2.0

Comment by Felix Geisendörfer — February 19, 2007

Isn’t this totally useless unless all major browser support this? Until then we are back in 199x as far as browser switches are concerned and I do not think that anyone will write his code both threaded and not threaded.

I did not hear anything yet about implementing JS1.7 in IE or Opera or … but I may be wrong here. And even if it would be implemented in IE8 we all know that this will be totally different to what FF2.0 is doing :-(

Comment by Landseer — February 19, 2007

Lack of 1.7 support in browsers is exactly why to use Neil’s main project, Narrative JavaScript, for coroutines/continuations/threading instead of just the threading library. The threading library was just to demonstrate that it could all be done without precompilation in 1.7.

Comment by Kris Zyp — February 19, 2007

Dion, this is posted in “Editorial”. Are you attempting to speak out against the trampolining technique I discussed? If so, I’m totally baffled because the quote from Brendan is taken completely out of context (he wasn’t talking about trampolining or generators), and you haven’t really made any case other than “not intuitive to some people”. (Most new language features are “unintuitive” at first sight.)

I don’t mind if you object to the technique (quite the opposite — I encourage you to speak up), but I might recommend you make your opinion and case a bit clearer.

Comment by Neil Mix — February 19, 2007

Leave a comment

You must be logged in to post a comment.