BadScript 2
Loading...
Searching...
No Matches
BadInteropHelper.cs
Go to the documentation of this file.
1using System.Collections;
2
8
10
14public static class BadInteropHelper
15{
23 public static void SetProperty(this BadObject elem,
24 string propName,
25 BadObject value,
26 BadPropertyInfo? info = null)
27 {
28 elem.GetProperty(propName)
29 .Set(value, null, info);
30 }
31
37 public static bool CanUnwrap(this BadObject obj)
38 {
39 return obj is IBadNative;
40 }
41
49 public static object Unwrap(this BadObject obj, BadScope? caller = null)
50 {
51 if(obj is IBadString str)
52 {
53 return str.Value;
54 }
55 if(obj is IBadBoolean boolean)
56 {
57 return boolean.Value;
58 }
59 if(obj is IBadNumber num)
60 {
61 return num.Value;
62 }
63 if(obj is IBadDate d)
64 {
65 return d.Value;
66 }
67 if(obj is IBadTime t)
68 {
69 return t.Value;
70 }
71 if(obj == BadObject.Null)
72 {
73 return null!;
74 }
75 if (obj is IBadNative native)
76 {
77 return native.Value;
78 }
79
80 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}'");
81 }
82
91 public static object Unwrap(this BadObject obj, Type t, BadScope? caller = null)
92 {
93 Type oType = obj.GetType();
94
95 if (t.IsAssignableFrom(oType))
96 {
97 return obj;
98 }
99
100 if (oType.IsGenericType && oType.GetGenericTypeDefinition() == typeof(BadNullable<>))
101 {
102 Type innerType = oType.GetGenericArguments()[0];
103
104 if (obj == BadObject.Null)
105 {
106 return Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType));
107 }
108
109 return Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType),
110 obj.Unwrap(innerType, caller)
111 );
112 }
113
114 switch (obj)
115 {
116 case IBadString str when t == typeof(string):
117 return str.Value;
118 case IBadNumber num when t.IsNumericType():
119 return Convert.ChangeType(num.Value, t);
120 case IBadNative native when t.IsAssignableFrom(native.Type):
121 return native.Value;
122 case BadArray arr when t.IsArray:
123 {
124 if (t.GetArrayRank() != 1)
125 {
126 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + t);
127 }
128
129 object[] sarr = arr.InnerArray.Select(x => x.Unwrap(t.GetElementType()!, caller))
130 .ToArray();
131 Array rarr = Array.CreateInstance(t.GetElementType()!, arr.InnerArray.Count);
132
133 for (int i = 0; i < sarr.Length; i++)
134 {
135 rarr.SetValue(sarr[i], i);
136 }
137
138 return rarr;
139 }
140 case BadArray arr when t.IsGenericType &&
141 (t.GetGenericTypeDefinition() == typeof(List<>) ||
142 t.GetGenericTypeDefinition() == typeof(IList<>)):
143 {
144 Type elemType = t.GetGenericArguments()[0];
145 IEnumerable<object> sarr = arr.InnerArray.Select(x => x.Unwrap(elemType, caller));
146 IList? rarr = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elemType));
147
148 foreach (object o in sarr)
149 {
150 rarr.Add(o);
151 }
152
153 return rarr;
154 }
155 default:
156 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + t);
157 }
158 }
159
168 public static T Unwrap<T>(this BadObject obj, BadScope? caller = null)
169 {
170 if (obj is T t)
171 {
172 return t;
173 }
174
175 if (obj == BadObject.Null)
176 {
177 return default!; //Ignore
178 }
179
180 if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(BadNullable<>))
181 {
182 Type innerType = typeof(T).GetGenericArguments()[0];
183
184 if (obj == BadObject.Null)
185 {
186 return (T)Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType));
187 }
188
189 return (T)Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType),
190 obj.Unwrap(innerType, caller)
191 );
192 }
193
194 if (obj is IBadString str && typeof(T) == typeof(string))
195 {
196 return (T)(object)str.Value;
197 }
198
199 if (obj is IBadNumber num && typeof(T).IsNumericType())
200 {
201 return (T)Convert.ChangeType(num.Value, typeof(T));
202 }
203
204 if (obj is BadNative<T> n)
205 {
206 return n.Value;
207 }
208
209 if (obj is not BadArray arr)
210 {
211 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
212 }
213
214 Type type = typeof(T);
215
216 if (type.IsArray)
217 {
218 if (type.GetArrayRank() != 1)
219 {
220 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
221 }
222
223 object[] sarr = arr.InnerArray.Select(x => x.Unwrap(type.GetElementType()!, caller))
224 .ToArray();
225 Array rarr = Array.CreateInstance(type.GetElementType()!, arr.InnerArray.Count);
226
227 for (int i = 0; i < sarr.Length; i++)
228 {
229 rarr.SetValue(sarr[i], i);
230 }
231
232 return (T)(object)rarr;
233 }
234
235 if (!type.IsGenericType ||
236 (type.GetGenericTypeDefinition() != typeof(List<>) &&
237 type.GetGenericTypeDefinition() != typeof(IList<>)))
238 {
239 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
240 }
241
242 Type elemType = type.GetGenericArguments()[0];
243 IEnumerable<object> suarr = arr.InnerArray.Select(x => x.Unwrap(elemType, caller));
244 IList? ruarr = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elemType));
245
246 foreach (object o in suarr)
247 {
248 ruarr.Add(o);
249 }
250
251 return (T)ruarr;
252 }
253
259 public static bool IsFunction(this Type t)
260 {
261 if (!t.IsGenericType)
262 {
263 return false;
264 }
265
266 Type? gt = t.GetGenericTypeDefinition();
267
268 return gt == typeof(Func<>) ||
269 gt == typeof(Func<,>) ||
270 gt == typeof(Func<,,>) ||
271 gt == typeof(Func<,,,>) ||
272 gt == typeof(Func<,,,,>) ||
273 gt == typeof(Func<,,,,,>) ||
274 gt == typeof(Func<,,,,,,>) ||
275 gt == typeof(Func<,,,,,,,>) ||
276 gt == typeof(Func<,,,,,,,,>) ||
277 gt == typeof(Func<,,,,,,,,,>) ||
278 gt == typeof(Func<,,,,,,,,,,>) ||
279 gt == typeof(Func<,,,,,,,,,,,>) ||
280 gt == typeof(Func<,,,,,,,,,,,,>) ||
281 gt == typeof(Func<,,,,,,,,,,,,,>) ||
282 gt == typeof(Func<,,,,,,,,,,,,,,>) ||
283 gt == typeof(Func<,,,,,,,,,,,,,,,>) ||
284 gt == typeof(Func<,,,,,,,,,,,,,,,,>);
285 }
286
292 public static bool IsAction(this Type t)
293 {
294 //Check if type is action or func of any kind
295 if (!t.IsGenericType)
296 {
297 return t == typeof(Action);
298 }
299
300 Type? gt = t.GetGenericTypeDefinition();
301
302 return gt == typeof(Action<>) ||
303 gt == typeof(Action<,>) ||
304 gt == typeof(Action<,,>) ||
305 gt == typeof(Action<,,,>) ||
306 gt == typeof(Action<,,,,>) ||
307 gt == typeof(Action<,,,,,>) ||
308 gt == typeof(Action<,,,,,,>) ||
309 gt == typeof(Action<,,,,,,,>) ||
310 gt == typeof(Action<,,,,,,,,>) ||
311 gt == typeof(Action<,,,,,,,,,>) ||
312 gt == typeof(Action<,,,,,,,,,,>) ||
313 gt == typeof(Action<,,,,,,,,,,,>) ||
314 gt == typeof(Action<,,,,,,,,,,,,>) ||
315 gt == typeof(Action<,,,,,,,,,,,,,>) ||
316 gt == typeof(Action<,,,,,,,,,,,,,,,>);
317 }
318}
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Interop Extensions for working with the runtime api.
static T Unwrap< T >(this BadObject obj, BadScope? caller=null)
Unwraps the given object to the given type.
static bool IsFunction(this Type t)
Returns true if the given type is a Func of any kind.
static bool CanUnwrap(this BadObject obj)
Returns true if the given object can be unwrapped.
static object Unwrap(this BadObject obj, Type t, BadScope? caller=null)
Unwraps the given object to the given type.
static bool IsAction(this Type t)
Returns true if the given type is an Action of any kind.
static void SetProperty(this BadObject elem, string propName, BadObject value, BadPropertyInfo? info=null)
Sets the Property with the given name to the given value.
static object Unwrap(this BadObject obj, BadScope? caller=null)
Unwraps the given object.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:141
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
void Set(BadObject obj, BadSourcePosition? position, BadPropertyInfo? info=null, bool noChangeEvent=false)
Sets the Referenced Object to a new Value.
Stores Meta Information about a Property.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Defines properties for Native Types.
Definition IBadNative.cs:7
object Value
The Value of the Native Object.
Definition IBadNative.cs:11
Implements the Interface for Native Numbers.
Definition IBadNumber.cs:7
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains the Error Objects for the BadScript2 Language.
Contains the Extension Classes for Functions.
Contains the Interop Reflection Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
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