Does juce javascript not support the switch statement?


#1

I'm getting:

Found '{' when expecting ';'


after a switch statement. Same in the demo.

 

Is there a list of supported syntax?


#2

No, don't think I added that.. (It's actually a bit of a messy one to implement, but I guess I'll need to at some point).


#3

I just added support for switch statements (getting the default and empty case stuff right was indeed messy).

Because I forked the engine (I needed to hack around in other places), I can’t simply put up a diff flie, but if there is any interest, I could collect the code additions manually and post it here.

I added a behaviour which is not 100% compliant, but heavily increases performance (and therefore the whole reason of existence of the switch statement):

The case conditions are evaluated once (at the first execution of the switch statement) and are then treated as constants.

This has the advantage that the expressions after the case keyword are not evaluated every time but stored as var object which makes the branching pretty fast (just comparing var objects for equality instead of parsing and evaluating an expression).

I made this assumption because I couldn’t think of one scenario where you would need a switch statement with variable case expressions - if you need variable conditions, the if/else structure is more suitable.

However, this has some weird side effects if you ignore this behaviour:

var y = 2;

function doTheSwitchWrong()
{
    var x = Math.random() > 0.5 ? 2 : 1;
    
    // The case condition values are supposed to be constants
    // If x gets another value than its first time, the false condition will be triggered.
    switch(y)
    {
        case x: Console.print("x should be 2"); break;
        default: Console.print("x should be 1");
    }

    Console.print("x is " + x);
}

function doTheSwitchRight()
{
    var x = Math.random() > 0.5 ? 2 : 1;

    // Since y is a constant, everything will work correctly
    // If x gets another value than its first time, it will compare against the correct value of y
    switch(x)
    {
        case y: Console.print("x should be 2"); break;
        default: Console.print("x should be 1");
    }

    Console.print("x is " + x);
}

Console.print("Wrong: ");

doTheSwitchWrong();
doTheSwitchWrong();
doTheSwitchWrong();
doTheSwitchWrong();

Console.print("Right: ");

doTheSwitchRight();
doTheSwitchRight();
doTheSwitchRight();
doTheSwitchRight();

The output is something like this:

Wrong: 
x should be 1
x is 1
x should be 1
x is 2 // ERROR, ERROR, System overload!!!
x should be 1
x is 1
x should be 1
x is 1
Right: 
x should be 1
x is 1
x should be 2
x is 2
x should be 1
x is 1
x should be 1
x is 1

As you can see, the second run is a faulty one, where the conditions differ and trigger the false case.

Unlike C-like switch statements, the Javascript switch statement supports all types (so you can use constant strings or objects as case expressions. Currently it does not check types (it seems that the official Javascript implementation of a switch statement is type safe), so this code:

var x = 2;

switch(x)
{
    case "2": Console.print("Wrong type, same value"); break;
    default: 
}

will trigger the condition. This is because the == operator of the var class does not check the type and implementing it correctly would mean writing more boring code so I didn’t add this yet.