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 Scope.GetTable().GetProperty(BadStaticKeys.THIS_KEY).Set(thisInstance, new BadPropertyInfo(thisInstance.Prototype, true));
82 m_BaseClass?.SetThis(thisInstance);
83 }
84
87 {
88 return Prototype;
89 }
90
92 public override bool HasProperty(string propName, BadScope? caller = null)
93 {
94 if (Scope.GetTable().InnerTable.ContainsKey(propName))
95 {
96 return true;
97 }
98
99 if (m_BaseClass != null)
100 {
101 return m_BaseClass.HasProperty(propName, caller);
102 }
103
104 return caller != null && caller.Provider.HasObject(GetType(), propName);
105 }
106
107
116 public BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller = null)
117 {
119
120 if (caller != null)
121 {
122 if (caller.ClassObject == null)
123 {
124 visibility = BadPropertyVisibility.Public;
125 }
126 else if (caller.ClassObject == this)
127 {
128 visibility = BadPropertyVisibility.All;
129 }
130 else if (caller.ClassObject.InheritsFrom(Prototype))
131 {
132 visibility = BadPropertyVisibility.Public | BadPropertyVisibility.Protected;
133 }
134 else
135 {
136 visibility = BadPropertyVisibility.Public;
137 }
138 }
139
140 if (!HasProperty(propName, Scope))
141 {
143 caller,
144 $"Property {propName} not found in class {Name} or any of its base classes"
145 );
146 }
147
148 if ((vis & visibility) == 0)
149 {
151 caller,
152 $"Property {Name}.{propName} is not visible from {caller?.Name ?? "global"}"
153 );
154 }
155
156 if (Scope.GetTable().InnerTable.ContainsKey(propName))
157 {
159 $"{Name}.{propName}",
160 () => Scope.GetTable().InnerTable[propName],
161 (o, t) =>
162 {
163 BadPropertyInfo info = Scope.GetTable().GetPropertyInfo(propName);
164
165 BadObject? existing = Scope.GetTable().InnerTable[propName];
166
167 if (existing != Null && info.IsReadOnly)
168 {
169 throw BadRuntimeException.Create(caller, $"{Name}.{propName} is read-only");
170 }
171
172 if (info.Type != null && !info.Type.IsAssignableFrom(o))
173 {
174 throw BadRuntimeException.Create(
175 caller,
176 $"Cannot assign object {o.GetType().Name} to property '{propName}' of type '{info.Type.Name}'"
177 );
178 }
179
180 if (existing is BadObjectReference reference)
181 {
182 reference.Set(o, t);
183 }
184 else
185 {
186 if (existing != Null && Scope.OnChange(propName, existing, o))
187 return;
188 Scope.GetTable().InnerTable[propName] = o;
189 Scope.OnChanged(propName, existing ?? Null, o);
190 }
191 }
192 );
193 }
194
195 if (m_BaseClass != null)
196 {
198 propName,
199 visibility & ~BadPropertyVisibility.Private & BadPropertyVisibility.All,
200 caller
201 ); //Allow public, protected
202 }
203
204 if (caller == null)
205 {
206 throw BadRuntimeException.Create(caller, $"No property named {propName} for type {Name}");
207 }
208
209 return caller.Provider.GetObjectReference(GetType(), propName, SuperClass ?? this, caller);
210 }
211
213 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
214 {
215 return GetProperty(propName, BadPropertyVisibility.Public, caller);
216 }
217
218
220 public override string ToSafeString(List<BadObject> done)
221 {
222 done.Add(this);
223
224 if (Scope.HasLocal("ToString", Scope, false) && Scope.GetProperty("ToString", Scope).Dereference() is BadFunction toString)
225 {
226 BadObject? result = Null;
227 foreach (BadObject? o in toString.Invoke(Array.Empty<BadObject>(), Context))
228 {
229 result = o;
230 }
231
232 result = result.Dereference();
233
234 return result.ToSafeString(done);
235 }
236
237 return
238 $"class {Name}\n{Scope.ToSafeString(done)}";
239 }
240}
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:238
BadTable GetTable()
Returns the Variable Table of the current scope.
Definition BadScope.cs:778
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadScope.cs:1079
bool OnChange(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:829
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:1061
static BadPropertyVisibility GetPropertyVisibility(string propName)
Returns the visibility of the specified property.
Definition BadScope.cs:966
void OnChanged(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:806
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< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? 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:220
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:86
BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller=null)
Gets a property from this class or any of its base classes.
Definition BadClass.cs:116
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:92
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadClass.cs:213
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.