BadScript 2
Loading...
Searching...
No Matches
BadObject.cs
Go to the documentation of this file.
7
9
13public abstract class BadObject
14{
19
23 private static readonly Dictionary<string, BadString> s_StringCache = new Dictionary<string, BadString>();
24
28 public static readonly BadObject Null = new BadNullObject();
29
33 public static readonly BadObject True = new BadBoolean(true);
34
38 public static readonly BadObject False = new BadBoolean(false);
39
45
51 public static bool CanWrap(object? o)
52 {
53 return o is string || o is decimal || o is null || o.GetType().IsNumericType();
54 }
55
64 public static BadObject Wrap<T>(T obj, bool allowNative = true)
65 {
66 switch (obj)
67 {
68 case BadObject bObj:
69 return bObj;
70 case bool b when b:
71 return True;
72 case bool:
73 return False;
74 case decimal d:
75 return new BadNumber(d);
76 }
77
78 if (typeof(T).IsNumericType() || obj != null && obj.GetType().IsNumericType())
79 {
80 return new BadNumber(Convert.ToDecimal(obj));
81 }
82
83 if (obj is string s)
84 {
85 if (!BadNativeOptimizationSettings.Instance.UseStringCaching)
86 {
87 return new BadString(s);
88 }
89
90 if (s_StringCache.TryGetValue(s, out BadString? wrap))
91 {
92 return wrap;
93 }
94
95 return s_StringCache[s] = new BadString(s);
96 }
97
98 if (Equals(obj, default(T)))
99 {
100 return Null;
101 }
102
103
104 if (allowNative)
105 {
106 return new BadNative<T>(obj);
107 }
108
109 throw new BadRuntimeException("Cannot wrap native type");
110 }
111
118 public virtual bool HasProperty(string propName, BadScope? caller = null)
119 {
120 return caller != null && caller.Provider.HasObject(GetType(), propName);
121 }
122
129 public virtual BadObjectReference GetProperty(string propName, BadScope? caller = null)
130 {
131 if (caller == null)
132 {
133 throw BadRuntimeException.Create(caller, $"No property named {propName} for type {GetType().Name}");
134 }
135
136 return caller.Provider.GetObjectReference(GetType(), propName, this, caller);
137 }
138
144 public static implicit operator BadObject(bool b)
145 {
146 return Wrap(b);
147 }
148
154 public static implicit operator BadObject(BadNullable<bool> b)
155 {
156 return b.HasValue ? b.Value : Null;
157 }
158
164 public static implicit operator BadObject(decimal d)
165 {
166 return Wrap(d);
167 }
168
174 public static implicit operator BadObject(BadNullable<decimal> b)
175 {
176 return b.HasValue ? b.Value : Null;
177 }
178
184 public static implicit operator BadObject(string s)
185 {
186 return Wrap(s);
187 }
188
194 public static implicit operator BadObject(BadNullable<string> b)
195 {
196 return b.HasValue ? b.Value! : Null;
197 }
198
204 public abstract string ToSafeString(List<BadObject> done);
205
210 public override string ToString()
211 {
212 return ToSafeString(new List<BadObject>());
213 }
214
219 {
221 public object Value => null!;
222
224 public Type Type => typeof(object);
225
231 public bool Equals(IBadNative? other)
232 {
233 return Equals((object?)other);
234 }
235
236
239 {
240 return s_Prototype;
241 }
242
244 public override string ToSafeString(List<BadObject> done)
245 {
246 return "null";
247 }
248
249
251 public override bool Equals(object? obj)
252 {
253 return ReferenceEquals(this, obj);
254 }
255
257 public override int GetHashCode()
258 {
259 throw new NotSupportedException();
260 }
261 }
262}
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implementation for the null-value.
Definition BadObject.cs:219
override string ToSafeString(List< BadObject > done)
Definition BadObject.cs:244
object Value
The Value of the Native Object.
Definition BadObject.cs:221
bool Equals(IBadNative? other)
Returns the Prototype for the Null Object.
Definition BadObject.cs:231
Type Type
The Type of the Native Object.
Definition BadObject.cs:224
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject True
The True Value for the BadScript Language.
Definition BadObject.cs:33
static readonly BadObject False
The False Value for the BadScript Language.
Definition BadObject.cs:38
static bool CanWrap(object? o)
Returns true if the given object cam be wrapped.
Definition BadObject.cs:51
string ToSafeString(List< BadObject > done)
Returns a String Representation of this Object. This function is recursion proof and supports circula...
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:210
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
static readonly Dictionary< string, BadString > s_StringCache
The String Cache for the BadScript Language.
Definition BadObject.cs:23
static readonly BadClassPrototype s_Prototype
The Any Prototype for the BadScript Language.
Definition BadObject.cs:18
BadClassPrototype GetPrototype()
Returns the Prototype of this Object.
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
static BadObject Wrap< T >(T obj, bool allowNative=true)
Wraps the given object into a BadObject Instance.
Definition BadObject.cs:64
Implements the base functionality for a BadScript Reference.
Implements a Native String.
Definition BadString.cs:9
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
static T Instance
Returns the Instance of the Settings Provider.
Defines properties for Native Types.
Definition IBadNative.cs:7
Contains the Error Objects for the BadScript2 Language.
Contains the Extension Classes for Functions.
Contains the Interop Reflection Classes for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Runtime Settings Objects.
This is a helper type that can be used when using the .SetFunction extensions to allow for nullable p...
Definition BadNullable.cs:8
readonly? T Value
The Value of the Parameter.