Search in sources :

Example 21 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TagFunction method addArgument.

private void addArgument(Function func, Tag tag) {
    Attribute attr;
    // name
    Expression name = tag.removeAttribute("name").getValue();
    // type
    attr = tag.removeAttribute("type");
    Expression type = (attr == null) ? tag.getFactory().createLitString("any") : attr.getValue();
    // required
    attr = tag.removeAttribute("required");
    Expression required = (attr == null) ? tag.getFactory().FALSE() : attr.getValue();
    // default
    attr = tag.removeAttribute("default");
    Expression defaultValue = (attr == null) ? null : attr.getValue();
    // passby
    attr = tag.removeAttribute("passby");
    LitBoolean passByReference = tag.getFactory().TRUE();
    if (attr != null) {
        // i can cast irt to LitString because he evulator check this before
        String str = ((LitString) attr.getValue()).getString();
        if (str.trim().equalsIgnoreCase("value"))
            passByReference = tag.getFactory().FALSE();
    }
    // displayname
    attr = tag.removeAttribute("displayname");
    Expression displayName = (attr == null) ? tag.getFactory().EMPTY() : attr.getValue();
    // hint
    attr = tag.removeAttribute("hint");
    if (attr == null)
        attr = tag.removeAttribute("description");
    Expression hint;
    if (attr == null)
        hint = tag.getFactory().EMPTY();
    else
        hint = attr.getValue();
    func.addArgument(name, type, required, defaultValue, passByReference, displayName, hint, tag.getAttributes());
}
Also used : LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) LitBoolean(lucee.transformer.expression.literal.LitBoolean) LitString(lucee.transformer.expression.literal.LitString)

Example 22 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TagTry method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    adapter.visitLabel(begin);
    Body tryBody = new BodyBase(getFactory());
    List<Tag> catches = new ArrayList<Tag>();
    Tag tmpFinal = null;
    tryBody.setParent(getBody().getParent());
    List<Statement> statements = getBody().getStatements();
    Statement stat;
    Tag tag;
    {
        Iterator<Statement> it = statements.iterator();
        while (it.hasNext()) {
            stat = it.next();
            if (stat instanceof Tag) {
                tag = (Tag) stat;
                if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Catch")) {
                    catches.add(tag);
                    continue;
                } else if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Finally")) {
                    tmpFinal = tag;
                    continue;
                }
            }
            tryBody.addStatement(stat);
        }
        ;
    }
    final Tag _finally = tmpFinal;
    // has no try body, if there is no try body, no catches are executed, only finally
    if (!tryBody.hasStatements()) {
        if (_finally != null && _finally.getBody() != null) {
            BodyBase.writeOut(bc, _finally.getBody());
        // ExpressionUtil.writeOut(_finally.getBody(), bc);
        }
        return;
    }
    final int old = adapter.newLocal(Types.PAGE_EXCEPTION);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
    adapter.storeLocal(old);
    TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) throws TransformerException {
            adapter.loadArg(0);
            adapter.loadLocal(old);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
            if (_finally != null) {
                ExpressionUtil.visitLine(bc, _finally.getStart());
                BodyBase.writeOut(bc, _finally.getBody());
            // ExpressionUtil.writeOut(_finally.getBody(), bc);
            }
        }
    }, getFlowControlFinal());
    // Try
    tcfv.visitTryBegin(bc);
    BodyBase.writeOut(bc, tryBody);
    // ExpressionUtil.writeOut(tryBody, bc);
    int e = tcfv.visitTryEndCatchBeging(bc);
    // if(e instanceof lucee.runtime.exp.Abort) throw e;
    Label abortEnd = new Label();
    adapter.loadLocal(e);
    // Abort.isAbort(t);
    adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
    // adapter.instanceOf(Types.ABORT);
    adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
    adapter.loadLocal(e);
    adapter.throwException();
    adapter.visitLabel(abortEnd);
    // PageExceptionImpl old=pc.getCatch();
    // PageException pe=Caster.toPageEception(e);
    int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
    adapter.loadLocal(e);
    adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
    adapter.storeLocal(pe);
    Iterator<Tag> it = catches.iterator();
    Attribute attrType;
    Expression type;
    Label endAllIfs = new Label();
    Tag tagElse = null;
    while (it.hasNext()) {
        tag = it.next();
        Label endIf = new Label();
        attrType = tag.getAttribute("type");
        type = bc.getFactory().createLitString("any");
        if (attrType != null)
            type = attrType.getValue();
        if (type instanceof LitString && ((LitString) type).getString().equalsIgnoreCase("any")) {
            tagElse = tag;
            continue;
        }
        ExpressionUtil.visitLine(bc, tag.getStart());
        // if(pe.typeEqual(@type)
        adapter.loadLocal(pe);
        type.writeOut(bc, Expression.MODE_REF);
        adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
        adapter.ifZCmp(Opcodes.IFEQ, endIf);
        catchBody(bc, adapter, tag, pe, true, true);
        adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
        adapter.visitLabel(endIf);
    }
    // else
    if (tagElse != null) {
        catchBody(bc, adapter, tagElse, pe, true, true);
    } else {
        // pc.setCatch(pe,true);
        adapter.loadArg(0);
        adapter.loadLocal(pe);
        adapter.push(false);
        adapter.push(true);
        adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH3);
        // throw pe;
        adapter.loadLocal(pe);
        adapter.throwException();
    }
    adapter.visitLabel(endAllIfs);
    // PageExceptionImpl old=pc.getCatch();
    tcfv.visitCatchEnd(bc);
}
Also used : Statement(lucee.transformer.bytecode.Statement) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) Iterator(java.util.Iterator) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryCatchFinallyVisitor(lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 23 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TagTry2 method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    adapter.visitLabel(begin);
    Body tryBody = new BodyBase(getFactory());
    List<Tag> catches = new ArrayList<Tag>();
    Tag tmpFinal = null;
    tryBody.setParent(getBody().getParent());
    List<Statement> statements = getBody().getStatements();
    Statement stat;
    Tag tag;
    {
        Iterator<Statement> it = statements.iterator();
        while (it.hasNext()) {
            stat = it.next();
            if (stat instanceof Tag) {
                tag = (Tag) stat;
                if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Catch")) {
                    catches.add(tag);
                    continue;
                } else if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Finally")) {
                    tmpFinal = tag;
                    continue;
                }
            }
            tryBody.addStatement(stat);
        }
        ;
    }
    final Tag _finally = tmpFinal;
    // has no try body, if there is no try body, no catches are executed, only finally
    if (!tryBody.hasStatements()) {
        if (_finally != null && _finally.getBody() != null) {
            BodyBase.writeOut(bc, _finally.getBody());
        // ExpressionUtil.writeOut(_finally.getBody(), bc);
        }
        return;
    }
    TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) throws TransformerException {
            if (_finally != null) {
                ExpressionUtil.visitLine(bc, _finally.getStart());
                BodyBase.writeOut(bc, _finally.getBody());
            // ExpressionUtil.writeOut(_finally.getBody(), bc);
            }
        }
    }, getFlowControlFinal());
    // Try
    tcfv.visitTryBegin(bc);
    BodyBase.writeOut(bc, tryBody);
    // ExpressionUtil.writeOut(tryBody, bc);
    int e = tcfv.visitTryEndCatchBeging(bc);
    // if(e instanceof lucee.runtime.exp.Abort) throw e;
    Label abortEnd = new Label();
    adapter.loadLocal(e);
    // Abort.isAbort(t);
    adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
    // adapter.instanceOf(Types.ABORT);
    adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
    adapter.loadLocal(e);
    adapter.throwException();
    adapter.visitLabel(abortEnd);
    // PageExceptionImpl old=pc.getCatch();
    int oldPE = adapter.newLocal(Types.PAGE_EXCEPTION);
    int oldName = adapter.newLocal(Types.STRING);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
    adapter.storeLocal(oldPE);
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, GET_CATCH_NAME);
    adapter.storeLocal(oldName);
    // PageException pe=Caster.toPageEception(e);
    int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
    adapter.loadLocal(e);
    adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
    adapter.storeLocal(pe);
    Iterator<Tag> it = catches.iterator();
    Attribute attrType;
    Expression type;
    Label endAllIfs = new Label();
    Tag tagElse = null;
    while (it.hasNext()) {
        tag = it.next();
        Label endIf = new Label();
        // type
        attrType = tag.getAttribute("type");
        type = bc.getFactory().createLitString("any");
        if (attrType != null)
            type = attrType.getValue();
        if (type instanceof LitString && ((LitString) type).getString().equalsIgnoreCase("any")) {
            tagElse = tag;
            continue;
        }
        ExpressionUtil.visitLine(bc, tag.getStart());
        // if(pe.typeEqual(@type)
        adapter.loadLocal(pe);
        type.writeOut(bc, Expression.MODE_REF);
        adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
        adapter.ifZCmp(Opcodes.IFEQ, endIf);
        catchBody(bc, adapter, tag, pe, true, true, extractName(tag));
        adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
        adapter.visitLabel(endIf);
    }
    // else
    if (tagElse != null) {
        catchBody(bc, adapter, tagElse, pe, true, true, extractName(tagElse));
    } else {
        // pc.setCatch(pe,true);
        adapter.loadArg(0);
        adapter.loadLocal(pe);
        adapter.push(false);
        adapter.push(true);
        adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH3);
        // throw pe;
        adapter.loadLocal(pe);
        adapter.throwException();
    }
    adapter.visitLabel(endAllIfs);
    adapter.loadLocal(oldName);
    Label notNull = new Label();
    adapter.visitJumpInsn(Opcodes.IFNONNULL, notNull);
    // NULL
    adapter.loadArg(0);
    adapter.loadLocal(oldPE);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
    Label end = new Label();
    adapter.visitJumpInsn(Opcodes.GOTO, end);
    adapter.visitLabel(notNull);
    // NOT NULL
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    adapter.loadLocal(oldPE);
    adapter.loadLocal(oldName);
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, SET_CATCH_PE2);
    adapter.visitLabel(end);
    tcfv.visitCatchEnd(bc);
}
Also used : Statement(lucee.transformer.bytecode.Statement) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) Iterator(java.util.Iterator) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryCatchFinallyVisitor(lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor) Body(lucee.transformer.bytecode.Body) BodyBase(lucee.transformer.bytecode.BodyBase) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 24 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class Function method createArguments.

private final void createArguments(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter ga = bc.getAdapter();
    ga.push(arguments.size());
    ga.newArray(FUNCTION_ARGUMENT);
    Argument arg;
    for (int i = 0; i < arguments.size(); i++) {
        arg = arguments.get(i);
        boolean canHaveKey = Factory.canRegisterKey(arg.getName());
        // CHECK if default values
        // type
        ExprString _strType = arg.getType();
        short _type = CFTypes.TYPE_UNKNOW;
        if (_strType instanceof LitString) {
            _type = CFTypes.toShortStrict(((LitString) _strType).getString(), CFTypes.TYPE_UNKNOW);
        }
        boolean useType = !canHaveKey || _type != CFTypes.TYPE_ANY;
        // boolean useStrType=useType && (_type==CFTypes.TYPE_UNDEFINED || _type==CFTypes.TYPE_UNKNOW || CFTypes.toString(_type, null)==null);
        // required
        ExprBoolean _req = arg.getRequired();
        boolean useReq = !canHaveKey || toBoolean(_req, null) != Boolean.FALSE;
        // default-type
        Expression _def = arg.getDefaultValueType(bc.getFactory());
        boolean useDef = !canHaveKey || toInt(_def, -1) != FunctionArgument.DEFAULT_TYPE_NULL;
        // pass by reference
        ExprBoolean _pass = arg.isPassByReference();
        boolean usePass = !canHaveKey || toBoolean(_pass, null) != Boolean.TRUE;
        // display-hint
        ExprString _dsp = arg.getDisplayName();
        boolean useDsp = !canHaveKey || !isLiteralEmptyString(_dsp);
        // hint
        ExprString _hint = arg.getHint();
        boolean useHint = !canHaveKey || !isLiteralEmptyString(_hint);
        // meta
        Map _meta = arg.getMetaData();
        boolean useMeta = !canHaveKey || (_meta != null && !_meta.isEmpty());
        int functionIndex = 7;
        if (!useMeta) {
            functionIndex--;
            if (!useHint) {
                functionIndex--;
                if (!useDsp) {
                    functionIndex--;
                    if (!usePass) {
                        functionIndex--;
                        if (!useDef) {
                            functionIndex--;
                            if (!useReq) {
                                functionIndex--;
                                if (!useType) {
                                    functionIndex--;
                                }
                            }
                        }
                    }
                }
            }
        }
        // write out arguments
        ga.dup();
        ga.push(i);
        // new FunctionArgument(...)
        ga.newInstance(canHaveKey && functionIndex < INIT_FAI_KEY_LIGHT.length ? FUNCTION_ARGUMENT_LIGHT : FUNCTION_ARGUMENT_IMPL);
        ga.dup();
        // name
        bc.getFactory().registerKey(bc, arg.getName(), false);
        // type
        if (functionIndex >= INIT_FAI_KEY.length - 7) {
            _strType.writeOut(bc, Expression.MODE_REF);
            bc.getAdapter().push(_type);
        }
        // required
        if (functionIndex >= INIT_FAI_KEY.length - 6)
            _req.writeOut(bc, Expression.MODE_VALUE);
        // default value
        if (functionIndex >= INIT_FAI_KEY.length - 5)
            _def.writeOut(bc, Expression.MODE_VALUE);
        // pass by reference
        if (functionIndex >= INIT_FAI_KEY.length - 4)
            _pass.writeOut(bc, Expression.MODE_VALUE);
        // display-name
        if (functionIndex >= INIT_FAI_KEY.length - 3)
            _dsp.writeOut(bc, Expression.MODE_REF);
        // hint
        if (functionIndex >= INIT_FAI_KEY.length - 2)
            _hint.writeOut(bc, Expression.MODE_REF);
        // meta
        if (functionIndex == INIT_FAI_KEY.length - 1)
            Page.createMetaDataStruct(bc, _meta, null);
        if (functionIndex < INIT_FAI_KEY_LIGHT.length)
            ga.invokeConstructor(FUNCTION_ARGUMENT_LIGHT, INIT_FAI_KEY[functionIndex]);
        else
            ga.invokeConstructor(FUNCTION_ARGUMENT_IMPL, INIT_FAI_KEY[functionIndex]);
        ga.visitInsn(Opcodes.AASTORE);
    }
}
Also used : LitString(lucee.transformer.expression.literal.LitString) Argument(lucee.transformer.bytecode.statement.Argument) FunctionArgument(lucee.runtime.type.FunctionArgument) ExprString(lucee.transformer.expression.ExprString) Expression(lucee.transformer.expression.Expression) ExprBoolean(lucee.transformer.expression.ExprBoolean) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class BodyBase method addStatement.

/**
 * @see lucee.transformer.bytecode.Body#addStatement(lucee.transformer.bytecode.Statement)
 */
@Override
public void addStatement(Statement statement) {
    if (statement instanceof PrintOut) {
        Expression expr = ((PrintOut) statement).getExpr();
        if (expr instanceof LitString && concatPrintouts(((LitString) expr).getString()))
            return;
    }
    statement.setParent(this);
    this.statements.add(statement);
    last = statement;
}
Also used : LitString(lucee.transformer.expression.literal.LitString) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression)

Aggregations

LitString (lucee.transformer.expression.literal.LitString)36 Expression (lucee.transformer.expression.Expression)21 ExprString (lucee.transformer.expression.ExprString)13 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)9 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)8 TransformerException (lucee.transformer.TransformerException)6 Statement (lucee.transformer.bytecode.Statement)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)5 Body (lucee.transformer.bytecode.Body)5 Argument (lucee.transformer.bytecode.expression.var.Argument)5 PrintOut (lucee.transformer.bytecode.statement.PrintOut)5 Literal (lucee.transformer.expression.literal.Literal)5 TemplateException (lucee.runtime.exp.TemplateException)4 BytecodeContext (lucee.transformer.bytecode.BytecodeContext)4 TagLibTag (lucee.transformer.library.tag.TagLibTag)4 Label (org.objectweb.asm.Label)4 BodyBase (lucee.transformer.bytecode.BodyBase)3 Page (lucee.transformer.bytecode.Page)3