Skip to content

Commit

Permalink
Merge pull request #1 from neo-project/master
Browse files Browse the repository at this point in the history
merge from origin
  • Loading branch information
陈志同 authored Sep 11, 2020
2 parents 98c29df + 8421317 commit cdc135f
Show file tree
Hide file tree
Showing 122 changed files with 2,951 additions and 2,530 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
Expand All @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
Expand All @@ -63,7 +63,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.Compiler.MSIL/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static Assembly CompileVBFiles(string[] filenames, string[] references, b
var tree = filenames.Select(u => VisualBasicSyntaxTree.ParseText(
File.ReadAllText(u),
path: u,
encoding: System.Text.Encoding.UTF8)).ToArray();
encoding: Utility.StrictUTF8)).ToArray();
var op = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: releaseMode ? OptimizationLevel.Release : OptimizationLevel.Debug);
return Assembly.Create(VisualBasicCompilation.Create("SmartContract", tree, CreateReferences(references), op));
}
Expand All @@ -179,7 +179,7 @@ public static Assembly CompileCSFiles(string[] filenames, string[] references, b
var tree = filenames.Select(u => CSharpSyntaxTree.ParseText(
File.ReadAllText(u),
path: u,
encoding: System.Text.Encoding.UTF8)).ToArray();
encoding: Utility.StrictUTF8)).ToArray();
var op = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: releaseMode ? OptimizationLevel.Release : OptimizationLevel.Debug);
return Assembly.Create(CSharpCompilation.Create("SmartContract", tree, CreateReferences(references), op));
}
Expand Down
134 changes: 134 additions & 0 deletions src/Neo.Compiler.MSIL/DebugExport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using Mono.Cecil.Cil;
using Neo.IO.Json;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Neo.Compiler
{
public static class DebugExport
{
private static JArray GetSequencePoints(IEnumerable<NeoCode> codes, IReadOnlyDictionary<string, int> docMap, IReadOnlyDictionary<int, int> addrConvTable)
{
var points = codes
.Where(code => code.sequencePoint != null)
.Select(code => (code.addr, code.sequencePoint));

var outjson = new JArray();

foreach (var (address, sequencePoint) in points)
{
var value = string.Format("{0}[{1}]{2}:{3}-{4}:{5}",
addrConvTable.TryGetValue(address, out var newAddress) ? newAddress : address,
docMap[sequencePoint.Document.Url],
sequencePoint.StartLine,
sequencePoint.StartColumn,
sequencePoint.EndLine,
sequencePoint.EndColumn);
outjson.Add(value);
}

return outjson;
}

private static JArray ConvertParamList(IEnumerable<NeoParam> @params)
{
@params ??= Enumerable.Empty<NeoParam>();
var paramsJson = new JArray();
foreach (var param in @params)
{
var value = string.Format("{0},{1}", param.name, FuncExport.ConvType(param.type));
paramsJson.Add(value);
}

return paramsJson;
}

private static JArray GetMethods(NeoModule module, IReadOnlyDictionary<string, int> docMap, IReadOnlyDictionary<int, int> addrConvTable)
{
var outjson = new JArray();

foreach (var method in module.mapMethods.Values)
{
if (method.body_Codes.Values.Count == 0)
{
continue;
}

var name = string.Format("{0},{1}",
method._namespace, method.displayName);

var range = string.Format("{0}-{1}",
method.body_Codes.Values.First().addr,
method.body_Codes.Values.Last().addr);

var methodJson = new JObject();
methodJson["id"] = method.name;
methodJson["name"] = name;
methodJson["range"] = range;
methodJson["params"] = ConvertParamList(method.paramtypes);
methodJson["return"] = FuncExport.ConvType(method.returntype);
methodJson["variables"] = ConvertParamList(method.method?.body_Variables);
methodJson["sequence-points"] = GetSequencePoints(method.body_Codes.Values, docMap, addrConvTable);
outjson.Add(methodJson);
}
return outjson;
}

private static JArray GetEvents(NeoModule module)
{
var outjson = new JArray();
foreach (var @event in module.mapEvents.Values)
{
var name = string.Format("{0},{1}",
@event._namespace, @event.displayName);

var eventJson = new JObject();
eventJson["id"] = @event.name;
eventJson["name"] = name;
eventJson["params"] = ConvertParamList(@event.paramtypes);
outjson.Add(eventJson);
}
return outjson;
}

private static IReadOnlyDictionary<string, int> GetDocumentMap(NeoModule module)
{
return module.mapMethods.Values
.SelectMany(m => m.body_Codes.Values)
.Where(code => code.sequencePoint != null)
.Select(code => code.sequencePoint.Document.Url)
.Distinct()
.Select((d, i) => (d, i))
.ToDictionary(t => t.d, t => t.i);
}

private static JArray GetDocuments(IReadOnlyDictionary<string, int> docMap)
{
var outjson = new JArray();
foreach (var doc in docMap.OrderBy(kvp => kvp.Value))
{
Debug.Assert(outjson.Count == doc.Value);
outjson.Add(doc.Key);
}
return outjson;
}

public static JObject Export(NeoModule module, byte[] script, IReadOnlyDictionary<int, int> addrConvTable)
{
var docMap = GetDocumentMap(module);
addrConvTable ??= ImmutableDictionary<int, int>.Empty;

var outjson = new JObject();
outjson["hash"] = FuncExport.ComputeHash(script);
// outjson["entrypoint"]= module.mainMethod;
outjson["documents"] = GetDocuments(docMap);
outjson["methods"] = GetMethods(module, docMap, addrConvTable);
outjson["events"] = GetEvents(module);
return outjson;
}
}
}
Loading

0 comments on commit cdc135f

Please sign in to comment.