-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathAstMacroFunction.java
More file actions
130 lines (119 loc) · 4.02 KB
/
AstMacroFunction.java
File metadata and controls
130 lines (119 loc) · 4.02 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.hubspot.jinjava.el.ext;
import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.interpret.CallStack;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.MacroTagCycleException;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import de.odysseus.el.misc.LocalMessages;
import de.odysseus.el.tree.Bindings;
import de.odysseus.el.tree.impl.ast.AstFunction;
import de.odysseus.el.tree.impl.ast.AstParameters;
import java.lang.reflect.InvocationTargetException;
import jakarta.el.ELContext;
import jakarta.el.ELException;
public class AstMacroFunction extends AstFunction {
public AstMacroFunction(String name, int index, AstParameters params, boolean varargs) {
super(name, index, params, varargs);
}
@Override
public Object eval(Bindings bindings, ELContext context) {
JinjavaInterpreter interpreter = (JinjavaInterpreter) context
.getELResolver()
.getValue(context, null, ExtendedParser.INTERPRETER);
MacroFunction macroFunction = interpreter.getContext().getGlobalMacro(getName());
if (macroFunction != null) {
if (macroFunction.isDeferred()) {
throw new DeferredValueException(
getName(),
interpreter.getLineNumber(),
interpreter.getPosition()
);
}
if (!macroFunction.isCaller()) {
if (checkAndPushMacroStack(interpreter, getName())) {
return "";
}
}
try {
return invoke(
bindings,
context,
macroFunction,
AbstractCallableMethod.EVAL_METHOD
);
} catch (IllegalAccessException e) {
throw new ELException(LocalMessages.get("error.function.access", getName()), e);
} catch (InvocationTargetException e) {
throw new ELException(
LocalMessages.get("error.function.invocation", getName()),
e.getCause()
);
} finally {
if (!macroFunction.isCaller()) {
interpreter.getContext().getMacroStack().pop();
}
}
}
return interpreter.getContext().isValidationMode()
? ""
: super.eval(bindings, context);
}
public static boolean checkAndPushMacroStack(
JinjavaInterpreter interpreter,
String name
) {
CallStack macroStack = interpreter.getContext().getMacroStack();
try {
if (interpreter.getConfig().isEnableRecursiveMacroCalls()) {
if (interpreter.getConfig().getMaxMacroRecursionDepth() != 0) {
macroStack.pushWithMaxDepth(
name,
interpreter.getConfig().getMaxMacroRecursionDepth(),
interpreter.getLineNumber(),
interpreter.getPosition()
);
} else {
macroStack.pushWithoutCycleCheck(
name,
interpreter.getLineNumber(),
interpreter.getPosition()
);
}
} else {
macroStack.push(name, -1, -1);
}
} catch (MacroTagCycleException e) {
int maxDepth = interpreter.getConfig().getMaxMacroRecursionDepth();
if (maxDepth != 0 && interpreter.getConfig().isValidationMode()) {
// validation mode is only concerned with syntax
return true;
}
String message = maxDepth == 0
? String.format("Cycle detected for macro '%s'", name)
: String.format(
"Max recursion limit of %d reached for macro '%s'",
maxDepth,
name
);
interpreter.addError(
new TemplateError(
TemplateError.ErrorType.WARNING,
TemplateError.ErrorReason.EXCEPTION,
TemplateError.ErrorItem.TAG,
message,
null,
e.getLineNumber(),
e.getStartPosition(),
e,
BasicTemplateErrorCategory.CYCLE_DETECTED,
ImmutableMap.of("name", name)
)
);
return true;
}
return false;
}
}