No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StaticDelegateRegistry.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Burst.Compiler.IL.Tests
  4. {
  5. /// <summary>
  6. /// This class provides a delegate for each test methods that should have been pre-generated by <see cref="StaticDelegateGenerator"/>
  7. /// when running the tests locally.
  8. ///
  9. /// NOTE: This code is a **SUPER LABORIOUS** workaround a bug in mono 5.11 that is preventing us from dynamically calling a delegate
  10. /// to native code through Delegate DynamicInvoke.
  11. ///
  12. /// On .NET Framework and newer version of mono, we would not need this, so we are going to keep this code around until
  13. /// mono is entirely upgraded in Unity.
  14. /// </summary>
  15. internal static partial class StaticDelegateRegistry
  16. {
  17. private static readonly Dictionary<SignatureKey, StaticDelegateCallback> RegisteredDelegateTypes = new Dictionary<SignatureKey, StaticDelegateCallback>();
  18. private static void Register(Type returnType, Type[] arguments, Type typeDelegate, Func<object, object[], object> caller)
  19. {
  20. if (returnType == null) throw new ArgumentNullException(nameof(returnType));
  21. if (arguments == null) throw new ArgumentNullException(nameof(arguments));
  22. if (typeDelegate == null) throw new ArgumentNullException(nameof(typeDelegate));
  23. // don't clone as we have the guarantee that it is generated internally
  24. RegisteredDelegateTypes.Add(new SignatureKey(returnType, arguments), new StaticDelegateCallback(typeDelegate, caller));
  25. }
  26. public static bool Contains(Type returnType, Type[] arguments)
  27. {
  28. if (returnType == null) throw new ArgumentNullException(nameof(returnType));
  29. if (arguments == null) throw new ArgumentNullException(nameof(arguments));
  30. return RegisteredDelegateTypes.ContainsKey(new SignatureKey(returnType, arguments));
  31. }
  32. public static bool TryFind(Type returnType, Type[] arguments, out StaticDelegateCallback staticDelegate)
  33. {
  34. if (returnType == null) throw new ArgumentNullException(nameof(returnType));
  35. if (arguments == null) throw new ArgumentNullException(nameof(arguments));
  36. return RegisteredDelegateTypes.TryGetValue(new SignatureKey(returnType, (Type[])arguments.Clone()), out staticDelegate);
  37. }
  38. internal struct SignatureKey : IEquatable<SignatureKey>
  39. {
  40. public SignatureKey(Type returnType, Type[] parameterTypes)
  41. {
  42. ReturnType = returnType;
  43. ParameterTypes = parameterTypes;
  44. }
  45. public readonly Type ReturnType;
  46. public readonly Type[] ParameterTypes;
  47. public bool Equals(SignatureKey other)
  48. {
  49. if (ParameterTypes.Length != other.ParameterTypes.Length) return false;
  50. if (ReturnType != other.ReturnType) return false;
  51. for (int i = 0; i < ParameterTypes.Length; i++)
  52. {
  53. var arg = ParameterTypes[i];
  54. var otherArg = other.ParameterTypes[i];
  55. if (arg != otherArg) return false;
  56. }
  57. return true;
  58. }
  59. public override bool Equals(object obj)
  60. {
  61. if (ReferenceEquals(null, obj)) return false;
  62. return obj is SignatureKey other && Equals(other);
  63. }
  64. public override int GetHashCode()
  65. {
  66. unchecked
  67. {
  68. var hashcode = ReturnType.GetHashCode();
  69. for (int i = 0; i < ParameterTypes.Length; i++)
  70. {
  71. hashcode = (hashcode * 397) ^ ParameterTypes[i].GetHashCode();
  72. }
  73. return hashcode;
  74. }
  75. }
  76. public static bool operator ==(SignatureKey left, SignatureKey right)
  77. {
  78. return left.Equals(right);
  79. }
  80. public static bool operator !=(SignatureKey left, SignatureKey right)
  81. {
  82. return !left.Equals(right);
  83. }
  84. }
  85. }
  86. internal struct StaticDelegateCallback
  87. {
  88. public StaticDelegateCallback(Type delegateType, Func<object, object[], object> caller)
  89. {
  90. DelegateType = delegateType;
  91. Caller = caller;
  92. }
  93. public readonly Type DelegateType;
  94. public readonly Func<object, object[], object> Caller;
  95. }
  96. }