BadScript 2
Loading...
Searching...
No Matches
BadNativeClassBuilder.cs
Go to the documentation of this file.
8
10
14public static class BadNativeClassBuilder
15{
19 public static readonly BadInterfacePrototype Disposable =
20 new BadInterfacePrototype("IDisposable",
21 typeArgs => Array.Empty<BadInterfacePrototype>(),
22 null,
24 Array.Empty<string>()
25 );
26
27 public static readonly BadInterfacePrototype Attribute =
28 new BadInterfacePrototype("IAttribute",
29 typeArgs => Array.Empty<BadInterfacePrototype>(),
30 null,
31 objects => Array.Empty<BadInterfaceConstraint>(),
32 Array.Empty<string>()
33 );
34
36 new BadInterfacePrototype("IInitializeAttribute",
37 typeArgs => new[] { Attribute },
38 null,
40 Array.Empty<string>()
41 );
42
44 new BadInterfacePrototype("IMemberChangeEventArgs",
45 _ => Array.Empty<BadInterfacePrototype>(),
46 null,
48 Array.Empty<string>()
49 );
50
52 new BadInterfacePrototype("IMemberChangedEventArgs",
53 _ => new[] { MemberChangeEventArgs },
54 null,
55 _ => Array.Empty<BadInterfaceConstraint>(),
56 Array.Empty<string>()
57 );
58
60 new BadInterfacePrototype("IMemberChangingEventArgs",
61 _ => new[] { MemberChangeEventArgs },
62 null,
64 Array.Empty<string>()
65 );
66
67 public static readonly BadInterfacePrototype ChangeAttribute =
68 new BadInterfacePrototype("IChangeAttribute",
69 typeArgs => new[] { Attribute },
70 null,
72 Array.Empty<string>()
73 );
74
75 public static readonly BadInterfacePrototype ChangedAttribute =
76 new BadInterfacePrototype("IChangedAttribute",
77 typeArgs => new[] { Attribute },
78 null,
80 Array.Empty<string>()
81 );
82
86 public static readonly BadInterfacePrototype Enumerable =
87 new BadInterfacePrototype("IEnumerable",
88 typeArgs => Array.Empty<BadInterfacePrototype>(),
89 null,
91 new[] { "T" }
92 );
93
97 public static readonly BadInterfacePrototype Enumerator =
98 new BadInterfacePrototype("IEnumerator",
99 typeArgs => Array.Empty<BadInterfacePrototype>(),
100 null,
102 new[] { "T" }
103 );
104
108 public static readonly BadInterfacePrototype ArrayLike =
109 new BadInterfacePrototype("IArray",
110 typeArgs => new[] { (BadInterfacePrototype)Enumerable.CreateGeneric(typeArgs) },
111 null,
113 new[] { "T" }
114 );
115
116 public static readonly BadInterfacePrototype ImportHandler =
117 new BadInterfacePrototype("IImportHandler",
118 typeArgs => Array.Empty<BadInterfacePrototype>(),
119 null,
121 Array.Empty<string>()
122 );
123
124 private static readonly Dictionary<string, BadObjectReference> s_NumberStaticMembers =
125 new Dictionary<string, BadObjectReference>
126 {
127 {
128 "Parse", BadObjectReference.Make("num.Parse",
129 (p) => new BadInteropFunction("Parse",
131 true,
132 GetNative("num"),
133 new BadFunctionParameter("str",
134 false,
135 true,
136 false,
137 null,
138 GetNative("string")
139 )
140 )
141 )
142 },
143 };
144
145 private static readonly Dictionary<string, BadObjectReference> s_BooleanStaticMembers =
146 new Dictionary<string, BadObjectReference>
147 {
148 {
149 "Parse", BadObjectReference.Make("bool.Parse",
150 (p) => new BadInteropFunction("Parse",
152 true,
153 GetNative("bool"),
154 new BadFunctionParameter("str",
155 false,
156 true,
157 false,
158 null,
159 GetNative("string")
160 )
161 )
162 )
163 },
164 };
165
166 private static readonly Dictionary<string, BadObjectReference> s_StringStaticMembers =
167 new Dictionary<string, BadObjectReference>
168 {
169 { "Empty", BadObjectReference.Make("string.Empty", (p) => BadString.Empty) },
170 {
171 "IsNullOrEmpty", BadObjectReference.Make("string.IsNullOrEmpty",
172 (p) => new BadInteropFunction("IsNullOrEmpty",
174 true,
175 GetNative("bool"),
176 new BadFunctionParameter("str",
177 false,
178 false,
179 false,
180 null,
181 GetNative("string")
182 )
183 )
184 )
185 },
186 };
187
191 private static readonly List<BadClassPrototype> s_NativeTypes = new List<BadClassPrototype>
192 {
195 CreateNativeType<BadString>("string", s_StringStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
196 CreateNativeType<BadBoolean>("bool", s_BooleanStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
197 CreateNativeType<BadNumber>("num", s_NumberStaticMembers, () => Array.Empty<BadInterfacePrototype>()),
198 CreateNativeType<BadFunction>("Function", () => Array.Empty<BadInterfacePrototype>()),
199 CreateNativeType<BadArray>("Array",
200 () => new[]
201 {
203 {
204 BadAnyPrototype.Instance,
205 }
206 ),
207 }
208 ),
209 CreateNativeType<BadTable>("Table",
210 () => new[]
211 {
213 {
215 }
216 ),
217 }
218 ),
225 //TODO: Make Native Class Builder non static
226 //(adding this prototype will cause a type initializer exception because of a circular reference due to a tightly packed static initializer list)
227 //BadFunction.Prototype,
231 ArrayLike,
233 Attribute,
240 };
241
245 public static IEnumerable<BadClassPrototype> NativeTypes => s_NativeTypes;
246
248 {
249 return new BadInterfaceConstraint[]
250 {
252 null,
254 Array.Empty<BadFunctionParameter>()
255 ),
256 };
257 }
258
269
271 {
272 return new BadInterfaceConstraint[]
273 {
274 new BadInterfaceFunctionConstraint("Initialize",
275 null,
277 new[]
278 {
279 new BadFunctionParameter("instance",
280 false,
281 true,
282 false,
283 null,
284 BadAnyPrototype.Instance
285 ),
286 new BadFunctionParameter("member",
287 false,
288 true,
289 false,
290 null,
291 BadMemberInfo.Prototype
292 ),
293 }
294 ),
295 };
296 }
297
299 {
300 return new BadInterfaceConstraint[]
301 {
302 new BadInterfaceFunctionConstraint("OnChange",
303 null,
305 new[]
306 {
307 new BadFunctionParameter("eventArgs",
308 false,
309 true,
310 false,
311 null,
312 MemberChangingEventArgs
313 ),
314 }
315 ),
316 };
317 }
318
320 {
321 return new BadInterfaceConstraint[]
322 {
323 new BadInterfaceFunctionConstraint("OnChanged",
324 null,
326 new[]
327 {
328 new BadFunctionParameter("eventArgs",
329 false,
330 true,
331 false,
332 null,
333 MemberChangedEventArgs
334 ),
335 }
336 ),
337 };
338 }
339
340
342 {
343 if (arg[0] is not IBadString str)
344 {
345 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
346 }
347
348 return string.IsNullOrEmpty(str.Value);
349 }
350
352 {
353 if (arg[0] is not IBadString str)
354 {
355 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
356 }
357
358 if (decimal.TryParse(str.Value, out decimal d))
359 {
360 return d;
361 }
362
363 throw BadRuntimeException.Create(ctx.Scope, $"The Supplied String '{str.Value}' is not a valid number");
364 }
365
367 {
368 if (arg[0] is not IBadString str)
369 {
370 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Type");
371 }
372
373 if (bool.TryParse(str.Value, out bool d))
374 {
375 return d;
376 }
377
378 throw BadRuntimeException.Create(ctx.Scope, $"The Supplied String '{str.Value}' is not a valid boolean");
379 }
380
386 {
387 return new BadInterfaceConstraint[]
388 {
389 new BadInterfaceFunctionConstraint("Dispose", null, Array.Empty<BadFunctionParameter>()),
390 };
391 }
392
398 {
399 return new BadInterfaceConstraint[]
400 {
401 new BadInterfaceFunctionConstraint("GetEnumerator",
402 null,
403 (BadClassPrototype)Enumerator.CreateGeneric(typeParams),
404 Array.Empty<BadFunctionParameter>()
405 ),
406 };
407 }
408
409
411 {
412 return new BadInterfaceConstraint[]
413 {
414 new BadInterfaceFunctionConstraint("GetHash",
415 null,
416 GetNative("string"),
417 new[]
418 {
419 new BadFunctionParameter("path",
420 false,
421 true,
422 false,
423 null,
424 GetNative("string")
425 ),
426 }
427 ),
429 null,
430 GetNative("bool"),
431 new[]
432 {
433 new BadFunctionParameter("path",
434 false,
435 true,
436 false,
437 null,
438 GetNative("string")
439 ),
440 }
441 ),
443 null,
445 new[]
446 {
447 new BadFunctionParameter("path",
448 false,
449 true,
450 false,
451 null,
452 GetNative("string")
453 ),
454 }
455 ),
456 new BadInterfaceFunctionConstraint("IsTransient",
457 null,
458 GetNative("bool"),
459 Array.Empty<BadFunctionParameter>()
460 ),
461 };
462 }
463
468 private static BadInterfaceConstraint[] ArrayConstraints(BadObject[] typeParams)
469 {
470 BadClassPrototype? listType =
471 typeParams.Length > 0 ? (BadClassPrototype)typeParams[0] : BadAnyPrototype.Instance;
472
473 return new BadInterfaceConstraint[]
474 {
475 //Add(any elem);
477 null,
478 new[]
479 {
480 new BadFunctionParameter("elem",
481 false,
482 false,
483 false,
484 null,
485 listType
486 ),
487 }
488 ),
489
490 //AddRange(IEnumerable elems);
491 new BadInterfaceFunctionConstraint("AddRange",
492 null,
493 new[]
494 {
495 new BadFunctionParameter("elems",
496 false,
497 false,
498 false,
499 null,
500 (BadClassPrototype)Enumerable
501 .CreateGeneric(typeParams)
502 ),
503 }
504 ),
505
506 //Clear();
507 new BadInterfaceFunctionConstraint("Clear", null, Array.Empty<BadFunctionParameter>()),
508
509 //Insert(num index, any elem);
511 null,
512 new[]
513 {
514 new BadFunctionParameter("index",
515 false,
516 false,
517 false,
518 null,
519 GetNative("num")
520 ),
521 new BadFunctionParameter("elem",
522 false,
523 false,
524 false,
525 null,
526 listType
527 ),
528 }
529 ),
530
531 //InsertRange(num index, IEnumerable elems);
532 new BadInterfaceFunctionConstraint("InsertRange",
533 null,
534 new[]
535 {
536 new BadFunctionParameter("index",
537 false,
538 false,
539 false,
540 null,
541 GetNative("num")
542 ),
543 new BadFunctionParameter("elems",
544 false,
545 false,
546 false,
547 null,
548 (BadClassPrototype)Enumerable
549 .CreateGeneric(typeParams)
550 ),
551 }
552 ),
553
554 //bool Remove(any elem);
556 null,
557 GetNative("bool"),
558 new[]
559 {
560 new BadFunctionParameter("elem",
561 false,
562 false,
563 false,
564 null,
565 listType
566 ),
567 }
568 ),
569
570 //RemoveAt(num index);
571 new BadInterfaceFunctionConstraint("RemoveAt",
572 null,
573 new[]
574 {
575 new BadFunctionParameter("index",
576 false,
577 false,
578 false,
579 null,
580 GetNative("num")
581 ),
582 }
583 ),
584
585 //bool Contains(any elem);
586 new BadInterfaceFunctionConstraint("Contains",
587 null,
588 GetNative("bool"),
589 new[]
590 {
591 new BadFunctionParameter("elem",
592 false,
593 false,
594 false,
595 null,
596 listType
597 ),
598 }
599 ),
600
601 //Get(num index);
603 null,
604 listType,
605 new[]
606 {
607 new BadFunctionParameter("index",
608 false,
609 false,
610 false,
611 null,
612 GetNative("num")
613 ),
614 }
615 ),
616
617 //Set(num index, any elem);
619 null,
620 new[]
621 {
622 new BadFunctionParameter("index",
623 false,
624 false,
625 false,
626 null,
627 GetNative("num")
628 ),
629 new BadFunctionParameter("elem",
630 false,
631 false,
632 false,
633 null,
634 listType
635 ),
636 }
637 ),
638
639 //op_ArrayAccess(num index);
641 null,
643 new[]
644 {
645 new BadFunctionParameter("index",
646 false,
647 false,
648 false,
649 null,
650 GetNative("num")
651 ),
652 }
653 ),
654
655 //op_ArrayAccessReverse(num index);
657 null,
659 new[]
660 {
661 new BadFunctionParameter("index",
662 false,
663 false,
664 false,
665 null,
666 GetNative("num")
667 ),
668 }
669 ),
670 new BadInterfacePropertyConstraint("Length", null, GetNative("num")),
671 };
672 }
673
679 {
680 BadClassPrototype? type = (BadClassPrototype)(typeParams.Length > 0 ? typeParams[0] : BadAnyPrototype.Instance);
681
682 return new BadInterfaceConstraint[]
683 {
684 new BadInterfaceFunctionConstraint("MoveNext",
685 null,
686 GetNative("bool"),
687 Array.Empty<BadFunctionParameter>()
688 ),
689 new BadInterfaceFunctionConstraint("GetCurrent", null, type, Array.Empty<BadFunctionParameter>()),
690 };
691 }
692
693
700 public static BadClassPrototype GetNative(string name)
701 {
702 return s_NativeTypes.FirstOrDefault(x => x.Name == name) ??
703 throw new BadRuntimeException("Native class not found");
704 }
705
710 public static void AddNative(BadClassPrototype native)
711 {
712 if (s_NativeTypes.Any(x => x.Name == native.Name))
713 {
714 return;
715 }
716
717 s_NativeTypes.Add(native);
718 }
719
720
729 private static BadNativeClassPrototype<T> Create<T>(string name,
730 Func<BadObject[], BadObject> constructor,
731 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
732 {
733 return new BadNativeClassPrototype<T>(name,
734 (_, a) => constructor(a),
735 interfaces
736 );
737 }
738
748 private static BadNativeClassPrototype<T> Create<T>(string name,
749 Func<BadObject[], BadObject> constructor,
750 Dictionary<string, BadObjectReference> staticMembers,
751 Func<BadInterfacePrototype[]> interfaces) where T : BadObject
752 {
753 return new BadNativeClassPrototype<T>(name,
754 (_, a) => constructor(a),
755 staticMembers,
756 interfaces
757 );
758 }
759
767 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name, Func<BadInterfacePrototype[]> interfaces)
768 where T : BadObject
769 {
770 return CreateNativeType<T>(name, BadNativeClassHelper.GetConstructor(name), interfaces);
771 }
772
773
782 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name,
783 Dictionary<string, BadObjectReference> staticMembers,
784 Func<BadInterfacePrototype[]> interfaces)
785 where T : BadObject
786 {
787 return CreateNativeType<T>(name, BadNativeClassHelper.GetConstructor(name), staticMembers, interfaces);
788 }
789
790
799 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name,
800 Func<BadObject[], BadObject> constructor,
801 Func<BadInterfacePrototype[]> interfaces)
802 where T : BadObject
803 {
804 return Create<T>(name, constructor, interfaces);
805 }
806
816 private static BadNativeClassPrototype<T> CreateNativeType<T>(string name,
817 Func<BadObject[], BadObject> constructor,
818 Dictionary<string, BadObjectReference> staticMembers,
819 Func<BadInterfacePrototype[]> interfaces)
820 where T : BadObject
821 {
822 return Create<T>(name, constructor, staticMembers, interfaces);
823 }
824}
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
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
static BadClassPrototype Prototype
A Class Prototype for the Scope.
Definition BadScope.cs:188
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< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? delete=null)
Creates a new Reference Object.
static BadClassPrototype Prototype
Definition BadDate.cs:9
Implements a Native String.
Definition BadString.cs:9
static BadClassPrototype Prototype
Definition BadTime.cs:8
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 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.