Example #1
0
        public void WithExceptions()
        {
            var dynamicMethod = new DynamicMethod(Guid.NewGuid().ToString(), typeof(int), new[] { typeof(int), typeof(int) }, typeof(string), true);

            using (var il = new GroboIL(dynamicMethod, false))
            {
                var endLabel    = il.DefineLabel("end");
                var resultLocal = il.DeclareLocal(typeof(int), "result");
                il.BeginExceptionBlock();
                il.Ldarg(0);
                il.Ldarg(1);
                il.Div(false);
                il.Stloc(resultLocal);
                il.BeginCatchBlock(typeof(DivideByZeroException));
                il.Pop();
                il.WriteLine("Division by zero caught");
                il.Ldc_I4(0);
                il.Stloc(resultLocal);
                il.BeginFinallyBlock();
                il.WriteLine("It is finally");
                il.EndExceptionBlock();
                il.MarkLabel(endLabel);
                il.Ldloc(resultLocal);
                il.Ret();
            }

            DynamicMethodTracingInstaller.InstallTracing(dynamicMethod);

            var func = (Func <int, int, int>)dynamicMethod.CreateDelegate(typeof(Func <int, int, int>));

            Console.WriteLine(func(12, 5));
            Console.WriteLine(func(5, 0));
        }
        private static void EmitOp(GroboIL il, ExpressionType nodeType, Type type)
        {
            switch (nodeType)
            {
            case ExpressionType.Add:
                il.Add();
                break;

            case ExpressionType.AddChecked:
                il.Add_Ovf(type.Unsigned());
                break;

            case ExpressionType.Subtract:
                il.Sub();
                break;

            case ExpressionType.SubtractChecked:
                il.Sub_Ovf(type.Unsigned());
                break;

            case ExpressionType.Multiply:
                il.Mul();
                break;

            case ExpressionType.MultiplyChecked:
                il.Mul_Ovf(type.Unsigned());
                break;

            case ExpressionType.Divide:
                il.Div(type.Unsigned());
                break;

            case ExpressionType.Modulo:
                il.Rem(type.Unsigned());
                break;

            case ExpressionType.LeftShift:
                il.Shl();
                break;

            case ExpressionType.RightShift:
                il.Shr(type.Unsigned());
                break;

            case ExpressionType.And:
                il.And();
                break;

            case ExpressionType.Or:
                il.Or();
                break;

            case ExpressionType.ExclusiveOr:
                il.Xor();
                break;

            default:
                throw new NotSupportedException("Node type '" + nodeType + "' is not supported");
            }
        }
Example #3
0
        private void TestSuccess(Type type1, Type type2)
        {
            var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] { type1, type2, }.Where(type => type != null).ToArray(), typeof(string), true);

            using (var il = new GroboIL(method))
            {
                var index = 0;
                if (type1 != null)
                {
                    il.Ldarg(index++);
                }
                else
                {
                    il.Ldnull();
                }
                if (type2 != null)
                {
                    il.Ldarg(index++);
                }
                else
                {
                    il.Ldnull();
                }
                il.Div(true);
                il.Pop();
                il.Ret();
                Console.WriteLine(il.GetILCode());
            }
        }
Example #4
0
        private void TestFailure(Type type1, Type type2)
        {
            var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] { type1, type2, }.Where(type => type != null).ToArray(), typeof(string), true);
            var il     = new GroboIL(method);
            var index  = 0;

            if (type1 != null)
            {
                il.Ldarg(index++);
            }
            else
            {
                il.Ldnull();
            }
            if (type2 != null)
            {
                il.Ldarg(index++);
            }
            else
            {
                il.Ldnull();
            }
            Assert.Throws <InvalidOperationException>(() => il.Div(true));
        }