forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegateFactory.tt
More file actions
208 lines (172 loc) · 7.6 KB
/
DelegateFactory.tt
File metadata and controls
208 lines (172 loc) · 7.6 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".Generated.cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<# const int maxArgCount = 16; #>
using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.ClearScript
{
internal static partial class DelegateFactory
{
private const int maxArgCount = <#= maxArgCount #>;
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeParamList = (count == 0) ? "<TDelegate>" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "T" + index)) + ", TDelegate>";
var allByValueExpr = (count == 0) ? "true" : "GetAllByValue(" + string.Join(", ", Enumerable.Range(1, count).Select(index => "typeof(T" + index + ")")) + ")";
var methodParamList = string.Join(", ", Enumerable.Range(1, count).Select(index => "T" + index + " a" + index));
var argList = string.Join(", ", Enumerable.Range(1, count).Select(index => "a" + index));
var varDeclList = string.Join("\r\n ", Enumerable.Range(1, count).Select(index => "var v" + index + " = GetArgValue(a" + index + ");"));
var byRefArgList = string.Join(", ", Enumerable.Range(1, count).Select(index => "ref v" + index));
var varAssignList = string.Join("\r\n ", Enumerable.Range(1, count).Select(index => "SetArgValue(a" + index + ", v" + index + ");"));
#>
[ExcludeFromCodeCoverage]
private class ProcShim<#= typeParamList #> : ProcShim
{
private static readonly MethodInfo method = typeof(ProcShim<#= typeParamList #>).GetMethod("InvokeTarget");
private static readonly bool allByValue = <#= allByValueExpr #>;
private readonly object target;
private readonly Delegate del;
public ProcShim(ScriptEngine engine, object target)
: base(engine)
{
this.target = GetCompatibleTarget(typeof(TDelegate), target);
del = Delegate.CreateDelegate(typeof(TDelegate), this, method);
}
// ReSharper disable UnusedMember.Local
public void InvokeTarget(<#= methodParamList #>)
{
if (allByValue || Engine.EnableAutoHostVariables)
{
Invoke(() => ((dynamic)target)(<#= argList #>));
}
else
{
<#= varDeclList #>
try
{
Invoke(() => ((dynamic)target)(<#= byRefArgList #>));
}
finally
{
<#= varAssignList #>
}
}
}
// ReSharper restore UnusedMember.Local
#region DelegateShim overrides
public override Delegate Delegate
{
get { return del; }
}
#endregion
}
<#
}
#>
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeParamList = (count == 0) ? "<TResult, TDelegate>" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "T" + index)) + ", TResult, TDelegate>";
var allByValueExpr = (count == 0) ? "true" : "GetAllByValue(" + string.Join(", ", Enumerable.Range(1, count).Select(index => "typeof(T" + index + ")")) + ")";
var methodParamList = string.Join(", ", Enumerable.Range(1, count).Select(index => "T" + index + " a" + index));
var argList = string.Join(", ", Enumerable.Range(1, count).Select(index => "a" + index));
var varDeclList = string.Join("\r\n ", Enumerable.Range(1, count).Select(index => "var v" + index + " = GetArgValue(a" + index + ");"));
var byRefArgList = string.Join(", ", Enumerable.Range(1, count).Select(index => "ref v" + index));
var varAssignList = string.Join("\r\n ", Enumerable.Range(1, count).Select(index => "SetArgValue(a" + index + ", v" + index + ");"));
#>
[ExcludeFromCodeCoverage]
private class FuncShim<#= typeParamList #> : FuncShim<TResult>
{
private static readonly MethodInfo method = typeof(FuncShim<#= typeParamList #>).GetMethod("InvokeTarget");
private static readonly bool allByValue = <#= allByValueExpr #>;
private readonly object target;
private readonly Delegate del;
public FuncShim(ScriptEngine engine, object target)
: base(engine)
{
this.target = GetCompatibleTarget(typeof(TDelegate), target);
del = Delegate.CreateDelegate(typeof(TDelegate), this, method);
}
// ReSharper disable UnusedMember.Local
public TResult InvokeTarget(<#= methodParamList #>)
{
if (allByValue || Engine.EnableAutoHostVariables)
{
return Invoke(() => (TResult)((dynamic)target)(<#= argList #>));
}
<#= varDeclList #>
try
{
return Invoke(() => (TResult)((dynamic)target)(<#= byRefArgList #>));
}
finally
{
<#= varAssignList #>
}
}
// ReSharper restore UnusedMember.Local
#region DelegateShim overrides
public override Delegate Delegate
{
get { return del; }
}
#endregion
}
<#
}
#>
private static readonly Type[] procTemplates =
{
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeArgList = (count == 0) ? "" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "/*T" + index + "*/")) + ">";
#>
typeof(Action<#= typeArgList #>),
<#
}
#>
};
private static readonly Type[] funcTemplates =
{
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeArgList = (count == 0) ? "</*TResult*/>" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "/*T" + index + "*/")) + ", /*TResult*/>";
#>
typeof(Func<#= typeArgList #>),
<#
}
#>
};
private static readonly Type[] procShimTemplates =
{
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeArgList = (count == 0) ? "</*TDelegate*/>" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "/*T" + index + "*/")) + ", /*TDelegate*/>";
#>
typeof(ProcShim<#= typeArgList #>),
<#
}
#>
};
private static readonly Type[] funcShimTemplates =
{
<#
for (var count = 0; count <= maxArgCount; count++)
{
var typeArgList = (count == 0) ? "</*TResult*/, /*TDelegate*/>" : "<" + string.Join(", ", Enumerable.Range(1, count).Select(index => "/*T" + index + "*/")) + ", /*TResult*/, /*TDelegate*/>";
#>
typeof(FuncShim<#= typeArgList #>),
<#
}
#>
};
}
}