2
0

ExtensionMethodCache.cs 4.1 KB

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