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.udf;
020
021import lucee.transformer.bytecode.Body;
022import lucee.transformer.bytecode.BytecodeContext;
023import lucee.transformer.bytecode.BytecodeException;
024import lucee.transformer.bytecode.Literal;
025import lucee.transformer.bytecode.Page;
026import lucee.transformer.bytecode.Position;
027import lucee.transformer.bytecode.expression.Expression;
028import lucee.transformer.bytecode.expression.var.Variable;
029import lucee.transformer.bytecode.util.Types;
030
031import org.objectweb.asm.commons.GeneratorAdapter;
032
033public final class FunctionImpl extends Function {
034
035        public FunctionImpl(Page page, Expression name, Expression returnType, Expression returnFormat, Expression output, Expression bufferOutput,
036                        int access, Expression displayName, Expression description,Expression hint, Expression secureJson, Expression verifyClient, Expression localMode,
037                        Literal cachedWithin, boolean _abstract, boolean _final,
038                        Body body, Position start,Position end) {
039                super(page,name, returnType, returnFormat, output, bufferOutput, access, displayName,description, hint, secureJson, verifyClient,localMode,cachedWithin,_abstract,_final, body, start, end);
040        }
041        
042
043        public FunctionImpl(Page page, String name, int access, String returnType, Body body,Position start,Position end) {
044                super(page,name, access, returnType, body, start, end);
045        }
046
047        public final void _writeOut(BytecodeContext bc, int pageType) throws BytecodeException{
048                GeneratorAdapter adapter = bc.getAdapter();
049                ////Page page = ASMUtil.getAncestorPage(this);
050                ////int index=page.addFunction(this);
051
052                // c.set(<name>,udf);
053                if(pageType==PAGE_TYPE_INTERFACE) {
054                        adapter.loadArg(0);
055                }
056                else if(pageType==PAGE_TYPE_COMPONENT) {
057                        adapter.loadArg(1);
058                }
059                // pc.variablesScope().set(<name>,udf);
060                else {
061                        adapter.loadArg(0);
062                        adapter.invokeVirtual(Types.PAGE_CONTEXT, VARIABLE_SCOPE);
063                }
064                
065                
066                Variable.registerKey(bc,name,true);
067                if(pageType==PAGE_TYPE_COMPONENT) {
068                        loadUDFProperties(bc,valueIndex,arrayIndex,false);
069                        adapter.invokeVirtual(Types.COMPONENT_IMPL,REG_UDF_KEY);
070                }
071                else if(pageType==PAGE_TYPE_INTERFACE) {
072                        loadUDFProperties(bc,valueIndex,arrayIndex,false);
073                        adapter.invokeVirtual(Types.INTERFACE_IMPL, REG_UDF_KEY);
074                }
075                else {
076                        adapter.newInstance(Types.UDF_IMPL);
077                        adapter.dup();
078                        loadUDFProperties(bc, valueIndex,arrayIndex,false);
079                        adapter.invokeConstructor(Types.UDF_IMPL, INIT_UDF_IMPL_PROP);
080                        
081                        //loadUDF(bc, index);
082                        adapter.invokeInterface(Types.VARIABLES, SET_KEY);
083                        adapter.pop();
084                }
085        }
086        
087}