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 ||
54 o is TimeSpan ||
55 o is DateTimeOffset ||
56 o is decimal ||
57 o is null ||
58 o.GetType()
59 .IsNumericType();
60 }
61
70 public static BadObject Wrap<T>(T obj, bool allowNative = true)
71 {
72 switch (obj)
73 {
74 case BadObject bObj:
75 return bObj;
76 case bool b when b:
77 return True;
78 case bool:
79 return False;
80 case decimal d:
81 return new BadNumber(d);
82 case DateTimeOffset d:
83 return new BadDate(d);
84 case TimeSpan t:
85 return new BadTime(t);
86 }
87
88 if (typeof(T).IsNumericType() ||
89 (obj != null &&
90 obj.GetType()
91 .IsNumericType()))
92 {
93 return new BadNumber(Convert.ToDecimal(obj));
94 }
95
96 if (obj is string s)
97 {
98 if (!BadNativeOptimizationSettings.Instance.UseStringCaching)
99 {
100 return new BadString(s);
101 }
102
103 if (s_StringCache.TryGetValue(s, out BadString? wrap))
104 {
105 return wrap;
106 }
107
108 return s_StringCache[s] = new BadString(s);
109 }
110
111 if (Equals(obj, default(T)))
112 {
113 return Null;
114 }
115
116 if (allowNative)
117 {
118 return new BadNative<T>(obj);
119 }
120
121 throw new BadRuntimeException("Cannot wrap native type");
122 }
123
130 public virtual bool HasProperty(string propName, BadScope? caller = null)
131 {
132 return caller != null && caller.Provider.HasObject(GetType(), propName);
133 }
134
141 public virtual BadObjectReference GetProperty(string propName, BadScope? caller = null)
142 {
143 if (caller == null)
144 {
145 throw BadRuntimeException.Create(caller, $"No property named {propName} for type {GetType().Name}");
146 }
147
148 return caller.Provider.GetObjectReference(GetType(), propName, this, caller);
149 }
150
156 public static implicit operator BadObject(bool b)
157 {
158 return Wrap(b);
159 }
160
166 public static implicit operator BadObject(BadNullable<bool> b)
167 {
168 return b.HasValue ? b.Value : Null;
169 }
170 public static implicit operator BadObject(DateTimeOffset d) => new BadDate(d);
171 public static implicit operator BadObject(TimeSpan t) => new BadTime(t);
172
178 public static implicit operator BadObject(decimal d)
179 {
180 return Wrap(d);
181 }
182
188 public static implicit operator BadObject(BadNullable<decimal> b)
189 {
190 return b.HasValue ? b.Value : Null;
191 }
192
198 public static implicit operator BadObject(string s)
199 {
200 return Wrap(s);
201 }
202
208 public static implicit operator BadObject(BadNullable<string> b)
209 {
210 return b.HasValue ? b.Value! : Null;
211 }
212
218 public abstract string ToSafeString(List<BadObject> done);
219
224 public override string ToString()
225 {
226 return ToSafeString(new List<BadObject>());
227 }
228
229#region Nested type: BadNullObject
230
235 {
236#region IBadNative Members
237
239 public object Value => null!;
240
242 public Type Type => typeof(object);
243
249 public bool Equals(IBadNative? other)
250 {
251 return Equals((object?)other);
252 }
253
254#endregion
255
256
259 {
260 return s_Prototype;
261 }
262
264 public override string ToSafeString(List<BadObject> done)
265 {
266 return "null";
267 }
268
269
271 public override bool Equals(object? obj)
272 {
273 return ReferenceEquals(this, obj);
274 }
275
277 public override int GetHashCode()
278 {
279 throw new NotSupportedException();
280 }
281 }
282
283#endregion
284}
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implementation for the null-value.
Definition BadObject.cs:235
override string ToSafeString(List< BadObject > done)
Definition BadObject.cs:264
object Value
The Value of the Native Object.
Definition BadObject.cs:239
bool Equals(IBadNative? other)
Returns the Prototype for the Null Object.
Definition BadObject.cs:249
Type Type
The Type of the Native Object.
Definition BadObject.cs:242
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:224
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:141
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:130
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:70
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.