use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class FormatNumDirective method applyForJbcSrc.
@Override
public SoyExpression applyForJbcSrc(JbcSrcPluginContext context, SoyExpression value, List<SoyExpression> args) {
Expression minFractionDigits = args.size() > 2 ? MethodRef.create(Integer.class, "valueOf", int.class).invoke(BytecodeUtils.numericConversion(args.get(2).unboxAs(long.class), Type.INT_TYPE)) : BytecodeUtils.constantNull(Type.getType(Integer.class));
Expression maxFractionDigits = args.size() > 3 ? MethodRef.create(Integer.class, "valueOf", int.class).invoke(BytecodeUtils.numericConversion(args.get(3).unboxAs(long.class), Type.INT_TYPE)) : minFractionDigits;
return SoyExpression.forString(JbcSrcMethods.FORMAT_NUM.invoke(context.getULocale(), value.coerceToDouble(), !args.isEmpty() ? args.get(0).unboxAs(String.class) : BytecodeUtils.constant(DEFAULT_FORMAT), args.size() > 1 ? args.get(1).unboxAs(String.class) : BytecodeUtils.constant("local"), minFractionDigits, maxFractionDigits));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class MaxFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression left = args.get(0);
SoyExpression right = args.get(1);
if (left.assignableToNullableInt() && right.assignableToNullableInt()) {
return SoyExpression.forInt(JbcSrcMethods.MATH_MAX_LONG.invoke(left.unboxAs(long.class), right.unboxAs(long.class)));
} else if (left.assignableToNullableFloat() && right.assignableToNullableFloat()) {
return SoyExpression.forFloat(JbcSrcMethods.MATH_MAX_DOUBLE.invoke(left.unboxAs(double.class), right.unboxAs(double.class)));
} else {
return SoyExpression.forSoyValue(NUMBER_TYPE, JbcSrcMethods.MAX_FN.invoke(left.box(), right.box()));
}
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class StrIndexOfFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression left = args.get(0);
SoyExpression right = args.get(1);
return SoyExpression.forInt(BytecodeUtils.numericConversion(left.unboxAs(String.class).invoke(JbcSrcMethods.STRING_INDEX_OF, right.unboxAs(String.class)), Type.LONG_TYPE));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class ExpressionCompiler method compile.
/**
* Compiles the given expression tree to a sequence of bytecode in the current method visitor.
*
* <p>The generated bytecode expects that the evaluation stack is empty when this method is called
* and it will generate code such that the stack contains a single SoyValue when it returns. The
* SoyValue object will have a runtime type equal to {@code node.getType().javaType()}.
*/
SoyExpression compile(ExprNode node) {
Label reattachPoint = new Label();
final SoyExpression exec = compile(node, reattachPoint);
return exec.withSource(exec.labelStart(reattachPoint));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class MsgCompiler method handleTranslationWithPlaceholders.
/**
* Handles a complex message with placeholders.
*/
private Statement handleTranslationWithPlaceholders(MsgNode msg, ImmutableList<SoyPrintDirective> escapingDirectives, Expression soyMsgParts, Expression locale, ImmutableList<SoyMsgPart> parts) {
// We need to render placeholders into a buffer and then pack them into a map to pass to
// Runtime.renderSoyMsgWithPlaceholders.
Expression placeholderMap = variables.getMsgPlaceholderMapField().accessor(thisVar);
Map<String, Statement> placeholderNameToPutStatement = new LinkedHashMap<>();
putPlaceholdersIntoMap(placeholderMap, msg, parts, placeholderNameToPutStatement);
// sanity check
checkState(!placeholderNameToPutStatement.isEmpty());
variables.setMsgPlaceholderMapMinSize(placeholderNameToPutStatement.size());
Statement populateMap = Statement.concat(placeholderNameToPutStatement.values());
Statement clearMap = placeholderMap.invokeVoid(MethodRef.LINKED_HASH_MAP_CLEAR);
Statement render;
if (areAllPrintDirectivesStreamable(escapingDirectives)) {
// No need to save/restore since rendering a message doesn't detach. All detaching for data
// should have already happened as part of constructing the placholder map.
AppendableAndOptions wrappedAppendable = applyStreamingEscapingDirectives(escapingDirectives, appendableExpression, parameterLookup.getRenderContext(), variables);
Statement initAppendable = Statement.NULL_STATEMENT;
Statement clearAppendable = Statement.NULL_STATEMENT;
Expression appendableExpression = wrappedAppendable.appendable();
if (wrappedAppendable.closeable()) {
Scope scope = variables.enterScope();
Variable appendableVar = scope.createTemporary("msg_appendable", wrappedAppendable.appendable());
initAppendable = appendableVar.initializer();
appendableExpression = appendableVar.local();
clearAppendable = Statement.concat(appendableVar.local().checkedCast(BytecodeUtils.CLOSEABLE_TYPE).invokeVoid(MethodRef.CLOSEABLE_CLOSE), scope.exitScope());
}
render = Statement.concat(initAppendable, MethodRef.RUNTIME_RENDER_SOY_MSG_PARTS_WITH_PLACEHOLDERS.invokeVoid(soyMsgParts, locale, placeholderMap, appendableExpression), clearAppendable);
} else {
// render into the handy buffer we already have!
Statement renderToBuffer = MethodRef.RUNTIME_RENDER_SOY_MSG_PARTS_WITH_PLACEHOLDERS.invokeVoid(soyMsgParts, locale, placeholderMap, tempBuffer());
// N.B. the type here is always 'string'
SoyExpression value = SoyExpression.forString(tempBuffer().invoke(MethodRef.ADVISING_STRING_BUILDER_GET_AND_CLEAR));
for (SoyPrintDirective directive : escapingDirectives) {
value = parameterLookup.getRenderContext().applyPrintDirective(directive, value);
}
render = Statement.concat(renderToBuffer, appendableExpression.appendString(value.coerceToString()).toStatement());
}
return Statement.concat(populateMap, render, clearMap);
}
Aggregations