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 **/
019/**
020 * Implements the CFML Function isdefined
021 */
022package lucee.runtime.functions.decision;
023
024import lucee.commons.lang.ExceptionUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.config.NullSupportHelper;
027import lucee.runtime.ext.function.Function;
028import lucee.runtime.interpreter.VariableInterpreter;
029import lucee.runtime.type.Collection;
030import lucee.runtime.type.KeyImpl;
031import lucee.runtime.type.Null;
032import lucee.runtime.type.scope.Scope;
033import lucee.runtime.util.VariableUtilImpl;
034
035public final class IsDefined implements Function {
036        
037        private static final long serialVersionUID = -6477602189364145523L;
038
039        public static boolean call(PageContext pc , String varName) {
040                return VariableInterpreter.isDefined(pc,varName);
041                //return pc.isDefined(varName);
042        }
043        
044        public static boolean call(PageContext pc , double scope,Collection.Key key) {
045                try {
046                        Object coll = VariableInterpreter.scope(pc, (int)scope, false);
047                        if(coll==null) return false;
048                        coll=((VariableUtilImpl)pc.getVariableUtil()).get(pc,coll,key,NullSupportHelper.NULL());
049                        if(coll==NullSupportHelper.NULL())return false;
050                        //return pc.scope((int)scope).get(key,null)!=null; 
051                } catch (Throwable t) {
052                        ExceptionUtil.rethrowIfNecessary(t);
053                return false;
054            }
055                return true;
056        }
057
058        public static boolean call(PageContext pc , double scope,Collection.Key[] varNames) {
059                Object defVal=NullSupportHelper.NULL();
060                try {
061                        Object coll =VariableInterpreter.scope(pc, (int)scope, false); 
062                        //Object coll =pc.scope((int)scope);
063                        VariableUtilImpl vu = ((VariableUtilImpl)pc.getVariableUtil());
064                        for(int i=0;i<varNames.length;i++) {
065                                coll=vu.getCollection(pc,coll,varNames[i],defVal);
066                                if(coll==defVal)return false;
067                        }
068                } catch (Throwable t) {
069                        ExceptionUtil.rethrowIfNecessary(t);
070                return false;
071            }
072                return true; 
073        }
074        
075        // used for older compiled code in ra files
076        public static boolean invoke(PageContext pc , String[] varNames, boolean allowNull) {
077                int scope = VariableInterpreter.scopeString2Int(varNames[0]);
078                
079                
080                Object defVal=allowNull?Null.NULL:null;
081                try {
082                        Object coll =VariableInterpreter.scope(pc, scope, false); 
083                        //Object coll =pc.scope((int)scope); 
084                        for(int i=scope==Scope.SCOPE_UNDEFINED?0:1;i<varNames.length;i++) {
085                                coll=pc.getVariableUtil().getCollection(pc,coll,varNames[i],defVal);
086                                if(coll==defVal)return false;
087                        }
088                } catch (Throwable t) {
089                        ExceptionUtil.rethrowIfNecessary(t);
090                return false;
091            }
092                return true; 
093        }
094                
095        // used for older compiled code in ra files
096        public static boolean call(PageContext pc , double scope,String key) {
097                return call(pc, scope, KeyImpl.getInstance(key));
098        }
099
100        // used for older compiled code in ra files
101        public static boolean call(PageContext pc , double scope,String[] varNames) {
102                return call(pc, scope, KeyImpl.toKeyArray(varNames));
103        }
104}