public static Expression Bind(Expression body, CompilationContext context, Expression parentContext, string templatePath) { var writerParameter = Expression.Parameter(typeof(TextWriter), "buffer"); var objectParameter = Expression.Parameter(typeof(object), "data"); if (parentContext == null) { parentContext = Expression.Constant(null, typeof(BindingContext)); } var constantExpression = Expression.Constant(templatePath, typeof(string)); var newBindingContext = Expression.New( typeof(BindingContext).GetConstructor( new[] { typeof(object), typeof(TextWriter), typeof(BindingContext), typeof(string) }), new Expression[] { objectParameter, writerParameter, parentContext, constantExpression }); return Expression.Lambda<Action<TextWriter, object>>( Expression.Block( new [] { context.BindingContext }, new Expression[] { Expression.IfThenElse( Expression.TypeIs(objectParameter, typeof(BindingContext)), Expression.Assign(context.BindingContext, Expression.TypeAs(objectParameter, typeof(BindingContext))), Expression.Assign(context.BindingContext, newBindingContext)) }.Concat( ((BlockExpression)body).Expressions )), new[] { writerParameter, objectParameter }); }
public Expression Compile(IEnumerable<Expression> expressions, Expression parentContext, string templatePath = null) { try { if (expressions.Any() == false) { return _emptyLambda; } if (expressions.IsOneOf<Expression, DefaultExpression>() == true) { return _emptyLambda; } var compilationContext = new CompilationContext(_configuration); var expression = CreateExpressionBlock(expressions); expression = StaticReplacer.Replace(expression, compilationContext); expression = UnencodedStatementVisitor.Visit(expression, compilationContext); expression = PartialBinder.Bind(expression, compilationContext); expression = IteratorBinder.Bind(expression, compilationContext); expression = BlockHelperFunctionBinder.Bind(expression, compilationContext); expression = DeferredSectionVisitor.Bind(expression, compilationContext); expression = HelperFunctionBinder.Bind(expression, compilationContext); expression = BoolishConverter.Convert(expression, compilationContext); expression = PathBinder.Bind(expression, compilationContext); expression = SubExpressionVisitor.Visit(expression, compilationContext); expression = ContextBinder.Bind(expression, compilationContext, parentContext, templatePath); return expression; } catch (Exception ex) { throw new HandlebarsCompilerException("An unhandled exception occurred while trying to compile the template", ex); } }
public static Expression Bind(Expression body, CompilationContext context, Expression parentContext, string templatePath) { var writerParameter = Expression.Parameter(typeof(TextWriter), "buffer"); var objectParameter = Expression.Parameter(typeof(object), "data"); if (parentContext == null) { parentContext = Expression.Constant(null, typeof(BindingContext)); } var encodedWriterExpression = ResolveEncodedWriter(writerParameter, context.Configuration.TextEncoder); var templatePathExpression = Expression.Constant(templatePath, typeof(string)); #if netstandard var newBindingContext = Expression.New( typeof(BindingContext).GetTypeInfo().GetConstructor( new[] { typeof(object), typeof(EncodedTextWriter), typeof(BindingContext), typeof(string) }), new [] { objectParameter, encodedWriterExpression, parentContext, templatePathExpression }); #else var newBindingContext = Expression.New( typeof(BindingContext).GetConstructor( new[] { typeof(object), typeof(EncodedTextWriter), typeof(BindingContext), typeof(string) }), new[] { objectParameter, encodedWriterExpression, parentContext, templatePathExpression }); #endif return Expression.Lambda<Action<TextWriter, object>>( Expression.Block( new [] { context.BindingContext }, new Expression[] { Expression.IfThenElse( Expression.TypeIs(objectParameter, typeof(BindingContext)), Expression.Assign(context.BindingContext, Expression.TypeAs(objectParameter, typeof(BindingContext))), Expression.Assign(context.BindingContext, newBindingContext)) }.Concat( ((BlockExpression)body).Expressions )), new[] { writerParameter, objectParameter }); }
public static Expression Replace(Expression expr, CompilationContext context) { return new StaticReplacer(context).Visit(expr); }
private StaticReplacer(CompilationContext context) : base(context) { }
private PathBinder(CompilationContext context) : base(context) { }
public static Expression Bind(Expression expr, CompilationContext context) { return new BlockHelperFunctionBinder(context).Visit(expr); }
private SubExpressionVisitor(CompilationContext context) : base(context) { }
public static Expression Visit(Expression expr, CompilationContext context) { return new UnencodedStatementVisitor(context).Visit(expr); }
private UnencodedStatementVisitor(CompilationContext context) : base(context) { }
private DeferredSectionVisitor(CompilationContext context) : base(context) { }
public static Expression Bind(Expression expr, CompilationContext context) { return new DeferredSectionVisitor(context).Visit(expr); }
private IteratorBinder(CompilationContext context) : base(context) { }
public BlockHelperFunctionBinder(CompilationContext compilationContext) { CompilationContext = compilationContext; }
public static Expression Bind(Expression expr, CompilationContext context) { return new PathBinder(context).Visit(expr); }
private PathBinder(CompilationContext context) : base(context) { }
private CommentVisitor(CompilationContext compilationContext) : base(compilationContext) { }
protected HandlebarsExpressionVisitor(CompilationContext compilationContext) { _compilationContext = compilationContext; }
private BoolishConverter(CompilationContext context) : base(context) { }
public static Expression Visit(Expression expr, CompilationContext context) { return new SubExpressionVisitor(context).Visit(expr); }
public static Expression Convert(Expression expr, CompilationContext context) { return new BoolishConverter(context).Visit(expr); }
private BlockHelperFunctionBinder(CompilationContext context) : base(context) { }
public static Expression Bind(Expression expr, CompilationContext context) { return(new PathBinder(context).Visit(expr)); }