ExtensionMethodCache.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// <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) ? 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. if (_allExtensionMethods.Count == 0)
  43. {
  44. methods = [];
  45. return false;
  46. }
  47. var methodLookup = _extensionMethods;
  48. if (methodLookup.TryGetValue(objectType, out methods))
  49. {
  50. return methods.Length > 0;
  51. }
  52. var results = new List<MethodInfo>();
  53. if (_allExtensionMethods.TryGetValue(objectType, out var ownExtensions))
  54. {
  55. results.AddRange(ownExtensions);
  56. }
  57. foreach (var parentType in GetParentTypes(objectType))
  58. {
  59. if (_allExtensionMethods.TryGetValue(parentType, out var parentExtensions))
  60. {
  61. results.AddRange(parentExtensions);
  62. }
  63. }
  64. // 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
  65. methods = results.ToArray();
  66. // racy, we don't care, worst case we'll catch up later
  67. Interlocked.CompareExchange(ref _extensionMethods, new Dictionary<Type, MethodInfo[]>(methodLookup) { [objectType] = methods }, methodLookup);
  68. return methods.Length > 0;
  69. }
  70. private static IEnumerable<Type> GetParentTypes(Type type)
  71. {
  72. // is there any base type?
  73. if (type == null)
  74. {
  75. yield break;
  76. }
  77. // return all implemented or inherited interfaces
  78. foreach (var i in type.GetInterfaces())
  79. {
  80. yield return i;
  81. if (i.IsConstructedGenericType)
  82. {
  83. yield return i.GetGenericTypeDefinition();
  84. }
  85. }
  86. // return all inherited types
  87. var currentBaseType = type.BaseType;
  88. while (currentBaseType != null)
  89. {
  90. yield return currentBaseType;
  91. if (currentBaseType.IsConstructedGenericType)
  92. {
  93. yield return currentBaseType.GetGenericTypeDefinition();
  94. }
  95. currentBaseType = currentBaseType.BaseType;
  96. }
  97. }
  98. }