DelegateWrapper.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Jint.Native;
  5. using Jint.Native.Function;
  6. namespace Jint.Runtime.Interop
  7. {
  8. /// <summary>
  9. /// Represents a FunctionInstance wrapper around a CLR method. This is used by user to pass
  10. /// custom methods to the engine.
  11. /// </summary>
  12. public sealed class DelegateWrapper : FunctionInstance
  13. {
  14. private readonly Delegate _d;
  15. public DelegateWrapper(Engine engine, Delegate d) : base(engine, null, null, false)
  16. {
  17. _d = d;
  18. Prototype = engine.Function.PrototypeObject;
  19. }
  20. public override JsValue Call(JsValue thisObject, JsValue[] jsArguments)
  21. {
  22. var parameterInfos = _d.Method.GetParameters();
  23. bool delegateContainsParamsArgument = parameterInfos.Any(p => Attribute.IsDefined(p, typeof(ParamArrayAttribute)));
  24. int delegateArgumentsCount = parameterInfos.Length;
  25. int delegateNonParamsArgumentsCount = delegateContainsParamsArgument ? delegateArgumentsCount - 1 : delegateArgumentsCount;
  26. int jsArgumentsCount = jsArguments.Length;
  27. int jsArgumentsWithoutParamsCount = Math.Min(jsArgumentsCount, delegateNonParamsArgumentsCount);
  28. var parameters = new object[delegateArgumentsCount];
  29. // convert non params parameter to expected types
  30. for (var i = 0; i < jsArgumentsWithoutParamsCount; i++)
  31. {
  32. var parameterType = parameterInfos[i].ParameterType;
  33. if (parameterType == typeof (JsValue))
  34. {
  35. parameters[i] = jsArguments[i];
  36. }
  37. else
  38. {
  39. parameters[i] = Engine.ClrTypeConverter.Convert(
  40. jsArguments[i].ToObject(),
  41. parameterType,
  42. CultureInfo.InvariantCulture);
  43. }
  44. }
  45. // assign null to parameters not provided
  46. for (var i = jsArgumentsWithoutParamsCount; i < delegateNonParamsArgumentsCount; i++)
  47. {
  48. if (parameterInfos[i].ParameterType.IsValueType)
  49. {
  50. parameters[i] = Activator.CreateInstance(parameterInfos[i].ParameterType);
  51. }
  52. else
  53. {
  54. parameters[i] = null;
  55. }
  56. }
  57. // assign params to array and converts each objet to expected type
  58. if(delegateContainsParamsArgument)
  59. {
  60. int paramsArgumentIndex = delegateArgumentsCount - 1;
  61. int paramsCount = Math.Max(0, jsArgumentsCount - delegateNonParamsArgumentsCount);
  62. object[] paramsParameter = new object[paramsCount];
  63. var paramsParameterType = parameterInfos[paramsArgumentIndex].ParameterType.GetElementType();
  64. for (var i = paramsArgumentIndex; i < jsArgumentsCount; i++)
  65. {
  66. var paramsIndex = i - paramsArgumentIndex;
  67. if (paramsParameterType == typeof(JsValue))
  68. {
  69. paramsParameter[paramsIndex] = jsArguments[i];
  70. }
  71. else
  72. {
  73. paramsParameter[paramsIndex] = Engine.ClrTypeConverter.Convert(
  74. jsArguments[i].ToObject(),
  75. paramsParameterType,
  76. CultureInfo.InvariantCulture);
  77. }
  78. }
  79. parameters[paramsArgumentIndex] = paramsParameter;
  80. }
  81. return JsValue.FromObject(Engine, _d.DynamicInvoke(parameters));
  82. }
  83. }
  84. }