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>("RemoveKey",
23 o => new BadDynamicInteropFunction<BadObject>("RemoveKey",
24 (c, k) =>
25 {
26 if (k is not IBadString s)
27 {
28 throw BadRuntimeException.Create(c.Scope, "Key is not a string");
29 }
30
31 return RemoveKey(o, s.Value);
32 },
34 "key"
35 )
36 );
37
38 provider.RegisterObject<BadTable>("MakeReadOnly",
39 table => new BadDynamicInteropFunction("MakeReadOnly",
40 _ =>
41 {
42 foreach (string key in table.InnerTable.Keys)
43 {
44 table.PropertyInfos[key].IsReadOnly = true;
45 }
46
47 return BadObject.Null;
48 },
49 BadAnyPrototype.Instance
50 )
51 );
52
55 .ARRAY_ACCESS_OPERATOR_NAME,
56 (c, o) => ArrayAccess(c, t, o),
58 "key"
59 )
60 );
61
62 provider.RegisterObject<BadTable>("Keys", Keys);
63 provider.RegisterObject<BadTable>("Values", Values);
64 provider.RegisterObject<BadTable>("Length", a => BadObject.Wrap((decimal)a.InnerTable.Count));
65
66 provider.RegisterObject<BadTable>("Join",
67 t => new BadInteropFunction("Join",
68 (c, a) => JoinTable(c, t, a),
69 false,
71 new BadFunctionParameter("overwrite",
72 false,
73 true,
74 false,
75 null,
77 ),
78 new BadFunctionParameter("others",
79 false,
80 true,
81 true,
82 null
83 )
84 )
85 );
86 }
87
88 private static BadObject ArrayAccess(BadExecutionContext context, BadTable table, BadObject enumerator)
89 {
90 BadSourcePosition position = BadSourcePosition.FromSource("ArrayAccess", 0, 1);
91
92 if (enumerator is IBadString str)
93 {
94 return table.GetProperty(str.Value, context.Scope);
95 }
96
97 BadTable result = new BadTable();
98
99 if (enumerator.HasProperty("GetEnumerator", context.Scope) &&
100 enumerator.GetProperty("GetEnumerator", context.Scope)
101 .Dereference(null) 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(null);
109 }
110
111 (BadFunction moveNext, BadFunction getCurrent) =
112 BadForEachExpression.FindEnumerator(enumerator, context, position);
113
114 BadObject cond = BadObject.Null;
115
116 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), context))
117 {
118 cond = o;
119 }
120
121 IBadBoolean bRet = cond.Dereference(null) as IBadBoolean ??
122 throw new BadRuntimeException("While Condition is not a boolean", position);
123
124 while (bRet.Value)
125 {
126 using BadExecutionContext loopContext = new BadExecutionContext(context.Scope.CreateChild("ForEachLoop",
127 context.Scope,
128 null,
129 BadScopeFlags.Breakable |
130 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,
147 table.GetProperty(key.Value, context.Scope)
148 .Dereference(null)
149 );
150
151 foreach (BadObject o in moveNext.Invoke(Array.Empty<BadObject>(), loopContext))
152 {
153 cond = o;
154 }
155
156 bRet = cond.Dereference(null) as IBadBoolean ??
157 throw BadRuntimeException.Create(context.Scope,
158 "Enumerator MoveNext did not return a boolean",
159 position
160 );
161 }
162
163 return result;
164 }
165
174 private static BadObject JoinTable(BadExecutionContext ctx, BadTable self, IReadOnlyList<BadObject> args)
175 {
176 if (args.Count == 0)
177 {
178 throw BadRuntimeException.Create(ctx.Scope, "Invalid Argument Count");
179 }
180
181 BadObject overwrite = args[0];
182
183 BadObject[] others = args.Skip(1)
184 .ToArray();
185
186 if (overwrite is not IBadBoolean ov)
187 {
188 throw BadRuntimeException.Create(ctx.Scope, "Overwrite is not a boolean value");
189 }
190
191 foreach (BadObject o in others)
192 {
193 if (o is not BadTable other)
194 {
195 throw BadRuntimeException.Create(ctx.Scope, "Others can only be tables");
196 }
197
198 foreach (KeyValuePair<string, BadObject> kvp in other.InnerTable)
199 {
200 if (ov.Value || !self.InnerTable.ContainsKey(kvp.Key))
201 {
202 self.GetProperty(kvp.Key, ctx.Scope)
203 .Set(kvp.Value, null);
204 }
205 }
206 }
207
208 return self;
209 }
210
217 private static BadObject RemoveKey(BadTable table, string key)
218 {
219 return table.RemoveKey(key);
220 }
221
227 private static BadObject Keys(BadTable table)
228 {
229 return new BadArray(table.InnerTable.Keys.Select(x => (BadObject)x)
230 .ToList()
231 );
232 }
233
239 private static BadObject Values(BadTable table)
240 {
241 return new BadArray(table.InnerTable.Values.ToList());
242 }
243}
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: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 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:141
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Definition BadTable.cs:54
bool RemoveKey(string key)
Removes a Property from the Table.
Definition BadTable.cs:119
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.