-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegateEmitter.cs
106 lines (87 loc) · 4.69 KB
/
DelegateEmitter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Mono.Cecil;
using System;
using System.Collections.Generic;
namespace LaborasLangCompiler.Codegen.Types
{
internal class DelegateEmitter : TypeEmitter
{
private const TypeAttributes DelegateTypeAttributes = TypeAttributes.NestedPublic | TypeAttributes.Sealed;
private const MethodAttributes ConstructorAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
private const MethodAttributes DelegateMethodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.VtableLayoutMask;
private TypeReference voidType;
private TypeReference objectType;
private TypeReference nativeIntType;
private TypeReference asyncResultType;
private TypeReference asyncCallbackType;
public static TypeDefinition Create(AssemblyEmitter assembly, TypeDefinition declaringType,
TypeReference returnType, IReadOnlyList<TypeReference> arguments)
{
return new DelegateEmitter(assembly, "Delegate", declaringType, returnType, arguments).typeDefinition;
}
public static TypeDefinition Create(AssemblyEmitter assembly, string delegateName, TypeDefinition declaringType,
TypeReference returnType, IReadOnlyList<TypeReference> arguments)
{
return new DelegateEmitter(assembly, delegateName, declaringType, returnType, arguments).typeDefinition;
}
private DelegateEmitter(AssemblyEmitter assembly, string delegateName, TypeDefinition declaringType, TypeReference returnType,
IReadOnlyList<TypeReference> arguments) :
base(assembly, delegateName, "", DelegateTypeAttributes, AssemblyRegistry.FindType(assembly, "System.MulticastDelegate"), false)
{
if (declaringType == null)
{
throw new ArgumentNullException("declaringType", "Delegate class must be have a valid declaring type!");
}
typeDefinition.DeclaringType = declaringType;
InitializeTypes();
AddConstructor();
AddBeginInvoke(arguments);
AddEndInvoke(returnType);
AddInvoke(returnType, arguments);
}
private void InitializeTypes()
{
voidType = Assembly.TypeSystem.Void;
objectType = Assembly.TypeSystem.Object;
nativeIntType = Assembly.TypeSystem.IntPtr;
asyncResultType = AssemblyRegistry.FindType(Assembly, "System.IAsyncResult");
asyncCallbackType = AssemblyRegistry.FindType(Assembly, "System.AsyncCallback");
}
private void AddConstructor()
{
var constructor = new MethodDefinition(".ctor", ConstructorAttributes, voidType);
constructor.Parameters.Add(new ParameterDefinition("objectInstance", ParameterAttributes.None, objectType));
constructor.Parameters.Add(new ParameterDefinition("functionPtr", ParameterAttributes.None, nativeIntType));
constructor.ImplAttributes = MethodImplAttributes.Runtime;
AddMethod(constructor);
}
private void AddBeginInvoke(IReadOnlyList<TypeReference> arguments)
{
var beginInvoke = new MethodDefinition("BeginInvoke", DelegateMethodAttributes, asyncResultType);
foreach (var argument in arguments)
{
beginInvoke.Parameters.Add(new ParameterDefinition(argument));
}
beginInvoke.Parameters.Add(new ParameterDefinition("callback", ParameterAttributes.None, asyncCallbackType));
beginInvoke.Parameters.Add(new ParameterDefinition("object", ParameterAttributes.None, objectType));
beginInvoke.ImplAttributes = MethodImplAttributes.Runtime;
AddMethod(beginInvoke);
}
private void AddEndInvoke(TypeReference returnType)
{
var endInvoke = new MethodDefinition("EndInvoke", DelegateMethodAttributes, returnType);
endInvoke.Parameters.Add(new ParameterDefinition("result", ParameterAttributes.None, asyncResultType));
endInvoke.ImplAttributes = MethodImplAttributes.Runtime;
AddMethod(endInvoke);
}
private void AddInvoke(TypeReference returnType, IReadOnlyList<TypeReference> arguments)
{
var invoke = new MethodDefinition("Invoke", DelegateMethodAttributes, returnType);
foreach (var argument in arguments)
{
invoke.Parameters.Add(new ParameterDefinition(argument));
}
invoke.ImplAttributes = MethodImplAttributes.Runtime;
AddMethod(invoke);
}
}
}