Skip to content

Commit

Permalink
1.fix a bug about convert func
Browse files Browse the repository at this point in the history
#196

because Insert a Opcode Convert

but newarr still get last opcode for a PushNumber

2.add some unittest for it.
  • Loading branch information
lightszero committed Mar 12, 2020
1 parent d6c2530 commit 275bae1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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[?].");

Expand Down
15 changes: 15 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
26 changes: 26 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

0 comments on commit 275bae1

Please sign in to comment.