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.runtime.op;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.PageContext;
023import lucee.runtime.interpreter.VariableInterpreter;
024import lucee.runtime.type.Collection;
025import lucee.runtime.type.KeyImpl;
026import lucee.runtime.type.scope.Scope;
027import lucee.runtime.util.VariableUtilImpl;
028
029public class Elvis {
030        
031        /**
032         *  called by the Elvis operator from generated bytecode
033         * @param pc
034         * @param scope
035         * @param varNames
036         * @return
037         */
038        public static boolean operate(PageContext pc , double scope,Collection.Key[] varNames) {
039                return _operate(pc, scope, varNames,0); 
040        }
041
042        /**
043         *  called by the Elvis operator from generated bytecode
044         * @param pc
045         * @param scope
046         * @param varNames
047         * @return
048         */
049        public static boolean operate(PageContext pc , double scope,String[] varNames) {
050                return _operate(pc, scope, KeyImpl.toKeyArray(varNames),0);
051        }
052        
053        /**
054         *  called by the Elvis operator from the interpreter
055         * @param pc
056         * @param scope
057         * @param varNames
058         * @return
059         */
060        public static boolean operate(PageContext pc , String[] varNames) {
061                int scope = VariableInterpreter.scopeString2Int(varNames[0]);
062                return _operate(pc, scope, KeyImpl.toKeyArray(varNames), scope==Scope.SCOPE_UNDEFINED?0:1);
063        }
064        
065        private static boolean _operate(PageContext pc , double scope,Collection.Key[] varNames, int startIndex) {
066                Object defVal=null;
067                try {
068                        Object coll =VariableInterpreter.scope(pc, (int)scope, false); 
069                        //Object coll =pc.scope((int)scope);
070                        VariableUtilImpl vu = ((VariableUtilImpl)pc.getVariableUtil());
071                        for(int i=startIndex;i<varNames.length;i++) {
072                                coll=vu.getCollection(pc,coll,varNames[i],defVal);
073                                if(coll==defVal)return false;
074                        }
075                } catch (Throwable t) {
076                        ExceptionUtil.rethrowIfNecessary(t);
077                return false;
078            }
079                return true; 
080        }
081}