BadScript 2
Loading...
Searching...
No Matches
BadClass.cs
Go to the documentation of this file.
4
6
10public class BadClass : BadObject
11{
15 private readonly BadClass? m_BaseClass;
16
24 public BadClass(string name, BadExecutionContext context, BadClass? baseClass, BadClassPrototype prototype)
25 {
26 Name = name;
27 Context = context;
28 m_BaseClass = baseClass;
29 Prototype = prototype;
30 }
31
36
38
42 public string Name { get; }
43
48
52 public BadClass? SuperClass { get; private set; }
53
54
61 {
62 return proto.IsSuperClassOf(Prototype);
63 }
64
65
69 public void SetThis()
70 {
71 SetThis(this);
72 }
73
78 private void SetThis(BadClass thisInstance)
79 {
80 SuperClass = thisInstance;
81
83 .GetProperty(BadStaticKeys.THIS_KEY)
84 .Set(thisInstance, null, new BadPropertyInfo(thisInstance.Prototype, true));
85 m_BaseClass?.SetThis(thisInstance);
86 }
87
90 {
91 return Prototype;
92 }
93
95 public override bool HasProperty(string propName, BadScope? caller = null)
96 {
97 if (Scope.GetTable()
98 .InnerTable.ContainsKey(propName))
99 {
100 return true;
101 }
102
103 if (m_BaseClass != null)
104 {
105 return m_BaseClass.HasProperty(propName, caller);
106 }
107
108 return caller != null && caller.Provider.HasObject(GetType(), propName);
109 }
110
111
120 public BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller = null)
121 {
123
124 if (caller != null)
125 {
126 if (caller.ClassObject == null)
127 {
128 visibility = BadPropertyVisibility.Public;
129 }
130 else if (caller.ClassObject == this)
131 {
132 visibility = BadPropertyVisibility.All;
133 }
134 else if (caller.ClassObject.InheritsFrom(Prototype))
135 {
136 visibility = BadPropertyVisibility.Public | BadPropertyVisibility.Protected;
137 }
138 else
139 {
140 visibility = BadPropertyVisibility.Public;
141 }
142 }
143
144 if (!HasProperty(propName, Scope))
145 {
146 throw BadRuntimeException.Create(caller,
147 $"Property {propName} not found in class {Name} or any of its base classes"
148 );
149 }
150
151 if ((vis & visibility) == 0)
152 {
153 throw BadRuntimeException.Create(caller,
154 $"Property {Name}.{propName} is not visible from {caller?.Name ?? "global"}"
155 );
156 }
157
158 if (Scope.GetTable()
159 .InnerTable.ContainsKey(propName))
160 {
161 return BadObjectReference.Make($"{Name}.{propName}",
162 (p) => Scope.GetTable()
163 .InnerTable[propName],
164 (o, p, t) =>
165 {
166 BadPropertyInfo info = Scope.GetTable()
167 .GetPropertyInfo(propName);
168
169 BadObject? existing = Scope.GetTable()
170 .InnerTable[propName];
171
172 if (existing != Null && info.IsReadOnly)
173 {
174 throw BadRuntimeException.Create(caller,
175 $"{Name}.{propName} is read-only",p
176 );
177 }
178
179 if (info.Type != null && !info.Type.IsAssignableFrom(o))
180 {
181 throw BadRuntimeException.Create(caller,
182 $"Cannot assign object {o.GetType().Name} to property '{propName}' of type '{info.Type.Name}'",p
183 );
184 }
185
186 if (existing is BadObjectReference reference)
187 {
188 reference.Set(o, p, t);
189 }
190 else
191 {
192 if (existing != Null && Scope.OnChange(propName, existing, o))
193 {
194 return;
195 }
196
198 .InnerTable[propName] = o;
199 Scope.OnChanged(propName, existing ?? Null, o);
200 }
201 }
202 );
203 }
204
205 if (m_BaseClass != null)
206 {
207 return m_BaseClass.GetProperty(propName,
208 visibility & ~BadPropertyVisibility.Private & BadPropertyVisibility.All,
209 caller
210 ); //Allow public, protected
211 }
212
213 if (caller == null)
214 {
215 throw BadRuntimeException.Create(caller, $"No property named {propName} for type {Name}");
216 }
217
218 return caller.Provider.GetObjectReference(GetType(), propName, SuperClass ?? this, caller);
219 }
220
222 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
223 {
224 return GetProperty(propName, BadPropertyVisibility.Public, caller);
225 }
226
227
229 public override string ToSafeString(List<BadObject> done)
230 {
231 done.Add(this);
232
233 if (Scope.HasLocal("ToString", Scope, false) &&
234 Scope.GetProperty("ToString", Scope)
235 .Dereference(null) is BadFunction toString)
236 {
237 BadObject? result = Null;
238
239 foreach (BadObject? o in toString.Invoke(Array.Empty<BadObject>(), Context))
240 {
241 result = o;
242 }
243
244 result = result.Dereference(null);
245
246 return result.ToSafeString(done);
247 }
248
249 return
250 $"class {Name}\n{Scope.ToSafeString(done)}";
251 }
252}
Contains Static Data for the BadScript Language.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
BadTable GetTable()
Returns the Variable Table of the current scope.
Definition BadScope.cs:583
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadScope.cs:943
bool OnChange(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:642
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:923
static BadPropertyVisibility GetPropertyVisibility(string propName)
Returns the visibility of the specified property.
Definition BadScope.cs:827
void OnChanged(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:610
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
string ToSafeString(List< BadObject > done)
Returns a String Representation of this Object. This function is recursion proof and supports circula...
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.
Stores Meta Information about a Property.
Implements a function that can be called from the script.
Implements a Type Instance in the BadScript Language.
Definition BadClass.cs:11
void SetThis()
Sets the 'this' parameter of the class scope to be this instance.
Definition BadClass.cs:69
override string ToSafeString(List< BadObject > done)
Definition BadClass.cs:229
BadClassPrototype Prototype
The Class Prototype used to create this instance.
Definition BadClass.cs:47
BadClass(string name, BadExecutionContext context, BadClass? baseClass, BadClassPrototype prototype)
Creates a new BadScript Class Instance.
Definition BadClass.cs:24
BadClass? SuperClass
The Super Class of this Class(if this class is not at the end of the inheritance chain)
Definition BadClass.cs:52
void SetThis(BadClass thisInstance)
Sets the 'this' parameter of the class scope to be the specified instance.
Definition BadClass.cs:78
readonly? BadClass m_BaseClass
Base Class Instance.
Definition BadClass.cs:15
override BadClassPrototype GetPrototype()
Definition BadClass.cs:89
BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller=null)
Gets a property from this class or any of its base classes.
Definition BadClass.cs:120
BadScope Scope
Table of all members of this type(excluding base class members)
Definition BadClass.cs:35
override 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 BadClass.cs:95
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadClass.cs:222
bool InheritsFrom(BadClassPrototype proto)
Returns true if the given object is an instance of the specified prototype.
Definition BadClass.cs:60
Implements a Class Prototype for the BadScript Language.
virtual bool IsSuperClassOf(BadClassPrototype proto)
Returns true if the Class is a Subclass of the given Class.
Contains Shared Data Structures and Functionality.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
BadPropertyVisibility
The Visibility of a Property.