espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.75k stars 739 forks source link

`continue` in a `switch` statement doesn't immediately continue to the next loop iteration #2339

Closed idobh2 closed 1 year ago

idobh2 commented 1 year ago

Continuing my attempts to transpile async/await to ES5, I encountered another issue. In both for and while loops, if the executed block has a switch statement in it, and the default block has a continue statement - the rest of the while block is executed instead of jumping to the next iteration.

(() => {
    let i = 0;
    while (i < 1) {
        console.log("i=", i);
        i++;
        switch (i) {
            default:
                continue;
        }
        console.log("this should never be logged"); // this is still being logged in espruino 
    }
})();

(() => {
    let i = 0;
    for (let i = 0; i < 1; i++) {
        console.log("i=", i);
        switch (i) {
            default:
                continue;
        }
        console.log("this should never be logged"); // this is still being logged in espruino 
    }
})();

In case the switch statement has a case which states continue - it crashes with a Uncaught SyntaxError: CONTINUE statement outside of FOR or WHILE loop error

(() => {
    let i = 0;
    while (i < 1) {
        console.log("i=", i);
        i++;
        switch (i) {
            case 1:
                continue; // this is crashing
        }
    }
})();

(() => {
    let i = 0;
    for (let i = 0; i < 1; i++) {
        console.log("i=", i);
        switch (i) {
            case 0:
                continue; // this is crashing
        }
    }
})();

This is reconstructed on both stable and latest builds, tested on emulator as well.

gfwilliams commented 1 year ago

Thanks! That's interesting - it must not be restoring state properly after the switch exits.

I don't think I'll get a chance to look into this today, but I'll see what can be done later in the week

idobh2 commented 1 year ago

@gfwilliams thank for the quick response - got async/await to work!