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(
24 this BadObject elem,
25 string propName,
26 BadObject value,
27 BadPropertyInfo? info = null)
28 {
29 elem.GetProperty(propName).Set(value, 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 IBadNative native)
52 {
53 return native.Value;
54 }
55
56 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}'");
57 }
58
67 public static object Unwrap(this BadObject obj, Type t, BadScope? caller = null)
68 {
69 Type oType = obj.GetType();
70
71 if (t.IsAssignableFrom(oType))
72 {
73 return obj;
74 }
75
76 if (oType.IsGenericType && oType.GetGenericTypeDefinition() == typeof(BadNullable<>))
77 {
78 Type innerType = oType.GetGenericArguments()[0];
79
80 if (obj == BadObject.Null)
81 {
82 return Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType));
83 }
84
85 return Activator.CreateInstance(
86 typeof(BadNullable<>).MakeGenericType(innerType),
87 obj.Unwrap(innerType, caller)
88 );
89 }
90
91 switch (obj)
92 {
93 case IBadString str when t == typeof(string):
94 return str.Value;
95 case IBadNumber num when t.IsNumericType():
96 return Convert.ChangeType(num.Value, t);
97 case IBadNative native when t.IsAssignableFrom(native.Type):
98 return native.Value;
99 case BadArray arr when t.IsArray:
100 {
101 if (t.GetArrayRank() != 1)
102 {
103 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + t);
104 }
105
106 object[] sarr = arr.InnerArray.Select(x => x.Unwrap(t.GetElementType()!, caller)).ToArray();
107 Array rarr = Array.CreateInstance(t.GetElementType()!, arr.InnerArray.Count);
108
109 for (int i = 0; i < sarr.Length; i++)
110 {
111 rarr.SetValue(sarr[i], i);
112 }
113
114 return rarr;
115 }
116 case BadArray arr when t.IsGenericType &&
117 (t.GetGenericTypeDefinition() == typeof(List<>) || t.GetGenericTypeDefinition() == typeof(IList<>)):
118 {
119 Type elemType = t.GetGenericArguments()[0];
120 IEnumerable<object> sarr = arr.InnerArray.Select(x => x.Unwrap(elemType, caller));
121 IList? rarr = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elemType));
122
123 foreach (object o in sarr)
124 {
125 rarr.Add(o);
126 }
127
128 return rarr;
129 }
130 default:
131 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + t);
132 }
133 }
134
143 public static T Unwrap<T>(this BadObject obj, BadScope? caller = null)
144 {
145 if (obj is T t)
146 {
147 return t;
148 }
149
150
151 if (obj == BadObject.Null)
152 {
153 return default!; //Ignore
154 }
155
156 if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(BadNullable<>))
157 {
158 Type innerType = typeof(T).GetGenericArguments()[0];
159
160 if (obj == BadObject.Null)
161 {
162 return (T)Activator.CreateInstance(typeof(BadNullable<>).MakeGenericType(innerType));
163 }
164
165 return (T)Activator.CreateInstance(
166 typeof(BadNullable<>).MakeGenericType(innerType),
167 obj.Unwrap(innerType, caller)
168 );
169 }
170
171 if (obj is IBadString str && typeof(T) == typeof(string))
172 {
173 return (T)(object)str.Value;
174 }
175
176 if (obj is IBadNumber num && typeof(T).IsNumericType())
177 {
178 return (T)Convert.ChangeType(num.Value, typeof(T));
179 }
180
181 if (obj is BadNative<T> n)
182 {
183 return n.Value;
184 }
185
186 if (obj is not BadArray arr)
187 {
188 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
189 }
190
191 Type type = typeof(T);
192
193 if (type.IsArray)
194 {
195 if (type.GetArrayRank() != 1)
196 {
197 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
198 }
199
200 object[] sarr = arr.InnerArray.Select(x => x.Unwrap(type.GetElementType()!, caller)).ToArray();
201 Array rarr = Array.CreateInstance(type.GetElementType()!, arr.InnerArray.Count);
202
203 for (int i = 0; i < sarr.Length; i++)
204 {
205 rarr.SetValue(sarr[i], i);
206 }
207
208 return (T)(object)rarr;
209 }
210
211 if (!type.IsGenericType ||
212 type.GetGenericTypeDefinition() != typeof(List<>) &&
213 type.GetGenericTypeDefinition() != typeof(IList<>))
214 {
215 throw BadRuntimeException.Create(caller, $"Can not unwrap object '{obj}' to type " + typeof(T));
216 }
217
218 Type elemType = type.GetGenericArguments()[0];
219 IEnumerable<object> suarr = arr.InnerArray.Select(x => x.Unwrap(elemType, caller));
220 IList? ruarr = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elemType));
221
222 foreach (object o in suarr)
223 {
224 ruarr.Add(o);
225 }
226
227 return (T)ruarr;
228 }
229
235 public static bool IsFunction(this Type t)
236 {
237 if (!t.IsGenericType)
238 {
239 return false;
240 }
241
242 Type? gt = t.GetGenericTypeDefinition();
243
244 return gt == typeof(Func<>) ||
245 gt == typeof(Func<,>) ||
246 gt == typeof(Func<,,>) ||
247 gt == typeof(Func<,,,>) ||
248 gt == typeof(Func<,,,,>) ||
249 gt == typeof(Func<,,,,,>) ||
250 gt == typeof(Func<,,,,,,>) ||
251 gt == typeof(Func<,,,,,,,>) ||
252 gt == typeof(Func<,,,,,,,,>) ||
253 gt == typeof(Func<,,,,,,,,,>) ||
254 gt == typeof(Func<,,,,,,,,,,>) ||
255 gt == typeof(Func<,,,,,,,,,,,>) ||
256 gt == typeof(Func<,,,,,,,,,,,,>) ||
257 gt == typeof(Func<,,,,,,,,,,,,,>) ||
258 gt == typeof(Func<,,,,,,,,,,,,,,>) ||
259 gt == typeof(Func<,,,,,,,,,,,,,,,>) ||
260 gt == typeof(Func<,,,,,,,,,,,,,,,,>);
261 }
262
268 public static bool IsAction(this Type t)
269 {
270 //Check if type is action or func of any kind
271 if (!t.IsGenericType)
272 {
273 return t == typeof(Action);
274 }
275
276 Type? gt = t.GetGenericTypeDefinition();
277
278 return gt == typeof(Action<>) ||
279 gt == typeof(Action<,>) ||
280 gt == typeof(Action<,,>) ||
281 gt == typeof(Action<,,,>) ||
282 gt == typeof(Action<,,,,>) ||
283 gt == typeof(Action<,,,,,>) ||
284 gt == typeof(Action<,,,,,,>) ||
285 gt == typeof(Action<,,,,,,,>) ||
286 gt == typeof(Action<,,,,,,,,>) ||
287 gt == typeof(Action<,,,,,,,,,>) ||
288 gt == typeof(Action<,,,,,,,,,,>) ||
289 gt == typeof(Action<,,,,,,,,,,,>) ||
290 gt == typeof(Action<,,,,,,,,,,,,>) ||
291 gt == typeof(Action<,,,,,,,,,,,,,>) ||
292 gt == typeof(Action<,,,,,,,,,,,,,,,>);
293 }
294}
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
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:129
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
void Set(BadObject obj, BadPropertyInfo? info=null, bool noChangeEvent=false)
Sets the Referenced Object to a new Value.
Stores Meta Information about a Property.
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