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

未关闭
#93 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Rust
星标
22
派生
2
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Dinnerbone/Rascal 的其他 Issue

查看 Dinnerbone/Rascal 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。