Search in sources :

Example 6 with BytecodeContext

use of lucee.transformer.bytecode.BytecodeContext in project Lucee by lucee.

the class ComponentUtil method createMethod.

private static int createMethod(ConstrBytecodeContext constr, java.util.List<LitString> keys, ClassWriter cw, String className, Object member, int max, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
    boolean hasOptionalArgs = false;
    if (member instanceof UDF) {
        UDF udf = (UDF) member;
        FunctionArgument[] args = udf.getFunctionArguments();
        Type[] types = new Type[max < 0 ? args.length : max];
        for (int y = 0; y < types.length; y++) {
            types[y] = toType(args[y].getTypeAsString(), true);
            if (!args[y].isRequired())
                hasOptionalArgs = true;
        }
        Type rtnType = toType(udf.getReturnTypeAsString(), true);
        Method method = new Method(udf.getFunctionName(), rtnType, types);
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null, cw);
        BytecodeContext bc = new BytecodeContext(null, constr, getPage(constr), keys, cw, className, adapter, method, writeLog, suppressWSbeforeArg, output, returnValue);
        Label start = adapter.newLabel();
        adapter.visitLabel(start);
        // ComponentController.invoke(name, args);
        // name
        adapter.push(udf.getFunctionName());
        // args
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(adapter, Types.OBJECT, types.length);
        for (int y = 0; y < types.length; y++) {
            av.visitBeginItem(adapter, y);
            adapter.loadArg(y);
            av.visitEndItem(bc.getAdapter());
        }
        av.visitEnd();
        adapter.invokeStatic(Types.COMPONENT_CONTROLLER, INVOKE);
        adapter.checkCast(rtnType);
        // ASMConstants.NULL(adapter);
        adapter.returnValue();
        Label end = adapter.newLabel();
        adapter.visitLabel(end);
        for (int y = 0; y < types.length; y++) {
            adapter.visitLocalVariable(args[y].getName().getString(), types[y].getDescriptor(), null, start, end, y + 1);
        }
        adapter.endMethod();
        if (hasOptionalArgs) {
            if (max == -1)
                max = args.length - 1;
            else
                max--;
            return max;
        }
    }
    return -1;
}
Also used : Type(org.objectweb.asm.Type) UDF(lucee.runtime.type.UDF) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor) FunctionArgument(lucee.runtime.type.FunctionArgument) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext)

Example 7 with BytecodeContext

use of lucee.transformer.bytecode.BytecodeContext in project Lucee by lucee.

the class ComponentUtil method getComponentJavaAccess.

// private static final Method INVOKE_PROPERTY = new Method("invoke",Types.OBJECT,new Type[]{Types.STRING,Types.OBJECT_ARRAY});
/**
 * generate a ComponentJavaAccess (CJA) class from a component
 * a CJA is a dynamic genarted java class that has all method defined inside a component as java methods.
 *
 * This is used to generated server side Webservices.
 * @param component
 * @param isNew
 * @return
 * @throws PageException
 */
public static Class getComponentJavaAccess(PageContext pc, Component component, RefBoolean isNew, boolean create, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
    isNew.setValue(false);
    String classNameOriginal = component.getPageSource().getClassName();
    String className = getClassname(component, null).concat("_wrap");
    String real = className.replace('.', '/');
    String realOriginal = classNameOriginal.replace('.', '/');
    Mapping mapping = component.getPageSource().getMapping();
    PhysicalClassLoader cl = null;
    try {
        cl = (PhysicalClassLoader) ((PageContextImpl) pc).getRPCClassLoader(false);
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    Resource classFile = cl.getDirectory().getRealResource(real.concat(".class"));
    Resource classFileOriginal = mapping.getClassRootDirectory().getRealResource(realOriginal.concat(".class"));
    // check last Mod
    if (classFile.lastModified() >= classFileOriginal.lastModified()) {
        try {
            Class clazz = cl.loadClass(className);
            if (clazz != null && !hasChangesOfChildren(classFile.lastModified(), clazz))
                return registerTypeMapping(clazz);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    if (!create)
        return null;
    isNew.setValue(true);
    // print.out("new");
    // CREATE CLASS
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, real, null, "java/lang/Object", null);
    // GeneratorAdapter ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC,Page.STATIC_CONSTRUCTOR,null,null,cw);
    // StaticConstrBytecodeContext statConstr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.STATIC_CONSTRUCTOR);
    // /ga = new GeneratorAdapter(Opcodes.ACC_PUBLIC,Page.CONSTRUCTOR,null,null,cw);
    // new BytecodeContext(null,null,null,cw,real,ga,Page.CONSTRUCTOR);
    ConstrBytecodeContext constr = null;
    // field component
    // FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "c", "Llucee/runtime/ComponentImpl;", null, null);
    // fv.visitEnd();
    java.util.List<LitString> _keys = new ArrayList<LitString>();
    // remote methods
    Collection.Key[] keys = component.keys(Component.ACCESS_REMOTE);
    int max;
    for (int i = 0; i < keys.length; i++) {
        max = -1;
        while ((max = createMethod(constr, _keys, cw, real, component.get(keys[i]), max, writeLog, suppressWSbeforeArg, output, returnValue)) != -1) {
            // for overload remove this
            break;
        }
    }
    // Constructor
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_OBJECT, null, null, cw);
    adapter.loadThis();
    adapter.invokeConstructor(Types.OBJECT, CONSTRUCTOR_OBJECT);
    lucee.transformer.bytecode.Page.registerFields(new BytecodeContext(null, constr, getPage(constr), _keys, cw, real, adapter, CONSTRUCTOR_OBJECT, writeLog, suppressWSbeforeArg, output, returnValue), _keys);
    adapter.returnValue();
    adapter.endMethod();
    cw.visitEnd();
    byte[] barr = cw.toByteArray();
    try {
        ResourceUtil.touch(classFile);
        IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);
        cl = (PhysicalClassLoader) ((PageContextImpl) pc).getRPCClassLoader(true);
        return registerTypeMapping(cl.loadClass(className, barr));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
}
Also used : Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) Mapping(lucee.runtime.Mapping) LitString(lucee.transformer.expression.literal.LitString) PageContextImpl(lucee.runtime.PageContextImpl) IOException(java.io.IOException) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext) ClassWriter(org.objectweb.asm.ClassWriter) LitString(lucee.transformer.expression.literal.LitString) PhysicalClassLoader(lucee.commons.lang.PhysicalClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Key(lucee.runtime.type.Collection.Key) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext)

Example 8 with BytecodeContext

use of lucee.transformer.bytecode.BytecodeContext in project Lucee by lucee.

the class ForEach method _writeOut.

@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    final int it = adapter.newLocal(Types.ITERATOR);
    final int item = adapter.newLocal(Types.REFERENCE);
    // Value
    // ForEachUtil.toIterator(value)
    value.writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(FOR_EACH_UTIL, FOR_EACH);
    // adapter.invokeStatic(COLLECTION_UTIL, TO_ITERATOR);
    // Iterator it=...
    adapter.storeLocal(it);
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) throws TransformerException {
            GeneratorAdapter a = bc.getAdapter();
            // if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(a,fcf.getFinalEntryLabel());
            a.loadLocal(it);
            a.invokeStatic(FOR_EACH_UTIL, RESET);
        /*if(fcf!=null){
						Label l=fcf.getAfterFinalGOTOLabel();
						if(l!=null)a.visitJumpInsn(Opcodes.GOTO, l);
					}*/
        }
    }, getFlowControlFinal());
    tfv.visitTryBegin(bc);
    // Key
    // new VariableReference(...)
    key.writeOut(bc, Expression.MODE_REF);
    // VariableReference item=...
    adapter.storeLocal(item);
    // while
    ExpressionUtil.visitLine(bc, getStart());
    adapter.visitLabel(begin);
    // hasNext
    adapter.loadLocal(it);
    adapter.invokeInterface(Types.ITERATOR, HAS_NEXT);
    adapter.ifZCmp(Opcodes.IFEQ, end);
    // item.set(pc,it.next());
    adapter.loadLocal(item);
    adapter.loadArg(0);
    adapter.loadLocal(it);
    adapter.invokeInterface(Types.ITERATOR, NEXT);
    adapter.invokeInterface(Types.REFERENCE, SET);
    adapter.pop();
    // Body
    body.writeOut(bc);
    adapter.visitJumpInsn(Opcodes.GOTO, begin);
    adapter.visitLabel(end);
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) TransformerException(lucee.transformer.TransformerException) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Example 9 with BytecodeContext

use of lucee.transformer.bytecode.BytecodeContext in project Lucee by lucee.

the class TagLoop method writeOutTypeFile.

/**
 * write out file loop
 * @param adapter
 * @throws TemplateException
 */
private void writeOutTypeFile(BytecodeContext bc) throws TransformerException {
    WhileVisitor whileVisitor = new WhileVisitor();
    loopVisitor = whileVisitor;
    GeneratorAdapter adapter = bc.getAdapter();
    // charset=@charset
    int charset = adapter.newLocal(Types.STRING);
    Attribute attrCharset = getAttribute("charset");
    if (attrCharset == null)
        adapter.visitInsn(Opcodes.ACONST_NULL);
    else
        attrCharset.getValue().writeOut(bc, Expression.MODE_REF);
    adapter.storeLocal(charset);
    // startline=@startline
    int startline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrStartLine = getAttribute("startline");
    // CF8
    if (attrStartLine == null)
        attrStartLine = getAttribute("from");
    if (attrStartLine == null)
        adapter.push(1);
    else {
        attrStartLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(startline);
    // endline=@endline
    int endline = adapter.newLocal(Types.INT_VALUE);
    Attribute attrEndLine = getAttribute("endline");
    if (attrEndLine == null)
        attrEndLine = getAttribute("to");
    if (attrEndLine == null)
        adapter.push(-1);
    else {
        attrEndLine.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.visitInsn(Opcodes.D2I);
    }
    adapter.storeLocal(endline);
    // VariableReference index=VariableInterpreter.getVariableReference(pc,@index);
    int index = -1, item = -1;
    // item
    Attribute attrItem = getAttribute("item");
    if (attrItem != null) {
        item = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrItem.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(item);
    }
    // index
    Attribute attrIndex = getAttribute("index");
    if (attrIndex != null) {
        index = adapter.newLocal(Types.VARIABLE_REFERENCE);
        adapter.loadArg(0);
        attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
        adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
        adapter.storeLocal(index);
    }
    // java.io.File file=FileUtil.toResourceExisting(pc,@file);
    int resource = adapter.newLocal(Types.RESOURCE);
    adapter.loadArg(0);
    getAttribute("file").getValue().writeOut(bc, Expression.MODE_REF);
    adapter.invokeStatic(RESOURCE_UTIL, TO_RESOURCE_EXISTING);
    adapter.storeLocal(resource);
    // pc.getConfig().getSecurityManager().checkFileLocation(resource);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CONFIG);
    adapter.invokeInterface(Types.CONFIG_WEB, GET_SECURITY_MANAGER);
    adapter.loadLocal(resource);
    adapter.invokeInterface(Types.SECURITY_MANAGER, CHECK_FILE_LOCATION);
    // char[] carr=new char[characters];
    Attribute attr = getAttribute("characters");
    int carr = -1;
    if (attr != null) {
        carr = adapter.newLocal(Types.CHAR_ARRAY);
        attr.getValue().writeOut(bc, Expression.MODE_VALUE);
        adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE);
        adapter.newArray(Types.CHAR);
        adapter.storeLocal(carr);
    }
    // BufferedReader reader = IOUtil.getBufferedReader(resource,charset);
    final int br = adapter.newLocal(Types.BUFFERED_READER);
    adapter.loadLocal(resource);
    adapter.loadLocal(charset);
    adapter.invokeStatic(IO_UTIL, GET_BUFFERED_READER);
    adapter.storeLocal(br);
    // String line;
    int line = adapter.newLocal(Types.STRING);
    // int count=0;
    int count = adapter.newLocal(Types.INT_VALUE);
    adapter.push(0);
    adapter.storeLocal(count);
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            bc.getAdapter().loadLocal(br);
            bc.getAdapter().invokeStatic(IO_UTIL, CLOSE_EL);
        }
    }, null);
    // TryFinallyVisitor tcfv=new TryFinallyVisitor();
    // try
    tfv.visitTryBegin(bc);
    // tcfv.visitTryBegin(bc);
    // while((line=br.readLine())!=null) {
    // WhileVisitor wv=new WhileVisitor();
    whileVisitor.visitBeforeExpression(bc);
    DecisionObjectVisitor dv = new DecisionObjectVisitor();
    dv.visitBegin();
    if (attr != null) {
        // IOUtil.read(bufferedreader,12)
        adapter.loadLocal(br);
        adapter.loadLocal(carr);
        adapter.arrayLength();
        adapter.invokeStatic(Types.IOUTIL, READ);
    } else {
        // br.readLine()
        adapter.loadLocal(br);
        adapter.invokeVirtual(Types.BUFFERED_READER, READ_LINE);
    }
    adapter.dup();
    adapter.storeLocal(line);
    dv.visitNEQ();
    adapter.visitInsn(Opcodes.ACONST_NULL);
    dv.visitEnd(bc);
    whileVisitor.visitAfterExpressionBeforeBody(bc);
    // if(++count < startLine) continue;
    DecisionIntVisitor dv2 = new DecisionIntVisitor();
    dv2.visitBegin();
    adapter.iinc(count, 1);
    adapter.loadLocal(count);
    dv2.visitLT();
    adapter.loadLocal(startline);
    dv2.visitEnd(bc);
    Label end = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end);
    whileVisitor.visitContinue(bc);
    adapter.visitLabel(end);
    // if(endLine!=-1 && count > endLine) break;
    DecisionIntVisitor div = new DecisionIntVisitor();
    div.visitBegin();
    adapter.loadLocal(endline);
    div.visitNEQ();
    adapter.push(-1);
    div.visitEnd(bc);
    Label end2 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end2);
    DecisionIntVisitor div2 = new DecisionIntVisitor();
    div2.visitBegin();
    adapter.loadLocal(count);
    div2.visitGT();
    adapter.loadLocal(endline);
    div2.visitEnd(bc);
    Label end3 = new Label();
    adapter.ifZCmp(Opcodes.IFEQ, end3);
    whileVisitor.visitBreak(bc);
    adapter.visitLabel(end3);
    adapter.visitLabel(end2);
    // index and item
    if (index != -1 && item != -1) {
        // index.set(pc,line);
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(count);
        adapter.cast(Types.INT_VALUE, Types.DOUBLE_VALUE);
        adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
        // item.set(pc,line);
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    } else // only index
    if (index != -1) {
        // index.set(pc,line);
        adapter.loadLocal(index);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    } else // only item
    {
        // item.set(pc,line);
        adapter.loadLocal(item);
        adapter.loadArg(0);
        adapter.loadLocal(line);
        adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
        adapter.pop();
    }
    getBody().writeOut(bc);
    whileVisitor.visitAfterBody(bc, getEnd());
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) DoWhileVisitor(lucee.transformer.bytecode.visitor.DoWhileVisitor) WhileVisitor(lucee.transformer.bytecode.visitor.WhileVisitor) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) DecisionObjectVisitor(lucee.transformer.bytecode.visitor.DecisionObjectVisitor)

Example 10 with BytecodeContext

use of lucee.transformer.bytecode.BytecodeContext in project Lucee by lucee.

the class TagSilent method _writeOut.

/**
 * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
 */
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
    final GeneratorAdapter adapter = bc.getAdapter();
    final int silentMode = adapter.newLocal(Types.BOOLEAN_VALUE);
    // boolean silentMode= pc.setSilent();
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_SILENT);
    adapter.storeLocal(silentMode);
    // call must be
    TryFinallyVisitor tfv = new TryFinallyVisitor(new OnFinally() {

        @Override
        public void _writeOut(BytecodeContext bc) {
            // if(fcf!=null && fcf.getAfterFinalGOTOLabel()!=null)ASMUtil.visitLabel(adapter,fcf.getFinalEntryLabel());
            // if(!silentMode)pc.unsetSilent();
            Label _if = new Label();
            adapter.loadLocal(silentMode);
            NotVisitor.visitNot(bc);
            adapter.ifZCmp(Opcodes.IFEQ, _if);
            adapter.loadArg(0);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, UNSET_SILENT);
            adapter.pop();
            adapter.visitLabel(_if);
        /*if(fcf!=null) {
					Label l = fcf.getAfterFinalGOTOLabel();
					if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
				}*/
        }
    }, getFlowControlFinal());
    tfv.visitTryBegin(bc);
    getBody().writeOut(bc);
    tfv.visitTryEnd(bc);
}
Also used : OnFinally(lucee.transformer.bytecode.visitor.OnFinally) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) TryFinallyVisitor(lucee.transformer.bytecode.visitor.TryFinallyVisitor) BytecodeContext(lucee.transformer.bytecode.BytecodeContext)

Aggregations

BytecodeContext (lucee.transformer.bytecode.BytecodeContext)15 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 OnFinally (lucee.transformer.bytecode.visitor.OnFinally)9 Label (org.objectweb.asm.Label)7 TryFinallyVisitor (lucee.transformer.bytecode.visitor.TryFinallyVisitor)6 TransformerException (lucee.transformer.TransformerException)5 LitString (lucee.transformer.expression.literal.LitString)5 TryCatchFinallyVisitor (lucee.transformer.bytecode.visitor.TryCatchFinallyVisitor)4 Type (org.objectweb.asm.Type)4 ArrayList (java.util.ArrayList)3 Expression (lucee.transformer.expression.Expression)3 Iterator (java.util.Iterator)2 Body (lucee.transformer.bytecode.Body)2 BodyBase (lucee.transformer.bytecode.BodyBase)2 ConstrBytecodeContext (lucee.transformer.bytecode.ConstrBytecodeContext)2 Statement (lucee.transformer.bytecode.Statement)2 ArrayVisitor (lucee.transformer.bytecode.visitor.ArrayVisitor)2 DecisionIntVisitor (lucee.transformer.bytecode.visitor.DecisionIntVisitor)2 WhileVisitor (lucee.transformer.bytecode.visitor.WhileVisitor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1