From 275bae1258f1cd923bde674c3d3f147908c79c11 Mon Sep 17 00:00:00 2001 From: lights Date: Thu, 12 Mar 2020 19:40:44 +0800 Subject: [PATCH] 1.fix a bug about convert func https://github.com/neo-project/neo-devpack-dotnet/pull/196 because Insert a Opcode Convert but newarr still get last opcode for a PushNumber 2.add some unittest for it. --- src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs | 10 +++++++ .../TestClasses/Contract_Array.cs | 15 +++++++++++ .../UnitTest_Array.cs | 26 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs index e7aba1b0f..c5e3b56be 100644 --- a/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs +++ b/src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs @@ -1034,6 +1034,9 @@ private bool TryInsertMethod(NeoModule outModule, Mono.Cecil.MethodDefinition me private int _ConvertCgt(ILMethod method, OpCode src, NeoMethod to) { var code = to.body_Codes.Last().Value; + + if (code.code == VM.OpCode.CONVERT) + code = to.body_Codes.TakeLast(2).First().Value; if (code.code == VM.OpCode.PUSHNULL) { //remove last code @@ -1052,6 +1055,10 @@ private int _ConvertCgt(ILMethod method, OpCode src, NeoMethod to) private int _ConvertCeq(ILMethod method, OpCode src, NeoMethod to) { var code = to.body_Codes.Last().Value; + + if (code.code == VM.OpCode.CONVERT) + code = to.body_Codes.TakeLast(2).First().Value; + if (code.code == VM.OpCode.PUSHNULL) { //remove last code @@ -1147,6 +1154,9 @@ private int _ConvertNewArr(ILMethod method, OpCode src, NeoMethod to) { var code = to.body_Codes.Last().Value; + if (code.code == VM.OpCode.CONVERT) + code = to.body_Codes.TakeLast(2).First().Value; + if (code.code > VM.OpCode.PUSH16) //we need a number throw new Exception("_ConvertNewArr::not support var lens for new byte[?]."); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Array.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Array.cs index 06eff6cae..42149b708 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Array.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Array.cs @@ -57,5 +57,20 @@ public static object TestStructArrayInit() } return null; } + + static readonly byte[] OwnerVar = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; + private static byte[] Owner() + { + var bs = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; + return bs; + } + public static object TestByteArrayOwner() + { + return OwnerVar; + } + public static object TestByteArrayOwnerCall() + { + return Owner(); + } } } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs index c082375d4..68c762eb6 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs @@ -72,5 +72,31 @@ public void Test_StructArrayInit() var bequal = neostruct != null; Assert.IsTrue(bequal); } + + [TestMethod] + public void Test_ByteArrayOwner() + { + var testengine = new TestEngine(); + testengine.AddEntryScript("./TestClasses/Contract_Array.cs"); + var result = testengine.ExecuteTestCaseStandard("testByteArrayOwner"); + + var bts = result.Pop() as ByteArray; + + ByteArray test = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; + Assert.IsTrue(ByteArray.Equals(bts, test)); + } + + [TestMethod] + public void Test_ByteArrayOwnerCall() + { + var testengine = new TestEngine(); + testengine.AddEntryScript("./TestClasses/Contract_Array.cs"); + var result = testengine.ExecuteTestCaseStandard("testByteArrayOwnerCall"); + + var bts = result.Pop().ConvertTo(StackItemType.ByteArray); + + ByteArray test = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; + Assert.IsTrue(ByteArray.Equals(bts, test)); + } } }