Skip to content

Commit

Permalink
Fixed incorrect handling of continue in for-in loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Oct 3, 2022
1 parent ea66e91 commit 5ea1285
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,14 @@ func (c *compiler) emitBlockExitCode(label *ast.Identifier, idx file.Idx, isBrea
c.throwSyntaxError(int(idx)-1, "Could not find block")
panic("unreachable")
}
contForLoop := !isBreak && block.typ == blockLoop
L:
for b := c.block; b != block; b = b.outer {
switch b.typ {
case blockIterScope:
if !isBreak && b.outer == block {
// blockIterScope in 'for' loops is shared across iterations, so
// continue should not pop it.
if contForLoop && b.outer == block {
break L
}
fallthrough
Expand Down
53 changes: 53 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5548,6 +5548,59 @@ func TestThisResolutionWithStackVar(t *testing.T) {
testScript(SCRIPT, valueTrue, t)
}

func TestForInLoopContinue(t *testing.T) {
const SCRIPT = `
var globalSink;
(function() {
const data = [{disabled: true}, {}];
function dummy() {}
function f1() {}
function f() {
dummy(); // move dummy to stash (so that f1 is at index 1)
for (const d of data) {
if (d.disabled) continue;
globalSink = () => d; // move d to stash
f1();
}
}
f();
})();
`
testScript(SCRIPT, _undefined, t)
}

func TestForInLoopContinueOuter(t *testing.T) {
const SCRIPT = `
var globalSink;
(function() {
const data = [{disabled: true}, {}];
function dummy1() {}
function f1() {}
function f() {
dummy1();
let counter = 0;
OUTER: for (let i = 0; i < 1; i++) {
for (const d of data) {
if (d.disabled) continue OUTER;
globalSink = () => d;
}
counter++;
}
f1();
if (counter !== 0) {
throw new Error(counter);
}
}
f();
})();
`
testScript(SCRIPT, _undefined, t)
}

/*
func TestBabel(t *testing.T) {
src, err := os.ReadFile("babel7.js")
Expand Down

0 comments on commit 5ea1285

Please sign in to comment.