diff --git a/CHANGELOG.md b/CHANGELOG.md index 3398e7a6b..03526bb0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,8 @@ All notable changes to this project will be documented in this file. - Unpin all context menu not clearing pins properly - AS3 - RegExp escaping - AS3 - Avoid Error Implicit coercion of a value of type XXX to an unrelated type YYY -- AS3 - XML - get descendants operator parenthesis +- AS3 - XML - get descendants operator parenthesis +- Switch decompilation in some corner cases ## [17.0.2] - 2022-11-22 ### Fixed diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java index 6e2308144..25fee77bc 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java @@ -1015,7 +1015,7 @@ public class AVM2Graph extends Graph { System.err.println("- " + p); }*/ - afterPart = getMostCommonPart(localData, partsToCalCommon, loops, throwStates); + afterPart = getMostCommonPart(localData, partsToCalCommon, loops, throwStates, new ArrayList<>() /*?*/); //System.err.println("result: " + afterPart); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/Graph.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/Graph.java index 7194b388a..5809038bb 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/Graph.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/Graph.java @@ -63,6 +63,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; +import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; @@ -343,7 +344,7 @@ public class Graph { return null; } - public GraphPart getMostCommonPart(BaseLocalData localData, List parts, List loops, List throwStates) throws InterruptedException { + public GraphPart getMostCommonPart(BaseLocalData localData, List parts, List loops, List throwStates, List stopPart) throws InterruptedException { if (parts.isEmpty()) { return null; } @@ -373,6 +374,7 @@ public class Graph { } int maxCommonLevel = -1; GraphPart maxCommonPart = null; + Set commonSet = new TreeSet<>(); for (GraphPart r : allReachable) { if (loopContinues.contains(r)) { continue; @@ -432,15 +434,61 @@ public class Graph { return r; } } - if (commonLevel > maxCommonLevel) { + /*if (commonLevel > maxCommonLevel) { maxCommonPart = r; maxCommonLevel = commonLevel; + commonSet.add(e) + }*/ + commonSet.add(new PartCommon(r, commonLevel)); + } + + Set partsLeadingToStopPart = new LinkedHashSet<>(); + if (stopPart != null) { + for (GraphPart p : parts) { + for (GraphPart sp : stopPart) { + if (sp == p || p.leadsTo(localData, this, code, sp, loops, throwStates, false)) { + partsLeadingToStopPart.add(p); + } + } } } - if (maxCommonLevel <= 1) { - return null; + + loopc: + for (PartCommon pc : commonSet) { + for (GraphPart p : partsLeadingToStopPart) { + if (p != pc.part && !p.leadsTo(localData, this, code, pc.part, loops, throwStates, false)) { + continue loopc; + } + } + if (pc.level <= 1) { + return null; + } + return pc.part; } - return maxCommonPart; + + return null; + } + + private class PartCommon implements Comparable { + + public GraphPart part; + public int level; + + public PartCommon(GraphPart part, int level) { + this.part = part; + this.level = level; + } + + @Override + public int compareTo(PartCommon o) { + return o.level - level; + } + + @Override + public String toString() { + return "" + part.toString() + " (level=" + level; + } + } public GraphPart getNextNoJump(GraphPart part, BaseLocalData localData) { @@ -1132,8 +1180,7 @@ public class Graph { } continue; - */ - + */ if ((!onTrue.isEmpty()) && (!onFalse.isEmpty())) { if ((onFalse.get(onFalse.size() - 1) instanceof ExitItem) || (onFalse.get(onFalse.size() - 1) instanceof BreakItem)) { if (onTrue.get(onTrue.size() - 1) instanceof ContinueItem) { @@ -1141,8 +1188,7 @@ public class Graph { } } } - - + /* if (xx) { A; @@ -1159,7 +1205,7 @@ public class Graph { } B; - */ + */ if ((!onTrue.isEmpty()) && (!onFalse.isEmpty())) { GraphTargetItem last = onTrue.get(onTrue.size() - 1); if ((last instanceof ExitItem) || (last instanceof ContinueItem) || (last instanceof BreakItem)) { @@ -1168,8 +1214,6 @@ public class Graph { } } - - //Prefer continue/return/throw/break in onTrue rather than onFalse if (!onFalse.isEmpty() && ((onFalse.get(onFalse.size() - 1) instanceof BreakItem) @@ -1580,7 +1624,7 @@ public class Graph { getLoopsWalk(localData, next, loops, throwStates, stopPart, false, visited, level); } } else if (part.nextParts.size() > 2 || partIsSwitch(part)) { - GraphPart next = getMostCommonPart(localData, part.nextParts, loops, throwStates); + GraphPart next = getMostCommonPart(localData, part.nextParts, loops, throwStates, stopPart); for (GraphPart p : part.nextParts) { List stopPart2 = stopPart == null ? new ArrayList<>() : new ArrayList<>(stopPart); @@ -3137,7 +3181,7 @@ public class Graph { } //gotoTargets - GraphPart breakPart = getMostCommonPart(localData, caseBodyParts, loops, throwStates); + GraphPart breakPart = getMostCommonPart(localData, caseBodyParts, loops, throwStates, stopPart); //removeEdgeToFromList(gotoTargets, breakPart); List> caseCommands = new ArrayList<>(); @@ -3235,7 +3279,7 @@ public class Graph { caseValuesMap.remove(caseValuesMap.size() - 1); valuesMapping.remove(valuesMapping.size() - 1); if (cnt2 == 1) { - caseCommands.remove(lastc); + caseCommands.remove(caseCommands.size() - 1); } } } diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicAirDecompileTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicAirDecompileTest.java index 4dc53ac18..ff0f2e1e1 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicAirDecompileTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicAirDecompileTest.java @@ -596,6 +596,28 @@ public class ActionScript3ClassicAirDecompileTest extends ActionScript3Decompile false); } + @Test + public void testForEachReturn2() { + decompileMethod("classic_air", "testForEachReturn2", "var item:* = undefined;\r\n" + + "var obj:* = null;\r\n" + + "var x:int = 5;\r\n" + + "if(x != null)\r\n" + + "{\r\n" + + "obj = {};\r\n" + + "item = {};\r\n" + + "switch(item[\"key\"])\r\n" + + "{\r\n" + + "case 1:\r\n" + + "case 2:\r\n" + + "case 3:\r\n" + + "case 4:\r\n" + + "return item;\r\n" + + "}\r\n" + + "}\r\n" + + "return null;\r\n", + false); + } + @Test public void testForGoto() { decompileMethod("classic_air", "testForGoto", "var i:* = 0;\r\n" @@ -1472,13 +1494,13 @@ public class ActionScript3ClassicAirDecompileTest extends ActionScript3Decompile + "switch(int(code) - 2)\r\n" + "{\r\n" + "case 0:\r\n" + + "case 1:\r\n" + "if(a)\r\n" + "{\r\n" + "trace(\"A\");\r\n" + "break;\r\n" + "}\r\n" + "break;\r\n" - + "case 1:\r\n" + "}\r\n" + "trace(\"B\");\r\n", false); diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicDecompileTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicDecompileTest.java index 8da3b8bd6..2a95225d6 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicDecompileTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3ClassicDecompileTest.java @@ -592,6 +592,28 @@ public class ActionScript3ClassicDecompileTest extends ActionScript3DecompileTes false); } + @Test + public void testForEachReturn2() { + decompileMethod("classic", "testForEachReturn2", "var item:* = undefined;\r\n" + + "var obj:* = null;\r\n" + + "var x:* = 5;\r\n" + + "if(x != null)\r\n" + + "{\r\n" + + "obj = {};\r\n" + + "item = {};\r\n" + + "switch(item[\"key\"])\r\n" + + "{\r\n" + + "case 1:\r\n" + + "case 2:\r\n" + + "case 3:\r\n" + + "case 4:\r\n" + + "return item;\r\n" + + "}\r\n" + + "}\r\n" + + "return null;\r\n", + false); + } + @Test public void testForGoto() { decompileMethod("classic", "testForGoto", "var c:int = 0;\r\n" diff --git a/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.air.swf b/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.air.swf index 44cfc950c..665881e69 100644 Binary files a/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.air.swf and b/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.air.swf differ diff --git a/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.flex.swf b/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.flex.swf index a2379aee2..56f4bfb48 100644 Binary files a/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.flex.swf and b/libsrc/ffdec_lib/testdata/as3_new/bin/as3_new.flex.swf differ diff --git a/libsrc/ffdec_lib/testdata/as3_new/src/Main.as b/libsrc/ffdec_lib/testdata/as3_new/src/Main.as index e505c17c0..acae7b144 100644 --- a/libsrc/ffdec_lib/testdata/as3_new/src/Main.as +++ b/libsrc/ffdec_lib/testdata/as3_new/src/Main.as @@ -46,6 +46,7 @@ package TestForEachObjectArray; TestForEachObjectAttribute; TestForEachReturn; + TestForEachReturn2; TestForGoto; TestForIn; TestForInIf; diff --git a/libsrc/ffdec_lib/testdata/as3_new/src/tests/TestForEachReturn2.as b/libsrc/ffdec_lib/testdata/as3_new/src/tests/TestForEachReturn2.as new file mode 100644 index 000000000..8a329bb26 --- /dev/null +++ b/libsrc/ffdec_lib/testdata/as3_new/src/tests/TestForEachReturn2.as @@ -0,0 +1,29 @@ +package tests +{ + + public class TestForEachReturn2 + { + public function run():* + { + var obj:* = null; + var x = 5; + if (x != null) + { + obj = {}; + var item = {}; + //for each (var item in obj) + //{ + switch (item["key"]) + { + case 1: + case 2: + case 3: + case 4: + return item; + }; + //}; + }; + return null; + } + } +}