BadScript 2
Loading...
Searching...
No Matches
BadReflectedMethod.cs
Go to the documentation of this file.
1using System.Reflection;
2
8
10
15{
19 private readonly List<MethodInfo> m_Methods = new List<MethodInfo>();
20
25 public BadReflectedMethod(MethodInfo method) : base(method.Name)
26 {
27 m_Methods.Add(method);
28 }
29
31 public override bool IsReadOnly => true;
32
33
38 public void AddMethod(MethodInfo method)
39 {
40 m_Methods.Add(method);
41 }
42
48 private BadFunction CreateFunction(object? instance)
49 {
50 bool isStatic = instance == null;
51
52 return new BadInteropFunction(Name,
53 args => Invoke(instance, args),
54 isStatic,
56 new BadFunctionParameter("args", false, false, true)
57 );
58 }
59
66 private static bool CanConvert(BadObject o, Type t)
67 {
68 if (o.CanUnwrap())
69 {
70 object? obj = o.Unwrap();
71
72 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
73 if (obj == null)
74 {
75 return !t.IsValueType;
76 }
77
78 return t.IsInstanceOfType(obj) ||
79 (t.IsNumericType() &&
80 obj.GetType()
81 .IsNumericType());
82 }
83
84 if (o is not BadReflectedObject ro)
85 {
86 return false;
87 }
88
89 {
90 object obj = ro.Instance;
91
92 return t.IsInstanceOfType(obj);
93 }
94 }
95
103 private static object? ConvertObject(BadObject o, Type t)
104 {
105 object? obj;
106
107 if (o.CanUnwrap())
108 {
109 obj = o.Unwrap();
110 }
111 else if (o is BadReflectedObject ro)
112 {
113 obj = ro.Instance;
114 }
115 else
116 {
117 throw new BadRuntimeException("Cannot convert object");
118 }
119
120 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
121 if (obj == null)
122 {
123 return null;
124 }
125
126 if (t.IsInstanceOfType(obj))
127 {
128 return obj;
129 }
130
131 if (t.IsNumericType())
132 {
133 return Convert.ChangeType(obj, t);
134 }
135
136 throw new BadRuntimeException("Cannot convert object");
137 }
138
147 private object?[] FindImplementation(object? instance, IReadOnlyList<BadObject> args, out MethodInfo info)
148 {
149 foreach (MethodInfo method in m_Methods)
150 {
151 if ((instance == null && !method.IsStatic) || (instance != null && method.IsStatic))
152 {
153 continue;
154 }
155
156 ParameterInfo[] parameters = method.GetParameters();
157
158 if (parameters.Length != args.Count)
159 {
160 continue;
161 }
162
163 object?[] converted = new object?[args.Count];
164 bool skipThis = false;
165
166 for (int i = 0; i < parameters.Length; i++)
167 {
168 ParameterInfo parameter = parameters[i];
169 BadObject argument = args[i];
170
171 if (!CanConvert(argument, parameter.ParameterType))
172 {
173 skipThis = true;
174
175 break;
176 }
177
178 converted[i] = ConvertObject(argument, parameter.ParameterType);
179 }
180
181 if (skipThis)
182 {
183 continue;
184 }
185
186 info = method;
187
188 return converted;
189 }
190
191 throw new BadRuntimeException("No matching method found");
192 }
193
200 private BadObject Invoke(object? instance, BadObject[] args)
201 {
202 object?[] implArgs = FindImplementation(instance, args, out MethodInfo info);
203
204 return Wrap(info.Invoke(instance, implArgs));
205 }
206
208 public override BadObject Get(object? instance)
209 {
210 return CreateFunction(instance);
211 }
212
214 public override void Set(object? instance, BadObject o)
215 {
216 throw new BadRuntimeException("Can not set a value to a method");
217 }
218}
Interop Function taking an array of arguments.
static BadObject Wrap(object? o)
Wraps an Object into a BadObject.
static ? object ConvertObject(BadObject o, Type t)
Converts the given Object to the given Type.
void AddMethod(MethodInfo method)
Adds a Method to the Reflected Method.
BadReflectedMethod(MethodInfo method)
Creates a new BadReflectedMethod.
static bool CanConvert(BadObject o, Type t)
Indicates if the given Object can be converted to the given Type.
BadFunction CreateFunction(object? instance)
Creates a new Function for the Reflected Method.
BadObject Invoke(object? instance, BadObject[] args)
Invokes the Method with the given Arguments.
object?[] FindImplementation(object? instance, IReadOnlyList< BadObject > args, out MethodInfo info)
Finds the Implementation of the Method that matches the given Arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements a function that can be called from the script.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Member Classes for Reflection Objects.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10