ExtensionMethodCache.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading;
  6. using Jint.Extensions;
  7. namespace Jint.Runtime.Interop.Reflection
  8. {
  9. /// <summary>
  10. /// A extension method lookup that can be shared between engines, build based on extension methods provided via options.
  11. /// </summary>
  12. internal sealed class ExtensionMethodCache
  13. {
  14. internal static readonly ExtensionMethodCache Empty = new(new Dictionary<Type, MethodInfo[]>());
  15. // starting point containing only extension methods targeting one type, based on given options configuration
  16. private readonly Dictionary<Type, MethodInfo[]> _allExtensionMethods;
  17. // cache of all possibilities for type including base types and implemented interfaces
  18. private Dictionary<Type, MethodInfo[]> _extensionMethods = new();
  19. private ExtensionMethodCache(Dictionary<Type, MethodInfo[]> extensionMethods)
  20. {
  21. _allExtensionMethods = extensionMethods;
  22. }
  23. internal static ExtensionMethodCache Build(List<Type> extensionMethodContainerTypes)
  24. {
  25. if (extensionMethodContainerTypes.Count == 0)
  26. {
  27. return Empty;
  28. }
  29. Type GetTypeDefinition(Type type)
  30. {
  31. return type.IsConstructedGenericType && type.GenericTypeArguments.Any(x => x.IsGenericParameter) ?
  32. type.GetGenericTypeDefinition() : type;
  33. }
  34. var methodsByTarget = extensionMethodContainerTypes
  35. .SelectMany(x => x.GetExtensionMethods())
  36. .GroupBy(x => GetTypeDefinition(x.GetParameters()[0].ParameterType))
  37. .ToDictionary(x => x.Key, x => x.ToArray());
  38. return new ExtensionMethodCache(methodsByTarget);
  39. }
  40. public bool HasMethods => _allExtensionMethods.Count > 0;
  41. private MethodInfo BindMethodGenericParameters(MethodInfo method)
  42. {
  43. if (method.IsGenericMethodDefinition && method.ContainsGenericParameters)
  44. {
  45. var methodGenerics = method.GetGenericArguments();
  46. var parameterList = Enumerable.Repeat(typeof(object), methodGenerics.Length).ToArray();
  47. try
  48. {
  49. return method.MakeGenericMethod(parameterList);
  50. }
  51. catch
  52. {
  53. // Generic parameter constraints failed probably.
  54. // If it does not work, let it be. We don't need to do anything.
  55. }
  56. }
  57. return method;
  58. }
  59. public bool TryGetExtensionMethods(Type objectType, out MethodInfo[] methods)
  60. {
  61. var methodLookup = _extensionMethods;
  62. if (methodLookup.TryGetValue(objectType, out methods))
  63. {
  64. return methods.Length > 0;
  65. }
  66. var results = new List<MethodInfo>();
  67. if (_allExtensionMethods.TryGetValue(objectType, out var ownExtensions))
  68. {
  69. results.AddRange(ownExtensions);
  70. }
  71. foreach (var parentType in GetParentTypes(objectType))
  72. {
  73. if (_allExtensionMethods.TryGetValue(parentType, out var parentExtensions))
  74. {
  75. results.AddRange(parentExtensions);
  76. }
  77. }
  78. methods = results.Select(BindMethodGenericParameters).ToArray();
  79. // racy, we don't care, worst case we'll catch up later
  80. Interlocked.CompareExchange(ref _extensionMethods, new Dictionary<Type, MethodInfo[]>(methodLookup)
  81. {
  82. [objectType] = methods
  83. }, methodLookup);
  84. return methods.Length > 0;
  85. }
  86. private static IEnumerable<Type> GetParentTypes(Type type)
  87. {
  88. // is there any base type?
  89. if (type == null)
  90. {
  91. yield break;
  92. }
  93. // return all implemented or inherited interfaces
  94. foreach (var i in type.GetInterfaces())
  95. {
  96. yield return i;
  97. if (i.IsConstructedGenericType)
  98. {
  99. yield return i.GetGenericTypeDefinition();
  100. }
  101. }
  102. // return all inherited types
  103. var currentBaseType = type.BaseType;
  104. while (currentBaseType != null)
  105. {
  106. yield return currentBaseType;
  107. if (currentBaseType.IsConstructedGenericType)
  108. {
  109. yield return currentBaseType.GetGenericTypeDefinition();
  110. }
  111. currentBaseType = currentBaseType.BaseType;
  112. }
  113. }
  114. }
  115. }