Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Jun 1, 2020
1 parent b4a282c commit efd99d1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Runtime_Notify = Register("System.Runtime.Notify", nameof(RuntimeNotify), 0_01000000, TriggerType.All, CallFlags.AllowNotify);
public static readonly InteropDescriptor System_Runtime_GetNotifications = Register("System.Runtime.GetNotifications", nameof(GetNotifications), 0_00010000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor System_Runtime_GasLeft = Register("System.Runtime.GasLeft", nameof(GasLeft), 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor CreateCallback = Register("System.Runtime.CreateCallback", nameof(Runtime_CreateCallback), 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor InvokeCallback = Register("System.Runtime.InvokeCallback", nameof(Runtime_InvokeCallback), 0_00000400, TriggerType.All, CallFlags.None);

internal void Runtime_CreateCallback(Pointer pointer, int parcount, int rvcount)
{
Push(new InteropInterface(new Callback(CurrentContext, pointer, parcount, rvcount)));
}

internal void Runtime_InvokeCallback(InteropInterface callbackItem)
{
var callback = callbackItem.GetInterface<Callback>();
if (callback == null) throw new ArgumentException();

var context = callback.Context.Clone(callback.RVcount);
LoadContext(context);
context.InstructionPointer = callback.Pointer.Position;

for (int x = callback.Params.Length - 1; x >= 0; x--)
{
Push(callback.Params[x]);
}
}

private static bool CheckItemForNotification(StackItem state)
{
Expand Down
26 changes: 26 additions & 0 deletions src/neo/SmartContract/Callback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Neo.VM;
using Neo.VM.Types;

namespace Neo.SmartContract
{
internal class Callback
{
public readonly Pointer Pointer;
public readonly ExecutionContext Context;
public readonly int RVcount;
public readonly StackItem[] Params;

public Callback(ExecutionContext context, Pointer pointer, int parcount, int rvcount)
{
Context = context;
Pointer = pointer;
RVcount = rvcount;
Params = new StackItem[parcount];

for (int x = 0; x < parcount; x++)
{
Params[x] = context.EvaluationStack.Pop();
}
}
}
}
1 change: 1 addition & 0 deletions src/neo/SmartContract/InteropParameterDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class InteropParameterDescriptor
private static readonly Dictionary<Type, Func<StackItem, object>> converters = new Dictionary<Type, Func<StackItem, object>>
{
[typeof(StackItem)] = p => p,
[typeof(VM.Types.Pointer)] = p => p,
[typeof(VM.Types.Array)] = p => p,
[typeof(InteropInterface)] = p => p,
[typeof(sbyte)] = p => (sbyte)p.GetBigInteger(),
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00219" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00223" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.Runtime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;

namespace Neo.UnitTests.SmartContract
{
public partial class UT_Syscalls
{
[TestMethod]
public void Runtime_CreateCallback()
{
using var script = new ScriptBuilder();

script.EmitPush(5);
script.EmitPush(2);
script.EmitPush(1);
script.Emit(OpCode.PUSHA, BitConverter.GetBytes(0));
script.EmitSysCall(ApplicationEngine.CreateCallback);

// Execute

var engine = new ApplicationEngine(TriggerType.Application, null, null, 100_000_000, false);
engine.LoadScript(script.ToArray());
Assert.AreEqual(engine.Execute(), VMState.HALT);

// Check the results

Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsTrue(engine.ResultStack.TryPop<InteropInterface>(out var item));
Assert.IsNotNull(item);

Assert.IsTrue(item.TryGetInterface<Callback>(out var callback));
Assert.AreEqual(2, callback.RVcount);
Assert.AreEqual(1, callback.Params.Length);
Assert.AreEqual(5, callback.Params[0].GetBigInteger());
}
}
}
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Neo.UnitTests.SmartContract
{
[TestClass]
public class UT_Syscalls
public partial class UT_Syscalls
{
[TestInitialize]
public void TestSetup()
Expand Down

0 comments on commit efd99d1

Please sign in to comment.