(comment on #611) `function g(a, d = (a = 10)) { return a; }` returns the argument instead of 10

Đang mở
#618 0 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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
30/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

Repro is ./qjs t.js with the g/k/f snippets; run it first to confirm 1 [1,10] and 1. The relevant path is FunctionDeclarationInstantiation: fd->has_parameter_expressions, the argument-scope copy through OP_get_arg and OP_scope_put_var_init, and body lookup via find_var/find_arg falling back to fd->args. Done when g(1) and f(1) print 10 and 5 and existing tests still pass.

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

Mô tả

One more from the same run, on master e585640 (2026-11-10) and release 2026-06-04 (./qjs t.js).

3. Parameter assignment in a default initializer (or parameter-scope closure) is not visible in the function body.

function g(a, d = (a = 10)) { return a; }
function k(a, c = () => a, d = (a = 10)) { return [a, c()]; }
console.log(g(1), JSON.stringify(k(1)));
// expected: 10 [10,10]

./qjs t.js prints 1 [1,10]. Because the body has no var a, the parameter list and body share one binding for a (FunctionDeclarationInstantiation steps 27-28), so the assignment a = 10 during d's initializer must be visible in the body. The same happens when a closure created in the parameter list assigns to a later:

function f(a, set = () => { a = 5; }) { set(); return a; }
console.log(f(1));   // expected: 5

./qjs t.js prints 1. When fd->has_parameter_expressions is true, QuickJS copies each plain parameter into a separate JS_VAR_DEF_LET variable in the argument scope (OP_get_arg + OP_scope_put_var_init) so expressions and closures in the parameter list read/write that lexical variable, while the body resolves a to the original fd->args slot via find_var (find_arg) unless a body var shadows it.

Credit: https://ucsbplse.github.io/

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.