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.expression.var;
020
021import lucee.transformer.bytecode.BytecodeContext;
022import lucee.transformer.bytecode.BytecodeException;
023import lucee.transformer.bytecode.Position;
024import lucee.transformer.bytecode.cast.CastString;
025import lucee.transformer.bytecode.expression.ExprString;
026import lucee.transformer.bytecode.expression.Expression;
027import lucee.transformer.bytecode.expression.ExpressionBase;
028import lucee.transformer.bytecode.util.Types;
029
030import org.objectweb.asm.Type;
031import org.objectweb.asm.commons.GeneratorAdapter;
032import org.objectweb.asm.commons.Method;
033
034public final class DynAssign extends ExpressionBase {
035
036        private ExprString name;
037        private Expression value;
038        
039        // Object setVariable(String, Object)
040    private final static Method METHOD_SET_VARIABLE = new Method("setVariable",
041                        Types.OBJECT,
042                        new Type[]{Types.STRING,Types.OBJECT}); 
043
044        public DynAssign(Position start,Position end) {
045                super(start,end);
046        }
047
048        /**
049         * Constructor of the class
050         * @param name
051         * @param value
052         */
053        public DynAssign(Expression name, Expression value) {
054                super(name.getStart(),name.getEnd());
055                this.name=CastString.toExprString(name);
056                this.value=value;
057        }
058
059        /**
060         *
061         * @see lucee.transformer.bytecode.expression.ExpressionBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
062         */
063        public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
064                GeneratorAdapter adapter = bc.getAdapter();
065                adapter.loadArg(0);
066                name.writeOut(bc, Expression.MODE_REF);
067                value.writeOut(bc, Expression.MODE_REF);
068                adapter.invokeVirtual(Types.PAGE_CONTEXT,METHOD_SET_VARIABLE);
069                return Types.OBJECT;
070        }
071
072        /* *
073         *
074         * @see lucee.transformer.bytecode.expression.Expression#getType()
075         * /
076        public int getType() {
077                return Types._OBJECT;
078        }*/
079
080        /**
081         * @return the name
082         */
083        public ExprString getName() {
084                return name;
085        }
086
087        /**
088         * @return the value
089         */
090        public Expression getValue() {
091                return value;
092        }
093
094}