Summer of GOTO

Finally... Goto in Javascript

Many of you know, that this summer has seen a big push for GOTO. Languages across the board including, but not (necessarily) limited to PHP 5.3, and potentially more have added GOTO to their language! And the summer is not over yet! No wonder they are calling it The Summer of GOTO!*

Goto.js is the latest and greatest in GOTO technology. This script will allow you to use the GOTO functionality that you know and love directly in native javascript. This is a feature that the community has really been pushing for in the last few years. Prior to this script, goto was thought to be impossible to implement in javascript. There is no alternative to the functionality that GOTO offers (I mean, there is... but ughh... its so much code), so this script could revolutionize the way you write your javascript!!1!1!oneeleven! It requires a preparser (James Padolsey's parseScript) to rewrite valid javascript that has the same effect as GOTO.

The following are the necessary files:

That's it!

The syntax is simple:

Use `[lbl] <label_name>` for labels, and use `goto <label_name>` to get back there.

* May or may not be accurate.

NOTE: Since goto was originally created in languages without the same scope laws as javascript, you can only go back and forth within your current scope. You wouldn't be able to, for instance, jump from the middle of one function to the middle of another function, because there were no variables passed into that function. If you are a real GOTO power user you probably don't use functions anyways. NOTE: Also, as of now, labels must be defined before goto can be called on them (forward jumping gotos). This is due to a limitation in this implementation of the script. However this feature is only supported in some of the languages that natively have GOTO, so it was not a priority as of yet. NOTE: This makes `[lbl]` and `goto` reserved keywords. However, this is not strictly enforced. NOTE: Don't listen to those wackos that consider GOTO harmful! (actually you might want to read this.) NOTE: Seriously. Never use this.

A simple example

<script type="text/jsplusgoto">
var a = 0;
[lbl] topOfLoop:
a++;
if (a < 292 ) {
    goto topOfLoop;
}
document.getElementById('relevantLink').setAttribute('href', 'http://www.xkcd.com/' + a);
</script>

Output

Relevant Link: GOTO on XKCD!


A more complex demo

I don't condone the use of functions, but if you are stuck with bad code, GOTO will still work.
<script type="text/jsplusgoto">
var log = [],
    p   = 0;

var testGoTo = function testGoto() {
	
	// Goto calls inside of nested functions work
	this.logme = function tgLogMe(num, times) {
		var j = 0;
		[lbl] logtop:
		if (j < times) {
			log.push(num);
			j++;
			goto logtop;
		}
		document.getElementById('log').innerHTML = log.join('|');
	};
	
	// Safe strings
	var proof = "Strings with special chars like -- ";
	proof     = "function() { -- & -- goto lbl -- & -- [lbl] test";
	proof     = "-- are totally safe!";
	
	// Nested/Overlapping Gotos (aka the magic)
	var i = 0,
	    j = 0;
	[lbl] top:
	i++;
	[lbl] middle:
	j++;
	if (i < 10) {
		goto top;
	}
	if (j < 22) {
		goto middle;
	}
	
	// p was set below at 11
	// j-i should be 22-10 = 12
	this.logme(p, j-i);
	
	return j-i;
};

// Functions don't need to exist to use goto
// In fact... functions are for the weak.
[lbl] outsideLabel:
p++;
if (p < 11) {
	goto outsideLabel;
}
</script>
<script type="text/javascript">
    // Ideally this would actually be the onDomReady function of your choice...
    var otherload = window.onload;
    window.onload = function(){
	if (otherload) {otherload();}
	document.getElementById('jsResult').innerHTML = testGoTo();
    };
</script>

Output

Return value:  
Log:  

If you have something to comment on or add, or if you are dying for a more technical description, please go to the related blog post.