Exemple #1
0
        public override void EmitParameterInfo()
        {
#if NETFRAMEWORK || NETSTANDARD
            foreach (var para in Parameters)
            {
                _builder.DefineParameter(para.Index, System.Reflection.ParameterAttributes.In, para.Name);
            }
#endif
        }
        static System.Func <StringTemplate, IStringTemplateWriter, bool> GetEvaluator(ASTExpr chunk, ITree condition)
        {
            if (EnableDynamicMethods)
            {
                try
                {
                    DynamicMethod method = null;
#if CACHE_FUNCTORS
                    if (!_methods.TryGetValue(condition, out method))
#endif
                    {
                        Type[] parameterTypes = { typeof(ASTExpr), typeof(StringTemplate), typeof(IStringTemplateWriter) };
                        method = new DynamicMethod("ConditionEvaluator" + _evaluatorNumber, typeof(bool), parameterTypes, typeof(ConditionalExpr), true);
#if !NETSTANDARD2_0
                        method.DefineParameter(1, ParameterAttributes.None, "chunk");
                        method.DefineParameter(2, ParameterAttributes.None, "self");
                        method.DefineParameter(3, ParameterAttributes.None, "writer");
#endif
                        _evaluatorNumber++;

                        var             gen          = method.GetILGenerator();
                        ActionEvaluator evalCompiled = new ActionEvaluator(null, chunk, null, condition);
                        evalCompiled.ifConditionCompiled(gen);
                        gen.Emit(OpCodes.Ret);
#if CACHE_FUNCTORS
                        _methods[condition] = method;
#endif
                    }

                    var dynamicEvaluator = (System.Func <StringTemplate, IStringTemplateWriter, bool>)method.CreateDelegate(typeof(System.Func <StringTemplate, IStringTemplateWriter, bool>), chunk);
                    return(dynamicEvaluator);
                }
                catch
                {
                    // fall back to functional (or interpreted) version
                }
            }

            if (EnableFunctionalMethods)
            {
                try
                {
                    ActionEvaluator            evalFunctional      = new ActionEvaluator(null, chunk, null, condition);
                    var                        functionalEvaluator = evalFunctional.ifConditionFunctional();
                    HoldsConditionFuncAndChunk holder = new HoldsConditionFuncAndChunk()
                    {
                        func  = functionalEvaluator,
                        chunk = chunk
                    };
                    return((System.Func <StringTemplate, IStringTemplateWriter, bool>)System.Delegate.CreateDelegate(typeof(System.Func <StringTemplate, IStringTemplateWriter, bool>), holder, typeof(ConditionalExpr).GetMethod("CallFunctionalConditionEvaluator")));
                }
                catch
                {
                    // fall back to interpreted version
                }
            }

            return(new System.Func <StringTemplate, IStringTemplateWriter, bool>((self, @out) =>
            {
                ActionEvaluator eval = new ActionEvaluator(self, chunk, @out, condition);
                return eval.ifCondition();
            }));
        }
Exemple #3
0
        static System.Func<StringTemplate, IStringTemplateWriter, bool> GetEvaluator( ASTExpr chunk, ITree condition )
        {
            if ( EnableDynamicMethods )
            {
                try
                {
                    DynamicMethod method = null;
            #if CACHE_FUNCTORS
                    if ( !_methods.TryGetValue( condition, out method ) )
            #endif
                    {
                        Type[] parameterTypes = { typeof( ASTExpr ), typeof( StringTemplate ), typeof( IStringTemplateWriter ) };
                        method = new DynamicMethod( "ConditionEvaluator" + _evaluatorNumber, typeof( bool ), parameterTypes, typeof( ConditionalExpr ), true );
                        method.DefineParameter( 1, ParameterAttributes.None, "chunk" );
                        method.DefineParameter( 2, ParameterAttributes.None, "self" );
                        method.DefineParameter( 3, ParameterAttributes.None, "writer" );
                        _evaluatorNumber++;

                        var gen = method.GetILGenerator();
                        ActionEvaluator evalCompiled = new ActionEvaluator( null, chunk, null, condition );
                        evalCompiled.ifConditionCompiled( gen );
                        gen.Emit( OpCodes.Ret );
            #if CACHE_FUNCTORS
                        _methods[condition] = method;
            #endif
                    }

                    var dynamicEvaluator = (System.Func<StringTemplate, IStringTemplateWriter, bool>)method.CreateDelegate( typeof( System.Func<StringTemplate, IStringTemplateWriter, bool> ), chunk );
                    return dynamicEvaluator;
                }
                catch
                {
                    // fall back to functional (or interpreted) version
                }
            }

            if ( EnableFunctionalMethods )
            {
                try
                {
                    ActionEvaluator evalFunctional = new ActionEvaluator( null, chunk, null, condition );
                    var functionalEvaluator = evalFunctional.ifConditionFunctional();
                    HoldsConditionFuncAndChunk holder = new HoldsConditionFuncAndChunk()
                    {
                        func = functionalEvaluator,
                        chunk = chunk
                    };
                    return (System.Func<StringTemplate, IStringTemplateWriter, bool>)System.Delegate.CreateDelegate( typeof( System.Func<StringTemplate, IStringTemplateWriter, bool> ), holder, typeof( ConditionalExpr ).GetMethod( "CallFunctionalConditionEvaluator" ) );
                }
                catch
                {
                    // fall back to interpreted version
                }
            }

            return new System.Func<StringTemplate, IStringTemplateWriter, bool>( ( self, @out ) =>
            {
                ActionEvaluator eval = new ActionEvaluator( self, chunk, @out, condition );
                return eval.ifCondition();
            } );
        }