Finally... Goto in Javascript
summerofgoto.com
summerofgoto.com
I've worked through various ways of trying to implement goto in Javascript via preprocessing for the parenscript CL library, and have always come away feeling unsatisfied. You can do it, but as far as I can tell, you have to use a trampoline.
A native goto statement in Javascript would go a long way into making JS an acceptable compilation target.
Much agreement here. I alternate between wishing for a goto in JS, and wishing for a lower level bytecode to be exposed from the browser.
goto does seem more likely though.
Labels "[lbl] foo" would become "case 'foo':goto_label=false;" and gotos "goto foo;" would become "goto_label='foo'; continue goto_loop;".
Example:
var goto_label='top';
goto_loop: while(goto_label) {
switch(goto_label) {
case 'top': goto_label=false;
...
goto_label='foo'; continue goto_loop;
...
case 'foo': goto_label=false;
...
}}
Not perfect, but better.edit: scratch that, labels within other control structures won't work, only top level ones.
Goto floor(10);
Open window;
Jump();I hope this one is a joke. Isn't goto looked down on because of its role in the Roman Inquisition? Seriously though, the project is just a cool technical hack not meant for public consumption, right? Right?
Without goto and TCO (goto by another name), you're left to transform your code to be run under a trampoline, which really makes the resulting JS unreadable.
Besides, I object to bondage and discipline in a programming language. If people don't have guns, then they'll use....
The analogy still holds :-)
edit: I mean to say literally that GOTOs certainly are useful, but are far easier to create spaghetti code with than something much safer like functions. It's a cost/benefit analysis.
Unfortunately great power (goto) comes with great responsibility (don't produce spagetti). Most $programmers don't get that right.
Looking over a fairly large project here, a majority of the code is written in CPS because of callbacks to the server or delays for user interaction.
(So far, I've avoided the need to trampoline, since my call stack isn't getting too deep yet before it unwinds on it its own. But, then again, I haven't done any cross-browser testing at all yet.)
Java doesn't have goto, but it has labeled break and continue statements. Great for exiting nested loops. Exceptions are another kind of structured goto. Of all the criticisms of Java, I've never heard the lack of goto bemoaned.
One thing I love about the Java designers is their humility: they thought the above was enough to cover all the cases where a goto was really convenient, but still made goto a reserved word. Just in case.
Anyway, AFAICS, cross-function gotos are impossible with this implementation, so you can't write real spaghetti code with it. Awww :).
[btw, I suppose it [edit: cross-function gotos and whatnot] should be possible. It would probably take a full-blown continuation passing style transform, though]
"NOTE: Seriously. Never use this."
loop: for(i=0;i<10;i++){ for(...) { break loop; } }
So not really a Goto in any real sense of the word.
Just one...
There plenty of examples.
It's not usually needed but it often helps.