-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFunction.cs
More file actions
182 lines (167 loc) · 5.72 KB
/
Function.cs
File metadata and controls
182 lines (167 loc) · 5.72 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
using Fnproject.Fn.Fdk.Context;
using System;
using System.Reflection;
using System.Threading;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Fnproject.Fn.Fdk.Tests")]
namespace Fnproject.Fn.Fdk
{
// Function class which contains the metadata of the function.
// This class has a private constuctor and static members can
// initialized once.
sealed internal class Function
{
private class UserFunctionData
{
public static Type classType;
public static MethodInfo method;
public static IInvokeStrategy invokeStrategy;
public static Type contextType;
public static int dataArgPosition = Constants.UNASSIGNED_PARAM_INDEX;
public static int ctxArgPosition = Constants.UNASSIGNED_PARAM_INDEX;
private UserFunctionData() { }
}
private static ReaderWriterLock rwl = new ReaderWriterLock();
public static int ContextParameterIndex
{
get
{
int index = -1;
rwl.AcquireReaderLock(Timeout.Infinite);
index = UserFunctionData.ctxArgPosition;
rwl.ReleaseReaderLock();
return index;
}
private set { }
}
public static int DataParameterIndex
{
get
{
int index = -1;
rwl.AcquireReaderLock(Timeout.Infinite);
index = UserFunctionData.dataArgPosition;
rwl.ReleaseReaderLock();
return index;
}
private set { }
}
public static Type ClassType
{
get
{
Type t;
rwl.AcquireReaderLock(Timeout.Infinite);
t = UserFunctionData.classType;
rwl.ReleaseReaderLock();
return t;
}
private set { }
}
public static MethodInfo Method
{
get
{
MethodInfo m;
rwl.AcquireReaderLock(Timeout.Infinite);
m = UserFunctionData.method;
rwl.ReleaseReaderLock();
return m;
}
private set { }
}
public static Type ContextType
{
get
{
Type t;
rwl.AcquireReaderLock(Timeout.Infinite);
t = UserFunctionData.contextType;
rwl.ReleaseReaderLock();
return t;
}
private set { }
}
private Function() { }
public static object Invoke(object[] args)
{
return UserFunctionData.invokeStrategy.Invoke(args);
}
private static void assignContext(ParameterInfo parameter, int index)
{
UserFunctionData.ctxArgPosition = index;
if (typeof(IRuntimeContext).IsAssignableFrom(parameter.ParameterType))
{
UserFunctionData.contextType = typeof(IRuntimeContext);
}
else
{
UserFunctionData.contextType = typeof(IHTTPContext);
}
}
private static bool isContext(ParameterInfo parameter)
{
Type paramType = parameter.ParameterType;
return typeof(IRuntimeContext).IsAssignableFrom(paramType) ||
typeof(IHTTPContext).IsAssignableFrom(paramType);
}
private static void assignParameters(ParameterInfo[] parameters)
{
for (int index = 0; index < parameters.Length; index++)
{
ParameterInfo parameter = parameters[index];
if (isContext(parameter))
{
if (UserFunctionData.ctxArgPosition == Constants.UNASSIGNED_PARAM_INDEX)
{
assignContext(parameter, index);
}
else
{
throw new InvalidOperationException("Function can contain maximum 1 context");
}
}
else
{
if (UserFunctionData.dataArgPosition == Constants.UNASSIGNED_PARAM_INDEX)
{
UserFunctionData.dataArgPosition = index;
}
else
{
throw new InvalidOperationException("Function can contain maximum 1 data argument");
}
}
}
}
internal static void Initialize(Type classType, MethodInfo method)
{
rwl.AcquireWriterLock(Timeout.Infinite);
try
{
if (UserFunctionData.classType != null || UserFunctionData.method != null)
{
throw new InvalidOperationException("Value of class type or method already set");
}
UserFunctionData.classType = classType;
UserFunctionData.method = method;
UserFunctionData.invokeStrategy = InvokeStrategyFactory.Create(method, classType);
var parameters = method.GetParameters();
if (parameters.Length > Constants.MAX_ALLOWED_USER_FN_PARAMETERS)
{
throw new ArgumentException("Function cannot contains more than 2 parameters");
}
else
{
assignParameters(parameters);
}
}
catch (Exception e)
{
rwl.ReleaseWriterLock();
throw e;
}
rwl.ReleaseWriterLock();
}
}
}