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(
53 Name,
54 args => Invoke(instance, args),
55 isStatic,
57 new BadFunctionParameter("args", false, false, true)
58 );
59 }
60
67 private static bool CanConvert(BadObject o, Type t)
68 {
69 if (o.CanUnwrap())
70 {
71 object? obj = o.Unwrap();
72
73 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
74 if (obj == null)
75 {
76 return !t.IsValueType;
77 }
78
79 return t.IsInstanceOfType(obj) || t.IsNumericType() && obj.GetType().IsNumericType();
80 }
81
82 if (o is not BadReflectedObject ro)
83 {
84 return false;
85 }
86
87 {
88 object obj = ro.Instance;
89
90 return t.IsInstanceOfType(obj);
91 }
92 }
93
101 private static object? ConvertObject(BadObject o, Type t)
102 {
103 object? obj;
104
105 if (o.CanUnwrap())
106 {
107 obj = o.Unwrap();
108 }
109 else if (o is BadReflectedObject ro)
110 {
111 obj = ro.Instance;
112 }
113 else
114 {
115 throw new BadRuntimeException("Cannot convert object");
116 }
117
118 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
119 if (obj == null)
120 {
121 return null;
122 }
123
124 if (t.IsInstanceOfType(obj))
125 {
126 return obj;
127 }
128
129 if (t.IsNumericType())
130 {
131 return Convert.ChangeType(obj, t);
132 }
133
134 throw new BadRuntimeException("Cannot convert object");
135 }
136
145 private object?[] FindImplementation(object? instance, IReadOnlyList<BadObject> args, out MethodInfo info)
146 {
147 foreach (MethodInfo method in m_Methods)
148 {
149 if (instance == null && !method.IsStatic || instance != null && method.IsStatic)
150 {
151 continue;
152 }
153
154 ParameterInfo[] parameters = method.GetParameters();
155
156 if (parameters.Length != args.Count)
157 {
158 continue;
159 }
160
161 object?[] converted = new object?[args.Count];
162 bool skipThis = false;
163
164 for (int i = 0; i < parameters.Length; i++)
165 {
166 ParameterInfo parameter = parameters[i];
167 BadObject argument = args[i];
168
169 if (!CanConvert(argument, parameter.ParameterType))
170 {
171 skipThis = true;
172
173 break;
174 }
175
176 converted[i] = ConvertObject(argument, parameter.ParameterType);
177 }
178
179 if (skipThis)
180 {
181 continue;
182 }
183
184 info = method;
185
186 return converted;
187 }
188
189 throw new BadRuntimeException("No matching method found");
190 }
191
198 private BadObject Invoke(object? instance, BadObject[] args)
199 {
200 object?[] implArgs = FindImplementation(instance, args, out MethodInfo info);
201
202 return Wrap(info.Invoke(instance, implArgs));
203 }
204
206 public override BadObject Get(object? instance)
207 {
208 return CreateFunction(instance);
209 }
210
212 public override void Set(object? instance, BadObject o)
213 {
214 throw new BadRuntimeException("Can not set a value to a method");
215 }
216}
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