[BUG] ExtendScript incorrectly handles the "continue" statement within "switch" statements
Description
ExtendScript does not handle the continue statement according to the ECMAScript 3rd Edition standard when located within a case clause of a switch statement. The problem is slightly different when a continue statement is placed within a switch statement's default clause. The specific issues are as follows:
- Within a [switch-case] statement: The continue statement is ignored.
- Within a [switch-default] statement: The continue statement breaks out of the default clause of the switch statement and continues processing after the switch statement, rather than with the next iteration of the enclosing loop.
This can cause fairly straightforward logic to explode in surprising ways. It can also make it difficult to use modern technologies with ExtendScript (e.g. transpilers).
Details
This is a simple test-case:
var test = function()
{
for (var i = 0; i < 3; ++i)
{
$.writeln("Pre Switch");
switch(i)
{
case 0:
$.writeln("\tCase 0");
break;
case 1:
$.writeln("\tCase 1");
continue;
default:
$.writeln("\tStart Default");
if (i == 2)
{
$.writeln("\t\tAt 2");
continue;
}
$.writeln("\tEnd Default");
}
$.writeln("Post Switch");
}
}
test();
Current Erroneous Behavior
Running this in the ExtendScript Toolkit (v4.0.0.1, ExtendScript 4.5.5) produces the following output:
Pre Switch
Case 0
Post Switch
Pre Switch
Case 1
Start Default
End Default
Post Switch
Pre Switch
Start Default
At 2
Post Switch
Lines that should not be printed are highlighted in red. There are two groupings of incorrect functionality, one for each of the two continue statements. Explanations of what is happening in these two cases follows:
- The first three erroneous print statements occurred because the continue statement at line 14 was ignored. It should have caused processing to return to the top of the for-loop at line 3. Instead, processing falls-through into the default clause.
- The last erroneous print statement occurred because the continue statement at line 20 did not correctly cause processing to return to the top of the for-loop at line 3. Instead, it caused processing to skip the rest of default clause and then continue after the end of the switch statement.
Expected Behavior
This is the expected output:
Pre Switch
Case 0
Post Switch
Pre Switch
Case 1
Pre Switch
Start Default
At 2
That output was generated in the JavaScript console of the Chrome 64 browser. It is possible to run the test code provided above in a browser context as long as you first run the following:
var $ = {
writeln: console.log
};
This creates a $ object with a writeln function that simply points to the standard console.log function. Once this has been processed in the browser's JavaScript context, the test script provided will run without issue.
Notes
According to the ECMAScript 3rd Edition spec [p. 67], continue statements always refer to "Iteration Statements" (for, while, do-while loops). They should ignore switch statements entirely.
