Esempio n. 1
0
        private static MSA.LambdaExpression /*!*/ CreateLambda(string /*!*/ name, MSA.ParameterExpression /*!*/[] /*!*/ parameters, MSA.Expression /*!*/ body)
        {
            Type lambdaType = DynamicSiteHelpers.GetStandardDelegateType(AstFactory.GetSignature(parameters, typeof(object)));

            if (lambdaType == null)
            {
                // to many parameters for Func<> delegate -> use object[]:
                MSA.ParameterExpression array = Ast.Parameter(typeof(object[]), "#params");
                var actualParameters          = new MSA.ParameterExpression[] { parameters[0], parameters[1], array };
                parameters = ArrayUtils.ShiftLeft(parameters, 2);

                var bodyWithParamInit = new MSA.Expression[parameters.Length + 1];
                for (int i = 0; i < parameters.Length; i++)
                {
                    bodyWithParamInit[i] = Ast.Assign(parameters[i], Ast.ArrayIndex(array, Ast.Constant(i)));
                }
                bodyWithParamInit[parameters.Length] = body;

                return(Ast.Lambda(
                           RubyMethodInfo.ParamsArrayDelegateType,
                           Ast.Block(
                               new ReadOnlyCollection <MSA.ParameterExpression>(parameters),
                               new ReadOnlyCollection <MSA.Expression>(bodyWithParamInit)
                               ),
                           name,
                           new ReadOnlyCollection <MSA.ParameterExpression>(actualParameters)
                           ));
            }
            else
            {
                return(Ast.Lambda(
                           lambdaType,
                           body,
                           name,
                           new ReadOnlyCollection <MSA.ParameterExpression>(parameters)
                           ));
            }
        }