Skip to content

Commit 9d3776d

Browse files
author
Fabrice Bellard
committed
fixed break statement in the presence of labels (bnoordhuis) (#275)
1 parent 00e6f29 commit 9d3776d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

quickjs.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19794,7 +19794,8 @@ typedef struct BlockEnv {
1979419794
int drop_count; /* number of stack elements to drop */
1979519795
int label_finally; /* -1 if none */
1979619796
int scope_level;
19797-
int has_iterator;
19797+
uint8_t has_iterator : 1;
19798+
uint8_t is_regular_stmt : 1; /* i.e. not a loop statement */
1979819799
} BlockEnv;
1979919800

1980019801
typedef struct JSGlobalVar {
@@ -25763,6 +25764,7 @@ static void push_break_entry(JSFunctionDef *fd, BlockEnv *be,
2576325764
be->label_finally = -1;
2576425765
be->scope_level = fd->scope_level;
2576525766
be->has_iterator = FALSE;
25767+
be->is_regular_stmt = FALSE;
2576625768
}
2576725769

2576825770
static void pop_break_entry(JSFunctionDef *fd)
@@ -25791,7 +25793,8 @@ static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont)
2579125793
}
2579225794
if (!is_cont &&
2579325795
top->label_break != -1 &&
25794-
(name == JS_ATOM_NULL || top->label_name == name)) {
25796+
((name == JS_ATOM_NULL && !top->is_regular_stmt) ||
25797+
top->label_name == name)) {
2579525798
emit_goto(s, OP_goto, top->label_break);
2579625799
return 0;
2579725800
}
@@ -26355,6 +26358,7 @@ static __exception int js_parse_statement_or_decl(JSParseState *s,
2635526358
label_break = new_label(s);
2635626359
push_break_entry(s->cur_func, &break_entry,
2635726360
label_name, label_break, -1, 0);
26361+
break_entry.is_regular_stmt = TRUE;
2635826362
if (!(s->cur_func->js_mode & JS_MODE_STRICT) &&
2635926363
(decl_mask & DECL_MASK_FUNC_WITH_LABEL)) {
2636026364
mask = DECL_MASK_FUNC | DECL_MASK_FUNC_WITH_LABEL;

tests/test_language.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,24 @@ function test_labels()
398398
while (0) x: { break x; };
399399
}
400400

401+
function test_labels2()
402+
{
403+
while (1) label: break
404+
var i = 0
405+
while (i < 3) label: {
406+
if (i > 0)
407+
break
408+
i++
409+
}
410+
assert(i, 1)
411+
for (;;) label: break
412+
for (i = 0; i < 3; i++) label: {
413+
if (i > 0)
414+
break
415+
}
416+
assert(i, 1)
417+
}
418+
401419
function test_destructuring()
402420
{
403421
function * g () { return 0; };
@@ -618,6 +636,7 @@ test_template_skip();
618636
test_object_literal();
619637
test_regexp_skip();
620638
test_labels();
639+
test_labels2();
621640
test_destructuring();
622641
test_spread();
623642
test_function_length();

0 commit comments

Comments
 (0)