001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.bytecode.statement.tag;
020
021import lucee.transformer.bytecode.BytecodeContext;
022import lucee.transformer.bytecode.BytecodeException;
023import lucee.transformer.bytecode.Position;
024import lucee.transformer.bytecode.cast.CastBoolean;
025import lucee.transformer.bytecode.expression.ExprBoolean;
026import lucee.transformer.bytecode.expression.Expression;
027import lucee.transformer.bytecode.literal.LitBoolean;
028import lucee.transformer.bytecode.util.Types;
029
030import org.objectweb.asm.Type;
031import org.objectweb.asm.commons.GeneratorAdapter;
032import org.objectweb.asm.commons.Method;
033
034public final class TagInclude extends TagBaseNoFinal {
035
036        private final static Method DO_INCLUDE_RUN_ONCE2 = new Method(
037                        "doInclude",
038                        Type.VOID_TYPE,
039                        new Type[]{Types.STRING,Types.BOOLEAN_VALUE});
040        
041        private final static Method DO_INCLUDE_RUN_ONCE3 = new Method(
042                        "doInclude",
043                        Type.VOID_TYPE,
044                        new Type[]{Types.STRING,Types.BOOLEAN_VALUE, Types.OBJECT});
045        
046        public TagInclude(Position start,Position end) {
047                super(start,end);
048        }
049
050        /**
051         * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
052         */
053        public void _writeOut(BytecodeContext bc) throws BytecodeException {
054                Type type = Types.PAGE_CONTEXT;
055                Method func = DO_INCLUDE_RUN_ONCE2;
056                
057                // cachedwithin
058                Expression cachedwithin=null;
059                Attribute attr = getAttribute("cachedwithin");
060                if(attr!=null && attr.getValue()!=null) {
061                        cachedwithin = attr.getValue();
062                        type = Types.PAGE_CONTEXT_IMPL;
063                        func = DO_INCLUDE_RUN_ONCE3;
064                }
065                
066                GeneratorAdapter adapter = bc.getAdapter();
067                adapter.loadArg(0);
068                if(cachedwithin!=null) adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
069
070                // template
071                getAttribute("template").getValue().writeOut(bc, Expression.MODE_REF);
072
073                // run Once
074                attr = getAttribute("runonce");
075                ExprBoolean expr = (attr==null)?
076                                LitBoolean.FALSE:
077                                CastBoolean.toExprBoolean(attr.getValue());
078                expr.writeOut(bc, Expression.MODE_VALUE);
079
080                // cachedwithin
081                if(cachedwithin!=null)
082                        cachedwithin.writeOut(bc, Expression.MODE_REF);
083
084                adapter.invokeVirtual(type,func);
085        }
086}