|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Management.Automation; |
| 4 | +using System.Management.Automation.Language; |
| 5 | +using System.Reflection; |
| 6 | +using System.Reflection.Emit; |
| 7 | + |
| 8 | +namespace ILAssembler |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Provides a binary entry point for the assembler. |
| 12 | + /// </summary> |
| 13 | + /// <remarks> |
| 14 | + /// This class is currently non-public. If you have a use case in mind for |
| 15 | + /// it, please open an issue and I'll make it public. |
| 16 | + /// </remarks> |
| 17 | + internal static class CilAssemblage |
| 18 | + { |
| 19 | + private static readonly ConcurrentDictionary<(Type, ScriptBlockAst), DynamicMethod> s_methodCache = new(); |
| 20 | + |
| 21 | + public static Delegate CreateDelegate(ScriptBlockAst signature, ScriptBlockAst body) |
| 22 | + { |
| 23 | + return CreateDelegate(signature, body, skipCache: false); |
| 24 | + } |
| 25 | + |
| 26 | + public static Delegate CreateDelegate(ScriptBlockAst signature, ScriptBlockAst body, bool skipCache) |
| 27 | + { |
| 28 | + var signatureParser = new MethodSignatureParser( |
| 29 | + rejectCtor: true, |
| 30 | + requireResolvableDeclaringType: false); |
| 31 | + |
| 32 | + signature.Visit(signatureParser); |
| 33 | + var identifier = (MethodIdentifier)signatureParser.GetMemberIdentifier(signature.Extent); |
| 34 | + |
| 35 | + Type delegateType = DelegateTypeFactory.GetDelegateType(identifier); |
| 36 | + return CreateDelegate(delegateType, body, skipCache); |
| 37 | + } |
| 38 | + |
| 39 | + public static TDelegate CreateDelegate<TDelegate>(ScriptBlockAst body) |
| 40 | + where TDelegate : Delegate |
| 41 | + { |
| 42 | + return CreateDelegate<TDelegate>(body, skipCache: false); |
| 43 | + } |
| 44 | + |
| 45 | + public static TDelegate CreateDelegate<TDelegate>(ScriptBlockAst body, bool skipCache) |
| 46 | + where TDelegate : Delegate |
| 47 | + { |
| 48 | + return (TDelegate)CreateDelegate(typeof(TDelegate), body, skipCache); |
| 49 | + } |
| 50 | + |
| 51 | + public static Delegate CreateDelegate(Type delegateType, ScriptBlockAst body) |
| 52 | + { |
| 53 | + return CreateDelegate(delegateType, body, skipCache: false); |
| 54 | + } |
| 55 | + |
| 56 | + public static Delegate CreateDelegate(Type delegateType, ScriptBlockAst body, bool skipCache) |
| 57 | + { |
| 58 | + return CreateCilMethod(delegateType, body, skipCache) |
| 59 | + .CreateDelegate(delegateType); |
| 60 | + } |
| 61 | + |
| 62 | + private static DynamicMethod CreateCilMethod(Type delegateType, ScriptBlockAst body, bool skipCache = false) |
| 63 | + { |
| 64 | + if (skipCache) |
| 65 | + { |
| 66 | + return CreateCilMethod((delegateType, body)); |
| 67 | + } |
| 68 | + |
| 69 | + return s_methodCache.GetOrAdd( |
| 70 | + (delegateType, body), |
| 71 | + args => CreateCilMethod(args)); |
| 72 | + } |
| 73 | + |
| 74 | + private static DynamicMethod CreateCilMethod((Type delegateType, ScriptBlockAst body) args) |
| 75 | + { |
| 76 | + (Type delegateType, ScriptBlockAst body) = args; |
| 77 | + DynamicMethod method = CreateDynamicMethod(delegateType); |
| 78 | + CilAssembler.CompileTo(body, method); |
| 79 | + return method; |
| 80 | + } |
| 81 | + |
| 82 | + private static DynamicMethod CreateDynamicMethod(ScriptBlockAst signature, out Type delegateType) |
| 83 | + { |
| 84 | + var signatureParser = new MethodSignatureParser( |
| 85 | + rejectCtor: true, |
| 86 | + requireResolvableDeclaringType: false); |
| 87 | + |
| 88 | + signature.Visit(signatureParser); |
| 89 | + var identifier = (MethodIdentifier)signatureParser.GetMemberIdentifier(signature.Extent); |
| 90 | + |
| 91 | + delegateType = DelegateTypeFactory.GetDelegateType(identifier); |
| 92 | + |
| 93 | + return new DynamicMethod( |
| 94 | + identifier.Name, |
| 95 | + identifier.ReturnType.GetModifiedType(), |
| 96 | + identifier.Parameters.ToModifiedTypeArray(), |
| 97 | + typeof(CilAssemblage).Module, |
| 98 | + skipVisibility: true); |
| 99 | + } |
| 100 | + |
| 101 | + private static DynamicMethod CreateDynamicMethod(Type delegateType) |
| 102 | + { |
| 103 | + if (!delegateType.IsSubclassOf(typeof(Delegate))) |
| 104 | + { |
| 105 | + throw new ArgumentException(SR.InvalidDelegateType, nameof(delegateType)) |
| 106 | + .WithErrorInfo( |
| 107 | + nameof(SR.InvalidDelegateType), |
| 108 | + ErrorCategory.InvalidArgument, |
| 109 | + delegateType); |
| 110 | + } |
| 111 | + |
| 112 | + MethodInfo? invokeMethod = delegateType.GetMethod(nameof(Action.Invoke)); |
| 113 | + if (invokeMethod is null) |
| 114 | + { |
| 115 | + throw new ArgumentException(SR.DelegateTypeMissingInvoke, nameof(delegateType)) |
| 116 | + .WithErrorInfo( |
| 117 | + nameof(SR.DelegateTypeMissingInvoke), |
| 118 | + ErrorCategory.InvalidArgument, |
| 119 | + delegateType); |
| 120 | + } |
| 121 | + |
| 122 | + ParameterInfo[] parameters = invokeMethod.GetParameters(); |
| 123 | + var parameterTypes = new Type[parameters.Length]; |
| 124 | + for (int i = parameterTypes.Length - 1; i >= 0; i--) |
| 125 | + { |
| 126 | + parameterTypes[i] = parameters[i].ParameterType; |
| 127 | + } |
| 128 | + |
| 129 | + return new DynamicMethod( |
| 130 | + invokeMethod.Name, |
| 131 | + invokeMethod.ReturnType, |
| 132 | + parameterTypes, |
| 133 | + typeof(CilAssemblage).Module, |
| 134 | + skipVisibility: true); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments