Skip to content

Commit

Permalink
Merge branch 'master' into pr/1207
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Oct 23, 2024
2 parents 1094a35 + 6e651f5 commit 08c94e4
Show file tree
Hide file tree
Showing 51 changed files with 790 additions and 954 deletions.
2 changes: 1 addition & 1 deletion neo
Submodule neo updated 190 files
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.VM;
using System.Linq;

namespace Neo.Compiler;

Expand All @@ -35,12 +36,9 @@ internal partial class MethodConvert
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/anonymous-types">Anonymous types</seealso>
private void ConvertAnonymousObjectCreationExpression(SemanticModel model, AnonymousObjectCreationExpressionSyntax expression)
{
AddInstruction(OpCode.NEWARRAY0);
foreach (AnonymousObjectMemberDeclaratorSyntax initializer in expression.Initializers)
{
AddInstruction(OpCode.DUP);
foreach (AnonymousObjectMemberDeclaratorSyntax initializer in expression.Initializers.Reverse()) // PACK works in a reversed way
ConvertExpression(model, initializer.Expression);
AddInstruction(OpCode.APPEND);
}
Push(expression.Initializers.Count);
AddInstruction(OpCode.PACK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ private void ConvertInvocationExpression(SemanticModel model, InvocationExpressi
/// <example><see href="https://github.com/neo-project/neo-devpack-dotnet/blob/master/examples/Example.SmartContract.Event/Event.cs"/></example>
private void ConvertEventInvocationExpression(SemanticModel model, IEventSymbol symbol, ArgumentSyntax[] arguments)
{
AddInstruction(OpCode.NEWARRAY0);
foreach (ArgumentSyntax argument in arguments)
{
AddInstruction(OpCode.DUP);
foreach (ArgumentSyntax argument in arguments.Reverse()) // PACK works in a reversed way
ConvertExpression(model, argument.Expression);
AddInstruction(OpCode.APPEND);
}
Push(arguments.Length);
AddInstruction(OpCode.PACK);
Push(symbol.GetDisplayName());
CallInteropMethod(ApplicationEngine.System_Runtime_Notify);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.VM;
using System.Linq;

namespace Neo.Compiler;

Expand All @@ -37,12 +38,9 @@ internal partial class MethodConvert
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples">Tuple types</seealso>
private void ConvertTupleExpression(SemanticModel model, TupleExpressionSyntax expression)
{
AddInstruction(OpCode.NEWSTRUCT0);
foreach (ArgumentSyntax argument in expression.Arguments)
{
AddInstruction(OpCode.DUP);
foreach (ArgumentSyntax argument in expression.Arguments.Reverse()) // PACKSTRUCT works in a reversed way
ConvertExpression(model, argument.Expression);
AddInstruction(OpCode.APPEND);
}
Push(expression.Arguments.Count);
AddInstruction(OpCode.PACKSTRUCT);
}
}
35 changes: 16 additions & 19 deletions src/Neo.Compiler.CSharp/MethodConvert/Helpers/ConvertHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,25 @@ private void CreateObject(SemanticModel model, ITypeSymbol type, InitializerExpr
{
var members = type.GetAllMembers().Where(p => !p.IsStatic).ToArray();
var fields = members.OfType<IFieldSymbol>().ToArray();
if (fields.Length == 0 || type.IsValueType || type.IsRecord)

int needVirtualMethodTable = 0;
var virtualMethods = members.OfType<IMethodSymbol>().Where(p => p.IsVirtualMethod()).ToArray();
if (!type.IsRecord && virtualMethods.Length > 0)
{
AddInstruction(type.IsValueType || type.IsRecord ? OpCode.NEWSTRUCT0 : OpCode.NEWARRAY0);
foreach (var field in fields)
{
AddInstruction(OpCode.DUP);
InitializeFieldForObject(model, field, initializer);
AddInstruction(OpCode.APPEND);
}
needVirtualMethodTable += 1;
byte vTableIndex = _context.AddVTable(type);
AccessSlot(OpCode.LDSFLD, vTableIndex);
}
else

if (fields.Length == 0 && needVirtualMethodTable == 0)
{
for (int i = fields.Length - 1; i >= 0; i--)
InitializeFieldForObject(model, fields[i], initializer);
Push(fields.Length);
AddInstruction(OpCode.PACK);
AddInstruction(type.IsValueType || type.IsRecord ? OpCode.NEWSTRUCT0 : OpCode.NEWARRAY0);
return;
}
var virtualMethods = members.OfType<IMethodSymbol>().Where(p => p.IsVirtualMethod()).ToArray();
if (type.IsRecord || virtualMethods.Length <= 0) return;
var index = _context.AddVTable(type);
AddInstruction(OpCode.DUP);
AccessSlot(OpCode.LDSFLD, index);
AddInstruction(OpCode.APPEND);

foreach (var field in fields.Reverse()) // PACK and PACKSTRUCT works in a reversed way
InitializeFieldForObject(model, field, initializer);
Push(fields.Length + needVirtualMethodTable);
AddInstruction(type.IsValueType || type.IsRecord ? OpCode.PACKSTRUCT : OpCode.PACK);
}
}
28 changes: 8 additions & 20 deletions src/Neo.Compiler.CSharp/MethodConvert/System/SystemCall.Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,10 @@ private static void HandleEnumGetNames(MethodConvert methodConvert, SemanticMode
.Where(field => field is { HasConstantValue: true, IsImplicitlyDeclared: false }).ToArray();

// Create an array of names
methodConvert.Push(enumMembers.Length); // Stack: [..., size]
methodConvert.AddInstruction(OpCode.NEWARRAY); // Stack: [..., array]

for (int i = 0; i < enumMembers.Length; i++)
{
methodConvert.Dup(); // Duplicate array reference
methodConvert.Push(i); // Index
methodConvert.Push(enumMembers[i].Name); // Name
methodConvert.AddInstruction(OpCode.SETITEM); // array[i] = name
}
foreach (IFieldSymbol m in enumMembers.Reverse()) // PACK works in a reversed way
methodConvert.Push(m.Name);
methodConvert.Push(enumMembers.Length);
methodConvert.AddInstruction(OpCode.PACK);

methodConvert.AddInstruction(OpCode.RET);
}
Expand Down Expand Up @@ -568,16 +562,10 @@ private static void HandleEnumGetValues(MethodConvert methodConvert, SemanticMod
.Where(field => field is { HasConstantValue: true, IsImplicitlyDeclared: false }).ToArray();

// Create an array of values
methodConvert.Push(enumMembers.Length); // Stack: [..., size]
methodConvert.AddInstruction(OpCode.NEWARRAY); // Stack: [..., array]

for (int i = 0; i < enumMembers.Length; i++)
{
methodConvert.Dup(); // Duplicate array reference
methodConvert.Push(i); // Index
methodConvert.Push(enumMembers[i].ConstantValue); // Value
methodConvert.AddInstruction(OpCode.SETITEM); // array[i] = value
}
foreach (IFieldSymbol m in enumMembers.Reverse()) // PACK works in a reversed way
methodConvert.Push(m.ConstantValue);
methodConvert.Push(enumMembers.Length);
methodConvert.AddInstruction(OpCode.PACK);

methodConvert.AddInstruction(OpCode.RET);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Neo.SmartContract.Testing/Storage/Rpc/RpcSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public void Put(byte[] key, byte[] value)
return Store.Seek(keyOrPrefix, direction);
}

public bool TryGet(byte[] key, out byte[]? value)
{
return Store.TryGet(key, out value);
}

public byte[]? TryGet(byte[] key)
{
return Store.TryGet(key);
Expand Down
18 changes: 15 additions & 3 deletions src/Neo.SmartContract.Testing/Storage/Rpc/RpcStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void Dispose() { }
throw new Exception();
}

public byte[]? TryGet(byte[] key)
public bool TryGet(byte[] key, out byte[]? value)
{
var skey = new StorageKey(key);
var requestBody = new
Expand All @@ -152,7 +152,8 @@ public void Dispose() { }
{
// {\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"Aw==\"}

return Convert.FromBase64String(result);
value = Convert.FromBase64String(result);
return true;
}
else
{
Expand All @@ -162,12 +163,23 @@ public void Dispose() { }
error["code"]?.Value<int>() is int errorCode &&
(errorCode == -100 || errorCode == -104))
{
return null;
value = null;
return false;
}

throw new Exception();
}
}

public byte[]? TryGet(byte[] key)
{
if (TryGet(key, out var value))
{
return value;
}

return null;
}

#endregion
}
Loading

0 comments on commit 08c94e4

Please sign in to comment.