Search in sources :

Example 16 with Expression

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));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression)

Example 17 with Expression

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));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) LocaleString(com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)

Example 18 with Expression

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));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Statement(com.google.template.soy.jbcsrc.restricted.Statement)

Example 19 with Expression

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());
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Label(org.objectweb.asm.Label)

Example 20 with Expression

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);
    }
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) Label(org.objectweb.asm.Label) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode)

Aggregations

Expression (com.google.template.soy.jbcsrc.restricted.Expression)32 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)26 Statement (com.google.template.soy.jbcsrc.restricted.Statement)15 Label (org.objectweb.asm.Label)15 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)8 ArrayList (java.util.ArrayList)8 Statement.returnExpression (com.google.template.soy.jbcsrc.restricted.Statement.returnExpression)5 Scope (com.google.template.soy.jbcsrc.TemplateVariableManager.Scope)4 Variable (com.google.template.soy.jbcsrc.TemplateVariableManager.Variable)4 LocalVariable (com.google.template.soy.jbcsrc.restricted.LocalVariable)4 AppendableAndOptions (com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions)4 ImmutableList (com.google.common.collect.ImmutableList)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 SoyClassWriter (com.google.template.soy.jbcsrc.internal.SoyClassWriter)2 FieldRef (com.google.template.soy.jbcsrc.restricted.FieldRef)2 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)2 RangeArgs (com.google.template.soy.shared.RangeArgs)2 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)2 ForNonemptyNode (com.google.template.soy.soytree.ForNonemptyNode)2 LinkedHashMap (java.util.LinkedHashMap)2