Skip to content

Commit

Permalink
add: Runtime.LoadScript (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Sep 23, 2022
1 parent 33c6ca4 commit fa29c6d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
<PackageReference Include="Neo" Version="3.4.0" />
<PackageReference Include="Neo" Version="3.4.0-CI01389" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@ public static extern byte AddressVersion

[Syscall("System.Runtime.GetNetwork")]
public static extern uint GetNetwork();

[Syscall("System.Runtime.LoadScript")]
public static extern object LoadScript(ByteString script, CallFlags flags, params object[] args);
}
}
99 changes: 33 additions & 66 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_IntegerParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@ public void SByteParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "127");
var value = result.Pop().GetInteger();
Assert.AreEqual(127, value);
Assert.AreEqual(sbyte.MaxValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-128");

value = result.Pop().GetInteger();
Assert.AreEqual(-128, value);
Assert.AreEqual(sbyte.MinValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "128");
Expand Down Expand Up @@ -77,26 +73,22 @@ public void ByteParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "0");
var value = result.Pop().GetInteger();
Assert.AreEqual(0, value);
Assert.AreEqual(byte.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "255");

value = result.Pop().GetInteger();
Assert.AreEqual(255, value);
Assert.AreEqual(byte.MaxValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-1");
Expand Down Expand Up @@ -138,26 +130,22 @@ public void UShortParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "0");
var value = result.Pop().GetInteger();
Assert.AreEqual(0, value);
Assert.AreEqual(ushort.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "65535");

value = result.Pop().GetInteger();
Assert.AreEqual(65535, value);
Assert.AreEqual(ushort.MaxValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-1");
Expand Down Expand Up @@ -199,26 +187,22 @@ public void ShortParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "-32768");
var value = result.Pop().GetInteger();
Assert.AreEqual(-32768, value);
Assert.AreEqual(short.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "32767");

value = result.Pop().GetInteger();
Assert.AreEqual(32767, value);
Assert.AreEqual(short.MaxValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-32769");
Expand Down Expand Up @@ -260,26 +244,22 @@ public void ULongParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "18446744073709551615");
var value = result.Pop().GetInteger();
Assert.AreEqual(18446744073709551615, value);
Assert.AreEqual(ulong.MaxValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "0");

value = result.Pop().GetInteger();
Assert.AreEqual(0, value);
Assert.AreEqual(ulong.MinValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "18446744073709551616");
Expand Down Expand Up @@ -319,29 +299,24 @@ public void LongParse_Test()
testengine.AddEntryScript("./TestClasses/Contract_IntegerParse.cs");
string methodname = "testLongparse";

var result = testengine.ExecuteTestCaseStandard(methodname, " -9223372036854775808");
var result = testengine.ExecuteTestCaseStandard(methodname, "-9223372036854775808");
var value = result.Pop().GetInteger();
Assert.AreEqual(-9223372036854775808, value);
Assert.AreEqual(long.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "9223372036854775807");

value = result.Pop().GetInteger();
Assert.AreEqual(9223372036854775807, value);
Assert.AreEqual(long.MaxValue, value);

//test backspace trip
testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);

Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-9223372036854775809");
Expand Down Expand Up @@ -383,25 +358,21 @@ public void UIntParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "4294967295");
var value = result.Pop().GetInteger();
Assert.AreEqual(4294967295, value);
Assert.AreEqual(uint.MaxValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "0");

value = result.Pop().GetInteger();
Assert.AreEqual(0, value);
Assert.AreEqual(uint.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-1");
Expand Down Expand Up @@ -443,25 +414,21 @@ public void IntParse_Test()

var result = testengine.ExecuteTestCaseStandard(methodname, "2147483647");
var value = result.Pop().GetInteger();
Assert.AreEqual(2147483647, value);
Assert.AreEqual(int.MaxValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "-2147483648");

value = result.Pop().GetInteger();
Assert.AreEqual(-2147483648, value);
Assert.AreEqual(int.MinValue, value);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, " 20 ");

value = result.Pop().GetInteger();
Assert.AreEqual(20, value);
Assert.AreEqual(VMState.FAULT, testengine.State);

testengine.Reset();
result = testengine.ExecuteTestCaseStandard(methodname, "2147483648");
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.CSharp.UnitTests/Utils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public EvaluationStack ExecuteTestCaseStandard(int offset, ushort rvcount, NefFi
Console.WriteLine("op:[" +
this.CurrentContext.InstructionPointer.ToString("X04") +
"]" +
this.CurrentContext.CurrentInstruction.OpCode);
this.CurrentContext.CurrentInstruction?.OpCode);
this.ExecuteNext();
}
return this.ResultStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public void Test_InvocationCounter()
Assert.AreEqual(0x01, item.GetInteger());
}

[TestMethod]
public void Test_LoadScript()
{
var result = _engine.ExecuteTestCaseStandard("dynamicSum", new StackItem[] { new Integer(1), new Integer(3) });
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Integer));
Assert.AreEqual(4, item.GetInteger());
}

[TestMethod]
public void Test_Time()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,11 @@ public static object GetTransactionScript()
var tx = (Transaction)Runtime.ScriptContainer;
return tx?.Script;
}

public static int DynamicSum(int a, int b)
{
ByteString script = (ByteString)new byte[] { 0x9E }; // ADD
return (int)Runtime.LoadScript(script, CallFlags.All, new object[] { a, b });
}
}
}

0 comments on commit fa29c6d

Please sign in to comment.