Skip to content

Breaks and labels interact incorrectly #741

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

Closed
WardBrian opened this issue Dec 4, 2024 · 3 comments · Fixed by #742
Closed

Breaks and labels interact incorrectly #741

WardBrian opened this issue Dec 4, 2024 · 3 comments · Fixed by #742
Labels
bug Something isn't working

Comments

@WardBrian
Copy link

This is essentially me duplicating bellard/quickjs#275 here, since it seems to still affect quickjs-ng:

./qjs-linux-x86_64 -e 'for (var i=0; i<4; i++)
label: {
  console.log(i);
  if (i === 1) {
    console.log("break");
    break;
  }
}'

prints

0
1
break
2
3

When the expected output does not print 2 or 3.

In particular, this seems to be an issue when using quickjs with code created the js_of_ocaml Ocaml->JS compiler, as this has moved to heavy use of labels and break statements for ocsigen/js_of_ocaml#1496

@bnoordhuis bnoordhuis added the bug Something isn't working label Dec 4, 2024
@bnoordhuis
Copy link
Contributor

Hah, it's even worse - the while variant just hangs:

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

Fix incoming!

bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Dec 4, 2024
In this snippet...

    for (;;) label: break

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

Fixes: quickjs-ng#741
bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Dec 4, 2024
In this snippet...

    for (;;) label: break

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

Fixes: quickjs-ng#741
@bnoordhuis
Copy link
Contributor

For posterity: the fix also fixes bellard/quickjs#282

@saghul
Copy link
Contributor

saghul commented Dec 4, 2024

Ha, nice one!

bluesky950520 pushed a commit to bluesky950520/quickjs that referenced this issue Mar 14, 2025
In this snippet...

    for (;;) label: break

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

Fixes: quickjs-ng/quickjs#741
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants