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.commons.lang.RandomUtil;
022import lucee.runtime.tag.ThreadTag;
023import lucee.transformer.bytecode.Body;
024import lucee.transformer.bytecode.BodyBase;
025import lucee.transformer.bytecode.BytecodeContext;
026import lucee.transformer.bytecode.BytecodeException;
027import lucee.transformer.bytecode.Page;
028import lucee.transformer.bytecode.Position;
029import lucee.transformer.bytecode.literal.LitString;
030import lucee.transformer.bytecode.util.ASMUtil;
031import lucee.transformer.bytecode.util.Types;
032
033import org.objectweb.asm.Type;
034import org.objectweb.asm.commons.GeneratorAdapter;
035import org.objectweb.asm.commons.Method;
036
037public final class TagThread extends TagBaseNoFinal {
038
039        
040        
041        public static final Type THREAD_TAG = Type.getType(ThreadTag.class);
042        
043        
044        private static final Method REGISTER = new Method(
045                        "register",Types.VOID,new Type[]{Types.PAGE,Types.INT_VALUE});
046
047
048        public TagThread(Position start,Position end) {
049                super(start,end);
050        }
051        
052        
053        
054        
055
056        /**
057         *
058         * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(lucee.transformer.bytecode.BytecodeContext)
059         */
060        public void _writeOut(BytecodeContext bc) throws BytecodeException {
061                String action=ASMUtil.getAttributeString(this, "action","run");
062                // no body
063                if(!"run".equalsIgnoreCase(action)) {
064                        super._writeOut(bc);
065                        return;
066                }
067                
068                Attribute name = getAttribute("name");
069                if(name==null){
070                        addAttribute(new Attribute(false, "name",LitString.toExprString("thread"+RandomUtil.createRandomStringLC(20)), "string"));
071                }
072
073                GeneratorAdapter adapter = bc.getAdapter();
074                Page page = ASMUtil.getAncestorPage(this);
075                
076                int index=page.addThread(this);
077                super._writeOut(bc,false);
078                
079                adapter.loadLocal(bc.getCurrentTag());
080                adapter.loadThis();
081        adapter.push(index);
082                adapter.invokeVirtual(THREAD_TAG, REGISTER);
083                
084        }
085
086
087
088
089
090        /**
091         * @see lucee.transformer.bytecode.statement.tag.TagBase#getBody()
092         */
093        public Body getBody() {
094                return new BodyBase();
095        }
096        
097        public Body getRealBody() {
098                return super.getBody();
099        }
100        
101}