Search in sources :

Example 6 with Variable

use of lucee.transformer.expression.var.Variable in project Lucee by lucee.

the class AbstrCFMLExprTransformer method newOp.

private Expression newOp(ExprData data, Expression expr) throws TemplateException {
    if (!(expr instanceof Variable))
        return expr;
    Variable var = (Variable) expr;
    Member m = var.getFirstMember();
    if (!(m instanceof DataMember))
        return expr;
    ExprString n = ((DataMember) m).getName();
    if (!(n instanceof LitString))
        return expr;
    LitString ls = (LitString) n;
    if (!"new".equalsIgnoreCase(ls.getString()))
        return expr;
    int start = data.srcCode.getPos();
    ExprString exprName = readComponentPath(data);
    if (exprName == null) {
        data.srcCode.setPos(start);
        return expr;
    }
    comments(data);
    if (data.srcCode.isCurrent('(')) {
        FunctionMember func = getFunctionMember(data, Identifier.toIdentifier(data.factory, "_createComponent", Identifier.CASE_ORIGNAL, null, null), true);
        func.addArgument(new Argument(exprName, "string"));
        Variable v = expr.getFactory().createVariable(expr.getStart(), expr.getEnd());
        v.addMember(func);
        comments(data);
        return v;
    }
    data.srcCode.setPos(start);
    return expr;
}
Also used : LitString(lucee.transformer.expression.literal.LitString) OpVariable(lucee.transformer.bytecode.op.OpVariable) Variable(lucee.transformer.expression.var.Variable) NamedArgument(lucee.transformer.bytecode.expression.var.NamedArgument) Argument(lucee.transformer.bytecode.expression.var.Argument) ExprString(lucee.transformer.expression.ExprString) DataMember(lucee.transformer.expression.var.DataMember) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember) DataMember(lucee.transformer.expression.var.DataMember) Member(lucee.transformer.expression.var.Member) FunctionMember(lucee.transformer.bytecode.expression.var.FunctionMember)

Example 7 with Variable

use of lucee.transformer.expression.var.Variable in project Lucee by lucee.

the class ASMUtil method cachedWithinValue.

public static Literal cachedWithinValue(Expression val) throws EvaluatorException {
    if (val instanceof Literal) {
        Literal l = (Literal) val;
        // double == days
        Double d = l.getDouble(null);
        if (d != null) {
            return val.getFactory().createLitLong(TimeSpanImpl.fromDays(d.doubleValue()).getMillis(), null, null);
        }
        return l;
    } else // createTimespan
    if (val instanceof Variable) {
        Variable var = (Variable) val;
        if (var.getMembers().size() == 1) {
            Member first = var.getFirstMember();
            if (first instanceof BIF) {
                BIF bif = (BIF) first;
                if ("createTimeSpan".equalsIgnoreCase(bif.getFlf().getName())) {
                    Argument[] args = bif.getArguments();
                    int len = ArrayUtil.size(args);
                    if (len >= 4 && len <= 5) {
                        double days = toDouble(args[0].getValue());
                        double hours = toDouble(args[1].getValue());
                        double minutes = toDouble(args[2].getValue());
                        double seconds = toDouble(args[3].getValue());
                        double millis = len == 5 ? toDouble(args[4].getValue()) : 0;
                        return val.getFactory().createLitLong(new TimeSpanImpl((int) days, (int) hours, (int) minutes, (int) seconds, (int) millis).getMillis(), null, null);
                    }
                }
            }
        }
    }
    throw cacheWithinException();
}
Also used : Variable(lucee.transformer.expression.var.Variable) Literal(lucee.transformer.expression.literal.Literal) TimeSpanImpl(lucee.runtime.type.dt.TimeSpanImpl) LitDouble(lucee.transformer.expression.literal.LitDouble) ExprDouble(lucee.transformer.expression.ExprDouble) DataMember(lucee.transformer.expression.var.DataMember) Member(lucee.transformer.expression.var.Member) BIF(lucee.transformer.bytecode.expression.var.BIF)

Example 8 with Variable

use of lucee.transformer.expression.var.Variable in project Lucee by lucee.

the class CastOther method _writeOut.

// Excel toExcel (Object)
/*final public static Method TO_EXCEL = new Method("toExcel",
			Types.EXCEL,
			new Type[]{Types.OBJECT});*/
@Override
public Type _writeOut(BytecodeContext bc, int mode) throws TransformerException {
    // Caster.toDecimal(null);
    GeneratorAdapter adapter = bc.getAdapter();
    char first = lcType.charAt(0);
    Type rtn;
    switch(first) {
        case 'a':
            if ("array".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.ARRAY))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_ARRAY);
                return Types.ARRAY;
            }
            break;
        case 'b':
            if ("base64".equals(lcType)) {
                expr.writeOut(bc, MODE_REF);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_BASE64);
                return Types.STRING;
            }
            if ("binary".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.BYTE_VALUE_ARRAY))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_BINARY);
                return Types.BYTE_VALUE_ARRAY;
            }
            if ("byte".equals(type)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.BYTE_VALUE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_BYTE_VALUE[Types.getType(rtn)]);
                return Types.BYTE_VALUE;
            }
            if ("byte".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.BYTE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_BYTE[Types.getType(rtn)]);
                return Types.BYTE;
            }
            if ("boolean".equals(lcType)) {
                return bc.getFactory().toExprBoolean(expr).writeOut(bc, MODE_REF);
            }
            break;
        case 'c':
            if ("char".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.CHAR))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_CHAR_VALUE[Types.getType(rtn)]);
                return Types.CHAR;
            }
            if ("character".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.CHARACTER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_CHARACTER[Types.getType(rtn)]);
                return Types.CHARACTER;
            }
            if ("collection".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.COLLECTION))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_COLLECTION);
                return Types.COLLECTION;
            }
            if ("component".equals(lcType) || "class".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.COMPONENT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_COMPONENT);
                return Types.COMPONENT;
            }
            break;
        case 'd':
            if ("double".equals(lcType)) {
                return bc.getFactory().toExprDouble(expr).writeOut(bc, MODE_REF);
            }
            if ("date".equals(lcType) || "datetime".equals(lcType)) {
                // First Arg
                rtn = expr.writeOut(bc, MODE_REF);
                if (rtn.equals(Types.DATE_TIME))
                    return Types.DATE_TIME;
                int type = Types.getType(rtn);
                // Second Arg
                adapter.loadArg(0);
                // adapter.invokeVirtual(Types.PAGE_CONTEXT,GET_CONFIG);
                // adapter.invokeInterface(Types.CONFIG_WEB,GET_TIMEZONE);
                adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_TIMEZONE);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_DATE[type]);
                return Types.DATE_TIME;
            }
            if ("decimal".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_DECIMAL[Types.getType(rtn)]);
                return Types.STRING;
            }
            break;
        case 'e':
            /*if("excel".equals(type)) {
            	expr.writeOut(bc,MODE_REF);
                adapter.invokeStatic(Types.EXCEL_UTIL,TO_EXCEL);
                return Types.EXCEL;
            }*/
            break;
        case 'f':
            if ("file".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.FILE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_FILE);
                return Types.FILE;
            }
            if ("float".equals(type)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.FLOAT_VALUE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_FLOAT_VALUE[Types.getType(rtn)]);
                return Types.FLOAT_VALUE;
            }
            if ("float".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.FLOAT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_FLOAT[Types.getType(rtn)]);
                return Types.FLOAT;
            }
            break;
        case 'i':
            if ("int".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.INT_VALUE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_INT_VALUE[Types.getType(rtn)]);
                return Types.INT_VALUE;
            }
            if ("integer".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.INTEGER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_INTEGER[Types.getType(rtn)]);
                return Types.INTEGER;
            }
            if ("image".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.IMAGE)) {
                    adapter.loadArg(0);
                    adapter.invokeStatic(Types.IMAGE, Methods_Caster.TO_IMAGE);
                }
                return Types.IMAGE;
            }
            break;
        case 'j':
            if ("java.lang.boolean".equals(lcType)) {
                return bc.getFactory().toExprBoolean(expr).writeOut(bc, MODE_REF);
            }
            if ("java.lang.double".equals(lcType)) {
                return bc.getFactory().toExprDouble(expr).writeOut(bc, MODE_REF);
            }
            if ("java.lang.string".equals(lcType)) {
                return bc.getFactory().toExprString(expr).writeOut(bc, MODE_REF);
            }
            if ("java.lang.stringbuffer".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.STRING_BUFFER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_STRING_BUFFER);
                return Types.STRING_BUFFER;
            }
            if ("java.lang.byte".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.BYTE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_BYTE[Types.getType(rtn)]);
                return Types.BYTE;
            }
            if ("java.lang.character".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.CHARACTER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_CHARACTER[Types.getType(rtn)]);
                return Types.CHARACTER;
            }
            if ("java.lang.short".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.SHORT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_SHORT[Types.getType(rtn)]);
                return Types.SHORT;
            }
            if ("java.lang.integer".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.INTEGER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_INTEGER[Types.getType(rtn)]);
                return Types.INTEGER;
            }
            if ("java.lang.long".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.LONG))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_LONG[Types.getType(rtn)]);
                return Types.LONG;
            }
            if ("java.lang.float".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.FLOAT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_FLOAT[Types.getType(rtn)]);
                return Types.FLOAT;
            }
            if ("java.io.file".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.FILE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_FILE);
                return Types.FILE;
            }
            if ("java.lang.object".equals(lcType)) {
                return expr.writeOut(bc, MODE_REF);
            } else if ("java.util.date".equals(lcType)) {
                // First Arg
                rtn = expr.writeOut(bc, MODE_REF);
                if (rtn.equals(Types.DATE))
                    return Types.DATE;
                if (rtn.equals(Types.DATE_TIME))
                    return Types.DATE_TIME;
                // Second Arg
                adapter.loadArg(0);
                // adapter.invokeVirtual(Types.PAGE_CONTEXT,GET_CONFIG);
                // adapter.invokeVirtual(Types.CONFIG_WEB,GET_TIMEZONE);
                adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_TIMEZONE);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_DATE[Types.getType(rtn)]);
                return Types.DATE_TIME;
            }
            break;
        case 'l':
            if ("long".equals(type)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.LONG_VALUE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_LONG_VALUE[Types.getType(rtn)]);
                return Types.LONG_VALUE;
            } else if ("long".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.LONG))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_LONG[Types.getType(rtn)]);
                return Types.LONG;
            } else if ("locale".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_LOCALE);
                return Types.LOCALE;
            }
            break;
        case 'n':
            if ("node".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.NODE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_NODE);
                return Types.NODE;
            } else if ("null".equals(lcType)) {
                expr.writeOut(bc, MODE_REF);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_NULL);
                // TODO gibt es einen null typ?
                return Types.OBJECT;
            }
            break;
        case 'o':
            if ("object".equals(lcType) || "other".equals(lcType)) {
                expr.writeOut(bc, MODE_REF);
                return Types.OBJECT;
            }
            break;
        case 't':
            if ("timezone".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_TIMEZONE);
                return Types.TIMEZONE;
            } else if ("timespan".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.TIMESPAN))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_TIMESPAN);
                return Types.TIMESPAN;
            }
            break;
        case 's':
            if ("struct".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.STRUCT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_STRUCT);
                return Types.STRUCT;
            }
            if ("short".equals(type)) {
                rtn = expr.writeOut(bc, MODE_VALUE);
                if (!rtn.equals(Types.SHORT_VALUE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_SHORT_VALUE[Types.getType(rtn)]);
                return Types.SHORT_VALUE;
            }
            if ("short".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.SHORT))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_SHORT[Types.getType(rtn)]);
                return Types.SHORT;
            }
            if ("stringbuffer".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.STRING_BUFFER))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_STRING_BUFFER);
                return Types.STRING_BUFFER;
            }
            break;
        case 'x':
            if ("xml".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.NODE))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_NODE);
                return Types.NODE;
            }
            break;
        default:
            if ("query".equals(lcType)) {
                rtn = expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.QUERY))
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_QUERY);
                return Types.QUERY;
            }
            if ("querycolumn".equals(lcType)) {
                rtn = (expr instanceof Variable) ? ((Variable) expr).writeOutCollection(bc, mode) : expr.writeOut(bc, MODE_REF);
                if (!rtn.equals(Types.QUERY_COLUMN)) {
                    adapter.loadArg(0);
                    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_QUERY_COLUMN);
                }
                return Types.QUERY_COLUMN;
            }
    }
    Type t = getType(type);
    expr.writeOut(bc, MODE_REF);
    adapter.checkCast(t);
    return t;
}
Also used : Type(org.objectweb.asm.Type) Variable(lucee.transformer.expression.var.Variable) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter)

Example 9 with Variable

use of lucee.transformer.expression.var.Variable in project Lucee by lucee.

the class NamedArgument method _writeOut.

@Override
public Type _writeOut(BytecodeContext bc, int mode) throws TransformerException {
    int form = VALUE;
    int type = STRING;
    if (name instanceof Variable && !((Variable) name).fromHash()) {
        GeneratorAdapter adapter = bc.getAdapter();
        String[] arr = VariableString.variableToStringArray((Variable) name, true);
        if (arr.length > 1) {
            form = ARRAY;
            ArrayVisitor av = new ArrayVisitor();
            av.visitBegin(adapter, Types.STRING, arr.length);
            for (int y = 0; y < arr.length; y++) {
                av.visitBeginItem(adapter, y);
                adapter.push(varKeyUpperCase ? arr[y].toUpperCase() : arr[y]);
                av.visitEndItem(bc.getAdapter());
            }
            av.visitEnd();
        } else {
            // VariableString.toExprString(name).writeOut(bc, MODE_REF);
            String str = VariableString.variableToString((Variable) name, true);
            name = bc.getFactory().createLitString(varKeyUpperCase ? str.toUpperCase() : str);
            getFactory().registerKey(bc, VariableString.toExprString(name), false);
            type = KEY;
        }
    } else {
        getFactory().registerKey(bc, name.getFactory().toExprString(name), false);
        type = KEY;
    }
    // name.writeOut(bc, MODE_REF);
    super._writeOut(bc, MODE_REF);
    // bc.getAdapter().push(variableString);
    bc.getAdapter().invokeStatic(Types.FUNCTION_VALUE_IMPL, NEW_INSTANCE[type][form]);
    return Types.FUNCTION_VALUE;
}
Also used : Variable(lucee.transformer.expression.var.Variable) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor)

Example 10 with Variable

use of lucee.transformer.expression.var.Variable in project Lucee by lucee.

the class Null method toVariable.

public Variable toVariable() {
    Variable v = getFactory().createVariable(Scope.SCOPE_UNDEFINED, getStart(), getEnd());
    v.addMember(getFactory().createDataMember(getFactory().createLitString("null")));
    return v;
}
Also used : Variable(lucee.transformer.expression.var.Variable)

Aggregations

Variable (lucee.transformer.expression.var.Variable)26 Expression (lucee.transformer.expression.Expression)16 OpVariable (lucee.transformer.bytecode.op.OpVariable)14 FunctionAsExpression (lucee.transformer.bytecode.expression.FunctionAsExpression)12 TemplateException (lucee.runtime.exp.TemplateException)8 Position (lucee.transformer.Position)5 Argument (lucee.transformer.bytecode.expression.var.Argument)5 BIF (lucee.transformer.bytecode.expression.var.BIF)5 ExprString (lucee.transformer.expression.ExprString)5 OPUnary (lucee.transformer.bytecode.op.OPUnary)4 LitString (lucee.transformer.expression.literal.LitString)4 Member (lucee.transformer.expression.var.Member)4 Body (lucee.transformer.bytecode.Body)3 Assign (lucee.transformer.bytecode.expression.var.Assign)3 Literal (lucee.transformer.expression.literal.Literal)3 TransformerException (lucee.transformer.TransformerException)2 Statement (lucee.transformer.bytecode.Statement)2 FunctionMember (lucee.transformer.bytecode.expression.var.FunctionMember)2 NamedArgument (lucee.transformer.bytecode.expression.var.NamedArgument)2 Identifier (lucee.transformer.bytecode.literal.Identifier)2