BadScript 2
Loading...
Searching...
No Matches
BadDynamicFunctionExtensions_Func.cs
Go to the documentation of this file.
5
7
8public static partial class BadDynamicFunctionExtensions
9{
15 private static BadClassPrototype? TryConvertType(Type t)
16 {
17 if (t == typeof(string))
18 {
19 return BadNativeClassBuilder.GetNative("string");
20 }
21
22 if (t == typeof(bool))
23 {
24 return BadNativeClassBuilder.GetNative("bool");
25 }
26
27 if (t.IsNumericType())
28 {
29 return BadNativeClassBuilder.GetNative("num");
30 }
31
32 if (t.IsArray ||
33 (t.IsGenericType &&
34 (t.GetGenericTypeDefinition() == typeof(List<>) || t.GetGenericTypeDefinition() == typeof(IList<>))))
35 {
36 return BadNativeClassBuilder.GetNative("Array");
37 }
38
39 //if (t.IsFunction() || t.IsAction()) return BadNativeClassBuilder.GetNative("Function");
40
41 return null;
42 }
48 private static BadFunctionParameter[] GetParameters(params Type[] t)
49 {
50 List<BadFunctionParameter> ret = new List<BadFunctionParameter>();
51 bool canBeOptional = true;
52
53 for (int i = t.Length - 1; i >= 0; i--)
54 {
55 Type type = t[i];
56 bool isNullable = false;
57
58 if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(BadNullable<>))
59 {
60 isNullable = true;
61 type = type.GetGenericArguments()[0];
62 }
63
64 bool optional = canBeOptional && isNullable;
65 bool nullChecked = !isNullable;
66 BadClassPrototype? bType = TryConvertType(type);
67 ret.Insert(0, new BadFunctionParameter(type.Name, optional, nullChecked, false, null, bType));
68
69 if (!isNullable)
70 {
71 canBeOptional = false;
72 }
73 }
74
75 return ret.ToArray();
76 }
77
85 public static void SetFunction(this BadObject elem,
86 string propName,
87 Func<BadExecutionContext, BadObject> func,
88 BadClassPrototype returnType)
89 {
90 elem.SetProperty(propName,
91 new BadDynamicInteropFunction(propName, func, returnType),
93 );
94 }
95
104 public static void SetFunction<T>(this BadObject elem,
105 string propName,
106 Func<BadExecutionContext, T, BadObject> func,
107 BadClassPrototype returnType)
108 {
109 var parameters = GetParameters(typeof(T));
110 elem.SetProperty(propName,
111 new BadDynamicInteropFunction<T>(propName, func, returnType, parameters[0]),
113 );
114 }
115
125 public static void SetFunction<T1, T2>(this BadObject elem,
126 string propName,
127 Func<BadExecutionContext, T1, T2, BadObject> func,
128 BadClassPrototype returnType)
129 {
130 var parameters = GetParameters(typeof(T1), typeof(T2));
131 elem.SetProperty(propName,
133 func,
134 returnType,
135 parameters[0], parameters[1]
136 )
137 );
138 }
139
150 public static void SetFunction<T1, T2, T3>(this BadObject elem,
151 string propName,
152 Func<BadExecutionContext, T1, T2, T3, BadObject> func,
153 BadClassPrototype returnType)
154 {
155 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3));
156 elem.SetProperty(propName,
158 func,
159 returnType,
160 parameters[0], parameters[1], parameters[2]
161 )
162 );
163 }
164
176 public static void SetFunction<T1, T2, T3, T4>(this BadObject elem,
177 string propName,
178 Func<BadExecutionContext, T1, T2, T3, T4, BadObject> func,
179 BadClassPrototype returnType)
180 {
181 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4));
182 elem.SetProperty(propName,
184 func,
185 returnType,
186 parameters[0], parameters[1], parameters[2], parameters[3]
187 )
188 );
189 }
190
203 public static void SetFunction<T1, T2, T3, T4, T5>(this BadObject elem,
204 string propName,
205 Func<BadExecutionContext, T1, T2, T3, T4, T5, BadObject> func,
206 BadClassPrototype returnType)
207 {
208 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
209 elem.SetProperty(propName,
211 func,
212 returnType,
213 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]
214 )
215 );
216 }
217
231 public static void SetFunction<T1, T2, T3, T4, T5, T6>(this BadObject elem,
232 string propName,
233 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, BadObject>
234 func,
235 BadClassPrototype returnType)
236 {
237 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
238 elem.SetProperty(propName,
240 func,
241 returnType,
242 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5]
243 )
244 );
245 }
246
262 string propName,
263 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7,
264 BadObject> func,
265 BadClassPrototype returnType)
266 {
267 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
268 elem.SetProperty(propName,
270 func,
271 returnType,
272 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6]
273 )
274 );
275 }
276
293 string propName,
294 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7,
295 T8, BadObject> func,
296 BadClassPrototype returnType)
297 {
298 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
299 elem.SetProperty(propName,
301 func,
302 returnType,
303 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7]
304 )
305 );
306 }
307
325 string propName,
326 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6,
327 T7, T8, T9, BadObject> func,
328 BadClassPrototype returnType)
329 {
330 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
331 elem.SetProperty(propName,
333 func,
334 returnType,
335 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8]
336 )
337 );
338 }
339
358 string propName,
359 Func<BadExecutionContext, T1, T2, T3, T4, T5
360 , T6, T7, T8, T9, T10, BadObject> func,
361 BadClassPrototype returnType)
362 {
363 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
364 elem.SetProperty(propName,
366 func,
367 returnType,
368 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9]
369 )
370 );
371 }
372
392 string propName,
393 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, BadObject> func,
394 BadClassPrototype returnType)
395 {
396 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11));
397 elem.SetProperty(propName,
399 func,
400 returnType,
401 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9], parameters[10]
402 )
403 );
404 }
405
426 string propName,
427 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, BadObject> func,
428 BadClassPrototype returnType)
429 {
430 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12));
431 elem.SetProperty(propName,
433 func,
434 returnType,
435 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9], parameters[10], parameters[11]
436 )
437 );
438 }
439
461 string propName,
462 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, BadObject> func,
463 BadClassPrototype returnType)
464 {
465 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13));
466 elem.SetProperty(propName,
468 func,
469 returnType,
470 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9], parameters[10], parameters[11], parameters[12]
471 )
472 );
473 }
474
497 string propName,
498 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, BadObject> func,
499 BadClassPrototype returnType)
500 {
501 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14));
502 elem.SetProperty(propName,
503 new BadDynamicInteropFunction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,
504 T14>(propName,
505 func,
506 returnType,
507 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9], parameters[10], parameters[11], parameters[12], parameters[13]
508 )
509 );
510 }
511
535 this BadObject elem,
536 string propName,
537 Func<BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, BadObject> func,
538 BadClassPrototype returnType)
539 {
540 var parameters = GetParameters(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14), typeof(T15));
541 elem.SetProperty(propName,
542 new BadDynamicInteropFunction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,
543 T15>(propName,
544 func,
545 returnType,
546 parameters[0], parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], parameters[7], parameters[8], parameters[9], parameters[10], parameters[11], parameters[12], parameters[13], parameters[14]
547 )
548 );
549 }
550}
The Execution Context. Every execution of a script needs a context the script is running in....
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2 > func)
Sets a Function on the given Object.
static void SetFunction(this BadObject elem, string propName, Func< BadExecutionContext, BadObject > func, BadClassPrototype returnType)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7 > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 > func)
Sets a Function on the given Object.
static void SetFunction< T >(this BadObject elem, string propName, Action< BadExecutionContext, T > func)
Sets a Function on the given Object.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 > func)
Sets a Function on the given Object.
static BadFunctionParameter[] GetParameters(params Type[] t)
Returns a list of BadFunctionParameters for the given Types.
static void SetFunction< T1, T2, T3, T4, T5, T6 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6 > func)
Sets a Function on the given Object.
static ? BadClassPrototype TryConvertType(Type t)
Tries to convert a Type to a BadClassPrototype.
static void SetFunction< T1, T2, T3, T4, T5, T6, T7, T8 >(this BadObject elem, string propName, Action< BadExecutionContext, T1, T2, T3, T4, T5, T6, T7, T8 > func)
Sets a Function on the given Object.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Stores Meta Information about a Property.
Implements a function that can be called from the script.
static readonly BadClassPrototype Prototype
The Prototype for the Function Object.
Implements a Class Prototype for the BadScript Language.
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Contains the Extension Classes for Functions.
Contains the Interop Reflection Classes for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
This is a helper type that can be used when using the .SetFunction extensions to allow for nullable p...
Definition BadNullable.cs:8