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

Aperta
#618 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, javascript
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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/

Lingua principale
C
Stelle
11.1k
Fork
1.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bellard/quickjs

Tutte le issue di bellard/quickjs

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.