Fixed Avoid Error Implicit coercion of a value of type XXX to an unrelated type YYY

This commit is contained in:
Jindra Petřík
2022-11-27 16:28:40 +01:00
parent a99df959bc
commit c26030620f
10 changed files with 77 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
- [#1892] AS3 - Package internal custom namespaces
- Unpin all context menu not clearing pins properly
- AS3 - RegExp escaping
- Avoid Error Implicit coercion of a value of type XXX to an unrelated type YYY
## [17.0.2] - 2022-11-22
### Fixed

View File

@@ -128,7 +128,7 @@ public class FullMultinameAVM2Item extends AVM2Item {
if (name instanceof IntegerValueAVM2Item) {
name.toString(writer, localData);
} else {
name.toStringString(writer, localData);
name.toString(writer, localData);
}
writer.append("]");
} else {

View File

@@ -54,7 +54,7 @@ public class NameValuePair extends AVM2Item {
if ((name instanceof ConvertAVM2Item) && ((ConvertAVM2Item)name).type.equals(TypeItem.STRING)) {
name = name.value;
}
name.toStringString(writer, localData);
name.toString(writer, localData);
if (needsParents) {
writer.append(")");
}

View File

@@ -44,6 +44,7 @@ import com.jpexs.decompiler.graph.model.TrueItem;
import com.jpexs.helpers.Reference;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -332,13 +333,26 @@ public abstract class GraphTargetItem implements Serializable, Cloneable {
public GraphTextWriter appendTry(GraphTextWriter writer, LocalData localData, String implicitCoerce) throws InterruptedException {
GraphTargetItem t = this;
/*if (!implicitCoerce.isEmpty()) { //if implicit coerce equals explicit
if (t instanceof ConvertAVM2Item) {
if (!implicitCoerce.isEmpty()) { //if implicit oerce equals explicit
/*if (t instanceof ConvertAVM2Item) {
if (implicitCoerce.equals((((ConvertAVM2Item) t).type.toString()))) {
t = t.value;
}
}*/
if (localData.abc != null) { //its AS3
List<String> numberTypes = Arrays.asList("int", "uint", "Number");
String returnTypeStr = t.returnType().toString();
//To avoid Error: Implicit coercion of a value of type XXX to an unrelated type YYY
if (!t.returnType().equals(TypeItem.UNBOUNDED) &&
!implicitCoerce.equals(returnTypeStr) &&
!(numberTypes.contains(implicitCoerce) && numberTypes.contains(returnTypeStr)) &&
!(implicitCoerce.equals("Boolean"))
) {
t = new ConvertAVM2Item(null, null, t, new TypeItem(implicitCoerce));
}
}
}*/
}
if (!implicitCoerce.isEmpty() && Configuration.simplifyExpressions.get()) {
t = t.simplify(implicitCoerce);
}

View File

@@ -1031,6 +1031,23 @@ public class ActionScript3ClassicAirDecompileTest extends ActionScript3Decompile
false);
}
@Test
public void testImplicitCoerce() {
decompileMethod("classic_air", "testImplicitCoerce", "var j:int = 2;\r\n"
+ "var i:int = 5;\r\n"
+ "var r:* = Math.random();\r\n"
+ "if(j & Number(r == 1) && 5)\r\n"
+ "{\r\n"
+ "trace(\"OK\");\r\n"
+ "}\r\n"
+ "var s:String = \"hello: \" + r;\r\n"
+ "if(s)\r\n"
+ "{\r\n"
+ "trace(\"F\");\r\n"
+ "}\r\n",
false);
}
@Test
public void testImportedVar() {
decompileMethod("classic_air", "testImportedVar", "trace(myvar);\r\n"

View File

@@ -1026,6 +1026,23 @@ public class ActionScript3ClassicDecompileTest extends ActionScript3DecompileTes
false);
}
@Test
public void testImplicitCoerce() {
decompileMethod("classic", "testImplicitCoerce", "var j:int = 2;\r\n"
+ "var i:int = 5;\r\n"
+ "var r:* = Math.random();\r\n"
+ "if(Boolean(j & Number(r == 1)) && Boolean(5))\r\n"
+ "{\r\n"
+ "trace(\"OK\");\r\n"
+ "}\r\n"
+ "var s:String = \"hello: \" + r;\r\n"
+ "if(Boolean(s))\r\n"
+ "{\r\n"
+ "trace(\"F\");\r\n"
+ "}\r\n",
false);
}
@Test
public void testImportedVar() {
decompileMethod("classic", "testImportedVar", "trace(myvar);\r\n"

View File

@@ -66,6 +66,7 @@ package
TestIfInIf;
TestIfTry;
TestIgnoreAndOr;
TestImplicitCoerce;
TestImportedVar;
TestInc2;
TestIncDec;

View File

@@ -0,0 +1,22 @@
package tests
{
public class TestImplicitCoerce
{
public function run():void
{
var j:int = 2;
var i:int = 5;
var r:* = Math.random();
if (j & Number(r == 1) && 5) {
trace("OK");
}
var s:String = "hello: " + r;
if (s){
trace("F");
}
}
}
}