|
| 1 | +// <copyright file="ValueTaskActivator`1.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +#if !NETCOREAPP3_1_OR_GREATER |
| 7 | +#nullable enable |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Reflection.Emit; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Datadog.Trace.DuckTyping; |
| 13 | +using Datadog.Trace.Logging; |
| 14 | +using Datadog.Trace.Util; |
| 15 | + |
| 16 | +#pragma warning disable SA1649 // File name must match first type name |
| 17 | + |
| 18 | +namespace Datadog.Trace.ClrProfiler.CallTarget.Handlers.Continuations; |
| 19 | + |
| 20 | +internal static class ValueTaskActivator<TValueTask, TResult> |
| 21 | +{ |
| 22 | + private static readonly Func<Task<TResult>, TValueTask> TaskActivator; |
| 23 | + private static readonly Func<TResult, TValueTask> ResultActivator; |
| 24 | + |
| 25 | + static ValueTaskActivator() |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + TaskActivator = CreateTaskActivator(); |
| 30 | + ResultActivator = CreateResultActivator(); |
| 31 | + } |
| 32 | + catch (Exception ex) |
| 33 | + { |
| 34 | + DatadogLogging.GetLoggerFor<ActivatorHelper>() |
| 35 | + .Error(ex, "Error creating the custom activator for: {Type}", typeof(TValueTask).FullName); |
| 36 | + |
| 37 | + // Unfortunately this will box the ValueTask, but I think it's still the best we can do in this scenario |
| 38 | + TaskActivator = FallbackTaskActivator; |
| 39 | + ResultActivator = FallbackResultActivator; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Internal for testing |
| 44 | + internal static Func<Task<TResult>, TValueTask> CreateTaskActivator() |
| 45 | + { |
| 46 | + var valueTaskType = typeof(TValueTask); |
| 47 | + var ctor = valueTaskType.GetConstructor([typeof(Task<TResult>)])!; |
| 48 | + |
| 49 | + var createValueTaskMethod = new DynamicMethod( |
| 50 | + $"TypeActivatorTask" + valueTaskType.Name, |
| 51 | + returnType: valueTaskType, |
| 52 | + parameterTypes: [typeof(Task<TResult>)], |
| 53 | + typeof(DuckType).Module, |
| 54 | + true); |
| 55 | + |
| 56 | + var il = createValueTaskMethod.GetILGenerator(); |
| 57 | + il.Emit(OpCodes.Ldarg_0); |
| 58 | + il.Emit(OpCodes.Newobj, ctor); |
| 59 | + il.Emit(OpCodes.Ret); |
| 60 | + |
| 61 | + return (Func<Task<TResult>, TValueTask>)createValueTaskMethod.CreateDelegate(typeof(Func<Task<TResult>, TValueTask>)); |
| 62 | + } |
| 63 | + |
| 64 | + // Internal for testing |
| 65 | + internal static Func<TResult, TValueTask> CreateResultActivator() |
| 66 | + { |
| 67 | + var valueTaskType = typeof(TValueTask); |
| 68 | + var ctor = valueTaskType.GetConstructor([typeof(TResult)])!; |
| 69 | + |
| 70 | + var createValueTaskMethod = new DynamicMethod( |
| 71 | + $"TypeActivatorResult" + valueTaskType.Name, |
| 72 | + returnType: valueTaskType, |
| 73 | + parameterTypes: [typeof(TResult)], |
| 74 | + typeof(DuckType).Module, |
| 75 | + true); |
| 76 | + |
| 77 | + var il = createValueTaskMethod.GetILGenerator(); |
| 78 | + il.Emit(OpCodes.Ldarg_0); |
| 79 | + il.Emit(OpCodes.Newobj, ctor); |
| 80 | + il.Emit(OpCodes.Ret); |
| 81 | + |
| 82 | + return (Func<TResult, TValueTask>)createValueTaskMethod.CreateDelegate(typeof(Func<TResult, TValueTask>)); |
| 83 | + } |
| 84 | + |
| 85 | + // Internal for testing |
| 86 | + internal static TValueTask FallbackTaskActivator(Task<TResult> task) |
| 87 | + => (TValueTask)Activator.CreateInstance(typeof(TValueTask), task)!; |
| 88 | + |
| 89 | + internal static TValueTask FallbackResultActivator(TResult task) |
| 90 | + => (TValueTask)Activator.CreateInstance(typeof(TValueTask), task)!; |
| 91 | + |
| 92 | + public static TValueTask CreateInstance(Task<TResult> task) => TaskActivator(task); |
| 93 | + |
| 94 | + public static TValueTask CreateInstance(TResult result) => ResultActivator(result); |
| 95 | +} |
| 96 | +#endif |
0 commit comments