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

Added CreateStruct & Fixed CreateMap #3494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
70 changes: 58 additions & 12 deletions src/Neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,64 @@ public static ScriptBuilder CreateArray<T>(this ScriptBuilder builder, IReadOnly
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="map">The key/value pairs of the map.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IEnumerable<KeyValuePair<TKey, TValue>> map = null)
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IEnumerable<KeyValuePair<TKey, TValue>> map)
where TKey : notnull
where TValue : notnull
Copy link
Contributor

@Jim8y Jim8y Sep 21, 2024

Choose a reason for hiding this comment

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

Value can not be null? Do we have this limitation before?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Sep 21, 2024

Choose a reason for hiding this comment

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

You can't do map[null] = null. You could before but wouldn't work. Needs to be StackItem.Null.

This just stop you from using value types from being null like byte?, int? and etc

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

if (count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
}
builder.EmitPush(count);
return builder.Emit(OpCode.PACKMAP);
}

/// <summary>
/// Emits the opcodes for creating a map.
/// </summary>
/// <typeparam name="TKey">The type of the key of the map.</typeparam>
/// <typeparam name="TValue">The type of the value of the map.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="map">The key/value pairs of the map.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateMap<TKey, TValue>(this ScriptBuilder builder, IReadOnlyDictionary<TKey, TValue> map)
where TKey : notnull
where TValue : notnull
{
if (map.Count == 0)
return builder.Emit(OpCode.NEWMAP);

foreach (var (key, value) in map.Reverse())
{
builder.EmitPush(value);
builder.EmitPush(key);
}
builder.EmitPush(map.Count);
return builder.Emit(OpCode.PACKMAP);
}

/// <summary>
/// Emits the opcodes for creating a struct.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="builder">The <see cref="ScriptBuilder"/> to be used.</param>
/// <param name="array">The list of properties.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
public static ScriptBuilder CreateStruct<T>(this ScriptBuilder builder, IReadOnlyList<T> array)
where T : notnull
{
if (array.Count == 0)
return builder.Emit(OpCode.NEWSTRUCT0);
for (var i = array.Count - 1; i >= 0; i--)
builder.EmitPush(array[i]);
builder.EmitPush(array.Count);
return builder.Emit(OpCode.PACKSTRUCT);
}

/// <summary>
Expand Down Expand Up @@ -218,7 +264,7 @@ public static ScriptBuilder EmitPush(this ScriptBuilder builder, object obj)
builder.EmitPush(data);
break;
case char data:
builder.EmitPush((ushort)data);
builder.EmitPush(data);
break;
case ushort data:
builder.EmitPush(data);
Expand Down
24 changes: 24 additions & 0 deletions tests/Neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ public void TestEmitArray()
Assert.AreEqual(0, engine2.ResultStack.Pop<VM.Types.Array>().Count);
}

[TestMethod]
public void TestEmitStruct()
{
var expected = new BigInteger[] { 1, 2, 3 };
var sb = new ScriptBuilder();
sb.CreateStruct(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.Struct>().Select(u => u.GetInteger()).ToArray());

expected = new BigInteger[] { };
sb = new ScriptBuilder();
sb.CreateStruct(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.Struct>().Count);
}

[TestMethod]
public void TestEmitMap()
{
Expand Down
Loading