Beispiel #1
0
        public ILCallMethodSnippet(ILPointer instance, MethodInfo method, params ILPointer[] parameters)
        {
            if (instance == null && !method.IsStatic)
            {
                throw new ArgumentException("Instance must be provided for instance methods");
            }
            if (instance != null && method.IsStatic)
            {
                throw new ArgumentException("Static method may not be invoked with an instance");
            }

            if (!(method is System.Reflection.Emit.MethodBuilder))
            {
                _methodParameters = method.GetParameters();

                if (_methodParameters.Length < parameters.Length)
                {
                    throw new ArgumentException("The parameter length supplied is greater than the method supports");
                }
            }

            _instance   = instance;
            _method     = method;
            _parameters = parameters;
            ReturnType  = method.ReturnType;
        }
Beispiel #2
0
        public static ILInstanceFieldVariable Field(ILPointer instance, string fieldName)
        {
            var field = instance.Type.GetRuntimeField(fieldName)
                        ?? throw new ArgumentException($"Could not find a field on {instance.Type} with name {fieldName}");

            return(new ILInstanceFieldVariable(instance, field));
        }
Beispiel #3
0
        public static ILInstancePropertyVariable Property(ILPointer instance, string propertyName)
        {
            var property = instance.Type.GetRuntimeProperty(propertyName)
                           ?? throw new ArgumentException($"Could not find a property on {instance.Type} with name {propertyName}");

            return(new ILInstancePropertyVariable(instance, property));
        }
 public static void Load(this ILGenerator gen, ILPointer pointer)
 {
     if (pointer == null)
     {
         pointer = ILPointer.Null;
     }
     ((IILPointer)pointer).Load(gen);
 }
        public static void ForLoop(this ILGenerator il, ILPointer initialValue, ILPointer lesserThan, ILPointer increment, ILGenerationHandler <ILPointer> body)
        {
            var loop = new ILForLoopSnippet(initialValue, value => {
                il.Load(value);
                il.Load(lesserThan);
                il.Emit(OpCodes.Clt);
            }, body, increment);

            il.Generate(loop);
        }
        public static void Throw(this ILGenerator il, ILPointer exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            ((IILPointer)exception).Load(il);
            il.Emit(OpCodes.Throw);
        }
        public static void Set(this ILGenerator gen, ILVariable variable, ILPointer value)
        {
            if (value == null)
            {
                value = ILPointer.Null;
            }

            ((IILVariable)variable).PreSet(gen);
            ((IILPointer)value).Load(gen);
            ((IILVariable)variable).Set(gen);
        }
Beispiel #8
0
        public ILForLoopSnippet(ILPointer initialValue, ILGenerationHandler <ILPointer> conditionHandler,
                                ILGenerationHandler <ILPointer> bodyHandler, ILPointer increment)
        {
            if (initialValue.Type != increment.Type)
            {
                throw new ArgumentException("The type of the initial value and the increment value must match");
            }

            _initialValue     = initialValue;
            _increment        = increment;
            _conditionHandler = new DelegatedILHandler <ILPointer>(conditionHandler);
            _bodyHandler      = new DelegatedILHandler <ILPointer>(bodyHandler);
        }
        public static void AreEqual(this ILGenerator il, ILPointer left, ILPointer right)
        {
            if (left == null)
            {
                left = ILPointer.Null;
            }
            if (right == null)
            {
                right = ILPointer.Null;
            }

            il.Load(left.Equal(right));
        }
Beispiel #10
0
        public void TransferLongIfTrue(ILPointer value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (value.Type != typeof(bool))
            {
                throw new ArgumentException("The type of the pointer must be boolean.");
            }

            _il.Load(value);
            TransferLongIfTrue();
        }
Beispiel #11
0
        public static ILCallMethodSnippet Call(ILPointer instance, string methodName, params ILPointer[] parameters)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (instance.Type == null)
            {
                throw new ArgumentException("The instance type is missing.");
            }
            var method = instance.Type.GetTypeInfo().GetMethod(methodName);

            if (method == null)
            {
                throw new ArgumentException($"The method {methodName} could not be found on type {instance.Type.FullName}");
            }
            return(new ILCallMethodSnippet(instance, method, parameters));
        }
Beispiel #12
0
        protected override void OnGenerate(ILGenerator il)
        {
            if (!_enumerable.Type.TryGetInterface(typeof(IEnumerable <>), out var enumerableType))
            {
                throw new InvalidOperationException("Could not enumerate the type " + _enumerable.Type.FullName);
            }

            var getEnumerator = ILSnippet.Call(_enumerable, enumerableType.GetRuntimeMethod("GetEnumerator", Type.EmptyTypes));

            var elementType = enumerableType.GetTypeInfo().GetGenericArguments()[0];
            var itLocal     = il.NewLocal(getEnumerator.ReturnType);

            il.Set(itLocal, getEnumerator);

            il.Try(() => {
                var itHeadLabel = il.NewLabel();
                var itBodyLabel = il.NewLabel();
                itHeadLabel.TransferLong();
                var itVarLocal = il.NewLocal(elementType);

                itBodyLabel.Mark();
                il.Set(itVarLocal, ILPointer.Property(itLocal, "Current"));

                _iterateBody.Invoke(il, itVarLocal);

                itHeadLabel.Mark();
                il.InvokeMethod(itLocal, EnumeratorMoveNext);

                itBodyLabel.TransferLongIfTrue();
            })
            .Finally(() => {
                var endLabel = il.NewLabel();
                endLabel.TransferIfNull(itLocal);

                il.InvokeMethod(itLocal, DisposableDispose);

                endLabel.Mark();
            })
            .End();
        }
 public static void Construct(this ILGenerator il, ConstructorInfo constructor, params ILPointer[] parameters)
 {
     il.Load(ILPointer.New(constructor, parameters));
 }
Beispiel #14
0
 public static ILInstanceFieldVariable Field(ILPointer instance, FieldInfo field)
 {
     return(new ILInstanceFieldVariable(instance, field));
 }
Beispiel #15
0
 public ILEnumerateSnippet(ILPointer enumerable, ILGenerationHandler <ILPointer> iterateBody)
 {
     _enumerable  = enumerable;
     _iterateBody = new DelegatedILHandler <ILPointer>(iterateBody);
 }
Beispiel #16
0
 public ILInstanceFieldVariable(ILPointer instance, FieldInfo info)
 {
     Instance = instance;
     Info     = info;
 }
Beispiel #17
0
 public ILInstancePropertyVariable(ILPointer instance, PropertyInfo info)
 {
     Instance = instance;
     Info     = info;
 }
 public static void LoadAddress(this ILGenerator gen, ILPointer pointer)
 {
     ((IILPointer)pointer).LoadAddress(gen);
 }
Beispiel #19
0
 public static ILCallMethodSnippet Call(ILPointer instance, MethodInfo method, params ILPointer[] parameters)
 {
     return(new ILCallMethodSnippet(instance, method, parameters));
 }
 public static void Enumerate(this ILGenerator il, ILPointer enumerable, ILGenerationMethodHandler <ILPointer> iterateBody)
 {
     il.Generate(new ILEnumerateSnippet(enumerable, iterateBody));
 }
 public static void Increment(this ILGenerator il, ILVariable value, ILPointer valueToAdd)
 {
     il.Set(value, value.Add(valueToAdd));
 }
        public static void InvokeMethod(this ILGenerator il, ILPointer instance, string methodName, params ILPointer[] parameters)
        {
            var call = ILSnippet.Call(instance, methodName, parameters);

            il.Generate(call);
        }
Beispiel #23
0
 public void TransferIfNotNull(ILPointer value)
 {
     _il.AreEqual(value, null);
     TransferLongIfFalse();
 }
        public static void Set(this ILGenerator il, ILPointer instance, FieldInfo field, ILPointer value)
        {
            var f = ILPointer.Field(instance, field);

            il.Set(f, value);
        }
 public static ILChainIfCondition IfEqual(this ILGenerator il, ILPointer left, ILPointer right)
 {
     return(new ILChainIfCondition(il, () => il.AreEqual(left, right)));
 }
Beispiel #26
0
 public static ILInstancePropertyVariable Property(ILPointer instance, PropertyInfo property)
 {
     return(new ILInstancePropertyVariable(instance, property));
 }
        public static void Set(this ILGenerator il, ILPointer instance, PropertyInfo property, ILPointer value)
        {
            var p = ILPointer.Property(instance, property);

            il.Set(p, value);
        }
 public static ILChainIfCondition IfNotEqual(this ILGenerator il, ILPointer left, ILPointer right)
 {
     return(new ILChainIfCondition(il, () => {
         il.Load(left.Equal(right).Negate());
     }));
 }