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; }
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)); }
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); }
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)); }
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(); }
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)); }
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)); }
public static ILInstanceFieldVariable Field(ILPointer instance, FieldInfo field) { return(new ILInstanceFieldVariable(instance, field)); }
public ILEnumerateSnippet(ILPointer enumerable, ILGenerationHandler <ILPointer> iterateBody) { _enumerable = enumerable; _iterateBody = new DelegatedILHandler <ILPointer>(iterateBody); }
public ILInstanceFieldVariable(ILPointer instance, FieldInfo info) { Instance = instance; Info = info; }
public ILInstancePropertyVariable(ILPointer instance, PropertyInfo info) { Instance = instance; Info = info; }
public static void LoadAddress(this ILGenerator gen, ILPointer pointer) { ((IILPointer)pointer).LoadAddress(gen); }
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); }
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))); }
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()); })); }