Example #1
0
        public static RuleGenerator /*!*/ GetException()
        {
            return(new RuleGenerator((metaBuilder, args, name) => {
                Debug.Assert(args.Target is Exception);

                // 1 optional parameter (exceptionArg):
                var argsBuilder = new ArgsBuilder(0, 0, 1, false);
                argsBuilder.AddCallArguments(metaBuilder, args);

                if (!metaBuilder.Error)
                {
                    if (argsBuilder.ExplicitArgumentCount == 0)
                    {
                        metaBuilder.Result = args.TargetExpression;
                    }
                    else
                    {
                        RubyClass cls = args.RubyContext.GetClassOf(args.Target);
                        var classExpression = Ast.Constant(cls);
                        args.SetTarget(classExpression, cls);

                        ParameterExpression messageVariable = null;

                        // ReinitializeException(new <exception-type>(GetClrMessage(<class>, #message = <message>)), #message)
                        metaBuilder.Result = Ast.Call(null, new Func <Exception, object, Exception>(ReinitializeException).Method,
                                                      cls.MakeAllocatorCall(args, () =>
                                                                            Ast.Call(null, new Func <RubyClass, object, string>(GetClrMessage).Method,
                                                                                     classExpression,
                                                                                     Ast.Assign(messageVariable = metaBuilder.GetTemporary(typeof(object), "#message"), AstFactory.Box(argsBuilder[0]))
                                                                                     )
                                                                            ),
                                                      messageVariable ?? AstFactory.Box(argsBuilder[0])
                                                      );
                    }
                }
            }));
        }