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.interpreter.ref.literal;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.exp.PageException;
026import lucee.runtime.interpreter.InterpreterException;
027import lucee.runtime.interpreter.ref.Ref;
028import lucee.runtime.interpreter.ref.RefSupport;
029import lucee.runtime.interpreter.ref.Set;
030import lucee.runtime.interpreter.ref.var.Variable;
031import lucee.runtime.type.FunctionValueImpl;
032
033/**
034 * ref for a functionValue
035 */
036public final class LFunctionValue extends RefSupport implements Ref {
037
038
039    private Ref name;
040    private Ref refValue;
041    private Object objValue;
042
043    /**
044     * constructor of the class
045     * @param name
046     * @param value
047     */
048    public LFunctionValue(Ref name, Ref value) {
049        this.name=name;
050        this.refValue=value;
051    }
052    public LFunctionValue(Ref name, Object value) {
053        this.name=name;
054        this.objValue=value;
055    }
056    
057    @Override
058    public Object getValue(PageContext pc) throws PageException {
059        
060        if(name instanceof Variable){
061                return new FunctionValueImpl(toStringArray(pc,(Set)name),refValue==null?objValue:refValue.getValue(pc));
062        }
063        if(name instanceof Literal) {
064                return new FunctionValueImpl(((Literal)name).getString(pc),refValue==null?objValue:refValue.getValue(pc));
065        }
066        
067        // TODO no idea if this is ever used
068        if(name instanceof Set){
069                return new FunctionValueImpl(lucee.runtime.type.util.ListUtil.arrayToList(toStringArray(pc,(Set)name),"."),refValue==null?objValue:refValue.getValue(pc));
070        }
071        throw new InterpreterException("invalid syntax in named argument");
072        //return new FunctionValueImpl(key,value.getValue());
073    }
074
075    public static String[] toStringArray(PageContext pc,Set set) throws PageException {
076        Ref ref=set;
077        String str;
078        List<String> arr=new ArrayList<String>();
079        do {
080            set=(Set) ref;
081            str=set.getKeyAsString(pc);
082            if(str!=null)arr.add(0, str);
083            else break;
084            ref=set.getParent(pc);
085        }while(ref instanceof Set);
086        return arr.toArray(new String[arr.size()]);
087        }
088
089    @Override
090    public String getTypeName() {
091        return "function value";
092    }
093
094}