BadScript 2
Loading...
Searching...
No Matches
BadTableExtension.cs
Go to the documentation of this file.
11
13
18{
20 protected override void AddExtensions(BadInteropExtensionProvider provider)
21 {
22 provider.RegisterObject<BadTable>(
23 "RemoveKey",
25 "RemoveKey",
26 (c, k) =>
27 {
28 if (k is not IBadString s)
29 {
30 throw BadRuntimeException.Create(c.Scope, "Key is not a string");
31 }
32
33 return RemoveKey(o, s.Value);
34 },
36 "key"
37 )
38 );
39
40 provider.RegisterObject<BadTable>(
41 "MakeReadOnly",
42 table => new BadDynamicInteropFunction(
43 "MakeReadOnly",
44 _ =>
45 {
46 foreach (string key in table.InnerTable.Keys)
47 {
48 table.PropertyInfos[key].IsReadOnly = true;
49 }
50
51 return BadObject.Null;
52 },
53 BadAnyPrototype.Instance
54 )
55 );
56
57 provider.RegisterObject<BadTable>(
61 (c, o) => ArrayAccess(c, t, o),
63 "key"
64 )
65 );
66
67 provider.RegisterObject<BadTable>("Keys", Keys);
68 provider.RegisterObject<BadTable>("Values", Values);
69 provider.RegisterObject<BadTable>("Length", a => BadObject.Wrap((decimal)a.InnerTable.Count));
70
71 provider.RegisterObject<BadTable>(
72 "Join",
73 t => new BadInteropFunction(
74 "Join",
75 (c, a) => JoinTable(c, t, a),
76 false,
79 "overwrite",
80 false,
81 true,
82 false,
83 null,
85 ),
86 new BadFunctionParameter("others", false, true, true, null)
87 )
88 );
89 }
90
91 private static BadObject ArrayAccess(BadExecutionContext context, BadTable table, BadObject enumerator)
92 {
93 BadSourcePosition position = BadSourcePosition.FromSource("ArrayAccess", 0, 1);
94 if (enumerator is IBadString str)
95 {
96 return table.GetProperty(str.Value, context.Scope);
97 }
98
99 BadTable result = new BadTable();
100
101 if (enumerator.HasProperty("GetEnumerator", context.Scope) && enumerator.GetProperty("GetEnumerator", context.Scope).Dereference() is BadFunction getEnumerator)
102 {
103 foreach (BadObject e in getEnumerator.Invoke(Array.Empty<BadObject>(), context))
104 {
105 enumerator = e;
106 }
107
108 enumerator = enumerator.Dereference();
109 }
110 (BadFunction moveNext, BadFunction getCurrent) = BadForEachExpression.FindEnumerator(enumerator, context, position);
111
112 BadObject cond = BadObject.Null;
113
114 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), context))
115 {
116 cond = o;
117 }
118
119
120 IBadBoolean bRet = cond.Dereference() as IBadBoolean ??
121 throw new BadRuntimeException("While Condition is not a boolean", position);
122
123 while (bRet.Value)
124 {
125 using BadExecutionContext loopContext = new BadExecutionContext(
126 context.Scope.CreateChild(
127 "ForEachLoop",
128 context.Scope,
129 null,
130 BadScopeFlags.Breakable | BadScopeFlags.Continuable
131 )
132 );
133
134 BadObject current = BadObject.Null;
135
136 foreach (BadObject o in getCurrent.Invoke(Array.Empty<BadObject>(), loopContext))
137 {
138 current = o;
139 }
140
141 if (current is not IBadString key)
142 {
143 throw BadRuntimeException.Create(context.Scope, "Enumerator Current did not return a string", position);
144 }
145
146 result.SetProperty(key.Value, table.GetProperty(key.Value, context.Scope).Dereference());
147
148 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), loopContext))
149 {
150 cond = o;
151 }
152
153 bRet = cond.Dereference() as IBadBoolean ??
155 context.Scope,
156 "Enumerator MoveNext did not return a boolean",
157 position
158 );
159 }
160
161 return result;
162 }
163
172 private static BadObject JoinTable(BadExecutionContext ctx, BadTable self, IReadOnlyList<BadObject> args)
173 {
174 if (args.Count == 0)
175 {
176 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
177 }
178
179 BadObject overwrite = args[0];
180 BadObject[] others = args.Skip(1).ToArray();
181
182 if (overwrite is not IBadBoolean ov)
183 {
184 throw BadRuntimeException.Create(ctx.Scope, "Overwrite is not a boolean value");
185 }
186
187 foreach (BadObject o in others)
188 {
189 if (o is not BadTable other)
190 {
191 throw BadRuntimeException.Create(ctx.Scope, "Others can only be tables");
192 }
193
194 foreach (KeyValuePair<string, BadObject> kvp in other.InnerTable)
195 {
196 if (ov.Value || !self.InnerTable.ContainsKey(kvp.Key))
197 {
198 self.GetProperty(kvp.Key, ctx.Scope).Set(kvp.Value);
199 }
200 }
201 }
202
203 return self;
204 }
205
212 private static BadObject RemoveKey(BadTable table, string key)
213 {
214 return table.RemoveKey(key);
215 }
216
222 private static BadObject Keys(BadTable table)
223 {
224 return new BadArray(table.InnerTable.Keys.Select(x => (BadObject)x).ToList());
225 }
226
232 private static BadObject Values(BadTable table)
233 {
234 return new BadArray(table.InnerTable.Values.ToList());
235 }
236}
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.
static BadObject Values(BadTable table)
Returns the Values of the Table.
static BadObject JoinTable(BadExecutionContext ctx, BadTable self, IReadOnlyList< BadObject > args)
Joins two or more tables together.
static BadObject ArrayAccess(BadExecutionContext context, BadTable table, BadObject enumerator)
static BadObject Keys(BadTable table)
Returns the Keys of the Table.
override void AddExtensions(BadInteropExtensionProvider provider)
static BadObject RemoveKey(BadTable table, string key)
Removes a key from the table.
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.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Interop Function taking an array of arguments.
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
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:118
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14
BadObjectReference GetProperty(string propName, bool useExtensions, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadTable.cs:142
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Definition BadTable.cs:60
bool RemoveKey(string key)
Removes a Property from the Table.
Definition BadTable.cs:120
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 BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implements the Interface for Native Strings.
Definition IBadString.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.