for (let …) loop: `continue` skips the per-iteration copy of the loop variables

Đang mở
#592 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, javascript
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C
Star
11.1k
Fork
1.2k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bellard/quickjs

Tất cả issue của bellard/quickjs

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.