`break` or `return` inside `for ... in` leaves the enumeration on the stack

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

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Start in crates/rascal/src/internal/as2_codegen/statement.rs, especially gen_for_loop and StatementKind::Return, then inspect how loop cleanup and return values are emitted. Reproduce the nested for ... in cases from the issue and check related code generation tests. Done means both break and return leave enumeration values cleaned up, so the outer loop prints x and y once each.

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

Descrizione

for ... in keeps the enumerated names on the stack, terminated by null, and only consumes all of them when the loop runs to the end. Leaving the loop early with break or return jumps out without removing the rest. Any enclosing for ... in then picks up those leftover values: depending on what's left, it iterates over stale names, or it stops early on the inner loop's null. In the reproducers below, the outer loop gets b from every inner loop, so it never ends.

break:

var o = {a: 1, b: 2};
for (var k in {x: 1, y: 2}) {
    trace(k);
    for (var j in o) {
        break;
    }
}

return:

function first(o) {
    for (var k in o) {
        return k;
    }
}
for (var k in {x: 1, y: 2}) {
    trace(k);
    first({a: 1, b: 2});
}

Expected for both: x and y, once each.

Actual, in Flash Player (Rascal 0.3.6, SWF 8), for both: x, then b repeated without end.

So the stack isn't reset when a function returns, which is why return is affected too.

gen_for_loop sets the break label to end_label, which is after the loop's cleanup.
https://github.com/Dinnerbone/Rascal/blob/3f1a80c85dcc657cf4a67b72754b9c7869a6d9c5/crates/rascal/src/internal/as2_codegen/statement.rs#L292-L302
StatementKind::Return emits Return directly. Before leaving a for ... in early, the remaining values need popping up to and including the null. Perhaps by emitting a small loop for that, similar to the loop header: Push null, Equals2, Not, If back to the start.

For a return from nested for ... in loops, each enclosing enumeration needs this, and the return value needs to be kept, for example in a register.

Real code hitting this: https://github.com/ruffle-rs/ruffle/blob/c7c2cc871c07d94f246d496e0b41479ba429085a/tests/tests/swfs/avm1/global_instance_decls/test.as#L46-L60

Lingua principale
Rust
Stelle
22
Fork
2
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 Dinnerbone/Rascal

Tutte le issue di Dinnerbone/Rascal

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.