ExtensionMethodCache.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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. Type GetTypeDefinition(Type type)
  26. {
  27. return type.IsConstructedGenericType && type.GenericTypeArguments.Any(x => x.IsGenericParameter) ?
  28. type.GetGenericTypeDefinition() : type;
  29. }
  30. var methodsByTarget = extensionMethodContainerTypes
  31. .SelectMany(x => x.GetExtensionMethods())
  32. .GroupBy(x => GetTypeDefinition(x.GetParameters()[0].ParameterType))
  33. .ToDictionary(x => x.Key, x => x.ToArray());
  34. return new ExtensionMethodCache(methodsByTarget);
  35. }
  36. public bool HasMethods => _allExtensionMethods.Count > 0;
  37. private MethodInfo BindMethodGenericParameters(MethodInfo method)
  38. {
  39. if (method.IsGenericMethodDefinition && method.ContainsGenericParameters)
  40. {
  41. var methodGenerics = method.GetGenericArguments();
  42. var parameterList = Enumerable.Repeat(typeof(object), methodGenerics.Length).ToArray();
  43. try
  44. {
  45. return method.MakeGenericMethod(parameterList);
  46. }
  47. catch
  48. {
  49. // Generic parameter constraints failed probably.
  50. // If it does not work, let it be. We don't need to do anything.
  51. }
  52. }
  53. return method;
  54. }
  55. public bool TryGetExtensionMethods(Type objectType, out MethodInfo[] methods)
  56. {
  57. var methodLookup = _extensionMethods;
  58. if (methodLookup.TryGetValue(objectType, out methods))
  59. {
  60. return methods.Length > 0;
  61. }
  62. var results = new List<MethodInfo>();
  63. if (_allExtensionMethods.TryGetValue(objectType, out var ownExtensions))
  64. {
  65. results.AddRange(ownExtensions);
  66. }
  67. foreach (var parentType in GetParentTypes(objectType))
  68. {
  69. if (_allExtensionMethods.TryGetValue(parentType, out var parentExtensions))
  70. {
  71. results.AddRange(parentExtensions);
  72. }
  73. }
  74. methods = results.Select(BindMethodGenericParameters).ToArray();
  75. // racy, we don't care, worst case we'll catch up later
  76. Interlocked.CompareExchange(ref _extensionMethods, new Dictionary<Type, MethodInfo[]>(methodLookup)
  77. {
  78. [objectType] = methods
  79. }, methodLookup);
  80. return methods.Length > 0;
  81. }
  82. private static IEnumerable<Type> GetParentTypes(Type type)
  83. {
  84. // is there any base type?
  85. if (type == null)
  86. {
  87. yield break;
  88. }
  89. // return all implemented or inherited interfaces
  90. foreach (var i in type.GetInterfaces())
  91. {
  92. yield return i;
  93. if (i.IsConstructedGenericType)
  94. {
  95. yield return i.GetGenericTypeDefinition();
  96. }
  97. }
  98. // return all inherited types
  99. var currentBaseType = type.BaseType;
  100. while (currentBaseType != null)
  101. {
  102. yield return currentBaseType;
  103. if (currentBaseType.IsConstructedGenericType)
  104. {
  105. yield return currentBaseType.GetGenericTypeDefinition();
  106. }
  107. currentBaseType = currentBaseType.BaseType;
  108. }
  109. }
  110. }
  111. }