MethodInfoFunctionInstance.cs 6.3 KB

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