• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 271

29 Sep 2025 12:12PM UTC coverage: 64.649% (+2.4%) from 62.222%
271

Pull #1096

circleci

Frotty
Merge branch 'perf-improvements' of https://github.com/wurstscript/WurstScript into perf-improvements
Pull Request #1096: Perf improvements

18202 of 28155 relevant lines covered (64.65%)

0.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrClosureCapturedVariables.java
1
package de.peeeq.wurstscript.attributes;
2

3
import com.google.common.collect.ImmutableMultimap;
4
import com.google.common.collect.ImmutableMultimap.Builder;
5
import de.peeeq.wurstscript.ast.*;
6
import de.peeeq.wurstscript.attributes.names.NameLink;
7
import de.peeeq.wurstscript.types.WurstTypeArray;
8

9
import java.util.Map.Entry;
10

11
public class AttrClosureCapturedVariables {
×
12

13
    public static ImmutableMultimap<Element, VarDef> calculate(ExprClosure e) {
14
        ImmutableMultimap.Builder<Element, VarDef> result = ImmutableMultimap.builder();
1✔
15
        // just use a visitor and select every local variable which is not defined in the
16
        // closure itself
17
        collect(result, e, e.getImplementation());
1✔
18

19
        return result.build();
1✔
20
    }
21

22
    private static void collect(Builder<Element, VarDef> result, ExprClosure closure, Element e) {
23
        if (e instanceof ExprClosure) {
1✔
24
            ExprClosure innerClosure = (ExprClosure) e;
1✔
25
            for (Entry<Element, VarDef> entry : innerClosure.attrCapturedVariables().entries()) {
1✔
26
                VarDef v = entry.getValue();
1✔
27
                if (v.attrNearestExprClosure() != closure) {
1✔
28
                    result.put(entry.getKey(), v);
1✔
29
                }
30
            }
1✔
31
            return;
1✔
32
        }
33
        if (e instanceof NameRef) {
1✔
34
            NameRef nr = (NameRef) e;
1✔
35
            NameLink def = nr.attrNameLink();
1✔
36

37
            if (def != null && isLocalVariable(def.getDef())) {
1✔
38
                VarDef v = (VarDef) def.getDef();
1✔
39
                if (v.attrNearestExprClosure() != closure) {
1✔
40
                    result.put(nr, v);
1✔
41
                    if (v.attrTyp() instanceof WurstTypeArray) {
1✔
42
                        nr.addError("Closures cannot capture local array variables.");
×
43
                    }
44
                }
45
            }
46
            if (nr.attrImplicitParameter() instanceof ExprThis) {
1✔
47
                result.put(nr, dummyThisVar(closure));
1✔
48
            }
49
        } else if (e instanceof FunctionCall) {
1✔
50
            FunctionCall fc = (FunctionCall) e;
1✔
51
            if (fc.attrImplicitParameter() instanceof ExprThis) {
1✔
52
                result.put(e, dummyThisVar(closure));
1✔
53
            }
54
        } else if (e instanceof ExprThis) {
1✔
55
            result.put(e, dummyThisVar(closure));
1✔
56
        }
57
        // visit children
58
        for (int i = 0; i < e.size(); i++) {
1✔
59
            collect(result, closure, e.get(i));
1✔
60
        }
61
    }
1✔
62

63
    private static boolean isLocalVariable(NameDef def) {
64
        return def instanceof LocalVarDef
1✔
65
                || def instanceof WParameter && !(def.getParent().getParent() instanceof TupleDef);
1✔
66

67
    }
68

69
    private static LocalVarDef dummyThisVar(ExprClosure closure) {
70
        return Ast.LocalVarDef(closure.getSource(), Ast.Modifiers(), Ast.NoTypeExpr(), Ast.Identifier(closure.getSource(), "this"), Ast.NoExpr());
1✔
71
    }
72

73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc