Search in sources :

Example 11 with SoyExpression

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));
}
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 12 with SoyExpression

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

Example 13 with SoyExpression

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

Example 14 with SoyExpression

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

Example 15 with SoyExpression

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);
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Variable(com.google.template.soy.jbcsrc.TemplateVariableManager.Variable) Scope(com.google.template.soy.jbcsrc.TemplateVariableManager.Scope) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) Statement(com.google.template.soy.jbcsrc.restricted.Statement) AppendableAndOptions(com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)21 Expression (com.google.template.soy.jbcsrc.restricted.Expression)8 Statement (com.google.template.soy.jbcsrc.restricted.Statement)5 SoyType (com.google.template.soy.types.SoyType)4 Label (org.objectweb.asm.Label)4 Scope (com.google.template.soy.jbcsrc.TemplateVariableManager.Scope)3 Variable (com.google.template.soy.jbcsrc.TemplateVariableManager.Variable)3 LegacyObjectMapType (com.google.template.soy.types.LegacyObjectMapType)3 MapType (com.google.template.soy.types.MapType)3 ArrayList (java.util.ArrayList)3 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 IfBlock (com.google.template.soy.jbcsrc.ControlFlow.IfBlock)2 SoyNode (com.google.template.soy.soytree.SoyNode)2 UnknownType (com.google.template.soy.types.UnknownType)2 SoyString (com.google.template.soy.data.restricted.SoyString)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 BasicExpressionCompiler (com.google.template.soy.jbcsrc.ExpressionCompiler.BasicExpressionCompiler)1 CodeBuilder (com.google.template.soy.jbcsrc.restricted.CodeBuilder)1 MethodRef (com.google.template.soy.jbcsrc.restricted.MethodRef)1 AppendableAndOptions (com.google.template.soy.jbcsrc.restricted.SoyJbcSrcPrintDirective.Streamable.AppendableAndOptions)1