BadScript 2
Loading...
Searching...
No Matches
BadArrayExtension.cs
Go to the documentation of this file.
11
16
21{
23 protected override void AddExtensions(BadInteropExtensionProvider provider)
24 {
25 provider.RegisterObject<BadArray>("Add",
27 (_, o) => Add(a, o),
29 "elem"
30 )
31 );
32
33 provider.RegisterObject<BadArray>("Clear",
34 a => new BadDynamicInteropFunction("Clear",
35 _ => Clear(a),
37 )
38 );
39
40 provider.RegisterObject<BadArray>("AddRange",
41 a => new BadDynamicInteropFunction<BadObject>("AddRange",
42 (ctx, elems) =>
43 {
44 if (elems is BadArray arr)
45 {
46 a.InnerArray.AddRange(arr.InnerArray);
47 }
49 .IsSuperClassOf(elems.GetPrototype()))
50 {
51 // Get the Enumerator
52 BadObject[] enumerated = BadNativeClassHelper
53 .ExecuteEnumerate(ctx, elems)
54 .ToArray();
55 a.InnerArray.AddRange(enumerated);
56 }
57 else
58 {
59 throw BadRuntimeException.Create(ctx.Scope,
60 "Argument is not of type IEnumerable"
61 );
62 }
63
64 return BadObject.Null;
65 },
67 "elems"
68 )
69 );
70
71 provider.RegisterObject<BadArray>("Insert",
73 (_, i, o) => Insert(a, i, o),
75 "elem"
76 )
77 );
78
79 provider.RegisterObject<BadArray>("InsertRange",
81 (ctx, num, elems) =>
82 {
83 if (elems is BadArray arr)
84 {
85 a.InnerArray.InsertRange((int)num, arr.InnerArray);
86 }
88 .IsSuperClassOf(elems.GetPrototype()))
89 {
90 // Get the Enumerator
91 BadObject[] enumerated = BadNativeClassHelper
92 .ExecuteEnumerate(ctx, elems)
93 .ToArray();
94 a.InnerArray.InsertRange((int)num, enumerated);
95 }
96 else
97 {
98 throw BadRuntimeException.Create(ctx.Scope,
99 "Argument is not of type IEnumerable"
100 );
101 }
102
103 return BadObject.Null;
104 },
106 "index",
107 "elems"
108 )
109 );
110
111 provider.RegisterObject<BadArray>("Remove",
112 a => new BadDynamicInteropFunction<BadObject>("Remove",
113 (_, o) => Remove(a, o),
115 "elem"
116 )
117 );
118
119 provider.RegisterObject<BadArray>("Contains",
120 a => new BadDynamicInteropFunction<BadObject>("Contains",
121 (_, o) => Contains(a, o),
123 "elem"
124 )
125 );
126
127 provider.RegisterObject<BadArray>("RemoveAt",
128 a => new BadDynamicInteropFunction<decimal>("RemoveAt",
129 (_, o) => RemoveAt(a, o),
131 "index"
132 )
133 );
134
135 provider.RegisterObject<BadArray>("Get",
137 (_, o) => Get(a, o),
139 "index"
140 )
141 );
142
143 provider.RegisterObject<BadArray>("Set",
145 (_, i, v) => Set(a, i, v),
147 "index"
148 )
149 );
150
151 provider.RegisterObject<BadArray>("GetEnumerator",
152 a => new BadDynamicInteropFunction("GetEnumerator",
153 _ => GetEnumerator(a),
155 )
156 );
157
160 .ARRAY_ACCESS_OPERATOR_NAME,
161 (c, i) => ArrayAccess(c, a, i),
163 "index"
164 )
165 );
166
169 .ARRAY_ACCESS_REVERSE_OPERATOR_NAME,
170 (_, i) => BadObjectReference.Make($"{a}[^{i}]",
171 (p) => Get(a, a.InnerArray.Count - i),
172 (v,_, _) => Set(a, a.InnerArray.Count - i, v)
173 ),
175 "index"
176 )
177 );
178
179 provider.RegisterObject<BadArray>("Length", a => BadObject.Wrap((decimal)a.InnerArray.Count));
180 }
181
182 private static BadObject ArrayAccess(BadExecutionContext context, BadArray array, BadObject enumerator)
183 {
184 BadSourcePosition position = BadSourcePosition.FromSource("ArrayAccess", 0, 1);
185
186 if (enumerator is IBadNumber num)
187 {
188 return BadObjectReference.Make($"{array}[{num.Value}]",
189 (p) => Get(array, num.Value),
190 (v, _, _) => Set(array, num.Value, v)
191 );
192 }
193
194 BadArray result = new BadArray();
195
196 if (enumerator.HasProperty("GetEnumerator", context.Scope) &&
197 enumerator.GetProperty("GetEnumerator", context.Scope)
198 .Dereference(null) is BadFunction getEnumerator)
199 {
200 foreach (BadObject e in getEnumerator.Invoke(Array.Empty<BadObject>(), context))
201 {
202 enumerator = e;
203 }
204
205 enumerator = enumerator.Dereference(null);
206 }
207
208 (BadFunction moveNext, BadFunction getCurrent) =
209 BadForEachExpression.FindEnumerator(enumerator, context, position);
210
211 BadObject cond = BadObject.Null;
212
213 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), context))
214 {
215 cond = o;
216 }
217
218 IBadBoolean bRet = cond.Dereference(null) as IBadBoolean ??
219 throw new BadRuntimeException("While Condition is not a boolean", position);
220
221 while (bRet.Value)
222 {
223 using BadExecutionContext loopContext = new BadExecutionContext(context.Scope.CreateChild("ForEachLoop",
224 context.Scope,
225 null,
226 BadScopeFlags.Breakable |
227 BadScopeFlags.Continuable
228 )
229 );
230
231 BadObject current = BadObject.Null;
232
233 foreach (BadObject o in getCurrent.Invoke(Array.Empty<BadObject>(), loopContext))
234 {
235 current = o;
236 }
237
238 if (current is not IBadNumber n)
239 {
240 throw BadRuntimeException.Create(context.Scope, "Enumerator Current did not return a number", position);
241 }
242
243 result.InnerArray.Add(Get(array, n.Value));
244
245 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), loopContext))
246 {
247 cond = o;
248 }
249
250 bRet = cond.Dereference(null) as IBadBoolean ??
251 throw BadRuntimeException.Create(context.Scope,
252 "Enumerator MoveNext did not return a boolean",
253 position
254 );
255 }
256
257 return result;
258 }
259
265 private static BadObject GetEnumerator(BadArray array)
266 {
267 return new BadInteropEnumerator(array.InnerArray.GetEnumerator());
268 }
269
275 private static BadObject Clear(BadArray arg)
276 {
277 arg.InnerArray.Clear();
278
279 return BadObject.Null;
280 }
281
288 private static BadObject Add(BadArray arg, BadObject obj)
289 {
290 arg.InnerArray.Add(obj);
291
292 return BadObject.Null;
293 }
294
302 private static BadObject Insert(BadArray arg, decimal index, BadObject obj)
303 {
304 arg.InnerArray.Insert((int)index, obj);
305
306 return BadObject.Null;
307 }
308
315 private static BadObject Contains(BadArray arg, BadObject obj)
316 {
317 return arg.InnerArray.Contains(obj);
318 }
319
326 private static BadObject Remove(BadArray arg, BadObject obj)
327 {
328 return arg.InnerArray.Remove(obj);
329 }
330
337 private static BadObject RemoveAt(BadArray arg, decimal obj)
338 {
339 int index = (int)obj;
340 arg.InnerArray.RemoveAt(index);
341
342 return BadObject.Null;
343 }
344
351 private static BadObject Get(BadArray arg, decimal obj)
352 {
353 int index = (int)obj;
354
355 return arg.InnerArray[index];
356 }
357
365 private static BadObject Set(BadArray arg, decimal obj, BadObject value)
366 {
367 int index = (int)obj;
368 arg.InnerArray[index] = value;
369
370 return BadObject.Null;
371 }
372}
Describes a specific position inside a source file.
static BadSourcePosition FromSource(string source, int index, int length)
Creates a new Source Position.
Contains Static Data for the BadScript Language.
const string ARRAY_ACCESS_REVERSE_OPERATOR_NAME
static BadObject ArrayAccess(BadExecutionContext context, BadArray array, BadObject enumerator)
static BadObject Get(BadArray arg, decimal obj)
Returns a value from the array.
static BadObject Remove(BadArray arg, BadObject obj)
Removes an element from the array.
static BadObject Add(BadArray arg, BadObject obj)
Adds an element to the array.
static BadObject Set(BadArray arg, decimal obj, BadObject value)
Sets a value in the array.
override void AddExtensions(BadInteropExtensionProvider provider)
static BadObject RemoveAt(BadArray arg, decimal obj)
Removes an element at the given index.
static BadObject Contains(BadArray arg, BadObject obj)
Returns True if the array contains the given element.
static BadObject GetEnumerator(BadArray array)
Returns an enumerator of the array.
static BadObject Clear(BadArray arg)
Clears the array.
static BadObject Insert(BadArray arg, decimal index, BadObject obj)
Inserts an element at the given index.
static BadFunction BadFunction getCurrent FindEnumerator(BadObject target, BadExecutionContext context, BadSourcePosition position)
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 BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implements a simple wrapper for C# IEnumerators to be used in BS2.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17
List< BadObject > InnerArray
The Inner Array.
Definition BadArray.cs:45
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
virtual bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
Definition BadObject.cs:130
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
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.
Implements a function that can be called from the script.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype Enumerable
The IEnumerable Interface Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
static readonly BadInterfacePrototype Enumerator
The IEnumerator Interface Prototype.
Helper Class for Creating Native Class Prototypes.
static IEnumerable< BadObject > ExecuteEnumerate(BadExecutionContext ctx, BadObject enumerable)
Executes the Enumerable.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Numbers.
Definition IBadNumber.cs:7
Contains Shared Data Structures and Functionality.
Contains Common Interop Extensions for the BadScript2 Runtime.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.