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

Add CreateArray and CreateMap to Helper #2145

Merged
merged 2 commits into from
Dec 12, 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
24 changes: 24 additions & 0 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ namespace Neo.VM
{
public static class Helper
{
public static ScriptBuilder CreateArray<T>(this ScriptBuilder sb, IReadOnlyList<T> list = null)
{
if (list is null || list.Count == 0)
return sb.Emit(OpCode.NEWARRAY0);
for (int i = list.Count - 1; i >= 0; i--)
sb.EmitPush(list[i]);
sb.EmitPush(list.Count);
return sb.Emit(OpCode.PACK);
}

public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder sb, IReadOnlyDictionary<TKey, TValue> map = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we keep the naming "Emit" or "Push"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it does not directly push the map, it actually creates a new map.

{
sb.Emit(OpCode.NEWMAP);
if (map != null)
foreach (var p in map)
{
sb.Emit(OpCode.DUP);
sb.EmitPush(p.Key);
sb.EmitPush(p.Value);
sb.Emit(OpCode.SETITEM);
}
return sb;
}

public static ScriptBuilder Emit(this ScriptBuilder sb, params OpCode[] ops)
{
foreach (OpCode op in ops)
Expand Down
42 changes: 42 additions & 0 deletions tests/neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ public void TestEmitAppCall1()
CollectionAssert.AreEqual(tempArray, sb.ToArray());
}

[TestMethod]
public void TestEmitArray()
{
var expected = new BigInteger[] { 1, 2, 3 };
var sb = new ScriptBuilder();
sb.CreateArray(expected);

using var engine = ApplicationEngine.Create(TriggerType.Application, null, null);
engine.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine.Execute());

CollectionAssert.AreEqual(expected, engine.ResultStack.Pop<VM.Types.Array>().Select(u => u.GetInteger()).ToArray());

expected = new BigInteger[] { };
sb = new ScriptBuilder();
sb.CreateArray(expected);

using var engine2 = ApplicationEngine.Create(TriggerType.Application, null, null);
engine2.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine2.Execute());

Assert.AreEqual(0, engine2.ResultStack.Pop<VM.Types.Array>().Count);
}

[TestMethod]
public void TestEmitMap()
{
var expected = new Dictionary<BigInteger, BigInteger>() { { 1, 2 }, { 3, 4 } };
var sb = new ScriptBuilder();
sb.CreateMap(expected);

using var engine = ApplicationEngine.Create(TriggerType.Application, null, null);
engine.LoadScript(sb.ToArray());
Assert.AreEqual(VMState.HALT, engine.Execute());

var map = engine.ResultStack.Pop<VM.Types.Map>();
var dic = map.ToDictionary(u => u.Key, u => u.Value);

CollectionAssert.AreEqual(expected.Keys, dic.Keys.Select(u => u.GetInteger()).ToArray());
CollectionAssert.AreEqual(expected.Values, dic.Values.Select(u => u.GetInteger()).ToArray());
}

[TestMethod]
public void TestEmitAppCall2()
{
Expand Down