MethodInfoFunctionInstance.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Globalization;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using Jint.Native;
  6. using Jint.Native.Function;
  7. namespace Jint.Runtime.Interop
  8. {
  9. internal sealed class MethodInfoFunctionInstance : FunctionInstance
  10. {
  11. private static readonly JsString _name = new JsString("Function");
  12. private readonly MethodDescriptor[] _methods;
  13. private readonly ClrFunctionInstance _fallbackClrFunctionInstance;
  14. public MethodInfoFunctionInstance(Engine engine, MethodDescriptor[] methods)
  15. : base(engine, _name)
  16. {
  17. _methods = methods;
  18. _prototype = engine.Function.PrototypeObject;
  19. }
  20. public MethodInfoFunctionInstance(Engine engine, MethodDescriptor[] methods, ClrFunctionInstance fallbackClrFunctionInstance)
  21. : this(engine, methods)
  22. {
  23. _fallbackClrFunctionInstance = fallbackClrFunctionInstance;
  24. }
  25. public override JsValue Call(JsValue thisObject, JsValue[] jsArguments)
  26. {
  27. JsValue[] ArgumentProvider(MethodDescriptor method)
  28. {
  29. if (method.IsExtensionMethod)
  30. {
  31. var jsArgumentsTemp = new JsValue[1 + jsArguments.Length];
  32. jsArgumentsTemp[0] = thisObject;
  33. Array.Copy(jsArguments, 0, jsArgumentsTemp, 1, jsArguments.Length);
  34. return method.HasParams
  35. ? ProcessParamsArrays(jsArgumentsTemp, method)
  36. : jsArgumentsTemp;
  37. }
  38. return method.HasParams
  39. ? ProcessParamsArrays(jsArguments, method)
  40. : jsArguments;
  41. }
  42. var converter = Engine.ClrTypeConverter;
  43. object[] parameters = null;
  44. foreach (var tuple in TypeConverter.FindBestMatch(_engine, _methods, ArgumentProvider))
  45. {
  46. var method = tuple.Item1;
  47. var arguments = tuple.Item2;
  48. var methodParameters = method.Parameters;
  49. if (parameters == null || parameters.Length != methodParameters.Length)
  50. {
  51. parameters = new object[methodParameters.Length];
  52. }
  53. var argumentsMatch = true;
  54. for (var i = 0; i < parameters.Length; i++)
  55. {
  56. var methodParameter = methodParameters[i];
  57. var parameterType = methodParameter.ParameterType;
  58. var argument = arguments.Length > i ? arguments[i] : null;
  59. if (typeof(JsValue).IsAssignableFrom(parameterType))
  60. {
  61. parameters[i] = argument;
  62. }
  63. else if (argument is null)
  64. {
  65. // optional
  66. parameters[i] = System.Type.Missing;
  67. }
  68. else if (parameterType == typeof(JsValue[]) && argument.IsArray())
  69. {
  70. // Handle specific case of F(params JsValue[])
  71. var arrayInstance = argument.AsArray();
  72. var len = TypeConverter.ToInt32(arrayInstance.Get(CommonProperties.Length, this));
  73. var result = new JsValue[len];
  74. for (uint k = 0; k < len; k++)
  75. {
  76. result[k] = arrayInstance.TryGetValue(k, out var value) ? value : Undefined;
  77. }
  78. parameters[i] = result;
  79. }
  80. else
  81. {
  82. if (!converter.TryConvert(argument.ToObject(), parameterType, CultureInfo.InvariantCulture, out parameters[i]))
  83. {
  84. argumentsMatch = false;
  85. break;
  86. }
  87. if (parameters[i] is LambdaExpression lambdaExpression)
  88. {
  89. parameters[i] = lambdaExpression.Compile();
  90. }
  91. }
  92. }
  93. if (!argumentsMatch)
  94. {
  95. continue;
  96. }
  97. // todo: cache method info
  98. try
  99. {
  100. return FromObject(Engine, method.Method.Invoke(thisObject.ToObject(), parameters));
  101. }
  102. catch (TargetInvocationException exception)
  103. {
  104. ExceptionHelper.ThrowMeaningfulException(_engine, exception);
  105. }
  106. }
  107. if (_fallbackClrFunctionInstance != null)
  108. {
  109. return _fallbackClrFunctionInstance.Call(thisObject, jsArguments);
  110. }
  111. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "No public methods with the specified arguments were found.");
  112. }
  113. /// <summary>
  114. /// Reduces a flat list of parameters to a params array, if needed
  115. /// </summary>
  116. private JsValue[] ProcessParamsArrays(JsValue[] jsArguments, MethodDescriptor methodInfo)
  117. {
  118. var parameters = methodInfo.Parameters;
  119. var nonParamsArgumentsCount = parameters.Length - 1;
  120. if (jsArguments.Length < nonParamsArgumentsCount)
  121. {
  122. return jsArguments;
  123. }
  124. var argsToTransform = jsArguments.Skip(nonParamsArgumentsCount);
  125. if (argsToTransform.Length == 1 && argsToTransform[0].IsArray())
  126. {
  127. return jsArguments;
  128. }
  129. var jsArray = Engine.Array.Construct(Arguments.Empty);
  130. Engine.Array.PrototypeObject.Push(jsArray, argsToTransform);
  131. var newArgumentsCollection = new JsValue[nonParamsArgumentsCount + 1];
  132. for (var j = 0; j < nonParamsArgumentsCount; ++j)
  133. {
  134. newArgumentsCollection[j] = jsArguments[j];
  135. }
  136. newArgumentsCollection[nonParamsArgumentsCount] = jsArray;
  137. return newArgumentsCollection;
  138. }
  139. }
  140. }