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.util;
020
021import lucee.runtime.type.scope.Application;
022import lucee.runtime.type.scope.Argument;
023import lucee.runtime.type.scope.CGI;
024import lucee.runtime.type.scope.Client;
025import lucee.runtime.type.scope.Cluster;
026import lucee.runtime.type.scope.Cookie;
027import lucee.runtime.type.scope.Form;
028import lucee.runtime.type.scope.Local;
029import lucee.runtime.type.scope.Request;
030import lucee.runtime.type.scope.Scope;
031import lucee.runtime.type.scope.ScopeSupport;
032import lucee.runtime.type.scope.Server;
033import lucee.runtime.type.scope.Session;
034import lucee.runtime.type.scope.URL;
035import lucee.runtime.type.scope.Undefined;
036
037import org.objectweb.asm.Type;
038import org.objectweb.asm.commons.GeneratorAdapter;
039import org.objectweb.asm.commons.Method;
040
041public final class TypeScope {
042        
043        public final static Type SCOPE = Type.getType(Scope.class);
044        public final static Type[] SCOPES = new Type[ScopeSupport.SCOPE_COUNT];
045    static {
046        SCOPES[Scope.SCOPE_APPLICATION]=        Type.getType(Application.class);
047        SCOPES[Scope.SCOPE_ARGUMENTS]=          Type.getType(lucee.runtime.type.scope.Argument.class);
048        SCOPES[Scope.SCOPE_CGI]=                        Type.getType(CGI.class);
049        SCOPES[Scope.SCOPE_CLIENT]=                     Type.getType(Client.class);
050        SCOPES[Scope.SCOPE_COOKIE]=                     Type.getType(Cookie.class);
051        SCOPES[Scope.SCOPE_FORM]=                       Type.getType(Form.class);
052        SCOPES[Scope.SCOPE_LOCAL]=                      Type.getType(Local.class);
053        SCOPES[Scope.SCOPE_REQUEST]=            Type.getType(Request.class);
054        SCOPES[Scope.SCOPE_SERVER]=                     Type.getType(Server.class);
055        SCOPES[Scope.SCOPE_SESSION]=            Type.getType(Session.class);
056        SCOPES[Scope.SCOPE_UNDEFINED]=          Type.getType(Undefined.class);
057        SCOPES[Scope.SCOPE_URL]=                        Type.getType(URL.class);
058        SCOPES[Scope.SCOPE_VARIABLES]=          Types.VARIABLES;
059        SCOPES[Scope.SCOPE_CLUSTER]=            Type.getType(Cluster.class); 
060        SCOPES[ScopeSupport.SCOPE_VAR]=                 SCOPES[Scope.SCOPE_LOCAL]; 
061    }
062    
063        public final static Method[] METHODS = new Method[ScopeSupport.SCOPE_COUNT];
064    static {
065        METHODS[Scope.SCOPE_APPLICATION]=       new Method("applicationScope",  SCOPES[Scope.SCOPE_APPLICATION],new Type[]{});
066        METHODS[Scope.SCOPE_ARGUMENTS]=         new Method("argumentsScope",    SCOPES[Scope.SCOPE_ARGUMENTS],new Type[]{});
067        METHODS[Scope.SCOPE_CGI]=                       new Method("cgiScope",                  SCOPES[Scope.SCOPE_CGI],new Type[]{});
068        METHODS[Scope.SCOPE_CLIENT]=            new Method("clientScope",               SCOPES[Scope.SCOPE_CLIENT],new Type[]{});
069        METHODS[Scope.SCOPE_COOKIE]=            new Method("cookieScope",               SCOPES[Scope.SCOPE_COOKIE],new Type[]{});
070        METHODS[Scope.SCOPE_FORM]=                      new Method("formScope",                 SCOPES[Scope.SCOPE_FORM],new Type[]{});
071        METHODS[Scope.SCOPE_LOCAL]=                     new Method("localGet",                  Types.OBJECT,new Type[]{});
072        METHODS[Scope.SCOPE_REQUEST]=           new Method("requestScope",              SCOPES[Scope.SCOPE_REQUEST],new Type[]{});
073        METHODS[Scope.SCOPE_SERVER]=            new Method("serverScope",               SCOPES[Scope.SCOPE_SERVER],new Type[]{});
074        METHODS[Scope.SCOPE_SESSION]=           new Method("sessionScope",              SCOPES[Scope.SCOPE_SESSION],new Type[]{});
075        METHODS[Scope.SCOPE_UNDEFINED]=         new Method("us",                                SCOPES[Scope.SCOPE_UNDEFINED],new Type[]{});
076        METHODS[Scope.SCOPE_URL]=                       new Method("urlScope",                  SCOPES[Scope.SCOPE_URL],new Type[]{});
077        METHODS[Scope.SCOPE_VARIABLES]=         new Method("variablesScope",    SCOPES[Scope.SCOPE_VARIABLES],new Type[]{});
078        METHODS[Scope.SCOPE_CLUSTER]=           new Method("clusterScope",              SCOPES[Scope.SCOPE_CLUSTER],new Type[]{}); 
079        METHODS[ScopeSupport.SCOPE_VAR]=        new Method("localScope",                SCOPES[ScopeSupport.SCOPE_VAR],new Type[]{}); 
080        }
081    // Argument argumentsScope (boolean)
082    public final static Method METHOD_ARGUMENT_BIND=new Method("argumentsScope",SCOPES[Scope.SCOPE_ARGUMENTS],new Type[]{Types.BOOLEAN_VALUE});
083    public final static Method METHOD_VAR_BIND=new Method("localScope",SCOPES[ScopeSupport.SCOPE_VAR],new Type[]{Types.BOOLEAN_VALUE});
084    
085    public final static Method METHOD_LOCAL_EL=new Method("localGet",Types.OBJECT,new Type[]{Types.BOOLEAN_VALUE, Types.OBJECT});
086    public final static Method METHOD_LOCAL_BIND=new Method("localGet",Types.OBJECT,new Type[]{Types.BOOLEAN_VALUE});
087    public final static Method METHOD_LOCAL_TOUCH=new Method("localTouch",                      Types.OBJECT,new Type[]{});
088    public final static Method METHOD_THIS_BIND=new Method("thisGet",Types.OBJECT,new Type[]{Types.BOOLEAN_VALUE});
089    public final static Method METHOD_THIS_TOUCH=new Method("thisTouch",                        Types.OBJECT,new Type[]{});
090        
091    
092    
093    public final static Type SCOPE_ARGUMENT=            Type.getType(Argument.class);
094    
095    
096    public static Type invokeScope(GeneratorAdapter adapter, int scope) {
097                return invokeScope(adapter,TypeScope.METHODS[scope],Types.PAGE_CONTEXT);
098        }
099        
100        public static Type invokeScope(GeneratorAdapter adapter, Method m, Type type) {
101                if(type==null) 
102                        type=Types.PAGE_CONTEXT;
103                        adapter.invokeVirtual(type,m);
104                        return m.getReturnType();
105        }
106    
107}