From aa255df3f409d1a2de0515a2d21ee801245d121b Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 15 Sep 2020 15:08:45 +0200 Subject: [PATCH 1/8] Fix concat --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 13 ++++- .../TestClasses/Contract_Concat.cs | 22 +++++++++ .../UnitTest_Concat.cs | 47 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index 7c2d8fc77..b0cbb336e 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -645,7 +645,18 @@ private int ConvertCall(OpCode src, NeoMethod to) else if (src.tokenMethod.Contains("::Concat(")) { //"System.String System.String::Concat(System.String,System.String)" - Convert1by1(VM.OpCode.CAT, src, to); + + if (src.tokenUnknown is MethodReference mref) + { + for (int x = 0; x < mref.Parameters.Count - 1; x++) + { + Convert1by1(VM.OpCode.CAT, src, to); + } + } + else + { + Convert1by1(VM.OpCode.CAT, src, to); + } Insert1(VM.OpCode.CONVERT, "", to, new byte[] { (byte)VM.Types.StackItemType.ByteString }); return 0; } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs new file mode 100644 index 000000000..dd5b25ee2 --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs @@ -0,0 +1,22 @@ +using Neo.SmartContract.Framework; + +namespace Neo.Compiler.MSIL.UnitTests.TestClasses +{ + public class Contract_Concat : SmartContract.Framework.SmartContract + { + public static string TestStringAdd1(string a) + { + return a + "hello"; + } + + public static string TestStringAdd2(string a, string b) + { + return a + b + "hello"; + } + + public static string TestStringAdd3(string a, string b, string c) + { + return a + b + c + "hello"; + } + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs new file mode 100644 index 000000000..82a33e550 --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs @@ -0,0 +1,47 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.VM; +using Neo.VM.Types; + +namespace Neo.Compiler.MSIL.UnitTests +{ + [TestClass] + public class UnitTest_Concat + { + private TestEngine _engine; + + [TestInitialize] + public void Init() + { + _engine = new TestEngine(); + _engine.AddEntryScript("./TestClasses/Contract_Concat.cs"); + } + + [TestMethod] + public void TestStringAdd1() + { + _engine.Reset(); + var result = _engine.GetMethod("testStringAdd1").Run("a").ConvertTo(StackItemType.ByteString); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual("ahello", result.GetString()); + } + + [TestMethod] + public void TestStringAdd2() + { + _engine.Reset(); + var result = _engine.GetMethod("testStringAdd2").Run("a", "b").ConvertTo(StackItemType.ByteString); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual("abhello", result.GetString()); + } + + [TestMethod] + public void TestStringAdd3() + { + _engine.Reset(); + var result = _engine.GetMethod("testStringAdd3").Run("a", "b", "c").ConvertTo(StackItemType.ByteString); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual("abchello", result.GetString()); + } + } +} From d7fe11bfbe2a6c2692cf6588171a229229e872b9 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 15 Sep 2020 15:10:41 +0200 Subject: [PATCH 2/8] Add comment --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index b0cbb336e..e486037bc 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -1,3 +1,4 @@ +using Mono.Cecil; using Neo.SmartContract; using System; using System.Collections.Generic; @@ -645,9 +646,9 @@ private int ConvertCall(OpCode src, NeoMethod to) else if (src.tokenMethod.Contains("::Concat(")) { //"System.String System.String::Concat(System.String,System.String)" - if (src.tokenUnknown is MethodReference mref) { + //Concat can have more than two arguments for (int x = 0; x < mref.Parameters.Count - 1; x++) { Convert1by1(VM.OpCode.CAT, src, to); From 6f51a0a3644bf03090c1cfefb4e9224004543149 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 15 Sep 2020 15:13:22 +0200 Subject: [PATCH 3/8] dotnet format --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index e486037bc..e95cb4b29 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -1,4 +1,4 @@ -using Mono.Cecil; +using Mono.Cecil; using Neo.SmartContract; using System; using System.Collections.Generic; From bd55e28e00544b7c000d11aec3fd1c98f9c90f96 Mon Sep 17 00:00:00 2001 From: lights Date: Wed, 16 Sep 2020 19:19:35 +0800 Subject: [PATCH 4/8] fix concat parse --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 47 +++++++++++++++---- .../TestClasses/Contract_Concat.cs | 5 ++ .../UnitTest_Concat.cs | 15 +++++- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index e95cb4b29..013437475 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -645,19 +645,50 @@ private int ConvertCall(OpCode src, NeoMethod to) } else if (src.tokenMethod.Contains("::Concat(")) { - //"System.String System.String::Concat(System.String,System.String)" - if (src.tokenUnknown is MethodReference mref) + //String::Concat has many overload,we can only support part of them. + if (src.tokenMethod == "System.String System.String::Concat(System.String,System.String)") { - //Concat can have more than two arguments - for (int x = 0; x < mref.Parameters.Count - 1; x++) - { - Convert1by1(VM.OpCode.CAT, src, to); - } + Convert1by1(VM.OpCode.CAT, src, to); } - else + else if (src.tokenMethod == "System.String System.String::Concat(System.String,System.String,System.String)") + { + Convert1by1(VM.OpCode.CAT, src, to); + Insert1(VM.OpCode.CAT, "", to); + } + else if (src.tokenMethod == "System.String System.String::Concat(System.String,System.String,System.String,System.String)") { Convert1by1(VM.OpCode.CAT, src, to); + Insert1(VM.OpCode.CAT, "", to); + Insert1(VM.OpCode.CAT, "", to); } + else if (src.tokenMethod == "System.String System.String::Concat(System.String[])") + { + //unpack array + Convert1by1(VM.OpCode.UNPACK, src, to); + + //loops + var loopaddr = this.addr; + Insert1(VM.OpCode.DUP, "", to); //+1 + Insert1(VM.OpCode.PUSH1, "", to);//+1 + Insert1(VM.OpCode.JMPLE_L, "", to, BitConverter.GetBytes((int)5 + 9));//+5 to end + Insert1(VM.OpCode.DEC, "", to);//+1 + Insert1(VM.OpCode.REVERSE3, "", to);//+1 + Insert1(VM.OpCode.CAT, "", to);//+1 + Insert1(VM.OpCode.SWAP, "", to);//+1 + var addrreset = loopaddr - this.addr; + + + Insert1(VM.OpCode.JMP_L, "", to, BitConverter.GetBytes((int)addrreset));//+5 to loops + + //:endpos + + Insert1(VM.OpCode.DROP, "", to); + } + else + { + throw new Exception("not support this overload:" + src.tokenMethod); + } + Insert1(VM.OpCode.CONVERT, "", to, new byte[] { (byte)VM.Types.StackItemType.ByteString }); return 0; } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs index dd5b25ee2..7dbed3f40 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs @@ -18,5 +18,10 @@ public static string TestStringAdd3(string a, string b, string c) { return a + b + c + "hello"; } + + public static string TestStringAdd4(string a, string b, string c,string d) + { + return a + b + c + d + "hello"; + } } } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs index 82a33e550..b878dc94e 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs @@ -14,7 +14,7 @@ public class UnitTest_Concat public void Init() { _engine = new TestEngine(); - _engine.AddEntryScript("./TestClasses/Contract_Concat.cs"); + _engine.AddEntryScript("./TestClasses/Contract_Concat.cs",false,false); } [TestMethod] @@ -43,5 +43,18 @@ public void TestStringAdd3() Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual("abchello", result.GetString()); } + [TestMethod] + public void TestStringAdd4() + { + _engine.Reset(); + var method = _engine.GetMethod("testStringAdd4"); + _engine.ScriptEntry.DumpNEF(); + + + var result = method.Run("a", "b", "c","d").ConvertTo(StackItemType.ByteString); + Assert.AreEqual(VMState.HALT, _engine.State); + var str = result.GetString(); + Assert.AreEqual("abcdhello", str); + } } } From ef0e8b043396aac94e90a03b7056bc60cae70835 Mon Sep 17 00:00:00 2001 From: lights Date: Wed, 16 Sep 2020 19:26:30 +0800 Subject: [PATCH 5/8] fix format --- tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs index b878dc94e..e109f508b 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs @@ -14,7 +14,7 @@ public class UnitTest_Concat public void Init() { _engine = new TestEngine(); - _engine.AddEntryScript("./TestClasses/Contract_Concat.cs",false,false); + _engine.AddEntryScript("./TestClasses/Contract_Concat.cs", false, false); } [TestMethod] @@ -51,7 +51,7 @@ public void TestStringAdd4() _engine.ScriptEntry.DumpNEF(); - var result = method.Run("a", "b", "c","d").ConvertTo(StackItemType.ByteString); + var result = method.Run("a", "b", "c", "d").ConvertTo(StackItemType.ByteString); Assert.AreEqual(VMState.HALT, _engine.State); var str = result.GetString(); Assert.AreEqual("abcdhello", str); From 602159c7dc821cd699bd27389c9f345add6f08b8 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 16 Sep 2020 15:31:28 +0200 Subject: [PATCH 6/8] format --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 3 +-- .../TestClasses/Contract_Concat.cs | 2 +- tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs | 9 ++------- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index 013437475..26d8326b3 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -645,7 +645,7 @@ private int ConvertCall(OpCode src, NeoMethod to) } else if (src.tokenMethod.Contains("::Concat(")) { - //String::Concat has many overload,we can only support part of them. + //String::Concat has many overload, we can only support part of them. if (src.tokenMethod == "System.String System.String::Concat(System.String,System.String)") { Convert1by1(VM.OpCode.CAT, src, to); @@ -677,7 +677,6 @@ private int ConvertCall(OpCode src, NeoMethod to) Insert1(VM.OpCode.SWAP, "", to);//+1 var addrreset = loopaddr - this.addr; - Insert1(VM.OpCode.JMP_L, "", to, BitConverter.GetBytes((int)addrreset));//+5 to loops //:endpos diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs index 7dbed3f40..3263fc2be 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs @@ -19,7 +19,7 @@ public static string TestStringAdd3(string a, string b, string c) return a + b + c + "hello"; } - public static string TestStringAdd4(string a, string b, string c,string d) + public static string TestStringAdd4(string a, string b, string c, string d) { return a + b + c + d + "hello"; } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs index e109f508b..34ab69629 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs @@ -47,14 +47,9 @@ public void TestStringAdd3() public void TestStringAdd4() { _engine.Reset(); - var method = _engine.GetMethod("testStringAdd4"); - _engine.ScriptEntry.DumpNEF(); - - - var result = method.Run("a", "b", "c", "d").ConvertTo(StackItemType.ByteString); + var result = _engine.GetMethod("testStringAdd4").Run("a", "b", "c", "d").ConvertTo(StackItemType.ByteString); Assert.AreEqual(VMState.HALT, _engine.State); - var str = result.GetString(); - Assert.AreEqual("abcdhello", str); + Assert.AreEqual("abcdhello", result.GetString()); } } } From 001eb15c1c081951afa4f711f14339440fda5440 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 16 Sep 2020 15:31:57 +0200 Subject: [PATCH 7/8] add enter --- tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs index 34ab69629..38a3eb52d 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs @@ -43,6 +43,7 @@ public void TestStringAdd3() Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual("abchello", result.GetString()); } + [TestMethod] public void TestStringAdd4() { From efe9199e37947991e658312b39aa4068d5273a6d Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 16 Sep 2020 15:34:59 +0200 Subject: [PATCH 8/8] Remove enter --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index 26d8326b3..95fa2e652 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -691,7 +691,6 @@ private int ConvertCall(OpCode src, NeoMethod to) Insert1(VM.OpCode.CONVERT, "", to, new byte[] { (byte)VM.Types.StackItemType.ByteString }); return 0; } - else if (src.tokenMethod == "System.String System.String::Substring(System.Int32,System.Int32)") { Convert1by1(VM.OpCode.SUBSTR, src, to);