for (let …) loop: `continue` skips the per-iteration copy of the loop variables
Maintainers usually reply within 5 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- c, javascript
- Domain
- compilers
Research direction
Start in quickjs.c, in js_parse_statement_or_decl under case TOK_FOR, and trace how the loop’s continue target reaches the body-end scope cleanup. Compare the emitted labels and scope closing with the issue’s minimal reproduction, then add a regression case for continue with a for (let …) closure. Done means the reproduction prints 0,1 and the regression case passes.
Written by the indexing model from the issue text.
Description
Environment
QuickJS 2026-06-04 (bellard.org tarball, sha256 b376e839b322978313d929fd20663b11ba58b75df5a46c126dd19ea2fa70ad2a),
make qjs, Debian 12 x86-64, gcc 12.2. Same result with 2024-01-13, 2025-04-26 and 2025-09-13: the bug is
long-standing, not a regression.
Reproduction
var fs = [];
for (let i = 0; i < 2; i++) {
fs.push(() => i);
continue;
}
console.log(fs.map(f => f()).join());
Expected 0,1 (node v20.20.2 and v26.13.0 print this; other engines not tested); QuickJS prints 2,2. Removing the continue gives
0,1. Conditional, nested-block, labelled (continue outer from an inner loop), try/finally and switch
variants fail the same way.
Spec
ECMA-262 ForBodyEvaluation (https://tc39.es/ecma262/#sec-forbodyevaluation): after Evaluation of stmt, a
completion for which LoopContinues is true (normal, or continue targeting this loop) is followed by
CreatePerIterationEnvironment(perIterationBindings) before the increment, so a closure created in an
iteration keeps that iteration's binding.
Cause and patch
quickjs.c, js_parse_statement_or_decl, case TOK_FOR: the body is followed by
/* XXX: check continue case */ close_scopes(...), but continue (emit_break) closes scopes only down to the
loop-head scope and jumps to label_cont, which is emitted after that close_scopes (the increment code is
moved there under OPTIMIZE). Fix: give continue its own label in front of the body-end close_scopes.
- int label_cont, label_break, label_body, label_test;
+ int label_cont, label_break, label_body, label_test, label_close;
@@
if (js_parse_expect(s, ')'))
goto fail;
+ /* `continue` must leave the per-iteration scope as the end of
+ the body does; otherwise closures created in this iteration
+ see the next iteration's bindings */
+ label_close = new_label(s);
+ break_entry.label_cont = label_close;
+
pos_body = s->cur_func->byte_code.size;
emit_label(s, label_body);
if (js_parse_statement(s))
goto fail;
/* close the closures before the next iteration */
- /* XXX: check continue case */
+ emit_label(s, label_close);
close_scopes(s, s->cur_func->scope_level, block_scope_level);
Applied to 2026-06-04 and rebuilt: a 10-case check (plain/conditional continue, no-increment and no-test loops,
body let, labelled continue, try/finally, switch, var loop, for-of) matches node in every case (unpatched:
7 wrong). The same fix for quickjs-ng, with a regression test: quickjs-ng/quickjs#1875, quickjs-ng/quickjs#1876.
Duplicates checked (2026-11-03)
GitHub search in bellard/quickjs (issues and PRs) for "continue", "continue let", "continue closure loop",
"for let closure", "per-iteration", "let binding loop closure": nothing matching (hits: #67, #112, #156,
#189, #192, #251, #259, #341, #445 — unrelated).
Related: closures share one binding after a caught throw leaves the block
Closures capturing a const/let of a block share one binding across loop iterations if the block exits through a throw that is caught in the same function:
const handlers = [];
for (const item of [{ id: 1 }, { id: 2 }, { id: 3 }]) {
try {
const id = item.id;
handlers.push(() => id);
if (id < 3) throw new Error("invalid");
} catch (e) {}
}
console.log(handlers.map((h) => h()).join(",")); // 3,3,3 expected 1,2,3
The same happens for a bare block ({ const k = …; push(() => k); throw 0; }) and for the binding of an inner for…of/for…in left by a throw; there the earlier closures throw ReferenceError: k is not initialized once the outer loop goes on. With break or without the throw the output is right. It looks like the exception path to the handler does not close the captured locals of the scopes it leaves, so the next iteration reuses the slot.
Versions: QuickJS master e5ac135 (2026-11-09), release 2026-06-04; quickjs-emscripten 0.32.0 (2025-09-13 base). node 26.13.0, SpiderMonkey 159 and Boa 0.22 print 1,2,3.
- Dominant language
- C
- Stars
- 11.1k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bellard/quickjs
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 5 days
-
RegExp: patterns with more than 255 capture groups fail with "too many captures"Possibly taken @andreasrosdal claimed this 3 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
bellard/quickjs#624 · 1 comment ·
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bellard/quickjs#620 · 1 comment ·
Maintainers usually reply within 5 days
-
(comment on #611) `function g(a, d = (a = 10)) { return a; }` returns the argument instead of 10Open
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
Similar issues
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
JulianKemmerer/PipelineC#409 ·
-
Linux build fails without libspeechd-dev, which INSTALL.md doesn't list and CMake reports as foundOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
needs-playtest slice
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Life-Framework/Life-Framework#4861 ·
Maintainers usually reply within 1 day