1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
16 using System.Collections.Generic;
17 using System.Reflection;
19 using System.Reflection.Emit;
21 using System.Security;
22 using Microsoft.Scripting.Runtime;
23 using Microsoft.Scripting.Utils;
25 namespace Microsoft.Scripting.Interpreter {
26 public abstract partial class CallInstruction : Instruction {
27 public abstract MethodInfo Info { get; }
30 /// The number of arguments including "this" for instance methods.
32 public abstract int ArgumentCount { get; }
36 internal CallInstruction() { }
38 private static readonly Dictionary<MethodInfo, CallInstruction> _cache = new Dictionary<MethodInfo, CallInstruction>();
40 /// <exception cref="SecurityException">Instruction can't be created due to insufficient privileges.</exception>
41 public static CallInstruction Create(MethodInfo info) {
42 return Create(info, info.GetParameters());
45 /// <exception cref="SecurityException">Instruction can't be created due to insufficient privileges.</exception>
46 public static CallInstruction Create(MethodInfo info, ParameterInfo[] parameters) {
47 int argumentCount = parameters.Length;
52 // A workaround for CLR bug #796414 (Unable to create delegates for Array.Get/Set):
53 // T[]::Address - not supported by ETs due to T& return value
54 if (info.DeclaringType != null && info.DeclaringType.IsArray && (info.Name == "Get" || info.Name == "Set")) {
55 return GetArrayAccessor(info, argumentCount);
58 if (ReflectionUtils.IsDynamicMethod(info) || !info.IsStatic && info.DeclaringType.IsValueType()) {
59 return new MethodInfoCallInstruction(info, argumentCount);
62 if (argumentCount >= MaxHelpers) {
63 // no delegate for this size, fallback to reflection invoke
64 return new MethodInfoCallInstruction(info, argumentCount);
67 foreach (ParameterInfo pi in parameters) {
68 if (pi.ParameterType.IsByRef) {
69 // we don't support ref args via generics.
70 return new MethodInfoCallInstruction(info, argumentCount);
74 // see if we've created one w/ a delegate
76 if (ShouldCache(info)) {
78 if (_cache.TryGetValue(info, out res)) {
86 if (argumentCount < MaxArgs) {
87 res = FastCreate(info, parameters);
89 res = SlowCreate(info, parameters);
91 } catch (TargetInvocationException tie) {
92 if (!(tie.InnerException is NotSupportedException)) {
93 throw tie.InnerException;
96 res = new MethodInfoCallInstruction(info, argumentCount);
97 } catch (NotSupportedException) {
98 // if Delegate.CreateDelegate can't handle the method fallback to
99 // the slow reflection version. For example this can happen w/
100 // a generic method defined on an interface and implemented on a class or
101 // a virtual generic method.
102 res = new MethodInfoCallInstruction(info, argumentCount);
105 // cache it for future users if it's a reasonable method to cache
106 if (ShouldCache(info)) {
115 private static CallInstruction GetArrayAccessor(MethodInfo info, int argumentCount) {
116 Type arrayType = info.DeclaringType;
117 bool isGetter = info.Name == "Get";
118 switch (arrayType.GetArrayRank()) {
120 return Create(isGetter ?
121 arrayType.GetMethod("GetValue", new[] { typeof(int)}) :
122 new Action<Array, int, object>(ArrayItemSetter1).GetMethodInfo()
126 return Create(isGetter ?
127 arrayType.GetMethod("GetValue", new[] { typeof(int), typeof(int) }) :
128 new Action<Array, int, int, object>(ArrayItemSetter2).GetMethodInfo()
132 return Create(isGetter ?
133 arrayType.GetMethod("GetValue", new[] { typeof(int), typeof(int), typeof(int) }) :
134 new Action<Array, int, int, int, object>(ArrayItemSetter3).GetMethodInfo()
138 return new MethodInfoCallInstruction(info, argumentCount);
142 public static void ArrayItemSetter1(Array array, int index0, object value) {
143 array.SetValue(value, index0);
146 public static void ArrayItemSetter2(Array array, int index0, int index1, object value) {
147 array.SetValue(value, index0, index1);
150 public static void ArrayItemSetter3(Array array, int index0, int index1, int index2, object value) {
151 array.SetValue(value, index0, index1, index2);
154 private static bool ShouldCache(MethodInfo info) {
155 return !ReflectionUtils.IsDynamicMethod(info);
159 /// Gets the next type or null if no more types are available.
161 private static Type TryGetParameterOrReturnType(MethodInfo target, ParameterInfo[] pi, int index) {
162 if (!target.IsStatic) {
165 return target.DeclaringType;
169 if (index < pi.Length) {
171 return pi[index].ParameterType;
174 if (target.ReturnType == typeof(void) || index > pi.Length) {
175 // no more parameters
179 // last parameter on Invoke is return type
180 return target.ReturnType;
183 private static bool IndexIsNotReturnType(int index, MethodInfo target, ParameterInfo[] pi) {
184 return pi.Length != index || (pi.Length == index && !target.IsStatic);
188 /// Uses reflection to create new instance of the appropriate ReflectedCaller
190 private static CallInstruction SlowCreate(MethodInfo info, ParameterInfo[] pis) {
191 List<Type> types = new List<Type>();
192 if (!info.IsStatic) types.Add(info.DeclaringType);
193 foreach (ParameterInfo pi in pis) {
194 types.Add(pi.ParameterType);
196 if (info.ReturnType != typeof(void)) {
197 types.Add(info.ReturnType);
199 Type[] arrTypes = types.ToArray();
201 return (CallInstruction)Activator.CreateInstance(GetHelperType(info, arrTypes), info);
208 public sealed override int ProducedStack { get { return Info.ReturnType == typeof(void) ? 0 : 1; } }
209 public sealed override int ConsumedStack { get { return ArgumentCount; } }
211 public sealed override string InstructionName {
212 get { return "Call"; }
215 public override string ToString() {
216 return "Call(" + Info + ")";
222 internal sealed partial class MethodInfoCallInstruction : CallInstruction {
223 private readonly MethodInfo _target;
224 private readonly int _argumentCount;
226 public override MethodInfo Info { get { return _target; } }
227 public override int ArgumentCount { get { return _argumentCount; } }
229 internal MethodInfoCallInstruction(MethodInfo target, int argumentCount) {
231 _argumentCount = argumentCount;
234 public override object Invoke(params object[] args) {
235 return InvokeWorker(args);
238 public override object InvokeInstance(object instance, params object[] args) {
239 if (_target.IsStatic) {
241 return _target.Invoke(null, args);
242 } catch (TargetInvocationException e) {
243 throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
248 return _target.Invoke(instance, args);
249 } catch (TargetInvocationException e) {
250 throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
254 private object InvokeWorker(params object[] args) {
255 if (_target.IsStatic) {
257 return _target.Invoke(null, args);
258 } catch (TargetInvocationException e) {
259 throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
264 return _target.Invoke(args[0], GetNonStaticArgs(args));
265 } catch (TargetInvocationException e) {
266 throw ExceptionHelpers.UpdateForRethrow(e.InnerException);
270 private static object[] GetNonStaticArgs(object[] args) {
271 object[] newArgs = new object[args.Length - 1];
272 for (int i = 0; i < newArgs.Length; i++) {
273 newArgs[i] = args[i + 1];
278 public sealed override int Run(InterpretedFrame frame) {
279 int first = frame.StackIndex - _argumentCount;
280 object[] args = new object[_argumentCount];
281 for (int i = 0; i < args.Length; i++) {
282 args[i] = frame.Data[first + i];
285 object ret = Invoke(args);
286 if (_target.ReturnType != typeof(void)) {
287 frame.Data[first] = ret;
288 frame.StackIndex = first + 1;
290 frame.StackIndex = first;