forked from amos402/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodcaller.tt
More file actions
368 lines (326 loc) · 10.6 KB
/
Copy pathmethodcaller.tt
File metadata and controls
368 lines (326 loc) · 10.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#
const int maxArgsCount = 16;
string[] actionGenerics = new string[maxArgsCount];
for (int i = 0; i < maxArgsCount; i++)
{
string[] paramStrs = new string[i + 1];
paramStrs[0] = "Cls";
for (int j = 0; j < i; j++)
{
paramStrs[j + 1] = string.Format("T{0}", j + 1);
}
string s = string.Join(",", paramStrs);
actionGenerics[i] = s;
}
#>
using System;
using System.Reflection;
using Python.Runtime.Binder;
namespace Python.Runtime.Method
{
interface IMethodCaller
{
bool IsStatic { get; }
bool CheckSelf(IntPtr self);
bool Check(IntPtr args, int start);
IntPtr Call(IntPtr self, IntPtr args, int start);
}
abstract class StaticMethodCaller
{
public bool IsStatic => true;
public bool CheckSelf(IntPtr self)
{
throw new NotImplementedException();
}
}
abstract class BoundMethodCaller<Cls>
{
public bool IsStatic => false;
public bool CheckSelf(IntPtr self)
{
return TypeTraits<Cls>.Is(self);
}
}
static class ActionCallerCreator
{
public static Func<Type[], Type>[] CreateDelgates = new Func<Type[], Type>[]
{
<# for (int i = 0; i < maxArgsCount; i++)
{
#>
CreateActionCaller_<#= i #>,
<#
}
#>
};
<# for (int i = 0; i < maxArgsCount; i++)
{
string comma = new string(',', i);
#>
static Type CreateActionCaller_<#= i #>(Type[] types)
{
Type genericType = typeof(ActionMethodCaller<<#= comma #>>);
return genericType.MakeGenericType(types);
}
<#
}
#>
}
static class ActionStaticCallerCreator
{
public static Func<Type[], Type>[] CreateDelgates = new Func<Type[], Type>[]
{
<# for (int i = 0; i < maxArgsCount; i++)
{
#>
CreateActionCaller_<#= i #>,
<#
}
#>
};
<# for (int i = 0; i < maxArgsCount; i++)
{
string comma = i == 0 ? string.Empty : "<" + new string(',', i - 1) + ">";
#>
static Type CreateActionCaller_<#= i #>(Type[] types)
{
<#
if (i == 0)
{
#>
return typeof(ActionStaticMethodCaller);
<#
}
else
{
#>
Type genericType = typeof(ActionStaticMethodCaller<#= comma #>);
return genericType.MakeGenericType(types);
<#
}
#>
}
<#
}
#>
}
static class FuncCallerCreator
{
public static Func<Type[], Type>[] CreateDelgates = new Func<Type[], Type>[]
{
<# for (int i = 0; i < maxArgsCount; i++)
{
#>
CreateFuncCaller_<#= i #>,
<#
}
#>
};
<# for (int i = 0; i < maxArgsCount; i++)
{
string comma = new string(',', i + 1);
#>
static Type CreateFuncCaller_<#= i #>(Type[] types)
{
Type genericType = typeof(FuncMethodCaller<<#= comma #>>);
return genericType.MakeGenericType(types);
}
<#
}
#>
}
static class FuncStaticCallerCreator
{
public static Func<Type[], Type>[] CreateDelgates = new Func<Type[], Type>[]
{
<# for (int i = 0; i < maxArgsCount; i++)
{
#>
CreateFuncCaller_<#= i #>,
<#
}
#>
};
<# for (int i = 0; i < maxArgsCount; i++)
{
string comma = "<" + new string(',', i) + ">";
#>
static Type CreateFuncCaller_<#= i #>(Type[] types)
{
Type genericType = typeof(FuncStaticMethodCaller<#= comma #>);
return genericType.MakeGenericType(types);
}
<#
}
#>
}
<#
for (int i = 0; i < maxArgsCount; i++)
{
string[] paramStrs = new string[i + 1];
paramStrs[0] = "Cls";
for (int j = 0; j < i; j++)
{
paramStrs[j + 1] = string.Format("T{0}", j + 1);
}
string genericParam = string.Join(", ", paramStrs);
string staticArgs = string.Join(", ", paramStrs.Skip(1).ToArray());
string staticGenericParam = staticArgs == string.Empty ? "" : "<" + staticArgs + ">";
string checkParams = string.Join(", ", paramStrs.Skip(1).ToArray());
#>
class ActionMethodCaller<<#= genericParam #>> : BoundMethodCaller<Cls>, IMethodCaller
{
private readonly Action<<#= genericParam #>> _action;
public ActionMethodCaller(MethodInfo info)
{
_action = (Action<<#= genericParam #>>)Delegate.CreateDelegate(typeof(Action<<#= genericParam #>>), info);
}
public bool Check(IntPtr args, int start)
{
return <#= checkParams == string.Empty ? "true" : string.Format("TypeCheck.Check<{0}>(args, start)", checkParams) #>;
}
public IntPtr Call(IntPtr self, IntPtr args, int start)
{
Cls clrObj = ValueConverter<Cls>.Get(self);
<#
string[] callParams = new string[i + 1];
callParams[0] = "clrObj";
PushIndent(" ");
for (int j = 0; j < i; j++)
{
int index = j + 1;
string s = string.Format("T{0} arg_{1} = ArgParser.Extract<T{2}>(args, start++);",
index, index, index);
WriteLine(s);
callParams[index] = string.Format("arg_{0}", index);
}
WriteLine("_action({0});", string.Join(", ", callParams));
PopIndent();
#>
Runtime.Py_IncRef(Runtime.PyNone);
return Runtime.PyNone;
}
}
class ActionStaticMethodCaller<#= staticGenericParam #> : StaticMethodCaller, IMethodCaller
{
private readonly Action<#= staticGenericParam #> _action;
public ActionStaticMethodCaller(MethodInfo info)
{
_action = (Action<#= staticGenericParam #>)Delegate.CreateDelegate(typeof(Action<#= staticGenericParam #>), info);
}
public bool Check(IntPtr args, int start)
{
return <#= checkParams == string.Empty ? "true" : string.Format("TypeCheck.Check<{0}>(args, start)", checkParams) #>;
}
public IntPtr Call(IntPtr self, IntPtr args, int start)
{
<#
callParams = new string[i];
PushIndent(" ");
for (int j = 0; j < i; j++)
{
int index = j + 1;
string s = string.Format("T{0} arg_{1} = ArgParser.Extract<T{2}>(args, start++);",
index, index, index);
WriteLine(s);
callParams[j] = string.Format("arg_{0}", index);
}
WriteLine("_action({0});", string.Join(", ", callParams));
PopIndent();
#>
Runtime.Py_IncRef(Runtime.PyNone);
return Runtime.PyNone;
}
}
<#
}
#>
<#
for (int i = 0; i < maxArgsCount; i++)
{
string[] paramStrs = new string[i + 2];
paramStrs[0] = "Cls";
for (int j = 0; j < i; j++)
{
paramStrs[j + 1] = string.Format("T{0}", j + 1);
}
paramStrs[i + 1] = "TResult";
string genericParam = string.Join(", ", paramStrs);
string staticGenericParam = string.Join(", ", paramStrs.Skip(1).ToArray());
string checkParams = string.Join(", ", paramStrs.Skip(1).Take(i).ToArray());
#>
class FuncMethodCaller<<#= genericParam #>> : BoundMethodCaller<Cls>, IMethodCaller
{
private readonly Func<<#= genericParam #>> _func;
public FuncMethodCaller(MethodInfo info)
{
_func = (Func<<#= genericParam #>>)Delegate.CreateDelegate(typeof(Func<<#= genericParam #>>), info);
}
public bool Check(IntPtr args, int start)
{
return <#= checkParams == string.Empty ? "true" : string.Format("TypeCheck.Check<{0}>(args, start)", checkParams) #>;
}
public IntPtr Call(IntPtr self, IntPtr args, int start)
{
Cls clrObj = ValueConverter<Cls>.Get(self);
<#
string[] callParams = new string[i + 1];
callParams[0] = "(Cls)clrObj";
PushIndent(" ");
for (int j = 0; j < i; j++)
{
int index = j + 1;
string s = string.Format("T{0} arg_{1} = ArgParser.Extract<T{2}>(args, start++);",
index, index, index);
WriteLine(s);
callParams[index] = string.Format("arg_{0}", index);
}
WriteLine("TResult result = _func({0});", string.Join(", ", callParams));
PopIndent();
#>
return PyValueConverter<TResult>.Convert(result);
}
}
class FuncStaticMethodCaller<<#= staticGenericParam #>> : StaticMethodCaller, IMethodCaller
{
private readonly Func<<#= staticGenericParam #>> _func;
public FuncStaticMethodCaller(MethodInfo info)
{
_func = (Func<<#= staticGenericParam #>>)Delegate.CreateDelegate(typeof(Func<<#= staticGenericParam #>>), info);
}
public bool Check(IntPtr args, int start)
{
return <#= checkParams == string.Empty ? "true" : string.Format("TypeCheck.Check<{0}>(args, start)", checkParams) #>;
}
public IntPtr Call(IntPtr self, IntPtr args, int start)
{
<#
callParams = new string[i];
PushIndent(" ");
for (int j = 0; j < i; j++)
{
int index = j + 1;
string s = string.Format("T{0} arg_{1} = ArgParser.Extract<T{2}>(args, start++);",
index, index, index);
WriteLine(s);
callParams[j] = string.Format("arg_{0}", index);
}
WriteLine("TResult result = _func({0});", string.Join(", ", callParams));
PopIndent();
#>
return PyValueConverter<TResult>.Convert(result);
}
}
<#
}
#>
}