Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix break statement in presence of labels #742

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -18626,7 +18626,8 @@ typedef struct BlockEnv {
int drop_count; /* number of stack elements to drop */
int label_finally; /* -1 if none */
int scope_level;
int has_iterator;
uint8_t has_iterator : 1;
uint8_t is_regular_stmt : 1; // i.e. not a loop statement
} BlockEnv;

typedef struct JSGlobalVar {
Expand Down Expand Up @@ -24891,6 +24892,7 @@ static void push_break_entry(JSFunctionDef *fd, BlockEnv *be,
be->label_finally = -1;
be->scope_level = fd->scope_level;
be->has_iterator = FALSE;
be->is_regular_stmt = FALSE;
}

static void pop_break_entry(JSFunctionDef *fd)
Expand All @@ -24917,11 +24919,12 @@ 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)) {
emit_goto(s, OP_goto, top->label_break);
return 0;
if (!is_cont && top->label_break != -1) {
if (top->label_name == name ||
(name == JS_ATOM_NULL && !top->is_regular_stmt)) {
emit_goto(s, OP_goto, top->label_break);
return 0;
}
}
i = 0;
if (top->has_iterator) {
Expand Down Expand Up @@ -25335,7 +25338,8 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name,
JS_FreeAtom(ctx, var_name);

if (token_is_pseudo_keyword(s, JS_ATOM_of)) {
break_entry.has_iterator = is_for_of = TRUE;
is_for_of = TRUE;
break_entry.has_iterator = TRUE;
break_entry.drop_count += 2;
if (has_initializer)
goto initializer_error;
Expand Down Expand Up @@ -25479,13 +25483,14 @@ static __exception int js_parse_statement_or_decl(JSParseState *s,
&& s->token.val != TOK_DO
&& s->token.val != TOK_WHILE) {
/* labelled regular statement */
JSFunctionDef *fd = s->cur_func;
int label_break, mask;
BlockEnv break_entry;

label_break = new_label(s);
push_break_entry(s->cur_func, &break_entry,
label_name, label_break, -1, 0);
if (!s->cur_func->is_strict_mode &&
push_break_entry(fd, &break_entry, label_name, label_break, -1, 0);
fd->top_break->is_regular_stmt = 1;
if (!fd->is_strict_mode &&
(decl_mask & DECL_MASK_FUNC_WITH_LABEL)) {
mask = DECL_MASK_FUNC | DECL_MASK_FUNC_WITH_LABEL;
} else {
Expand All @@ -25494,7 +25499,7 @@ static __exception int js_parse_statement_or_decl(JSParseState *s,
if (js_parse_statement_or_decl(s, mask))
goto fail;
emit_label(s, label_break);
pop_break_entry(s->cur_func);
pop_break_entry(fd);
goto done;
}
}
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)
Loading