ExtensionMethodCache.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading;
  5. using Jint.Extensions;
  6. #pragma warning disable IL2067
  7. #pragma warning disable IL2070
  8. namespace Jint.Runtime.Interop.Reflection
  9. {
  10. /// <summary>
  11. /// A extension method lookup that can be shared between engines, build based on extension methods provided via options.
  12. /// </summary>
  13. internal sealed class ExtensionMethodCache
  14. {
  15. internal static readonly ExtensionMethodCache Empty = new(new Dictionary<Type, MethodInfo[]>());
  16. // starting point containing only extension methods targeting one type, based on given options configuration
  17. private readonly Dictionary<Type, MethodInfo[]> _allExtensionMethods;
  18. // cache of all possibilities for type including base types and implemented interfaces
  19. private Dictionary<Type, MethodInfo[]> _extensionMethods = new();
  20. private ExtensionMethodCache(Dictionary<Type, MethodInfo[]> extensionMethods)
  21. {
  22. _allExtensionMethods = extensionMethods;
  23. }
  24. internal static ExtensionMethodCache Build(List<Type> extensionMethodContainerTypes)
  25. {
  26. if (extensionMethodContainerTypes.Count == 0)
  27. {
  28. return Empty;
  29. }
  30. Type GetTypeDefinition(Type type)
  31. {
  32. return type.IsConstructedGenericType && type.GenericTypeArguments.Any(x => x.IsGenericParameter) ? 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. public bool TryGetExtensionMethods(Type objectType, [NotNullWhen(true)] out MethodInfo[]? methods)
  42. {
  43. if (_allExtensionMethods.Count == 0)
  44. {
  45. methods = Array.Empty<MethodInfo>();
  46. return false;
  47. }
  48. var methodLookup = _extensionMethods;
  49. if (methodLookup.TryGetValue(objectType, out methods))
  50. {
  51. return methods.Length > 0;
  52. }
  53. var results = new List<MethodInfo>();
  54. if (_allExtensionMethods.TryGetValue(objectType, out var ownExtensions))
  55. {
  56. results.AddRange(ownExtensions);
  57. }
  58. foreach (var parentType in GetParentTypes(objectType))
  59. {
  60. if (_allExtensionMethods.TryGetValue(parentType, out var parentExtensions))
  61. {
  62. results.AddRange(parentExtensions);
  63. }
  64. }
  65. // don't create generic methods bound to an array of object - as this will prevent value types and other generics that don't support covariants/contravariants
  66. methods = results.ToArray();
  67. // racy, we don't care, worst case we'll catch up later
  68. Interlocked.CompareExchange(ref _extensionMethods, new Dictionary<Type, MethodInfo[]>(methodLookup) { [objectType] = methods }, methodLookup);
  69. return methods.Length > 0;
  70. }
  71. private static IEnumerable<Type> GetParentTypes(Type type)
  72. {
  73. // is there any base type?
  74. if (type == null)
  75. {
  76. yield break;
  77. }
  78. // return all implemented or inherited interfaces
  79. foreach (var i in type.GetInterfaces())
  80. {
  81. yield return i;
  82. if (i.IsConstructedGenericType)
  83. {
  84. yield return i.GetGenericTypeDefinition();
  85. }
  86. }
  87. // return all inherited types
  88. var currentBaseType = type.BaseType;
  89. while (currentBaseType != null)
  90. {
  91. yield return currentBaseType;
  92. if (currentBaseType.IsConstructedGenericType)
  93. {
  94. yield return currentBaseType.GetGenericTypeDefinition();
  95. }
  96. currentBaseType = currentBaseType.BaseType;
  97. }
  98. }
  99. }
  100. }