Search in sources :

Example 1 with TryFinallyVisitor

use of lucee.transformer.bytecode.visitor.TryFinallyVisitor in project Lucee by lucee.

the class TagGroupUtil method writeOutTypeQuery.

public static void writeOutTypeQuery(final TagGroup tag, BytecodeContext bc) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    tag.setNumberIterator(adapter.newLocal(NUMBER_ITERATOR));
    boolean isOutput = tag.getType() == TagGroup.TAG_OUTPUT;
    ParseBodyVisitor pbv = isOutput ? new ParseBodyVisitor() : null;
    if (isOutput)
        pbv.visitBegin(bc);
    // Query query=pc.getQuery(@query);
    tag.setQuery(adapter.newLocal(Types.QUERY));
    adapter.loadArg(0);
    Expression val = tag.getAttribute("query").getValue();
    val.writeOut(bc, Expression.MODE_REF);
    if (val instanceof LitString)
        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_QUERY_STRING);
    else
        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_QUERY_OBJ);
    adapter.storeLocal(tag.getQuery());
    tag.setPID(adapter.newLocal(Types.INT_VALUE));
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
    adapter.storeLocal(tag.getPID());
    // int startAt=query.getCurrentrow();
    final int startAt = adapter.newLocal(Types.INT_VALUE);
    adapter.loadLocal(tag.getQuery());
    adapter.loadLocal(tag.getPID());
    // adapter.loadArg(0);
    // adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
    adapter.invokeInterface(Types.QUERY, TagLoop.GET_CURRENTROW_1);
    adapter.storeLocal(startAt);
    // if(query.getRecordcount()>0) {
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(Types.QUERY, GET_RECORDCOUNT);
    div.visitGT();
    adapter.push(0);
    div.visitEnd(bc);
    Label ifRecCount = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, ifRecCount);
    // startrow
    int from = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartRow = tag.getAttribute("startrow");
    if (attrStartRow != null) {
        // NumberRange.range(@startrow,1)
        // attrStartRow.getValue().writeOut(bc, Expression.MODE_VALUE);
        bc.getFactory().toExprInt(attrStartRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        // adapter.visitInsn(Opcodes.D2I);
        adapter.push(1);
        adapter.invokeStatic(Types.NUMBER_RANGE, RANGE);
    // adapter.visitInsn(Opcodes.D2I);
    } else {
        adapter.push(1);
    }
    adapter.storeLocal(from);
    // numberIterator
    adapter.loadLocal(from);
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(Types.QUERY, GET_RECORDCOUNT);
    // adapter.visitInsn(Opcodes.I2D);
    Attribute attrMaxRow = tag.getAttribute("maxrows");
    Attribute attrEndRow = tag.getAttribute("endrow");
    if (attrMaxRow != null) {
        bc.getFactory().toExprInt(attrMaxRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_MAX);
    } else if (attrEndRow != null) {
        bc.getFactory().toExprInt(attrEndRow.getValue()).writeOut(bc, Expression.MODE_VALUE);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_END);
    } else {
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_2);
    }
    adapter.storeLocal(tag.getNumberIterator());
    // Group
    Attribute attrGroup = tag.getAttribute("group");
    Attribute attrGroupCS = tag.getAttribute("groupcasesensitive");
    tag.setGroup(adapter.newLocal(Types.STRING));
    final int groupCaseSensitive = adapter.newLocal(Types.BOOLEAN_VALUE);
    if (attrGroup != null) {
        attrGroup.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.storeLocal(tag.getGroup());
        if (attrGroupCS != null)
            attrGroupCS.getValue().writeOut(bc, Expression.MODE_VALUE);
        else
            adapter.push(false);
        adapter.storeLocal(groupCaseSensitive);
    }
    // pc.us().addQuery(query);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, US);
    adapter.loadLocal(tag.getQuery());
    adapter.invokeInterface(UNDEFINED, ADD_QUERY);
    // current
    final int current = adapter.newLocal(Types.INT_VALUE);
    adapter.loadLocal(from);
    adapter.push(1);
    adapter.visitInsn(Opcodes.ISUB);
    adapter.storeLocal(current);
    // Try
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            // query.reset();
            // query.go(startAt);
            adapter.loadLocal(tag.getQuery());
            adapter.loadLocal(startAt);
            adapter.loadLocal(tag.getPID());
            // adapter.loadArg(0);
            // adapter.invokeVirtual(Types.PAGE_CONTEXT, TagLoop.GET_ID);
            adapter.invokeInterface(Types.QUERY, TagLoop.GO);
            adapter.pop();
            // pc.us().removeQuery();
            adapter.loadArg(0);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, US);
            adapter.invokeInterface(UNDEFINED, REMOVE_QUERY);
            // NumberIterator.release(ni);
            adapter.loadLocal(tag.getNumberIterator());
            adapter.invokeStatic(NUMBER_ITERATOR, REALEASE);
        }
    }, null);
    tfv.visitTryBegin(bc);
    WhileVisitor wv = new WhileVisitor();
    if (tag instanceof TagLoop)
        ((TagLoop) tag).setLoopVisitor(wv);
    wv.visitBeforeExpression(bc);
    // while(ni.isValid()) {
    adapter.loadLocal(tag.getNumberIterator());
    adapter.loadLocal(current);
    adapter.push(1);
    adapter.visitInsn(Opcodes.IADD);
    adapter.invokeVirtual(NUMBER_ITERATOR, IS_VALID_1);
    wv.visitAfterExpressionBeforeBody(bc);
    // if(!query.go(ni.current()))break;
    adapter.loadLocal(tag.getQuery());
    adapter.loadLocal(tag.getNumberIterator());
    adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
    adapter.loadLocal(tag.getPID());
    adapter.invokeInterface(Types.QUERY, TagLoop.GO);
    NotVisitor.visitNot(bc);
    Label _if = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, _if);
    wv.visitBreak(bc);
    adapter.visitLabel(_if);
    if (attrGroup != null) {
        // NumberIterator oldNi=numberIterator;
        int oldNi = adapter.newLocal(NUMBER_ITERATOR);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.storeLocal(oldNi);
        // numberIterator=NumberIterator.load(ni,query,group,grp_case);
        adapter.loadArg(0);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.loadLocal(tag.getQuery());
        adapter.loadLocal(tag.getGroup());
        adapter.loadLocal(groupCaseSensitive);
        adapter.invokeStatic(NUMBER_ITERATOR, LOAD_5);
        adapter.storeLocal(tag.getNumberIterator());
        // current=oldNi.current();
        adapter.loadLocal(oldNi);
        adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
        adapter.storeLocal(current);
        tag.getBody().writeOut(bc);
        // tmp(adapter,current);
        // NumberIterator.release(ni);
        adapter.loadLocal(tag.getNumberIterator());
        adapter.invokeStatic(NUMBER_ITERATOR, REALEASE);
        // numberIterator=oldNi;
        adapter.loadLocal(oldNi);
        adapter.storeLocal(tag.getNumberIterator());
    } else {
        // current=ni.current();
        adapter.loadLocal(tag.getNumberIterator());
        adapter.invokeVirtual(NUMBER_ITERATOR, CURRENT);
        adapter.storeLocal(current);
        tag.getBody().writeOut(bc);
    }
    // ni.setCurrent(current+1);
    /*adapter.loadLocal(tag.getNumberIterator());
					adapter.loadLocal(current);
					adapter.push(1);
					adapter.visitInsn(Opcodes.IADD);
					adapter.invokeVirtual(NUMBER_ITERATOR, SET_CURRENT);*/
    wv.visitAfterBody(bc, tag.getEnd());
    tfv.visitTryEnd(bc);
    adapter.visitLabel(ifRecCount);
    if (isOutput)
        pbv.visitEnd(bc);
}
Also used : ParseBodyVisitor(lucee.transformer.bytecode.visitor.ParseBodyVisitor) Label(org.objectweb.asm.Label) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) WhileVisitor(lucee.transformer.bytecode.visitor.WhileVisitor) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 2 with TryFinallyVisitor

use of lucee.transformer.bytecode.visitor.TryFinallyVisitor in project Lucee by lucee.

the class TagHelper method writeOut.

/**
 * writes out the tag
 * @param tag
 * @param bc
 * @param doReuse
 * @throws TransformerException
 * @throws BundleException
 * @throws ClassException
 */
public static void writeOut(Tag tag, BytecodeContext bc, boolean doReuse, final FlowControlFinal fcf) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    final TagLibTag tlt = tag.getTagLibTag();
    final ClassDefinition cd = tlt.getTagClassDefinition();
    final boolean fromBundle = cd.getName() != null;
    final Type currType;
    if (fromBundle) {
        try {
            if (Reflector.isInstaneOf(cd.getClazz(), BodyTag.class))
                currType = BODY_TAG;
            else
                currType = TAG;
        } catch (Exception e) {
            if (e instanceof TransformerException)
                throw (TransformerException) e;
            throw new TransformerException(e, tag.getStart());
        }
    } else
        currType = getTagType(tag);
    final int currLocal = adapter.newLocal(currType);
    Label tagBegin = new Label();
    Label tagEnd = new Label();
    ExpressionUtil.visitLine(bc, tag.getStart());
    // TODO adapter.visitLocalVariable("tag", "L"+currType.getInternalName()+";", null, tagBegin, tagEnd, currLocal);
    adapter.visitLabel(tagBegin);
    // tag=pc.use(String tagClassName,String tagBundleName, String tagBundleVersion, String fullname,int attrType) throws PageException {
    adapter.loadArg(0);
    adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
    adapter.push(cd.getClassName());
    // has bundle info/version
    if (fromBundle) {
        // name
        adapter.push(cd.getName());
        // version
        if (cd.getVersion() != null)
            adapter.push(cd.getVersionAsString());
        else
            ASMConstants.NULL(adapter);
    }
    adapter.push(tlt.getFullName());
    adapter.push(tlt.getAttributeType());
    adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, fromBundle ? USE5 : USE3);
    if (currType != TAG)
        adapter.checkCast(currType);
    adapter.storeLocal(currLocal);
    TryFinallyVisitor outerTcfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            adapter.loadArg(0);
            adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
            adapter.loadLocal(currLocal);
            if (cd.getName() != null) {
                adapter.push(cd.getName());
                if (cd.getVersion() != null)
                    adapter.push(cd.getVersionAsString());
                else
                    ASMConstants.NULL(adapter);
            }
            adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, fromBundle ? RE_USE3 : RE_USE1);
        }
    }, null);
    if (doReuse)
        outerTcfv.visitTryBegin(bc);
    // appendix
    if (tlt.hasAppendix()) {
        adapter.loadLocal(currLocal);
        adapter.push(tag.getAppendix());
        if (// PageContextUtil.setAppendix(tag,appendix)
        fromBundle)
            ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, SET_APPENDIX2);
        else
            // tag.setAppendix(appendix)
            ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, SET_APPENDIX1);
    }
    // hasBody
    boolean hasBody = tag.getBody() != null;
    if (tlt.isBodyFree() && tlt.hasBodyMethodExists()) {
        adapter.loadLocal(currLocal);
        adapter.push(hasBody);
        if (// PageContextUtil.setAppendix(tag,appendix)
        fromBundle)
            ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, HAS_BODY2);
        else
            // tag.setAppendix(appendix)
            ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, HAS_BODY1);
    }
    // default attributes (get overwritten by attributeCollection because of that set before)
    setAttributes(bc, tag, currLocal, currType, true, fromBundle);
    // attributeCollection
    Attribute attrColl = tag.getAttribute("attributecollection");
    if (attrColl != null) {
        int attrType = tag.getTagLibTag().getAttributeType();
        if (TagLibTag.ATTRIBUTE_TYPE_NONAME != attrType) {
            tag.removeAttribute("attributecollection");
            // TagUtil.setAttributeCollection(Tag, Struct)
            adapter.loadArg(0);
            adapter.loadLocal(currLocal);
            if (currType != TAG)
                adapter.cast(currType, TAG);
            // /
            TagLibTagAttr[] missings = tag.getMissingAttributes();
            if (!ArrayUtil.isEmpty(missings)) {
                ArrayVisitor av = new ArrayVisitor();
                av.visitBegin(adapter, MISSING_ATTRIBUTE, missings.length);
                int count = 0;
                TagLibTagAttr miss;
                for (int i = 0; i < missings.length; i++) {
                    miss = missings[i];
                    av.visitBeginItem(adapter, count++);
                    bc.getFactory().registerKey(bc, bc.getFactory().createLitString(miss.getName()), false);
                    adapter.push(miss.getType());
                    if (ArrayUtil.isEmpty(miss.getAlias()))
                        adapter.invokeStatic(MISSING_ATTRIBUTE, NEW_INSTANCE_MAX2);
                    else {
                        new LiteralStringArray(bc.getFactory(), miss.getAlias()).writeOut(bc, Expression.MODE_REF);
                        adapter.invokeStatic(MISSING_ATTRIBUTE, NEW_INSTANCE_MAX3);
                    }
                    av.visitEndItem(bc.getAdapter());
                }
                av.visitEnd();
            } else {
                ASMConstants.NULL(adapter);
            }
            // /
            attrColl.getValue().writeOut(bc, Expression.MODE_REF);
            adapter.push(attrType);
            adapter.invokeStatic(TAG_UTIL, SET_ATTRIBUTE_COLLECTION);
        }
    }
    // metadata
    Attribute attr;
    Map<String, Attribute> metadata = tag.getMetaData();
    if (metadata != null) {
        Iterator<Attribute> it = metadata.values().iterator();
        while (it.hasNext()) {
            attr = it.next();
            adapter.loadLocal(currLocal);
            adapter.push(attr.getName());
            attr.getValue().writeOut(bc, Expression.MODE_REF);
            if (fromBundle)
                ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, SET_META_DATA3);
            else
                ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, SET_META_DATA2);
        }
    }
    // set attributes
    setAttributes(bc, tag, currLocal, currType, false, fromBundle);
    // Body
    if (hasBody) {
        final int state = adapter.newLocal(Types.INT_VALUE);
        // int state=tag.doStartTag();
        adapter.loadLocal(currLocal);
        ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_START_TAG);
        // adapter.invokeVirtual(currType, DO_START_TAG);
        adapter.storeLocal(state);
        // if (state!=Tag.SKIP_BODY)
        Label endBody = new Label();
        adapter.loadLocal(state);
        adapter.push(javax.servlet.jsp.tagext.Tag.SKIP_BODY);
        adapter.visitJumpInsn(Opcodes.IF_ICMPEQ, endBody);
        // pc.initBody(tag, state);
        adapter.loadArg(0);
        adapter.loadLocal(currLocal);
        adapter.loadLocal(state);
        adapter.invokeVirtual(Types.PAGE_CONTEXT, INIT_BODY);
        OnFinally onFinally = new OnFinally() {

            @Override
            public void _writeOut(BytecodeContext bc) {
                Label endIf = new Label();
                /*if(tlt.handleException() && fcf!=null && fcf.getAfterFinalGOTOLabel()!=null){
							ASMUtil.visitLabel(adapter, fcf.getFinalEntryLabel());
						}*/
                adapter.loadLocal(state);
                adapter.push(javax.servlet.jsp.tagext.Tag.EVAL_BODY_INCLUDE);
                adapter.visitJumpInsn(Opcodes.IF_ICMPEQ, endIf);
                // ... pc.popBody();
                adapter.loadArg(0);
                adapter.invokeVirtual(Types.PAGE_CONTEXT, POP_BODY);
                adapter.pop();
                adapter.visitLabel(endIf);
                // tag.doFinally();
                if (tlt.handleException()) {
                    adapter.loadLocal(currLocal);
                    ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_FINALLY);
                // adapter.invokeVirtual(currType, DO_FINALLY);
                }
            // GOTO after execution body, used when a continue/break was called before
            /*if(fcf!=null) {
							Label l = fcf.getAfterFinalGOTOLabel();
							if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
						}*/
            }
        };
        if (tlt.handleException()) {
            TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(onFinally, fcf);
            tcfv.visitTryBegin(bc);
            doTry(bc, adapter, tag, currLocal, currType, fromBundle);
            int t = tcfv.visitTryEndCatchBeging(bc);
            // tag.doCatch(t);
            adapter.loadLocal(currLocal);
            adapter.loadLocal(t);
            // adapter.visitVarInsn(Opcodes.ALOAD,t);
            ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_CATCH);
            // adapter.invokeVirtual(currType, DO_CATCH);
            tcfv.visitCatchEnd(bc);
        } else {
            TryFinallyVisitor tfv = new TryFinallyVisitor(onFinally, fcf);
            tfv.visitTryBegin(bc);
            doTry(bc, adapter, tag, currLocal, currType, fromBundle);
            tfv.visitTryEnd(bc);
        }
        adapter.visitLabel(endBody);
    } else {
        // tag.doStartTag();
        adapter.loadLocal(currLocal);
        ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_START_TAG);
        // adapter.invokeVirtual(currType, DO_START_TAG);
        adapter.pop();
    }
    // if (tag.doEndTag()==Tag.SKIP_PAGE) throw new Abort(0<!-- SCOPE_PAGE -->);
    Label endDoEndTag = new Label();
    adapter.loadLocal(currLocal);
    ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_END_TAG);
    // adapter.invokeVirtual(currType, DO_END_TAG);
    adapter.push(javax.servlet.jsp.tagext.Tag.SKIP_PAGE);
    adapter.visitJumpInsn(Opcodes.IF_ICMPNE, endDoEndTag);
    adapter.push(Abort.SCOPE_PAGE);
    adapter.invokeStatic(ABORT, NEW_INSTANCE);
    adapter.throwException();
    adapter.visitLabel(endDoEndTag);
    if (doReuse) {
        // } finally{pc.reuse(tag);}
        outerTcfv.visitTryEnd(bc);
    }
    adapter.visitLabel(tagEnd);
    ExpressionUtil.visitLine(bc, tag.getEnd());
}
Also used : TagLibTagAttr(lucee.transformer.library.tag.TagLibTagAttr) TagLibTag(lucee.transformer.library.tag.TagLibTag) LiteralStringArray(lucee.transformer.bytecode.expression.type.LiteralStringArray) MissingAttribute(lucee.runtime.tag.MissingAttribute) Label(org.objectweb.asm.Label) ClassDefinition(lucee.runtime.db.ClassDefinition) ClassException(lucee.commons.lang.ClassException) TransformerException(lucee.transformer.TransformerException) BundleException(org.osgi.framework.BundleException) Type(org.objectweb.asm.Type) OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryCatchFinallyVisitor(lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 3 with TryFinallyVisitor

use of lucee.transformer.bytecode.visitor.TryFinallyVisitor in project Lucee by lucee.

the class ForEach method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    final int it = adapter.newLocal(Types.ITERATOR);
    final int item = adapter.newLocal(Types.REFERENCE);
    // Value
    // ForEachUtil.toIterator(value)
    value.writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(FOR_EACH_UTIL, FOR_EACH);
    // adapter.invokeStatic(COLLECTION_UTIL, TO_ITERATOR);
    // Iterator it=...
    adapter.storeLocal(it);
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) throws TransformerException {
            GeneratorAdapter a = bc.getAdapter();
            // if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(a,fcf.getFinalEntryLabel());
            a.loadLocal(it);
            a.invokeStatic(FOR_EACH_UTIL, RESET);
        /*if(fcf!=null){
						Label l=fcf.getAfterFinalGOTOLabel();
						if(l!=null)a.visitJumpInsn(Opcodes.GOTO, l);
					}*/
        }
    }, getFlowControlFinal());
    tfv.visitTryBegin(bc);
    // Key
    // new VariableReference(...)
    key.writeOut(bc, Expression.MODE_REF);
    // VariableReference item=...
    adapter.storeLocal(item);
    // while
    ExpressionUtil.visitLine(bc, getStart());
    adapter.visitLabel(begin);
    // hasNext
    adapter.loadLocal(it);
    adapter.invokeInterface(Types.ITERATOR, HAS_NEXT);
    adapter.ifZCmp(Opcodes.IFEQ, end);
    // item.set(pc,it.next());
    adapter.loadLocal(item);
    adapter.loadArg(0);
    adapter.loadLocal(it);
    adapter.invokeInterface(Types.ITERATOR, NEXT);
    adapter.invokeInterface(Types.REFERENCE, SET);
    adapter.pop();
    // Body
    body.writeOut(bc);
    adapter.visitJumpInsn(Opcodes.GOTO, begin);
    adapter.visitLabel(end);
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 4 with TryFinallyVisitor

use of lucee.transformer.bytecode.visitor.TryFinallyVisitor in project Lucee by lucee.

the class TagLoop method writeOutTypeFile.

/**
 * write out file loop
 * @param adapter
 * @throws TemplateException
 */
private void writeOutTypeFile(BytecodeContext bc) throws TransformerException {
    WhileVisitor whileVisitor = new WhileVisitor();
    loopVisitor = whileVisitor;
    GeneratorAdapter adapter = bc.getAdapter();
    // charset=@charset
    int charset = adapter.newLocal(Types.STRING);
    Attribute attrCharset = getAttribute("charset");
    if (attrCharset == null)
        adapter.visitInsn(Opcodes.ACONST_NULL);
    else
        attrCharset.getValue().writeOut(bc, Expression.MODE_REF);
    adapter.storeLocal(charset);
    // startline=@startline
    int startline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartLine = getAttribute("startline");
    // CF8
    if (attrStartLine == null)
        attrStartLine = getAttribute("from");
    if (attrStartLine == null)
        adapter.push(1);
    else {
        attrStartLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(startline);
    // endline=@endline
    int endline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrEndLine = getAttribute("endline");
    if (attrEndLine == null)
        attrEndLine = getAttribute("to");
    if (attrEndLine == null)
        adapter.push(-1);
    else {
        attrEndLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(endline);
    // VariableReference index=VariableInterpreter.getVariableReference(pc,@index);
    int index = -1, item = -1;
    // item
    Attribute attrItem = getAttribute("item");
    if (attrItem != null) {
        item = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrItem.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(item);
    }
    // index
    Attribute attrIndex = getAttribute("index");
    if (attrIndex != null) {
        index = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(index);
    }
    // java.io.File file=FileUtil.toResourceExisting(pc,@file);
    int resource = adapter.newLocal(Types.RESOURCE);
    adapter.loadArg(0);
    getAttribute("file").getValue().writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(RESOURCE_UTIL, TO_RESOURCE_EXISTING);
    adapter.storeLocal(resource);
    // pc.getConfig().getSecurityManager().checkFileLocation(resource);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CONFIG);
    adapter.invokeInterface(Types.CONFIG_WEB, GET_SECURITY_MANAGER);
    adapter.loadLocal(resource);
    adapter.invokeInterface(Types.SECURITY_MANAGER, CHECK_FILE_LOCATION);
    // char[] carr=new char[characters];
    Attribute attr = getAttribute("characters");
    int carr = -1;
    if (attr != null) {
        carr = adapter.newLocal(Types.CHAR_ARRAY);
        attr.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE);
        adapter.newArray(Types.CHAR);
        adapter.storeLocal(carr);
    }
    // BufferedReader reader = IOUtil.getBufferedReader(resource,charset);
    final int br = adapter.newLocal(Types.BUFFERED_READER);
    adapter.loadLocal(resource);
    adapter.loadLocal(charset);
    adapter.invokeStatic(IO_UTIL, GET_BUFFERED_READER);
    adapter.storeLocal(br);
    // String line;
    int line = adapter.newLocal(Types.STRING);
    // int count=0;
    int count = adapter.newLocal(Types.INT_VALUE);
    adapter.push(0);
    adapter.storeLocal(count);
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            bc.getAdapter().loadLocal(br);
            bc.getAdapter().invokeStatic(IO_UTIL, CLOSE_EL);
        }
    }, null);
    // TryFinallyVisitor tcfv=new TryFinallyVisitor();
    // try
    tfv.visitTryBegin(bc);
    // tcfv.visitTryBegin(bc);
    // while((line=br.readLine())!=null) {
    // WhileVisitor wv=new WhileVisitor();
    whileVisitor.visitBeforeExpression(bc);
    DecisionObjectVisitor dv = new DecisionObjectVisitor();
    dv.visitBegin();
    if (attr != null) {
        // IOUtil.read(bufferedreader,12)
        adapter.loadLocal(br);
        adapter.loadLocal(carr);
        adapter.arrayLength();
        adapter.invokeStatic(Types.IOUTIL, READ);
    } else {
        // br.readLine()
        adapter.loadLocal(br);
        adapter.invokeVirtual(Types.BUFFERED_READER, READ_LINE);
    }
    adapter.dup();
    adapter.storeLocal(line);
    dv.visitNEQ();
    adapter.visitInsn(Opcodes.ACONST_NULL);
    dv.visitEnd(bc);
    whileVisitor.visitAfterExpressionBeforeBody(bc);
    // if(++count < startLine) continue;
    DecisionIntVisitor dv2 = new DecisionIntVisitor();
    dv2.visitBegin();
    adapter.iinc(count, 1);
    adapter.loadLocal(count);
    dv2.visitLT();
    adapter.loadLocal(startline);
    dv2.visitEnd(bc);
    Label end = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end);
    whileVisitor.visitContinue(bc);
    adapter.visitLabel(end);
    // if(endLine!=-1 && count > endLine) break;
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(endline);
    div.visitNEQ();
    adapter.push(-1);
    div.visitEnd(bc);
    Label end2 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end2);
    DecisionIntVisitor div2 = new DecisionIntVisitor();
    div2.visitBegin();
    adapter.loadLocal(count);
    div2.visitGT();
    adapter.loadLocal(endline);
    div2.visitEnd(bc);
    Label end3 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end3);
    whileVisitor.visitBreak(bc);
    adapter.visitLabel(end3);
    adapter.visitLabel(end2);
    // index and item
    if (index != -1 && item != -1) {
        // index.set(pc,line);
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(count);
        adapter.cast(Types.INT_VALUE, Types.DOUBLE_VALUE);
        adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
        // item.set(pc,line);
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    } else // only index
    if (index != -1) {
        // index.set(pc,line);
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    } else // only item
    {
        // item.set(pc,line);
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    }
    getBody().writeOut(bc);
    whileVisitor.visitAfterBody(bc, getEnd());
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) DoWhileVisitor(lucee.transformer.bytecode.visitor.DoWhileVisitor) WhileVisitor(lucee.transformer.bytecode.visitor.WhileVisitor) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) DecisionObjectVisitor(lucee.transformer.bytecode.visitor.DecisionObjectVisitor)

Example 5 with TryFinallyVisitor

use of lucee.transformer.bytecode.visitor.TryFinallyVisitor in project Lucee by lucee.

the class TagSilent method _writeOut.

/**
 * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
 */
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    final int silentMode = adapter.newLocal(Types.BOOLEAN_VALUE);
    // boolean silentMode= pc.setSilent();
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_SILENT);
    adapter.storeLocal(silentMode);
    // call must be
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            // if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(adapter,fcf.getFinalEntryLabel());
            // if(!silentMode)pc.unsetSilent();
            Label _if = new Label();
            adapter.loadLocal(silentMode);
            NotVisitor.visitNot(bc);
            adapter.ifZCmp(Opcodes.IFEQ, _if);
            adapter.loadArg(0);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, UNSET_SILENT);
            adapter.pop();
            adapter.visitLabel(_if);
        /*if(fcf!=null) {
					Label l = fcf.getAfterFinalGOTOLabel();
					if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
				}*/
        }
    }, getFlowControlFinal());
    tfv.visitTryBegin(bc);
    getBody().writeOut(bc);
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Aggregations

BytecodeContext (lucee.transformer.bytecode.BytecodeContext)6 OnFinally (lucee.transformer.bytecode.visitor.OnFinally)6 TryFinallyVisitor (lucee.transformer.bytecode.visitor.TryFinallyVisitor)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)5 Label (org.objectweb.asm.Label)4 TransformerException (lucee.transformer.TransformerException)2 DecisionIntVisitor (lucee.transformer.bytecode.visitor.DecisionIntVisitor)2 WhileVisitor (lucee.transformer.bytecode.visitor.WhileVisitor)2 LitString (lucee.transformer.expression.literal.LitString)2 ClassException (lucee.commons.lang.ClassException)1 ClassDefinition (lucee.runtime.db.ClassDefinition)1 MissingAttribute (lucee.runtime.tag.MissingAttribute)1 LiteralStringArray (lucee.transformer.bytecode.expression.type.LiteralStringArray)1 ArrayVisitor (lucee.transformer.bytecode.visitor.ArrayVisitor)1 DecisionObjectVisitor (lucee.transformer.bytecode.visitor.DecisionObjectVisitor)1 DoWhileVisitor (lucee.transformer.bytecode.visitor.DoWhileVisitor)1 ParseBodyVisitor (lucee.transformer.bytecode.visitor.ParseBodyVisitor)1 TryCatchFinallyVisitor (lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor)1 ExprString (lucee.transformer.expression.ExprString)1 Expression (lucee.transformer.expression.Expression)1