Better Comma operator detection WIP

This commit is contained in:
Jindra Petřík
2025-09-01 17:01:25 +02:00
parent b3a693e4e3
commit 9a9811f170
36 changed files with 166 additions and 135 deletions
@@ -3369,8 +3369,7 @@ public class Graph {
if (code.size() <= part.start) {
if (!(!ret.isEmpty() && ret.get(ret.size() - 1) instanceof ExitItem)) {
stack.moveToOutput(ret, true);
ret.add(new ScriptEndItem(dialect));
stack.addToOutput(new ScriptEndItem(dialect));
}
return ret;
}
@@ -3534,8 +3533,7 @@ public class Graph {
} while (exHappened);
if ((part.end >= code.size() - 1) && getNextParts(localData, part).isEmpty()) {
if (!(!output.isEmpty() && output.get(output.size() - 1) instanceof ExitItem)) {
stack.moveToOutput(output, true);
output.add(new ScriptEndItem(dialect));
stack.addToOutput(new ScriptEndItem(dialect));
}
}
}
@@ -4541,50 +4539,7 @@ public class Graph {
* @param stack Stack
*/
public void makeAllCommands(List<GraphTargetItem> commands, TranslateStack stack) {
int clen = commands.size();
boolean isExit = false;
if (clen > 0) {
if (commands.get(clen - 1) instanceof ScriptEndItem) {
clen--;
isExit = true;
}
}
if (clen > 0) {
if (commands.get(clen - 1) instanceof ExitItem) {
isExit = true;
clen--;
}
}
if (clen > 0) {
if (commands.get(clen - 1) instanceof BreakItem) {
clen--;
}
}
if (clen > 0) {
if (commands.get(clen - 1) instanceof ContinueItem) {
clen--;
}
}
for (int i = stack.size() - 1; i >= 0; i--) {
GraphTargetItem p = stack.get(i);
if (p instanceof BranchStackResistant) {
continue;
}
stack.remove(i);
if (!(p instanceof PopItem)) {
if (isExit) {
//ASC2 leaves some function calls unpopped on stack before returning from a method
commands.add(clen, p);
} else {
int pos = 0;
if (p.outputPos < commands.size()) {
commands.add(p.outputPos, new PushItem(p));
} else {
commands.add(clen + pos, new PushItem(p));
}
}
}
}
stack.finishBlock(commands);
}
/**
@@ -19,9 +19,15 @@ package com.jpexs.decompiler.graph;
import com.jpexs.decompiler.flash.abc.avm2.model.NewActivationAVM2Item;
import com.jpexs.decompiler.flash.abc.avm2.model.clauses.ExceptionAVM2Item;
import com.jpexs.decompiler.graph.model.BranchStackResistant;
import com.jpexs.decompiler.graph.model.BreakItem;
import com.jpexs.decompiler.graph.model.CommaExpressionItem;
import com.jpexs.decompiler.graph.model.ContinueItem;
import com.jpexs.decompiler.graph.model.ExitItem;
import com.jpexs.decompiler.graph.model.PopItem;
import com.jpexs.decompiler.graph.model.PushItem;
import com.jpexs.decompiler.graph.model.ScriptEndItem;
import com.jpexs.decompiler.graph.model.SwapItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -51,7 +57,23 @@ public class TranslateStack extends Stack<GraphTargetItem> {
private int prevOutputSize = 0;
private Map<String, GraphTargetItem> marks = new HashMap<>();
public List<GraphTargetItem> outputQueue = new ArrayList<>();
@Override
public synchronized Object clone() {
TranslateStack st = (TranslateStack) super.clone();
st.outputQueue = new ArrayList<>(outputQueue);
return st;
}
@Override
public void clear() {
super.clear();
outputQueue.clear();
}
public void setConnectedOutput(int prevOutputSize, List<GraphTargetItem> connectedOutput) {
this.prevOutputSize = prevOutputSize;
this.connectedOutput = connectedOutput;
@@ -59,6 +81,11 @@ public class TranslateStack extends Stack<GraphTargetItem> {
@Override
public GraphTargetItem push(GraphTargetItem item) {
if (!outputQueue.isEmpty()) {
outputQueue.add(item);
item = new CommaExpressionItem(item.dialect, null, item.lineStartItem, outputQueue);
outputQueue = new ArrayList<>();
}
if (connectedOutput != null && item != null) {
item.outputPos = prevOutputSize + connectedOutput.size();
}
@@ -187,6 +214,14 @@ public class TranslateStack extends Stack<GraphTargetItem> {
*/
@Override
public synchronized GraphTargetItem pop() {
if (!outputQueue.isEmpty()) {
List<GraphTargetItem> oldQueue = outputQueue;
outputQueue = new ArrayList<>();
finishBlock(connectedOutput);
connectedOutput.addAll(oldQueue);
}
if (path != null) {
if (this.isEmpty()) {
/*if (connectedOutput != null && !connectedOutput.isEmpty() && connectedOutput.get(connectedOutput.size() - 1) instanceof PushItem) {
@@ -220,11 +255,22 @@ public class TranslateStack extends Stack<GraphTargetItem> {
}
}
public void moveToOutput(List<GraphTargetItem> output, boolean beforeExit) {
if (true) {
//return;
public void addToOutput(GraphTargetItem item) {
if (isEmpty()
|| peek() instanceof ExceptionAVM2Item
|| peek() instanceof NewActivationAVM2Item
) {
connectedOutput.add(item);
return;
}
int pos = output.size();
outputQueue.add(item);
if (item instanceof ExitItem) {
finishBlock(connectedOutput);
}
}
public void finishBlock(List<GraphTargetItem> output) {
/*int pos = output.size();
for (int i = size() - 1; i >= 0; i--) {
GraphTargetItem item = get(i);
@@ -238,7 +284,59 @@ public class TranslateStack extends Stack<GraphTargetItem> {
break;
}
remove(i);
if (item instanceof PopItem) {
continue;
}
output.add(pos, beforeExit ? item : new PushItem(item));
}*/
output.addAll(outputQueue);
outputQueue.clear();
int clen = output.size();
boolean isExit = false;
if (clen > 0) {
if (output.get(clen - 1) instanceof ScriptEndItem) {
clen--;
isExit = true;
}
}
if (clen > 0) {
if (output.get(clen - 1) instanceof ExitItem) {
isExit = true;
clen--;
}
}
if (clen > 0) {
if (output.get(clen - 1) instanceof BreakItem) {
clen--;
}
}
if (clen > 0) {
if (output.get(clen - 1) instanceof ContinueItem) {
clen--;
}
}
for (int i = size() - 1; i >= 0; i--) {
GraphTargetItem p = get(i);
if (p instanceof BranchStackResistant) {
continue;
}
remove(i);
if (!(p instanceof PopItem)) {
if (isExit) {
//ASC2 leaves some function calls unpopped on stack before returning from a method
output.add(clen, p);
} else {
/*int pos = 0;
if (p.outputPos < output.size()) {
output.add(p.outputPos, new PushItem(p));
} else {
output.add(clen + pos, new PushItem(p));
}*/
output.add(clen, new PushItem(p));
}
}
}
}