Skip to content

Commit

Permalink
Fix break statement in presence of labels
Browse files Browse the repository at this point in the history
In this snippet...

    for (;;) label: break

...the break statement jumped back to the start of the loop instead of
*out* of the loop.

Fixes: #741
  • Loading branch information
bnoordhuis committed Dec 4, 2024
1 parent ebc1a65 commit fcdebd7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 1 addition & 3 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24917,9 +24917,7 @@ static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont)
emit_goto(s, OP_goto, top->label_cont);
return 0;
}
if (!is_cont &&
top->label_break != -1 &&
(name == JS_ATOM_NULL || top->label_name == name)) {
if (!is_cont && top->label_break != -1 && top->label_name == name) {
emit_goto(s, OP_goto, top->label_break);
return 0;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/bug741.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {assert} from "./assert.js"

while (1) label: break

var i = 0
while (i < 3) label: {
if (i > 0)
break
i++
}
assert(i, 1)

for (;;) label: break

for (i = 0; i < 3; i++) label: {
if (i > 0)
break
}
assert(i, 1)

0 comments on commit fcdebd7

Please sign in to comment.