MethodDescriptor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Jint.Native;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. namespace Jint.Runtime.Interop
  7. {
  8. internal 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. try
  92. {
  93. for (var i = 0; i < arguments.Length; i++)
  94. {
  95. var parameterType = methodParameters[i].ParameterType;
  96. if (typeof(JsValue).IsAssignableFrom(parameterType))
  97. {
  98. parameters[i] = arguments[i];
  99. }
  100. else
  101. {
  102. parameters[i] = _engine.ClrTypeConverter.Convert(
  103. arguments[i].ToObject(),
  104. parameterType,
  105. System.Globalization.CultureInfo.InvariantCulture);
  106. }
  107. }
  108. if (Method is MethodInfo m)
  109. {
  110. var retVal = m.Invoke(instance, parameters);
  111. return JsValue.FromObject(_engine, retVal);
  112. }
  113. else if (Method is ConstructorInfo c)
  114. {
  115. var retVal = c.Invoke(parameters);
  116. return JsValue.FromObject(_engine, retVal);
  117. }
  118. else
  119. {
  120. throw new Exception("Method is unknown type");
  121. }
  122. }
  123. catch (TargetInvocationException exception)
  124. {
  125. ExceptionHelper.ThrowMeaningfulException(_engine, exception);
  126. return null;
  127. }
  128. }
  129. }
  130. }