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

オープン
#618 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 5 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, javascript
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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/

主要言語
C
スター
11.1k
フォーク
1.2k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bellard/quickjs のほかの issue

bellard/quickjs の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。