use of com.google.template.soy.jbcsrc.restricted.Expression in project closure-templates by google.
the class LengthFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression soyExpression = args.get(0);
Expression lengthExpressionAsInt;
if (soyExpression.isBoxed()) {
lengthExpressionAsInt = soyExpression.checkedCast(SoyList.class).invoke(JbcSrcMethods.SOYLIST_LENGTH);
} else {
lengthExpressionAsInt = soyExpression.checkedCast(List.class).invoke(JbcSrcMethods.LIST_SIZE);
}
return SoyExpression.forInt(BytecodeUtils.numericConversion(lengthExpressionAsInt, Type.LONG_TYPE));
}
use of com.google.template.soy.jbcsrc.restricted.Expression 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.Expression in project closure-templates by google.
the class MsgCompiler method compileMessage.
/**
* Compiles the given {@link MsgNode} to a statement with the given escaping directives applied.
*
* <p>The returned statement must be written to a location with a stack depth of zero, since
* placeholder formatting may require detach logic.
*
* @param partsAndId The computed msg id
* @param msg The msg node
* @param escapingDirectives The set of escaping directives to apply.
*/
Statement compileMessage(MsgPartsAndIds partsAndId, MsgNode msg, ImmutableList<SoyPrintDirective> escapingDirectives) {
Expression soyMsgDefaultParts = compileDefaultMessagePartsConstant(partsAndId);
Expression soyMsgParts = parameterLookup.getRenderContext().getSoyMsgParts(partsAndId.id, soyMsgDefaultParts);
Statement printMsg;
if (msg.isRawTextMsg()) {
// Simplest case, just a static string translation
printMsg = handleBasicTranslation(escapingDirectives, soyMsgParts);
} else {
// String translation + placeholders
printMsg = handleTranslationWithPlaceholders(msg, escapingDirectives, soyMsgParts, parameterLookup.getRenderContext().getULocale(), partsAndId.parts);
}
return Statement.concat(printMsg.withSourceLocation(msg.getSourceLocation()), detachState.detachLimited(appendableExpression));
}
use of com.google.template.soy.jbcsrc.restricted.Expression in project closure-templates by google.
the class MsgCompiler method addPrintNodeToPlaceholderMap.
/**
* Returns a statement that adds the content rendered by the call to the map.
*
* @param mapExpression The map to put the new entry in
* @param mapKey The map key
* @param printNode The node
*/
private Statement addPrintNodeToPlaceholderMap(Expression mapExpression, String mapKey, PrintNode printNode) {
// This is much like the escaping path of visitPrintNode but somewhat simpler because our
// ultimate target is a string rather than putting bytes on the output stream.
Label reattachPoint = new Label();
Expression compileToString = soyNodeCompiler.compileToString(printNode, reattachPoint);
return putToMap(mapExpression, mapKey, compileToString).labelStart(reattachPoint).withSourceLocation(printNode.getSourceLocation());
}
use of com.google.template.soy.jbcsrc.restricted.Expression in project closure-templates by google.
the class MsgCompiler method putSelectPartIntoMap.
private void putSelectPartIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgSelectPart select) {
MsgSelectNode repSelectNode = originalMsg.getRepSelectNode(select.getSelectVarName());
if (!placeholderNameToPutStatement.containsKey(select.getSelectVarName())) {
Label reattachPoint = new Label();
Expression value = soyNodeCompiler.compileToString(repSelectNode.getExpr(), reattachPoint);
placeholderNameToPutStatement.put(select.getSelectVarName(), putToMap(mapExpression, select.getSelectVarName(), value).labelStart(reattachPoint));
}
// Recursively visit select cases
for (Case<String> caseOrDefault : select.getCases()) {
putPlaceholdersIntoMap(mapExpression, originalMsg, caseOrDefault.parts(), placeholderNameToPutStatement);
}
}
Aggregations