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 null
26 );
27
31 private readonly T m_Value;
32
33
39 public BadNative(T value)
40 {
41 if (value == null)
42 {
43 throw new Exception("Can not construct native object with null value");
44 }
45
46 m_Value = value;
47 }
48
52 public T Value => m_Value;
53
54#region IBadNative Members
55
59 object IBadNative.Value => m_Value!;
60
64 Type IBadNative.Type => m_Value!.GetType();
65
66
72 public bool Equals(IBadNative? other)
73 {
74 if (other is null)
75 {
76 return false;
77 }
78
79 IBadNative thisN = this;
80
81 return thisN.Value.Equals(other.Value);
82 }
83
84#endregion
85
87 public override int GetHashCode()
88 {
89 return EqualityComparer<T>.Default.GetHashCode(Value!);
90 }
91
92
94 public override string ToSafeString(List<BadObject> done)
95 {
96 return m_Value!.ToString()!;
97 }
98
105 public static bool operator ==(BadNative<T> a, BadObject b)
106 {
107 return a.Equals(b);
108 }
109
116 public static bool operator !=(BadNative<T> a, BadObject b)
117 {
118 return !(a == b);
119 }
120
126 public override bool Equals(object? obj)
127 {
128 return obj is IBadNative other && Equals(other);
129 }
130
133 {
134 return s_Prototype;
135 }
136
138 public override bool HasProperty(string propName, BadScope? caller = null)
139 {
140 return caller != null && caller.Provider.HasObject<T>(propName);
141 }
142
144 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
145 {
146 return BadObjectReference.Make($"BadNative<{typeof(T).Name}>.{propName}",
147 (p) => caller != null
148 ? caller.Provider.GetObject<T>(propName, this, caller, p)
149 : throw BadRuntimeException.Create(caller,
150 $"No property named {propName} for type {GetType().Name}"
151 )
152 );
153 }
154}
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:116
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:52
override string ToSafeString(List< BadObject > done)
Definition BadNative.cs:94
BadNative(T value)
Creates a new Native Type.
Definition BadNative.cs:39
override bool Equals(object? obj)
Returns true if this and the other objects are equal.
Definition BadNative.cs:126
bool Equals(IBadNative? other)
Returns true if this and the other native types are equal.
Definition BadNative.cs:72
static bool operator==(BadNative< T > a, BadObject b)
Implements the == operator.
Definition BadNative.cs:105
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:138
Type IBadNative. Type
The Type of the Native Type.
Definition BadNative.cs:64
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadNative.cs:144
override BadClassPrototype GetPrototype()
Definition BadNative.cs:132
readonly T m_Value
The Value of the Native Type.
Definition BadNative.cs:31
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