oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.81k stars 190 forks source link

Empty case clauses ignored in switch statements #292

Closed Alhadis closed 4 years ago

Alhadis commented 4 years ago

GraalJS doesn't branch to an empty case clause if there's a default specified. For example:

let value = 2;
switch(value){
    default:
        console.log("Default");
        break;
    case 1:
        console.log("One");
        break;
    case 2:
}

In other JS engines, this script will (correctly) output nothing. In GraalJS, however, it prints "Default" as if case 2 didn't match. Note that adding a break statement changes this:

 let value = 2;
 switch(value){
    default:
        console.log("Default");
        break;
    case 1:
        console.log("One");
        break;
    case 2:
+       break;
 }
iamstolis commented 4 years ago

Thank you for the report! Yes, this looks like a bug in GraalJS. It seems that we "overoptimize" the empty case.

iamstolis commented 4 years ago

Fixed by https://github.com/graalvm/graaljs/commit/bec8d9eea00cb140f3c6ec8aaab9fbd853de191a