Search in sources :

Example 6 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class KeysFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression soyExpression = args.get(0);
    SoyType argType = soyExpression.soyType();
    // TODO(lukes): this logic should live in ResolveExpressionTypesVisitor
    SoyType listElementType;
    if (argType.getKind() == Kind.LEGACY_OBJECT_MAP) {
        // pretty much just string
        listElementType = ((LegacyObjectMapType) argType).getKeyType();
    } else if (argType.getKind() == Kind.LIST) {
        listElementType = IntType.getInstance();
    } else {
        listElementType = UnknownType.getInstance();
    }
    return SoyExpression.forList(ListType.of(listElementType), JbcSrcMethods.KEYS_FN.invoke(soyExpression.box().checkedCast(SoyLegacyObjectMap.class)));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) SoyType(com.google.template.soy.types.SoyType)

Example 7 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression 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 8 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class StrContainsFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression left = args.get(0);
    SoyExpression right = args.get(1);
    return SoyExpression.forBool(left.unboxAs(String.class).invoke(JbcSrcMethods.STRING_CONTAINS, right.coerceToString()));
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression)

Example 9 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class FloatFunction method computeForJbcSrc.

@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
    SoyExpression arg = args.get(0);
    SoyExpression unboxed = arg.isBoxed() ? arg.unboxAs(long.class) : arg;
    SoyExpression result = SoyExpression.forFloat(BytecodeUtils.numericConversion(unboxed, Type.DOUBLE_TYPE));
    return arg.isCheap() ? result.asCheap() : result;
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression)

Example 10 with SoyExpression

use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.

the class ExpressionCompilerTest method testCollectionLiterals_map.

@Test
public void testCollectionLiterals_map() {
    assertExpression("[:]").evaluatesTo(SoyValueConverter.EMPTY_DICT);
    // Map values are always boxed.  SoyMaps use == for equality, so check equivalence by comparing
    // their string representations.
    SoyExpression compile = compileExpression("['a': 1, 'b': 1.0, 'c': 'asdf', 'd': false]").coerceToString();
    ExpressionTester.assertThatExpression(compile).evaluatesTo(DictImpl.forProviderMap(ImmutableMap.<String, SoyValue>of("a", IntegerData.forValue(1), "b", FloatData.forValue(1.0), "c", StringData.forValue("asdf"), "d", BooleanData.FALSE), RuntimeMapTypeTracker.Type.LEGACY_OBJECT_MAP_OR_RECORD).toString());
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Test(org.junit.Test)

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