D
David Given
I have a situation where I need to use GOTO in a Javascript program.
No, I can't use break or continue. Yes, I really do know what I'm doing.
What I've got is a huge mass of automatically generated code that
contains thousands of clauses like this:
label1:
...do something...
if (condition)
goto label3;
else
goto label9;
This situation is exactly what GOTO is the ideal tool for. Except that
Javascript doesn't have GOTO.
One alternative is to use tail calls, so that each block would turn into
a function like this:
function label1()
{
...do something...
if (condition)
return label3();
else
return label9();
}
Unfortunately, Javascript doesn't support tail calls either --- each
function call adds another entry to the stack which means I only get a
few thousand calls before everything grinds to a halt.
The only other thing I can think of is to use a switch statement like this:
while (true)
{
switch (label)
{
case 1:
...do something...
if (condition)
label = 3;
else
label = 9;
break;
}
}
However this is going to do horrible things to my performance due to
having to keep reading and writing the label variable; also, I'm very
dubious has to how well Javascript switch statements perform with very
big numbers of entries (thousands, remember).
Can anyone suggest anything else I could try?
No, I can't use break or continue. Yes, I really do know what I'm doing.
What I've got is a huge mass of automatically generated code that
contains thousands of clauses like this:
label1:
...do something...
if (condition)
goto label3;
else
goto label9;
This situation is exactly what GOTO is the ideal tool for. Except that
Javascript doesn't have GOTO.
One alternative is to use tail calls, so that each block would turn into
a function like this:
function label1()
{
...do something...
if (condition)
return label3();
else
return label9();
}
Unfortunately, Javascript doesn't support tail calls either --- each
function call adds another entry to the stack which means I only get a
few thousand calls before everything grinds to a halt.
The only other thing I can think of is to use a switch statement like this:
while (true)
{
switch (label)
{
case 1:
...do something...
if (condition)
label = 3;
else
label = 9;
break;
}
}
However this is going to do horrible things to my performance due to
having to keep reading and writing the label variable; also, I'm very
dubious has to how well Javascript switch statements perform with very
big numbers of entries (thousands, remember).
Can anyone suggest anything else I could try?