Skip to content

Commit b0b2906

Browse files
author
chaowlert
committed
remove GlobalReference
1 parent f0c0bd0 commit b0b2906

File tree

4 files changed

+47
-105
lines changed

4 files changed

+47
-105
lines changed

ExpressionDebugger/DebugInfoInjector.cs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
#if !NETSTANDARD1_3
45
using System.Dynamic;
56
#endif
@@ -12,7 +13,7 @@
1213

1314
namespace ExpressionDebugger
1415
{
15-
internal class DebugInfoInjector : ExpressionVisitor, IDisposable
16+
public class DebugInfoInjector : ExpressionVisitor, IDisposable
1617
{
1718
private const int Tabsize = 4;
1819
private readonly SymbolDocumentInfo _document;
@@ -373,6 +374,7 @@ protected override Expression VisitBinary(BinaryExpression node)
373374
Write(" ", Translate(node.NodeType), " ");
374375
right = VisitGroup(node.Right, node.NodeType, true);
375376
}
377+
376378
return node.Update(left, node.Conversion, right);
377379
}
378380

@@ -543,11 +545,12 @@ IEnumerable<Expression> VisitBlockBody(IList<Expression> exprs, bool shouldRetur
543545

544546
Expression VisitBlock(BlockExpression node, bool shouldReturn)
545547
{
546-
var assignedVariables = new HashSet<ParameterExpression>(node.Expressions
548+
var assignedVariables = node.Expressions
547549
.Where(exp => exp.NodeType == ExpressionType.Assign)
548550
.Select(exp => ((BinaryExpression)exp).Left)
549551
.Where(exp => exp.NodeType == ExpressionType.Parameter)
550-
.Select(exp => (ParameterExpression)exp));
552+
.Select(exp => (ParameterExpression)exp)
553+
.ToHashSet();
551554

552555
var list = new List<ParameterExpression>();
553556
var hasDeclaration = false;
@@ -681,48 +684,7 @@ protected override Expression VisitConstant(ConstantExpression node)
681684
Write("valueof(", Translate(type), ")");
682685
}
683686

684-
if (_document == null || CanEmitConstant(value, node.Type))
685-
return node;
686-
687-
var i = GlobalReference.GetIndex(value);
688-
return Expression.Convert(
689-
Expression.Call(
690-
typeof(GlobalReference).GetMethod(nameof(GlobalReference.GetObject)),
691-
Expression.Constant(i)),
692-
node.Type);
693-
}
694-
695-
private static bool CanEmitConstant(object value, Type type)
696-
{
697-
if (value == null
698-
|| type.GetTypeInfo().IsPrimitive
699-
|| type == typeof(string)
700-
|| type == typeof(decimal))
701-
return true;
702-
703-
if (value is Type t)
704-
return ShouldLdtoken(t);
705-
706-
if (value is MethodBase mb)
707-
return ShouldLdtoken(mb);
708-
709-
return false;
710-
}
711-
712-
private static bool ShouldLdtoken(Type t)
713-
{
714-
return t is TypeBuilder
715-
|| t.IsGenericParameter
716-
|| t.IsVisible;
717-
}
718-
719-
private static bool ShouldLdtoken(MethodBase mb)
720-
{
721-
if (mb is DynamicMethod)
722-
return false;
723-
724-
Type dt = mb.DeclaringType;
725-
return dt == null || ShouldLdtoken(dt);
687+
return node;
726688
}
727689

728690
protected override Expression VisitDefault(DefaultExpression node)
@@ -951,6 +913,8 @@ protected override Expression VisitIndex(IndexExpression node)
951913
: VisitGroup(node.Object, node.NodeType);
952914

953915
var args = VisitArguments("[", node.Arguments, Visit, "]");
916+
917+
//TODO:
954918
return node.Update(obj, args);
955919
}
956920

@@ -1480,6 +1444,36 @@ protected override Expression VisitUnary(UnaryExpression node)
14801444
return node.Update(operand);
14811445
}
14821446

1447+
public Delegate Compile(LambdaExpression node)
1448+
{
1449+
#if NETSTANDARD1_3
1450+
return node.Compile();
1451+
#else
1452+
1453+
var assemblyName = "m_" + Guid.NewGuid().ToString("N");
1454+
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run);
1455+
1456+
var daType = typeof(DebuggableAttribute);
1457+
var daCtor = daType.GetConstructor(new[] { typeof(DebuggableAttribute.DebuggingModes) });
1458+
var daBuilder = new CustomAttributeBuilder(daCtor, new object[] {
1459+
DebuggableAttribute.DebuggingModes.DisableOptimizations |
1460+
DebuggableAttribute.DebuggingModes.Default });
1461+
asm.SetCustomAttribute(daBuilder);
1462+
1463+
var mod = asm.DefineDynamicModule(assemblyName, true);
1464+
var type = mod.DefineType("Program", TypeAttributes.Public | TypeAttributes.Class);
1465+
var meth = type.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static);
1466+
1467+
var injected = (LambdaExpression)Inject(node);
1468+
var gen = DebugInfoGenerator.CreatePdbGenerator();
1469+
injected.CompileToMethod(meth, gen);
1470+
1471+
var newtype = type.CreateType();
1472+
1473+
return Delegate.CreateDelegate(node.Type, newtype.GetMethod("Main"));
1474+
#endif
1475+
}
1476+
14831477
enum LambdaType
14841478
{
14851479
Main,

ExpressionDebugger/ExpressionExtensions.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,10 @@ public static T CompileWithDebugInfo<T>(this Expression<T> node, string filename
3535

3636
public static Delegate CompileWithDebugInfo(this LambdaExpression node, string filename = null)
3737
{
38-
#if NETSTANDARD1_3
39-
return node.Compile();
40-
#else
41-
42-
if (filename == null)
43-
filename = Path.GetTempFileName();
44-
var assemblyName = "m_" + Guid.NewGuid().ToString("N");
45-
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run);
46-
47-
var daType = typeof(DebuggableAttribute);
48-
var daCtor = daType.GetConstructor(new[] { typeof(DebuggableAttribute.DebuggingModes) });
49-
var daBuilder = new CustomAttributeBuilder(daCtor, new object[] {
50-
DebuggableAttribute.DebuggingModes.DisableOptimizations |
51-
DebuggableAttribute.DebuggingModes.Default });
52-
asm.SetCustomAttribute(daBuilder);
53-
54-
var mod = asm.DefineDynamicModule(assemblyName, true);
55-
var type = mod.DefineType("Program", TypeAttributes.Public | TypeAttributes.Class);
56-
var meth = type.DefineMethod("Main", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Static);
57-
58-
LambdaExpression injected;
5938
using (var injector = new DebugInfoInjector(filename))
6039
{
61-
injected = (LambdaExpression)injector.Inject(node);
40+
return injector.Compile(node);
6241
}
63-
var gen = DebugInfoGenerator.CreatePdbGenerator();
64-
injected.CompileToMethod(meth, gen);
65-
66-
var newtype = type.CreateType();
67-
68-
return Delegate.CreateDelegate(node.Type, newtype.GetMethod("Main"));
69-
#endif
7042
}
7143
}
7244
}

ExpressionDebugger/ReflectionExtensions.cs renamed to ExpressionDebugger/Extensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45

56
namespace ExpressionDebugger
67
{
7-
internal static class ReflectionExtensions
8+
internal static class Extensions
89
{
10+
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
11+
{
12+
return new HashSet<T>(source);
13+
}
14+
915
#if NET40
1016
public static Type GetTypeInfo(this Type type) {
1117
return type;

ExpressionDebugger/GlobalReference.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)