Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Handlebars.Test/ClosureBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static List<DecoratorDelegate> GenerateDecoratorDelegates(ClosureBuilder
var helpers = new List<DecoratorDelegate>();
for (int i = 0; i < count; i++)
{
DecoratorDelegate helper = (in EncodedTextWriter writer, BindingContext context, TemplateDelegate function) => function;
DecoratorDelegate helper = (EncodedTextWriter writer, BindingContext context, TemplateDelegate function) => function;
builder.Add(Const(helper));
helpers.Add(helper);
}
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars.Test/DecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void OverrideFunctionWithDecorator(IHandlebars handlebars)
(TemplateDelegate function, in DecoratorOptions options, in Context context, in Arguments arguments) =>
{
var value = arguments.At<int>(0);
return (in EncodedTextWriter writer, BindingContext bindingContext) =>
return (EncodedTextWriter writer, BindingContext bindingContext) =>
{
writer.WriteSafeString(value);
function(writer, bindingContext);
Expand Down
27 changes: 27 additions & 0 deletions source/Handlebars.Test/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,5 +733,32 @@ public void UnrecognisedExpressionThrowsOutOfMemoryException()

Assert.Throws<HandlebarsCompilerException>(()=> Handlebars.Compile(source));
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/458
// System.NotImplementedException: byref delegate on Xamarin.iOS / Mono
// The Mono runtime does not support delegates with byref (in/ref) parameters.
// TemplateDelegate previously used `in EncodedTextWriter` which produced a
// byref delegate incompatible with Mono's AOT/JIT compiler.
[Fact]
public void Issue458_BasicTemplateCompilationAndRender()
{
// Validates the scenario that fails on Mono: simple compile + render
var handlebars = Handlebars.Create();
var render = handlebars.Compile("{{input}}");
object data = new { input = 42 };
var actual = render(data);
Assert.Equal("42", actual);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/458
// Ensures block helpers still work after the byref delegate fix
[Fact]
public void Issue458_BlockHelperTemplateCompilationAndRender()
{
var handlebars = Handlebars.Create();
var render = handlebars.Compile("{{#if show}}visible{{/if}}");
var actual = render(new { show = true });
Assert.Equal("visible", actual);
}
}
}
4 changes: 2 additions & 2 deletions source/Handlebars/Compiler/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public CompilationContext(ICompiledHandlebarsConfiguration configuration)
{
Configuration = configuration;
BindingContext = Expression.Parameter(typeof(BindingContext), "context");
EncodedWriter = Expression.Parameter(typeof(EncodedTextWriter).MakeByRefType(), "writer");
EncodedWriter = Expression.Parameter(typeof(EncodedTextWriter), "writer");

Args = new CompilationContextArgs(this);
}
Expand All @@ -18,7 +18,7 @@ public CompilationContext(CompilationContext context)
{
Configuration = context.Configuration;
BindingContext = Expression.Parameter(typeof(BindingContext), "context");
EncodedWriter = Expression.Parameter(typeof(EncodedTextWriter).MakeByRefType(), "writer");
EncodedWriter = Expression.Parameter(typeof(EncodedTextWriter), "writer");

Args = new CompilationContextArgs(this);
}
Expand Down
4 changes: 2 additions & 2 deletions source/Handlebars/Compiler/FunctionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace HandlebarsDotNet.Compiler
{
internal static class FunctionBuilder
{
private static readonly TemplateDelegate EmptyTemplateLambda =
(in EncodedTextWriter writer, BindingContext context) => { };
private static readonly TemplateDelegate EmptyTemplateLambda =
(EncodedTextWriter writer, BindingContext context) => { };

public static Expression Reduce(Expression expression, CompilationContext context, out IReadOnlyList<DecoratorDefinition> decorators)
{
Expand Down
8 changes: 4 additions & 4 deletions source/Handlebars/Compiler/HandlebarsCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace HandlebarsDotNet.Compiler
{
public delegate void TemplateDelegate(in EncodedTextWriter writer, BindingContext context);
public delegate void TemplateDelegate(EncodedTextWriter writer, BindingContext context);

internal static class HandlebarsCompiler
{
Expand All @@ -29,7 +29,7 @@ public static TemplateDelegate Compile(ExtendedStringReader source, CompilationC
{
var a1 = action;
var decorator = decorators.Compile(compilationContext);
action = (in EncodedTextWriter writer, BindingContext context) =>
action = (EncodedTextWriter writer, BindingContext context) =>
{
decorator(writer, context, a1)(writer, context);
};
Expand Down Expand Up @@ -63,7 +63,7 @@ internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFact
{
var a1 = compiledView;
var decorator = decorators.Compile(compilationContext);
compiledView = (in EncodedTextWriter writer, BindingContext context) =>
compiledView = (EncodedTextWriter writer, BindingContext context) =>
{
decorator(writer, context, a1)(writer, context);
};
Expand All @@ -78,7 +78,7 @@ internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFact

var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext));

return (in EncodedTextWriter writer, BindingContext context) =>
return (EncodedTextWriter writer, BindingContext context) =>
{
var config = context.Configuration;
using var bindingContext = BindingContext.Create(config, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace HandlebarsDotNet.Compiler
{
public delegate TemplateDelegate DecoratorDelegate(in EncodedTextWriter writer, BindingContext context, TemplateDelegate function);
public delegate TemplateDelegate DecoratorDelegate(EncodedTextWriter writer, BindingContext context, TemplateDelegate function);

internal readonly struct DecoratorDefinition
{
Expand All @@ -20,7 +20,7 @@ public DecoratorDefinition(Expression decorator, ExpressionContainer<TemplateDel

public DecoratorDelegate Compile(CompilationContext context)
{
if (Function is null || Decorator is null) return (in EncodedTextWriter writer, BindingContext bindingContext, TemplateDelegate function) => function;
if (Function is null || Decorator is null) return (EncodedTextWriter writer, BindingContext bindingContext, TemplateDelegate function) => function;

var lambda = Expression.Lambda<DecoratorDelegate>(
Decorator,
Expand All @@ -47,7 +47,7 @@ CompilationContext context
var definition = decoratorDefinitions[index];
var f = definition.Compile(context);
var current = decorator;
decorator = (in EncodedTextWriter writer, BindingContext bindingContext, TemplateDelegate function) =>
decorator = (EncodedTextWriter writer, BindingContext bindingContext, TemplateDelegate function) =>
f(writer, bindingContext, current(writer, bindingContext, function));
}

Expand Down
Loading