BadScript 2
Loading...
Searching...
No Matches
BadInterfaceFunctionConstraint.cs
Go to the documentation of this file.
7
9
15{
19 private readonly BadClassPrototype? m_Prototype;
20
24 public readonly string Name;
25
30
34 public readonly BadExpression? Return;
35
42 public BadInterfaceFunctionConstraint(string name, BadExpression? @return, BadFunctionParameter[] parameters)
43 {
44 Name = name;
45 Return = @return;
46 Parameters = parameters;
47 }
48
49
57 public BadInterfaceFunctionConstraint(string name, BadExpression? @return, BadClassPrototype returnProto, BadFunctionParameter[] parameters)
58 {
59 Name = name;
60 Return = @return;
61 Parameters = parameters;
62 m_Prototype = returnProto;
63 }
64
65
67 public override string ToString()
68 {
69 return "FunctionConstraint";
70 }
71
73 public override void Validate(BadClass obj, List<BadInterfaceValidatorError> errors)
74 {
75 if (Return != null && m_Prototype == null)
76 {
77 throw new BadRuntimeException("Return Type was not Evaluated.");
78 }
79
80 if (!obj.HasProperty(Name, obj.Scope))
81 {
82 errors.Add(new BadInterfaceValidatorError($"Missing Property. Expected {Name}", this));
83
84 return;
85 }
86
87 BadObject o = obj.GetProperty(Name).Dereference();
88
89 if (o is not BadFunction f)
90 {
91 errors.Add(new BadInterfaceValidatorError($"Property {Name} is not a function", this));
92
93 return;
94 }
95
96 if (m_Prototype != null && !m_Prototype.IsSuperClassOf(f.ReturnType))
97 {
98 errors.Add(
100 $"Return Type Mismatch. Expected {m_Prototype} but got {f.ReturnType} in {f}",
101 this
102 )
103 );
104 }
105
106 if (f.Parameters.Length != Parameters.Length)
107 {
108 errors.Add(
110 $"Parameter Count Mismatch. Expected {Parameters.Length} but got {f.Parameters.Length} in {f}",
111 this
112 )
113 );
114
115 return;
116 }
117
118
119 for (int i = 0; i < Parameters.Length; i++)
120 {
121 BadFunctionParameter actual = f.Parameters[i];
122 BadFunctionParameter expected = Parameters[i];
123
124 if (actual.IsOptional != expected.IsOptional)
125 {
126 errors.Add(
128 $"{f}: Parameter Optional Flags are not equal. Implementation: {actual}, Expectation: {expected}",
129 this
130 )
131 );
132
133 return;
134 }
135
136 if (actual.IsNullChecked != expected.IsNullChecked)
137 {
138 errors.Add(
140 $"{f}: Parameter Null Check Flags are not equal. Implementation: {actual}, Expectation: {expected}",
141 this
142 )
143 );
144
145 return;
146 }
147
148 if (actual.IsRestArgs != expected.IsRestArgs)
149 {
150 errors.Add(
152 $"{f}: Parameter Rest Args Flags are not equal. Implementation: {actual}, Expectation: {expected}",
153 this
154 )
155 );
156
157
158 return;
159 }
160
161 if (actual.TypeExpr == null)
162 {
163 continue;
164 }
165
166 if (expected.Type == null || actual.Type == expected.Type)
167 {
168 continue;
169 }
170
171 errors.Add(
173 $"{f}: Parameter Types not equal. Implementation: {actual}, Expectation: {expected}",
174 this
175 )
176 );
177
178 return;
179 }
180 }
181
182 public override bool Equals(BadInterfaceConstraint? other)
183 {
184 return other is BadInterfaceFunctionConstraint c &&
185 c.Name == Name &&
186 c.Return == Return &&
187 c.Parameters.Length == Parameters.Length &&
188 c.Parameters.Zip(Parameters, (x, y) => (x, y)).All(z => z.x.Equals(z.y));
189 }
190
191 protected override int GetConstraintHash()
192 {
194 }
195}
Base Implementation for all Expressions used inside the Script.
Implements a Function Constraint for an Interface The Constraints specifies how a specific function s...
BadInterfaceFunctionConstraint(string name, BadExpression? @return, BadFunctionParameter[] parameters)
Creates a new Function Constraint.
BadInterfaceFunctionConstraint(string name, BadExpression? @return, BadClassPrototype returnProto, BadFunctionParameter[] parameters)
Creates a new Function Constraint.
readonly BadFunctionParameter[] Parameters
The Parameters of the Function.
readonly? BadClassPrototype m_Prototype
The Return Type Prototype.
override void Validate(BadClass obj, List< BadInterfaceValidatorError > errors)
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements a function that can be called from the script.
bool IsOptional
Indicates if this parameter is optional.
BadExpression? TypeExpr
The Expression that returns the type of the parameter if evaluated.
BadClassPrototype? Type
The Class Prototype of the Parameter.
bool IsRestArgs
Indicates if this parameter is the rest parameter of the function.
bool IsNullChecked
Indicates if this parameter is null checked by the runtime.
Implements a Type Instance in the BadScript Language.
Definition BadClass.cs:11
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
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.
Implements Combination of HashCode Functions Taken from decompiled source of System....
Contains the Type Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains Runtime Interface Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Utility Functions and Classes.
Definition BadEnum.cs:5