Search in sources :

Example 16 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class SimpleExprTransformer method string.

/**
 * Liest den String ein
 * @return Element
 * @throws TemplateException
 */
public Expression string(Factory f, SourceCode cfml) throws TemplateException {
    cfml.removeSpace();
    char quoter = cfml.getCurrentLower();
    if (quoter != '"' && quoter != '\'')
        return null;
    StringBuffer str = new StringBuffer();
    boolean insideSpecial = false;
    Position line = cfml.getPosition();
    while (cfml.hasNext()) {
        cfml.next();
        // check special
        if (cfml.isCurrent(specialChar)) {
            insideSpecial = !insideSpecial;
            str.append(specialChar);
        } else // check quoter
        if (!insideSpecial && cfml.isCurrent(quoter)) {
            // Ecaped sharp
            if (cfml.isNext(quoter)) {
                cfml.next();
                str.append(quoter);
            } else // finsish
            {
                break;
            }
        } else // all other character
        {
            str.append(cfml.getCurrent());
        }
    }
    if (!cfml.forwardIfCurrent(quoter))
        throw new TemplateException(cfml, "Invalid Syntax Closing [" + quoter + "] not found");
    LitString rtn = f.createLitString(str.toString(), line, cfml.getPosition());
    cfml.removeSpace();
    return rtn;
}
Also used : LitString(lucee.transformer.expression.literal.LitString) Position(lucee.transformer.Position) TemplateException(lucee.runtime.exp.TemplateException)

Example 17 with LitString

use of lucee.transformer.expression.literal.LitString 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 18 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TryCatchFinally method _writeOutCatch.

private void _writeOutCatch(BytecodeContext bc, int lRef, int lThrow, int old) throws TransformerException {
    GeneratorAdapter adapter = bc.getAdapter();
    int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
    // instance of Abort
    Label abortEnd = new Label();
    adapter.loadLocal(lThrow);
    adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
    // adapter.instanceOf(Types.ABORT);
    adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
    adapter.loadLocal(lThrow);
    adapter.throwException();
    adapter.visitLabel(abortEnd);
    /*
	        // PageExceptionImpl old=pc.getCatch();
	        int old=adapter.newLocal(Types.PAGE_EXCEPTION);
	        adapter.loadArg(0);
	        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.GET_CATCH);
			adapter.storeLocal(old);
*/
    // cast to PageException  Caster.toPagException(t);
    adapter.loadLocal(lThrow);
    adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
    // PageException pe=...
    adapter.storeLocal(pe);
    // catch loop
    Label endAllIf = new Label();
    Iterator<Catch> it = catches.iterator();
    Catch ctElse = null;
    while (it.hasNext()) {
        Catch ct = it.next();
        // store any for else
        if (ct.type != null && ct.type instanceof LitString && ((LitString) ct.type).getString().equalsIgnoreCase("any")) {
            ctElse = ct;
            continue;
        }
        ExpressionUtil.visitLine(bc, ct.line);
        // pe.typeEqual(type)
        if (ct.type == null) {
            getFactory().TRUE().writeOut(bc, Expression.MODE_VALUE);
        } else {
            adapter.loadLocal(pe);
            ct.type.writeOut(bc, Expression.MODE_REF);
            adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
        }
        Label endIf = new Label();
        adapter.ifZCmp(Opcodes.IFEQ, endIf);
        catchBody(bc, adapter, ct, pe, lRef, true, true);
        adapter.visitJumpInsn(Opcodes.GOTO, endAllIf);
        adapter.visitLabel(endIf);
    }
    if (ctElse != null) {
        catchBody(bc, adapter, ctElse, pe, lRef, true, true);
    } else {
        // pc.setCatch(pe,true);
        adapter.loadArg(0);
        adapter.loadLocal(pe);
        adapter.push(false);
        adapter.push(false);
        adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH3);
        adapter.loadLocal(pe);
        adapter.throwException();
    }
    adapter.visitLabel(endAllIf);
/*adapter.loadArg(0);
            adapter.loadLocal(old);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH_PE);*/
}
Also used : LitString(lucee.transformer.expression.literal.LitString) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter)

Example 19 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TryCatchFinally method addCatch.

/**
 * @param type
 * @param name
 * @param b
 * @param line
 * @throws TransformerException
 */
public void addCatch(Expression type, Expression name, Body b, Position line) throws TransformerException {
    // type
    if (type == null || type instanceof ExprString)
        ;
    else if (type instanceof Variable) {
        type = VariableString.toExprString(type);
    } else
        throw new TransformerException("type from catch statement is invalid", type.getStart());
    // name
    if (name instanceof LitString) {
        Variable v = getFactory().createVariable(Scope.SCOPE_UNDEFINED, name.getStart(), name.getEnd());
        v.addMember(getFactory().createDataMember(getFactory().toExprString(name)));
        name = new VariableRef(v, true);
    } else if (name instanceof Variable)
        name = new VariableRef((Variable) name, true);
    else
        throw new TransformerException("name from catch statement is invalid", name.getStart());
    addCatch((ExprString) type, (VariableRef) name, b, line);
}
Also used : LitString(lucee.transformer.expression.literal.LitString) VariableRef(lucee.transformer.bytecode.expression.var.VariableRef) Variable(lucee.transformer.expression.var.Variable) ExprString(lucee.transformer.expression.ExprString) TransformerException(lucee.transformer.TransformerException)

Example 20 with LitString

use of lucee.transformer.expression.literal.LitString in project Lucee by lucee.

the class TagFunction method createFunction.

private Function createFunction(Page page, Body body, RefBoolean isStatic, boolean defaultOutput) throws TransformerException {
    Attribute attr;
    LitString ANY = page.getFactory().createLitString("any");
    LitString PUBLIC = page.getFactory().createLitString("public");
    // name
    Expression name = removeAttribute("name").getValue();
    /*if(name instanceof LitString) {
			((LitString)name).upperCase();
		}*/
    // return
    attr = removeAttribute("returntype");
    // if(attr==null) attr = getAttribute("return");
    // if(attr==null) attr = getAttribute("type");
    Expression returnType = (attr == null) ? ANY : attr.getValue();
    // output
    attr = removeAttribute("output");
    Expression output = (attr == null) ? (defaultOutput ? page.getFactory().TRUE() : page.getFactory().TRUE()) : attr.getValue();
    // bufferOutput
    attr = removeAttribute("bufferoutput");
    Expression bufferOutput = (attr == null) ? null : attr.getValue();
    // modifier
    isStatic.setValue(false);
    int modifier = Component.MODIFIER_NONE;
    attr = removeAttribute("modifier");
    if (attr != null) {
        Expression val = attr.getValue();
        if (val instanceof Literal) {
            Literal l = (Literal) val;
            String str = StringUtil.emptyIfNull(l.getString()).trim();
            if ("abstract".equalsIgnoreCase(str))
                modifier = Component.MODIFIER_ABSTRACT;
            else if ("final".equalsIgnoreCase(str))
                modifier = Component.MODIFIER_FINAL;
            else if ("static".equalsIgnoreCase(str))
                isStatic.setValue(true);
        }
    }
    // access
    attr = removeAttribute("access");
    Expression access = (attr == null) ? PUBLIC : attr.getValue();
    // dspLabel
    attr = removeAttribute("displayname");
    Expression displayname = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
    // hint
    attr = removeAttribute("hint");
    Expression hint = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
    // description
    attr = removeAttribute("description");
    Expression description = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
    // returnformat
    attr = removeAttribute("returnformat");
    Expression returnFormat = (attr == null) ? null : attr.getValue();
    // secureJson
    attr = removeAttribute("securejson");
    Expression secureJson = (attr == null) ? null : attr.getValue();
    // verifyClient
    attr = removeAttribute("verifyclient");
    Expression verifyClient = (attr == null) ? null : attr.getValue();
    // localMode
    attr = removeAttribute("localmode");
    Expression localMode = (attr == null) ? null : attr.getValue();
    // cachedWithin
    Literal cachedWithin = null;
    attr = removeAttribute("cachedwithin");
    if (attr != null) {
        Expression val = attr.getValue();
        if (val instanceof Literal)
            cachedWithin = ((Literal) val);
    }
    String strAccess = ((LitString) access).getString();
    int acc = ComponentUtil.toIntAccess(strAccess, -1);
    if (acc == -1)
        throw new TransformerException("invalid access type [" + strAccess + "], access types are remote, public, package, private", getStart());
    Function func = new FunctionImpl(page, name, returnType, returnFormat, output, bufferOutput, acc, displayname, description, hint, secureJson, verifyClient, localMode, cachedWithin, modifier, body, getStart(), getEnd());
    // %**%
    Map attrs = getAttributes();
    Iterator it = attrs.entrySet().iterator();
    HashMap<String, Attribute> metadatas = new HashMap<String, Attribute>();
    while (it.hasNext()) {
        attr = (Attribute) ((Map.Entry) it.next()).getValue();
        metadatas.put(attr.getName(), attr);
    }
    func.setMetaData(metadatas);
    return func;
}
Also used : HashMap(java.util.HashMap) LitString(lucee.transformer.expression.literal.LitString) LitString(lucee.transformer.expression.literal.LitString) Function(lucee.transformer.bytecode.statement.udf.Function) IFunction(lucee.transformer.bytecode.statement.IFunction) Expression(lucee.transformer.expression.Expression) Literal(lucee.transformer.expression.literal.Literal) FunctionImpl(lucee.transformer.bytecode.statement.udf.FunctionImpl) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map) TransformerException(lucee.transformer.TransformerException)

Aggregations

LitString (lucee.transformer.expression.literal.LitString)36 Expression (lucee.transformer.expression.Expression)21 ExprString (lucee.transformer.expression.ExprString)13 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)11 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)9 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)8 TransformerException (lucee.transformer.TransformerException)6 Statement (lucee.transformer.bytecode.Statement)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)5 Body (lucee.transformer.bytecode.Body)5 Argument (lucee.transformer.bytecode.expression.var.Argument)5 PrintOut (lucee.transformer.bytecode.statement.PrintOut)5 Literal (lucee.transformer.expression.literal.Literal)5 TemplateException (lucee.runtime.exp.TemplateException)4 BytecodeContext (lucee.transformer.bytecode.BytecodeContext)4 TagLibTag (lucee.transformer.library.tag.TagLibTag)4 Label (org.objectweb.asm.Label)4 BodyBase (lucee.transformer.bytecode.BodyBase)3 Page (lucee.transformer.bytecode.Page)3