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

Change some native methods to properties #354

Merged
merged 4 commits into from
Sep 20, 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
30 changes: 28 additions & 2 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.SmartContract;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -878,8 +879,7 @@ private int ConvertCall(OpCode src, NeoMethod to)
Convert1by1(VM.OpCode.PACK, null, to);

// Push call method name, the first letter should be lowercase.
var methodName = defs.Body.Method.Name;
ConvertPushString(methodName[..1].ToLowerInvariant() + methodName[1..], src, to);
ConvertPushString(GetMethodName(defs.Body.Method), src, to);

// Push contract hash.
ConvertPushDataArray(callhash, src, to);
Expand Down Expand Up @@ -923,6 +923,32 @@ private int ConvertCall(OpCode src, NeoMethod to)
return 0;
}

private string GetMethodName(MethodDefinition method)
{
foreach (var attr in method.CustomAttributes)
{
if (attr.AttributeType.Name == nameof(DisplayNameAttribute))
{
return (string)attr.ConstructorArguments[0].Value;
}
}

string methodName = null;
if (method.SemanticsAttributes.HasFlag(MethodSemanticsAttributes.Getter))
Copy link
Member Author

Choose a reason for hiding this comment

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

Nice!

{
foreach (PropertyDefinition property in method.DeclaringType.Properties)
{
if (property.GetMethod == method)
{
methodName = property.Name;
break;
}
}
}
if (methodName is null) methodName = method.Name;
return methodName[..1].ToLowerInvariant() + methodName[1..];
}

private List<string> GetAllConstStringAfter(ILMethod method, OpCode src)
{
List<string> strlist = new List<string>();
Expand Down
3 changes: 2 additions & 1 deletion src/Neo.Compiler.MSIL/MSIL/ILModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.ComponentModel;

/// <summary>
/// This file is responsible for the parsing of the IL DLL. The core parsing is using mono.cecil, which is prearranged to make it easier to use
Expand Down Expand Up @@ -121,7 +122,7 @@ public ILField(ILType type, FieldDefinition field)
Mono.Collections.Generic.Collection<CustomAttribute> ca = ev.CustomAttributes;
foreach (var attr in ca)
{
if (attr.AttributeType.Name == "DisplayNameAttribute")
if (attr.AttributeType.Name == nameof(DisplayNameAttribute))
{
this.displayName = (string)attr.ConstructorArguments[0].Value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.SmartContract.Framework/Services/Neo/GAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Neo.SmartContract.Framework.Services.Neo
[Contract("0x668e0c1f9d7b70a99dd9e06eadd4c784d641afbc")]
public class GAS
{
public static extern string Name();
public static extern string Symbol();
public static extern byte Decimals();
public static extern string Name { get; }
public static extern string Symbol { get; }
public static extern byte Decimals { get; }
public static extern BigInteger TotalSupply();
public static extern BigInteger BalanceOf(byte[] account);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.SmartContract.Framework/Services/Neo/NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Neo.SmartContract.Framework.Services.Neo
[Contract("0xde5f57d430d3dece511cf975a8d37848cb9e0525")]
public class NEO
{
public static extern string Name();
public static extern string Symbol();
public static extern BigInteger Decimals();
public static extern string Name { get; }
public static extern string Symbol { get; }
public static extern byte Decimals { get; }
public static extern BigInteger TotalSupply();
public static extern BigInteger BalanceOf(byte[] account);

Expand Down
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Neo.SmartContract.Framework.Services.Neo
[Contract("0x3c05b488bf4cf699d0631bf80190896ebbf38c3b")]
public class Oracle
{
public static extern string Name();
public static extern string Name { get; }
public static extern byte[][] GetOracleNodes();
public static extern void Request(string url, string filter, string callback, object userData, long gasForResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ class Contract_NativeContracts : SmartContract.Framework.SmartContract
{
public static string NEOName()
{
return Neo.SmartContract.Framework.Services.Neo.NEO.Name();
return Neo.SmartContract.Framework.Services.Neo.NEO.Name;
}

public static string GASName()
{
return Neo.SmartContract.Framework.Services.Neo.GAS.Name();
return Neo.SmartContract.Framework.Services.Neo.GAS.Name;
}

public static string OracleName()
{
return Neo.SmartContract.Framework.Services.Neo.Oracle.Name();
return Neo.SmartContract.Framework.Services.Neo.Oracle.Name;
}

public static string NEOSymbol()
{
return Neo.SmartContract.Framework.Services.Neo.NEO.Symbol;
}

public static string GASSymbol()
{
return Neo.SmartContract.Framework.Services.Neo.GAS.Symbol;
}

public static byte[][] getOracleNodes()
Expand Down
20 changes: 20 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public void Test_NEO()
var entry = result.Pop();

Assert.AreEqual("NEO", entry.GetString());

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("nEOSymbol");

Assert.AreEqual(VMState.HALT, testengine.State);
Assert.AreEqual(1, result.Count);

entry = result.Pop();

Assert.AreEqual("neo", entry.GetString());
}

[TestMethod]
Expand All @@ -98,6 +108,16 @@ public void Test_GAS()
var entry = result.Pop();

Assert.AreEqual("GAS", entry.GetString());

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("gASSymbol");

Assert.AreEqual(VMState.HALT, testengine.State);
Assert.AreEqual(1, result.Count);

entry = result.Pop();

Assert.AreEqual("gas", entry.GetString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class Contract_Native : SmartContract.Framework.SmartContract
[DisplayName("NEO_Decimals")]
public static int NEO_Decimals()
{
return (int)NEO.Decimals();
return NEO.Decimals;
}

[DisplayName("NEO_Name")]
public static string NEO_Name()
{
return NEO.Name();
return NEO.Name;
}

[DisplayName("NEO_BalanceOf")]
Expand Down Expand Up @@ -46,13 +46,13 @@ public static (string, BigInteger)[] NEO_GetCandidates()
[DisplayName("GAS_Decimals")]
public static int GAS_Decimals()
{
return (int)GAS.Decimals();
return GAS.Decimals;
}

[DisplayName("GAS_Name")]
public static string GAS_Name()
{
return GAS.Name();
return GAS.Name;
}

public static BigInteger Policy_GetFeePerByte()
Expand Down