ExtensionMethodCache.cs 4.1 KB

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