BadScript 2
Loading...
Searching...
No Matches
BadNativeClassBuilder.cs
Go to the documentation of this file.
9
11
15public static class BadNativeClassBuilder
16{
20 public static readonly BadInterfacePrototype Disposable =
21 new BadInterfacePrototype("IDisposable", (typeArgs) => Array.Empty<BadInterfacePrototype>(), null, DisposableConstraints, Array.Empty<string>());
22 public static readonly BadInterfacePrototype Attribute =
23 new BadInterfacePrototype("IAttribute", (typeArgs) => Array.Empty<BadInterfacePrototype>(), null, objects => Array.Empty<BadInterfaceConstraint>(), Array.Empty<string>());
25 new BadInterfacePrototype("IInitializeAttribute", (typeArgs) => new []{Attribute}, null,
26 InitializeAttributeConstraints, Array.Empty<string>());
27
28 public static readonly BadInterfacePrototype MemberChangeEventArgs = new BadInterfacePrototype("IMemberChangeEventArgs",
29 (_) => Array.Empty<BadInterfacePrototype>(), null, MemberChangeEventArgsConstraints, Array.Empty<string>());
30
31 public static readonly BadInterfacePrototype MemberChangedEventArgs = new BadInterfacePrototype("IMemberChangedEventArgs",
32 (_) => new []{MemberChangedEventArgs}, null, (_) => Array.Empty<BadInterfaceConstraint>(), Array.Empty<string>());
33
34 public static readonly BadInterfacePrototype MemberChangingEventArgs = new BadInterfacePrototype("IMemberChangingEventArgs",
35 (_) => new []{MemberChangedEventArgs}, null, MemberChangingEventArgsConstraints, Array.Empty<string>());
36
38 {
39 return new BadInterfaceConstraint[]
40 {
42 };
43 }
44
55
56 public static readonly BadInterfacePrototype ChangeAttribute =
57 new BadInterfacePrototype("IChangeAttribute", (typeArgs) => new []{Attribute}, null,
58 ChangeAttributeConstraints, Array.Empty<string>());
59 public static readonly BadInterfacePrototype ChangedAttribute =
60 new BadInterfacePrototype("IChangedAttribute", (typeArgs) => new []{Attribute}, null,
61 ChangedAttributeConstraints, Array.Empty<string>());
62
64 {
65 return new BadInterfaceConstraint[]
66 {
68 new[]
69 {
70 new BadFunctionParameter("instance", false, true, false, null, BadAnyPrototype.Instance),
71 new BadFunctionParameter("member", false, true, false, null, BadMemberInfo.Prototype)
72 })
73 };
74 }
76 {
77 return new BadInterfaceConstraint[]
78 {
80 new[]
81 {
82 new BadFunctionParameter("eventArgs", false, true, false, null, MemberChangingEventArgs),
83 })
84 };
85 }
87 {
88 return new BadInterfaceConstraint[]
89 {
91 new[]
92 {
93 new BadFunctionParameter("eventArgs", false, true, false, null, MemberChangedEventArgs),
94 })
95 };
96 }
97
101 public static readonly BadInterfacePrototype Enumerable =
102 new BadInterfacePrototype("IEnumerable", (typeArgs) => Array.Empty<BadInterfacePrototype>(), null, EnumerableConstraints, new []{"T"});
103
107 public static readonly BadInterfacePrototype Enumerator =
108 new BadInterfacePrototype("IEnumerator", (typeArgs) => Array.Empty<BadInterfacePrototype>(), null, EnumeratorConstraints, new []{"T"});
109
113 public static readonly BadInterfacePrototype ArrayLike =
114 new BadInterfacePrototype("IArray", (typeArgs) => new[] { (BadInterfacePrototype)Enumerable.CreateGeneric(typeArgs) }, null, ArrayConstraints, new []{"T"});
115
116 public static readonly BadInterfacePrototype ImportHandler =
117 new BadInterfacePrototype("IImportHandler", (typeArgs) => Array.Empty<BadInterfacePrototype>(), null, ImportHandlerConstraints, Array.Empty<string>());
118
119 private static readonly Dictionary<string, BadObjectReference> s_NumberStaticMembers = new Dictionary<string, BadObjectReference>
120 {
121 {
122 "Parse", BadObjectReference.Make(
123 "num.Parse",
124 () => new BadInteropFunction(
125 "Parse",
127 true,
128 GetNative("num"),
129 new BadFunctionParameter("str", false, true, false, null, GetNative("string"))
130 )
131 )
132 }
133 };
134
135 private static readonly Dictionary<string, BadObjectReference> s_BooleanStaticMembers = new Dictionary<string, BadObjectReference>
136 {
137 {
138 "Parse", BadObjectReference.Make(
139 "bool.Parse",
140 () => new BadInteropFunction(
141 "Parse",
143 true,
144 GetNative("bool"),
145 new BadFunctionParameter("str", false, true, false, null, GetNative("string"))
146 )
147 )
148 }
149 };
150
151 private static readonly Dictionary<string, BadObjectReference> s_StringStaticMembers = new Dictionary<string, BadObjectReference>
152 {
153 { "Empty", BadObjectReference.Make("string.Empty", () => BadString.Empty) },
154 { "IsNullOrEmpty" ,BadObjectReference.Make(
155 "string.IsNullOrEmpty",
156 () => new BadInteropFunction(
157 "IsNullOrEmpty",
159 true,
160 GetNative("bool"),
161 new BadFunctionParameter("str", false, false, false, null, GetNative("string"))
162 )
163 )}
164 };
165
166
168 {
169 if (arg[0] is not IBadString str)
170 {
171 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
172 }
173
174 return string.IsNullOrEmpty(str.Value);
175 }
177 {
178 if (arg[0] is not IBadString str)
179 {
180 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
181 }
182
183 if (decimal.TryParse(str.Value, out decimal d))
184 {
185 return d;
186 }
187
188 throw BadRuntimeException.Create(ctx.Scope, $"The Supplied String '{str.Value}' is not a valid number");
189 }
190
192 {
193 if (arg[0] is not IBadString str)
194 {
195 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
196 }
197
198 if (bool.TryParse(str.Value, out bool d))
199 {
200 return d;
201 }
202
203 throw BadRuntimeException.Create(ctx.Scope, $"The Supplied String '{str.Value}' is not a valid boolean");
204 }
205
209 private static readonly List<BadClassPrototype> s_NativeTypes = new List<BadClassPrototype>
210 {
213 CreateNativeType<BadString>("string", s_StringStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
214 CreateNativeType<BadBoolean>("bool", s_BooleanStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
215 CreateNativeType<BadNumber>("num", s_NumberStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
216 CreateNativeType<BadFunction>("Function", () => Array.Empty<BadInterfacePrototype>()),
217 CreateNativeType<BadArray>("Array", () => new []{(BadInterfacePrototype)ArrayLike.CreateGeneric(new []{BadAnyPrototype.Instance})}),
218 CreateNativeType<BadTable>("Table", () => new []{(BadInterfacePrototype)Enumerable.CreateGeneric(new []{BadAnyPrototype.Instance})}),
223 //TODO: Make Native Class Builder non static
224 //(adding this prototype will cause a type initializer exception because of a circular reference due to a tightly packed static initializer list)
225 //BadFunction.Prototype,
229 ArrayLike,
231 Attribute,
238 };
239
243 public static IEnumerable<BadClassPrototype> NativeTypes => s_NativeTypes;
244
250 {
251 return new BadInterfaceConstraint[]
252 {
253 new BadInterfaceFunctionConstraint("Dispose", null, Array.Empty<BadFunctionParameter>()),
254 };
255 }
256
262 {
263 return new BadInterfaceConstraint[]
264 {
265 new BadInterfaceFunctionConstraint("GetEnumerator", null, (BadClassPrototype)Enumerator.CreateGeneric(typeParams), Array.Empty<BadFunctionParameter>()),
266 };
267 }
268
269
271 {
272 return new BadInterfaceConstraint[]
273 {
275 "GetHash",
276 null,
277 GetNative("string"),
278 new[]
279 {
280 new BadFunctionParameter("path", false, true, false, null, GetNative("string")),
281 }
282 ),
284 "Has",
285 null,
286 GetNative("bool"),
287 new[]
288 {
289 new BadFunctionParameter("path", false, true, false, null, GetNative("string")),
290 }
291 ),
293 "Get",
294 null,
296 new[]
297 {
298 new BadFunctionParameter("path", false, true, false, null, GetNative("string")),
299 }
300 ),
302 "IsTransient",
303 null,
304 GetNative("bool"),
305 Array.Empty<BadFunctionParameter>()
306 ),
307 };
308 }
309
314 private static BadInterfaceConstraint[] ArrayConstraints(BadObject[] typeParams)
315 {
316 BadClassPrototype? listType = typeParams.Length > 0 ? (BadClassPrototype)typeParams[0] : BadAnyPrototype.Instance;
317 return new BadInterfaceConstraint[]
318 {
319 //Add(any elem);
320 new BadInterfaceFunctionConstraint("Add", null, new[] { new BadFunctionParameter("elem", false, false, false, null, listType) }),
321
322 //AddRange(IEnumerable elems);
323 new BadInterfaceFunctionConstraint("AddRange", null, new[] { new BadFunctionParameter("elems", false, false, false, null, (BadClassPrototype)Enumerable.CreateGeneric(typeParams)) }),
324
325 //Clear();
326 new BadInterfaceFunctionConstraint("Clear", null, Array.Empty<BadFunctionParameter>()),
327
328 //Insert(num index, any elem);
330 "Insert",
331 null,
332 new[]
333 {
334 new BadFunctionParameter("index", false, false, false, null, GetNative("num")),
335 new BadFunctionParameter("elem", false, false, false, null, listType),
336 }
337 ),
338
339 //InsertRange(num index, IEnumerable elems);
341 "InsertRange",
342 null,
343 new[]
344 {
345 new BadFunctionParameter("index", false, false, false, null, GetNative("num")),
346 new BadFunctionParameter("elems", false, false, false, null, (BadClassPrototype)Enumerable.CreateGeneric(typeParams)),
347 }
348 ),
349
350 //bool Remove(any elem);
351 new BadInterfaceFunctionConstraint("Remove", null, GetNative("bool"), new[] { new BadFunctionParameter("elem", false, false, false, null, listType) }),
352
353 //RemoveAt(num index);
354 new BadInterfaceFunctionConstraint("RemoveAt", null, new[] { new BadFunctionParameter("index", false, false, false, null, GetNative("num")) }),
355
356 //bool Contains(any elem);
357 new BadInterfaceFunctionConstraint("Contains", null, GetNative("bool"), new[] { new BadFunctionParameter("elem", false, false, false, null, listType) }),
358
359 //Get(num index);
360 new BadInterfaceFunctionConstraint("Get", null, listType, new[] { new BadFunctionParameter("index", false, false, false, null, GetNative("num")) }),
361
362 //Set(num index, any elem);
364 "Set",
365 null,
366 new[]
367 {
368 new BadFunctionParameter("index", false, false, false, null, GetNative("num")),
369 new BadFunctionParameter("elem", false, false, false, null, listType),
370 }
371 ),
372
373 //op_ArrayAccess(num index);
376 null,
378 new[] { new BadFunctionParameter("index", false, false, false, null, GetNative("num")) }
379 ),
380
381 //op_ArrayAccessReverse(num index);
384 null,
386 new[] { new BadFunctionParameter("index", false, false, false, null, GetNative("num")) }
387 ),
388 new BadInterfacePropertyConstraint("Length", null, GetNative("num")),
389 };
390 }
391
397 {
398 BadClassPrototype? type = (BadClassPrototype)(typeParams.Length > 0 ? typeParams[0] : BadAnyPrototype.Instance);
399 return new BadInterfaceConstraint[]
400 {
401 new BadInterfaceFunctionConstraint("MoveNext", null, GetNative("bool"), Array.Empty<BadFunctionParameter>()),
402 new BadInterfaceFunctionConstraint("GetCurrent", null, type, Array.Empty<BadFunctionParameter>()),
403 };
404 }
405
406
413 public static BadClassPrototype GetNative(string name)
414 {
415 return s_NativeTypes.FirstOrDefault(x => x.Name == name) ??
416 throw new BadRuntimeException("Native class not found");
417 }
418
423 public static void AddNative(BadClassPrototype native)
424 {
425 if (s_NativeTypes.Any(x => x.Name == native.Name))
426 {
427 return;
428 }
429
430 s_NativeTypes.Add(native);
431 }
432
433
442 private static BadNativeClassPrototype<T> Create<T>(
443 string name,
444 Func<BadObject[], BadObject> constructor,
445 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
446 {
448 name,
449 (_, a) => constructor(a),
450 interfaces
451 );
452 }
453
463 private static BadNativeClassPrototype<T> Create<T>(
464 string name,
465 Func<BadObject[], BadObject> constructor,
466 Dictionary<string, BadObjectReference> staticMembers,
467 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
468 {
470 name,
471 (_, a) => constructor(a),
472 staticMembers,
473 interfaces
474 );
475 }
476
484 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name, Func<BadInterfacePrototype[]> interfaces) where T : BadObject
485 {
486 return CreateNativeType<T>(name, BadNativeClassHelper.GetConstructor(name), interfaces);
487 }
488
489
498 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name, Dictionary<string, BadObjectReference> staticMembers, Func<BadInterfacePrototype[]> interfaces) where T : BadObject
499 {
500 return CreateNativeType<T>(name, BadNativeClassHelper.GetConstructor(name), staticMembers, interfaces);
501 }
502
503
512 private static BadNativeClassPrototype<T> CreateNativeType<T>(
513 string name,
514 Func<BadObject[], BadObject> constructor,
515 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
516 {
517 return Create<T>(name, constructor, interfaces);
518 }
519
529 private static BadNativeClassPrototype<T> CreateNativeType<T>(
530 string name,
531 Func<BadObject[], BadObject> constructor,
532 Dictionary<string, BadObjectReference> staticMembers,
533 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
534 {
535 return Create<T>(name, constructor, staticMembers, interfaces);
536 }
537}
Contains Static Data for the BadScript Language.
const string ARRAY_ACCESS_REVERSE_OPERATOR_NAME
Implements a Function Constraint for an Interface The Constraints specifies how a specific function s...
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
static BadClassPrototype Prototype
Definition BadScope.cs:125
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
static BadClassPrototype Prototype
A Class Prototype for the Scope.
Definition BadScope.cs:420
Implements the Error Object Type.
static readonly BadClassPrototype Prototype
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Interop Function taking an array of arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
Implements a Native String.
Definition BadString.cs:9
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
static readonly BadClassPrototype Prototype
The Prototype of the ClassPrototype(can not be used)
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype MemberChangedEventArgs
static BadInterfaceConstraint[] ChangedAttributeConstraints(BadObject[] arg)
static readonly BadInterfacePrototype MemberChangingEventArgs
static readonly BadInterfacePrototype Disposable
The IDisposible Interface Prototype.
static BadInterfaceConstraint[] MemberChangingEventArgsConstraints(BadObject[] arg)
static BadInterfaceConstraint[] DisposableConstraints(BadObject[] typeParams)
The IDisposible Interface Constraints.
static readonly BadInterfacePrototype Enumerable
The IEnumerable Interface Prototype.
static readonly BadInterfacePrototype MemberChangeEventArgs
static readonly Dictionary< string, BadObjectReference > s_StringStaticMembers
static BadObject ParseBoolean(BadExecutionContext ctx, BadObject[] arg)
static BadInterfaceConstraint[] ChangeAttributeConstraints(BadObject[] arg)
static BadInterfaceConstraint[] InitializeAttributeConstraints(BadObject[] arg)
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
static readonly BadInterfacePrototype InitializeAttribute
static BadInterfaceConstraint[] EnumerableConstraints(BadObject[] typeParams)
The IEnumerable Interface Constraints.
static readonly BadInterfacePrototype ArrayLike
The IArray Interface Prototype.
static BadInterfaceConstraint[] ImportHandlerConstraints(BadObject[] typeParams)
static readonly Dictionary< string, BadObjectReference > s_BooleanStaticMembers
static BadObject StringIsNullOrEmpty(BadExecutionContext ctx, BadObject[] arg)
static void AddNative(BadClassPrototype native)
Adds a native Type.
static BadInterfaceConstraint[] ArrayConstraints(BadObject[] typeParams)
The IArray Interface Constraints.
static BadInterfaceConstraint[] EnumeratorConstraints(BadObject[] typeParams)
The IEnumerator Interface Constraints.
static readonly BadInterfacePrototype Enumerator
The IEnumerator Interface Prototype.
static BadObject ParseNumber(BadExecutionContext ctx, BadObject[] arg)
static BadInterfaceConstraint[] MemberChangeEventArgsConstraints(BadObject[] arg)
static readonly Dictionary< string, BadObjectReference > s_NumberStaticMembers
static readonly List< BadClassPrototype > s_NativeTypes
Collection of all Native Class Prototypes.
Helper Class for Creating Native Class Prototypes.
static Func< BadObject[], BadObject > GetConstructor(string name)
Returns a Constructor for the given Native Type.
The Void Prototype, can be assigned to nothing, can not be inherited from, can not be instantiated....
static BadVoidPrototype Instance
The Instance of the BadVoidPrototype.
BadObject CreateGeneric(BadObject[] args)
Resolves the Generic Object to a concrete type.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Contains Shared Data Structures and Functionality.
Contains the Type Expressions for the BadScript2 Language.
Contains the Reader Tokens for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains Runtime Interface Objects.