Skip to content

Commit fa1013c

Browse files
committed
Fix break statement in presence of labels
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
1 parent ebc1a65 commit fa1013c

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

quickjs.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18626,7 +18626,8 @@ typedef struct BlockEnv {
1862618626
int drop_count; /* number of stack elements to drop */
1862718627
int label_finally; /* -1 if none */
1862818628
int scope_level;
18629-
int has_iterator;
18629+
BOOL has_iterator : 1;
18630+
BOOL is_regular_stmt : 1; // i.e. not a loop statement
1863018631
} BlockEnv;
1863118632

1863218633
typedef struct JSGlobalVar {
@@ -24891,6 +24892,7 @@ static void push_break_entry(JSFunctionDef *fd, BlockEnv *be,
2489124892
be->label_finally = -1;
2489224893
be->scope_level = fd->scope_level;
2489324894
be->has_iterator = FALSE;
24895+
be->is_regular_stmt = FALSE;
2489424896
}
2489524897

2489624898
static void pop_break_entry(JSFunctionDef *fd)
@@ -24917,11 +24919,12 @@ static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont)
2491724919
emit_goto(s, OP_goto, top->label_cont);
2491824920
return 0;
2491924921
}
24920-
if (!is_cont &&
24921-
top->label_break != -1 &&
24922-
(name == JS_ATOM_NULL || top->label_name == name)) {
24923-
emit_goto(s, OP_goto, top->label_break);
24924-
return 0;
24922+
if (!is_cont && top->label_break != -1) {
24923+
if (top->label_name == name ||
24924+
(name == JS_ATOM_NULL && !top->is_regular_stmt)) {
24925+
emit_goto(s, OP_goto, top->label_break);
24926+
return 0;
24927+
}
2492524928
}
2492624929
i = 0;
2492724930
if (top->has_iterator) {
@@ -25479,13 +25482,14 @@ static __exception int js_parse_statement_or_decl(JSParseState *s,
2547925482
&& s->token.val != TOK_DO
2548025483
&& s->token.val != TOK_WHILE) {
2548125484
/* labelled regular statement */
25485+
JSFunctionDef *fd = s->cur_func;
2548225486
int label_break, mask;
2548325487
BlockEnv break_entry;
2548425488

2548525489
label_break = new_label(s);
25486-
push_break_entry(s->cur_func, &break_entry,
25487-
label_name, label_break, -1, 0);
25488-
if (!s->cur_func->is_strict_mode &&
25490+
push_break_entry(fd, &break_entry, label_name, label_break, -1, 0);
25491+
fd->top_break->is_regular_stmt = 1;
25492+
if (!fd->is_strict_mode &&
2548925493
(decl_mask & DECL_MASK_FUNC_WITH_LABEL)) {
2549025494
mask = DECL_MASK_FUNC | DECL_MASK_FUNC_WITH_LABEL;
2549125495
} else {
@@ -25494,7 +25498,7 @@ static __exception int js_parse_statement_or_decl(JSParseState *s,
2549425498
if (js_parse_statement_or_decl(s, mask))
2549525499
goto fail;
2549625500
emit_label(s, label_break);
25497-
pop_break_entry(s->cur_func);
25501+
pop_break_entry(fd);
2549825502
goto done;
2549925503
}
2550025504
}

tests/bug741.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {assert} from "./assert.js"
2+
3+
while (1) label: break
4+
5+
var i = 0
6+
while (i < 3) label: {
7+
if (i > 0)
8+
break
9+
i++
10+
}
11+
assert(i, 1)
12+
13+
for (;;) label: break
14+
15+
for (i = 0; i < 3; i++) label: {
16+
if (i > 0)
17+
break
18+
}
19+
assert(i, 1)

0 commit comments

Comments
 (0)