Fixed: AS3 direct editation - compilation of top level classes

This commit is contained in:
Jindra Petřík
2021-02-26 21:05:17 +01:00
parent 5afdba0600
commit 0ae78aacf3
9 changed files with 23 additions and 22 deletions

View File

@@ -68,8 +68,6 @@ public class SourceGeneratorLocalData implements Serializable {
public List<GraphTargetItem> scopeStack = new ArrayList<>();
public boolean documentClass;
public ScriptInfo currentScript;
public boolean subMethod = false;

View File

@@ -1372,6 +1372,9 @@ public class ABC {
public int findClassByName(String nameWithSuffix) {
for (int c = 0; c < instance_info.size(); c++) {
if (instance_info.get(c).deleted) {
continue;
}
DottedChain s = constants.getMultiname(instance_info.get(c).name_index).getNameWithNamespace(constants, true);
if (nameWithSuffix.equals(s.toRawString())) {
return c;

View File

@@ -1607,7 +1607,6 @@ public class AVM2SourceGenerator implements SourceGenerator {
newlocalData.callStack.addAll(localData.callStack);
newlocalData.traitUsages = localData.traitUsages;
newlocalData.currentScript = localData.currentScript;
newlocalData.documentClass = localData.documentClass;
newlocalData.privateNs = localData.privateNs;
newlocalData.protectedNs = localData.protectedNs;
newlocalData.isStatic = isStatic;
@@ -2463,8 +2462,10 @@ public class AVM2SourceGenerator implements SourceGenerator {
for (Trait t : si.traits.traits) {
if (t instanceof TraitClass) {
TraitClass tc = (TraitClass) t;
DottedChain className = tc.getName(abc).getNameWithNamespace(abc.constants, true);
List<Integer> parents = new ArrayList<>();
if (localData.documentClass) {
if (className.size() == 1) {
mbCode.add(ins(AVM2Instructions.GetScopeObject, 0));
traitScope++;
} else {
@@ -2682,19 +2683,21 @@ public class AVM2SourceGenerator implements SourceGenerator {
public List<GraphSourceItem> generate(SourceGeneratorLocalData localData, TypeItem item) throws CompilationException {
String currentFullClassName = localData.getFullClass();
if (localData.documentClass && item.toString().equals(currentFullClassName)) {
int slotId = 0;
int globalSlotId = 0;
if (item.fullTypeName.size() == 1 && item.toString().equals(currentFullClassName)) {
int c = abcIndex.getSelectedAbc().findClassByName(currentFullClassName);
for (Trait t : localData.currentScript.traits.traits) {
if (t instanceof TraitClass) {
TraitClass tc = (TraitClass) t;
if (tc.class_info == c) {
slotId = tc.slot_id;
globalSlotId = tc.slot_id;
break;
}
}
}
return GraphTargetItem.toSourceMerge(localData, this, ins(AVM2Instructions.GetGlobalScope), ins(AVM2Instructions.GetSlot, slotId));
}
if (globalSlotId > 0) {
return GraphTargetItem.toSourceMerge(localData, this, ins(AVM2Instructions.GetGlobalScope), ins(AVM2Instructions.GetSlot, globalSlotId));
} else {
return GraphTargetItem.toSourceMerge(localData, this, ins(AVM2Instructions.GetLex, resolveType(localData, item, abcIndex)));
}

View File

@@ -2534,18 +2534,17 @@ public class ActionScript3Parser {
return ret;
}
public void addScriptFromTree(List<List<NamespaceItem>> allOpenedNamespaces, List<GraphTargetItem> items, boolean documentClass, int classPos) throws AVM2ParseException, CompilationException {
public void addScriptFromTree(List<List<NamespaceItem>> allOpenedNamespaces, List<GraphTargetItem> items, int classPos) throws AVM2ParseException, CompilationException {
AVM2SourceGenerator gen = new AVM2SourceGenerator(abcIndex);
SourceGeneratorLocalData localData = new SourceGeneratorLocalData(
new HashMap<>(), 0, Boolean.FALSE, 0);
localData.documentClass = documentClass;
abcIndex.getSelectedAbc().script_info.add(gen.generateScriptInfo(allOpenedNamespaces, localData, items, classPos));
}
public void addScript(String s, boolean documentClass, String fileName, int classPos, int scriptIndex) throws AVM2ParseException, IOException, CompilationException {
public void addScript(String s, String fileName, int classPos, int scriptIndex) throws AVM2ParseException, IOException, CompilationException {
List<List<NamespaceItem>> allOpenedNamespaces = new ArrayList<>();
List<GraphTargetItem> traits = scriptTraitsFromString(allOpenedNamespaces, s, fileName, scriptIndex);
addScriptFromTree(allOpenedNamespaces, traits, documentClass, classPos);
addScriptFromTree(allOpenedNamespaces, traits, classPos);
}
public ActionScript3Parser(ABC abc, List<ABC> otherAbcs) throws IOException, InterruptedException {
@@ -2571,14 +2570,14 @@ public class ActionScript3Parser {
}
}
public static void compile(String src, ABC abc, List<ABC> otherABCs, boolean documentClass, String fileName, int classPos, int scriptIndex) throws AVM2ParseException, IOException, InterruptedException, CompilationException {
public static void compile(String src, ABC abc, List<ABC> otherABCs, String fileName, int classPos, int scriptIndex) throws AVM2ParseException, IOException, InterruptedException, CompilationException {
//List<ABC> parABCs = new ArrayList<>();
initPlayer();
ActionScript3Parser parser = new ActionScript3Parser(abc, otherABCs);
boolean success = false;
ABC originalAbc = ((ABCContainerTag) ((Tag) abc.parentTag).cloneTag()).getABC();
try {
parser.addScript(src, documentClass, fileName, classPos, scriptIndex);
parser.addScript(src, fileName, classPos, scriptIndex);
success = true;
} finally {
if (!success) {
@@ -2601,7 +2600,7 @@ public class ActionScript3Parser {
initPlayer();
ABC abc = new ABC(null);
ActionScript3Parser parser = new ActionScript3Parser(abc, new ArrayList<>());
parser.addScript(new String(Helper.readFile(src), Utf8Helper.charset), true, src, classPos, scriptIndex);
parser.addScript(new String(Helper.readFile(src), Utf8Helper.charset), src, classPos, scriptIndex);
try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(new File(dst)))) {
abc.saveToStream(fos);

View File

@@ -258,7 +258,7 @@ public abstract class Trait implements Cloneable, Serializable {
if (imp.size() > 1) { //No imports from root package
writer.appendNoHilight("import ");
if (!imp.isTopLevel()) {
if (imp.size() > 1) {
writer.appendNoHilight(imp.getWithoutLast().toPrintableString(true));
writer.appendNoHilight(".");
}

View File

@@ -41,9 +41,6 @@ public class FFDecAs3ScriptReplacer implements As3ScriptReplacerInterface {
int oldIndex = pack.scriptIndex;
int newIndex = abc.script_info.size();
try {
String documentClass = swf.getDocumentClass();
boolean isDocumentClass = documentClass != null && documentClass.equals(pack.getClassPath().toString());
ScriptInfo si = abc.script_info.get(oldIndex);
if (pack.isSimple) {
si.delete(abc, true);
@@ -66,7 +63,7 @@ public class FFDecAs3ScriptReplacer implements As3ScriptReplacerInterface {
otherAbcs.remove(abc);
abc.script_info.get(oldIndex).delete(abc, true);
ActionScript3Parser.compile(text, abc, otherAbcs, isDocumentClass, scriptName, newClassIndex, oldIndex);
ActionScript3Parser.compile(text, abc, otherAbcs, scriptName, newClassIndex, oldIndex);
if (pack.isSimple) {
// Move newly added script to its position
abc.script_info.set(oldIndex, abc.script_info.get(newIndex));