ExtensionMethodCache.cs 4.1 KB

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