BadScript 2
Loading...
Searching...
No Matches
BadNative.cs
Go to the documentation of this file.
3
5
10public class BadNative<T> : BadObject, IBadNative
11{
15 private static readonly BadClassPrototype s_Prototype = new BadNativeClassPrototype<BadNative<T>>(typeof(T).Name,
16 (_, args) =>
17 {
18 if (args.Length != 1 || args[0] is not BadNative<T> t)
19 {
20 throw new BadRuntimeException("BadNative<T> constructor takes one argument of type BadNative<T>");
21 }
22
23 return t;
24 }
25 );
26
30 private readonly T m_Value;
31
32
38 public BadNative(T value)
39 {
40 if (value == null)
41 {
42 throw new Exception("Can not construct native object with null value");
43 }
44
45 m_Value = value;
46 }
47
51 public T Value => m_Value;
52
53#region IBadNative Members
54
58 object IBadNative.Value => m_Value!;
59
63 Type IBadNative.Type => m_Value!.GetType();
64
65
71 public bool Equals(IBadNative? other)
72 {
73 if (other is null)
74 {
75 return false;
76 }
77
78 IBadNative thisN = this;
79
80 return thisN.Value.Equals(other.Value);
81 }
82
83#endregion
84
86 public override int GetHashCode()
87 {
88 return EqualityComparer<T>.Default.GetHashCode(Value!);
89 }
90
91
93 public override string ToSafeString(List<BadObject> done)
94 {
95 return m_Value!.ToString()!;
96 }
97
104 public static bool operator ==(BadNative<T> a, BadObject b)
105 {
106 return a.Equals(b);
107 }
108
115 public static bool operator !=(BadNative<T> a, BadObject b)
116 {
117 return !(a == b);
118 }
119
125 public override bool Equals(object? obj)
126 {
127 return obj is IBadNative other && Equals(other);
128 }
129
132 {
133 return s_Prototype;
134 }
135
137 public override bool HasProperty(string propName, BadScope? caller = null)
138 {
139 return caller != null && caller.Provider.HasObject<T>(propName);
140 }
141
143 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
144 {
145 return BadObjectReference.Make($"BadNative<{typeof(T).Name}>.{propName}",
146 (p) => caller != null
147 ? caller.Provider.GetObject<T>(propName, this, caller, p)
148 : throw BadRuntimeException.Create(caller,
149 $"No property named {propName} for type {GetType().Name}"
150 )
151 );
152 }
153}
Implements the Scope for the Script Engine.
Definition BadScope.cs:16
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
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.
static bool operator!=(BadNative< T > a, BadObject b)
Implements the != operator.
Definition BadNative.cs:115
static readonly BadClassPrototype s_Prototype
The Prototype for the Native Type.
Definition BadNative.cs:15
T Value
The Value of the Native Type.
Definition BadNative.cs:51
override string ToSafeString(List< BadObject > done)
Definition BadNative.cs:93
BadNative(T value)
Creates a new Native Type.
Definition BadNative.cs:38
override bool Equals(object? obj)
Returns true if this and the other objects are equal.
Definition BadNative.cs:125
bool Equals(IBadNative? other)
Returns true if this and the other native types are equal.
Definition BadNative.cs:71
static bool operator==(BadNative< T > a, BadObject b)
Implements the == operator.
Definition BadNative.cs:104
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 BadNative.cs:137
Type IBadNative. Type
The Type of the Native Type.
Definition BadNative.cs:63
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadNative.cs:143
override BadClassPrototype GetPrototype()
Definition BadNative.cs:131
readonly T m_Value
The Value of the Native Type.
Definition BadNative.cs:30
Implements a Class Prototype for the BadScript Language.
Defines properties for Native Types.
Definition IBadNative.cs:7
object Value
The Value of the Native Object.
Definition IBadNative.cs:11
Contains the Error Objects for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6