ExtensionMethodCache.cs 4.0 KB

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