Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concat #356

Merged
merged 9 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mono.Cecil;
using Neo.SmartContract;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -644,12 +645,52 @@ 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);
//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);
}
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;
}

else if (src.tokenMethod == "System.String System.String::Substring(System.Int32,System.Int32)")
{
Convert1by1(VM.OpCode.SUBSTR, src, to);
Expand Down
27 changes: 27 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_Concat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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";
}

public static string TestStringAdd4(string a, string b, string c, string d)
{
return a + b + c + d + "hello";
}
}
}
56 changes: 56 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Concat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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", false, false);
}

[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());
}

[TestMethod]
public void TestStringAdd4()
{
_engine.Reset();
var result = _engine.GetMethod("testStringAdd4").Run("a", "b", "c", "d").ConvertTo(StackItemType.ByteString);
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual("abcdhello", result.GetString());
}
}
}