-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathFunctionCallSegment.cs
More file actions
57 lines (46 loc) · 1.82 KB
/
FunctionCallSegment.cs
File metadata and controls
57 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Fluid.Values;
namespace Fluid.Ast
{
public sealed class FunctionCallSegment : MemberSegment
{
private static readonly FunctionArguments NonCacheableArguments = new();
private volatile FunctionArguments _cachedArguments;
public FunctionCallSegment(IReadOnlyList<FunctionCallArgument> arguments)
{
Arguments = arguments ?? [];
}
public IReadOnlyList<FunctionCallArgument> Arguments { get; }
public override async ValueTask<FluidValue> ResolveAsync(FluidValue value, TemplateContext context)
{
var arguments = _cachedArguments;
// Do we need to evaluate arguments?
if (arguments == null || arguments == NonCacheableArguments)
{
if (Arguments.Count == 0)
{
arguments = FunctionArguments.Empty;
_cachedArguments = arguments;
}
else
{
var newArguments = new FunctionArguments();
foreach (var argument in Arguments)
{
newArguments.Add(argument.Name, await argument.Expression.EvaluateAsync(context));
}
// The arguments can be cached if all the parameters are LiteralExpression
if (arguments == null && Arguments.All(x => x.Expression is LiteralExpression))
{
_cachedArguments = newArguments;
}
else
{
_cachedArguments = NonCacheableArguments;
}
arguments = newArguments;
}
}
return await value.InvokeAsync(arguments, context);
}
}
}