MethodDescriptor.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Jint.Native;
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using Jint.Extensions;
  5. #pragma warning disable IL2072
  6. namespace Jint.Runtime.Interop
  7. {
  8. internal sealed class MethodDescriptor
  9. {
  10. internal MethodDescriptor(MethodBase method)
  11. {
  12. Method = method;
  13. Parameters = method.GetParameters();
  14. IsExtensionMethod = method.IsDefined(typeof(ExtensionAttribute), true);
  15. foreach (var parameter in Parameters)
  16. {
  17. if (Attribute.IsDefined(parameter, typeof(ParamArrayAttribute)))
  18. {
  19. HasParams = true;
  20. break;
  21. }
  22. if (parameter.HasDefaultValue)
  23. {
  24. ParameterDefaultValuesCount++;
  25. }
  26. }
  27. }
  28. public MethodBase Method { get; }
  29. public ParameterInfo[] Parameters { get; }
  30. public bool HasParams { get; }
  31. public int ParameterDefaultValuesCount { get; }
  32. public bool IsExtensionMethod { get; }
  33. public static MethodDescriptor[] Build<T>(List<T> source) where T : MethodBase
  34. {
  35. var descriptors = new MethodDescriptor[source.Count];
  36. for (var i = 0; i < source.Count; i++)
  37. {
  38. descriptors[i] = new MethodDescriptor(source[i]);
  39. }
  40. return Prioritize(descriptors);
  41. }
  42. public static MethodDescriptor[] Build<T>(T[] source) where T : MethodBase
  43. {
  44. var descriptors = new MethodDescriptor[source.Length];
  45. for (var i = 0; i < source.Length; i++)
  46. {
  47. descriptors[i] = new MethodDescriptor(source[i]);
  48. }
  49. return Prioritize(descriptors);
  50. }
  51. private static MethodDescriptor[] Prioritize(MethodDescriptor[] descriptors)
  52. {
  53. static int CreateComparison(MethodDescriptor d1, MethodDescriptor d2)
  54. {
  55. // if its a generic method, put it on the end
  56. if (d1.Method.IsGenericMethod && !d2.Method.IsGenericMethod)
  57. {
  58. return 1;
  59. }
  60. if (d2.Method.IsGenericMethod && !d1.Method.IsGenericMethod)
  61. {
  62. return -1;
  63. }
  64. // put params versions to end, they can be tricky to match and can cause trouble / extra overhead
  65. if (d1.HasParams && !d2.HasParams)
  66. {
  67. return 1;
  68. }
  69. if (d2.HasParams && !d1.HasParams)
  70. {
  71. return -1;
  72. }
  73. // then favor less parameters
  74. if (d1.Parameters.Length > d2.Parameters.Length)
  75. {
  76. return 1;
  77. }
  78. if (d2.Parameters.Length > d1.Parameters.Length)
  79. {
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. Array.Sort(descriptors, CreateComparison);
  85. return descriptors;
  86. }
  87. public JsValue Call(Engine engine, object? instance, JsValue[] arguments)
  88. {
  89. var parameters = new object?[arguments.Length];
  90. var methodParameters = Parameters;
  91. var valueCoercionType = engine.Options.Interop.ValueCoercion;
  92. try
  93. {
  94. for (var i = 0; i < arguments.Length; i++)
  95. {
  96. var methodParameter = methodParameters[i];
  97. var parameterType = methodParameter.ParameterType;
  98. var value = arguments[i];
  99. object? converted;
  100. if (typeof(JsValue).IsAssignableFrom(parameterType))
  101. {
  102. converted = value;
  103. }
  104. else if (value.IsUndefined() && methodParameter.IsOptional)
  105. {
  106. // undefined is considered missing, null is consider explicit value
  107. converted = methodParameter.DefaultValue;
  108. }
  109. else if (!ReflectionExtensions.TryConvertViaTypeCoercion(parameterType, valueCoercionType, value, out converted))
  110. {
  111. converted = engine.TypeConverter.Convert(
  112. value.ToObject(),
  113. parameterType,
  114. System.Globalization.CultureInfo.InvariantCulture);
  115. }
  116. parameters[i] = converted;
  117. }
  118. if (Method is MethodInfo m)
  119. {
  120. var retVal = m.Invoke(instance, parameters);
  121. return JsValue.FromObject(engine, retVal);
  122. }
  123. else if (Method is ConstructorInfo c)
  124. {
  125. var retVal = c.Invoke(parameters);
  126. return JsValue.FromObject(engine, retVal);
  127. }
  128. else
  129. {
  130. throw new NotSupportedException("Method is unknown type");
  131. }
  132. }
  133. catch (TargetInvocationException exception)
  134. {
  135. ExceptionHelper.ThrowMeaningfulException(engine, exception);
  136. return null;
  137. }
  138. }
  139. }
  140. }