Search in sources :

Example 1 with Factory

use of lucee.transformer.Factory in project Lucee by lucee.

the class CFMLCompilerImpl method _compile.

/*private byte[] _compiless(ConfigImpl config,PageSource ps,SourceCode sc,String className, TagLib[] tld, FunctionLib[] fld, 
			Resource classRootDir,TransfomerSettings settings) throws TemplateException {
		Factory factory = BytecodeFactory.getInstance(config);
		
		Page page=null;
		
		TagLib[][] _tlibs=new TagLib[][]{null,new TagLib[0]};
		_tlibs[CFMLTransformer.TAG_LIB_GLOBAL]=tld;
		// reset page tlds
		if(_tlibs[CFMLTransformer.TAG_LIB_PAGE].length>0) {
			_tlibs[CFMLTransformer.TAG_LIB_PAGE]=new TagLib[0];
		}
		
		CFMLScriptTransformer scriptTransformer = new CFMLScriptTransformer();
		scriptTransformer.transform(
				BytecodeFactory.getInstance(config)
				, page
				, new EvaluatorPool()
				, _tlibs, fld
				, null
				, config.getCoreTagLib(ps.getDialect()).getScriptTags()
				, sc
				, settings);
		
		//CFMLExprTransformer extr=new CFMLExprTransformer();
		//extr.transform(factory, page, ep, tld, fld, scriptTags, cfml, settings)
		
		return null;
	}*/
private Result _compile(ConfigImpl config, PageSource ps, SourceCode sc, String className, TagLib[] tld, FunctionLib[] fld, Resource classRootDir, boolean returnValue, boolean ignoreScopes) throws TemplateException, IOException {
    if (className == null)
        className = ps.getClassName();
    Result result = null;
    // byte[] barr = null;
    Page page = null;
    Factory factory = BytecodeFactory.getInstance(config);
    try {
        page = sc == null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
        page.setSplitIfNecessary(false);
        try {
            result = new Result(page, page.execute(className));
        } catch (RuntimeException re) {
            String msg = StringUtil.emptyIfNull(re.getMessage());
            if (StringUtil.indexOfIgnoreCase(msg, "Method code too large!") != -1) {
                page = sc == null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
                page.setSplitIfNecessary(true);
                result = new Result(page, page.execute(className));
            } else
                throw re;
        } catch (ClassFormatError cfe) {
            String msg = StringUtil.emptyIfNull(cfe.getMessage());
            if (StringUtil.indexOfIgnoreCase(msg, "Invalid method Code length") != -1) {
                page = ps != null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
                page.setSplitIfNecessary(true);
                result = new Result(page, page.execute(className));
            } else
                throw cfe;
        }
        // store
        if (classRootDir != null) {
            Resource classFile = classRootDir.getRealResource(page.getClassName() + ".class");
            Resource classFileDirectory = classFile.getParentResource();
            if (!classFileDirectory.exists())
                classFileDirectory.mkdirs();
            IOUtil.copy(new ByteArrayInputStream(result.barr), classFile, true);
        }
        return result;
    } catch (AlreadyClassException ace) {
        byte[] bytes = ace.getEncrypted() ? readEncrypted(ace) : readPlain(ace);
        result = new Result(null, bytes);
        String displayPath = ps != null ? "[" + ps.getDisplayPath() + "] " : "";
        String srcName = ASMUtil.getClassName(result.barr);
        int dialect = sc == null ? ps.getDialect() : sc.getDialect();
        // source is cfm and target cfc
        if (dialect == CFMLEngine.DIALECT_CFML && endsWith(srcName, Constants.getCFMLTemplateExtensions(), dialect) && className.endsWith("_" + Constants.getCFMLComponentExtension() + (dialect == CFMLEngine.DIALECT_CFML ? Constants.CFML_CLASS_SUFFIX : Constants.LUCEE_CLASS_SUFFIX))) {
            throw new TemplateException("source file " + displayPath + "contains the bytecode for a regular cfm template not for a component");
        }
        // source is cfc and target cfm
        if (dialect == CFMLEngine.DIALECT_CFML && srcName.endsWith("_" + Constants.getCFMLComponentExtension() + (dialect == CFMLEngine.DIALECT_CFML ? Constants.CFML_CLASS_SUFFIX : Constants.LUCEE_CLASS_SUFFIX)) && endsWith(className, Constants.getCFMLTemplateExtensions(), dialect))
            throw new TemplateException("source file " + displayPath + "contains a component not a regular cfm template");
        // rename class name when needed
        if (!srcName.equals(className))
            result = new Result(result.page, ClassRenamer.rename(result.barr, className));
        // store
        if (classRootDir != null) {
            Resource classFile = classRootDir.getRealResource(className + ".class");
            Resource classFileDirectory = classFile.getParentResource();
            if (!classFileDirectory.exists())
                classFileDirectory.mkdirs();
            result = new Result(result.page, Page.setSourceLastModified(result.barr, ps != null ? ps.getPhyscalFile().lastModified() : System.currentTimeMillis()));
            IOUtil.copy(new ByteArrayInputStream(result.barr), classFile, true);
        }
        return result;
    } catch (TransformerException bce) {
        Position pos = bce.getPosition();
        int line = pos == null ? -1 : pos.line;
        int col = pos == null ? -1 : pos.column;
        if (ps != null)
            bce.addContext(ps, line, col, null);
        throw bce;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateException(lucee.runtime.exp.TemplateException) Position(lucee.transformer.Position) Resource(lucee.commons.io.res.Resource) BytecodeFactory(lucee.transformer.bytecode.BytecodeFactory) Factory(lucee.transformer.Factory) Page(lucee.transformer.bytecode.Page) AlreadyClassException(lucee.transformer.util.AlreadyClassException) TransformerException(lucee.transformer.TransformerException)

Example 2 with Factory

use of lucee.transformer.Factory in project Lucee by lucee.

the class TagLoop method writeOutTypeTimes.

private void writeOutTypeTimes(BytecodeContext bc) throws TransformerException {
    Factory f = bc.getFactory();
    GeneratorAdapter adapter = bc.getAdapter();
    int times = adapter.newLocal(Types.INT_VALUE);
    ExprInt timesExpr = CastInt.toExprInt(getAttribute("times").getValue());
    ExpressionUtil.writeOutSilent(timesExpr, bc, Expression.MODE_VALUE);
    adapter.storeLocal(times);
    ForVisitor fiv = new ForVisitor();
    fiv.visitBegin(adapter, 1, false);
    getBody().writeOut(bc);
    fiv.visitEnd(bc, times, true, getStart());
}
Also used : ForVisitor(lucee.transformer.bytecode.visitor.ForVisitor) ExprInt(lucee.transformer.expression.ExprInt) Factory(lucee.transformer.Factory) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter)

Aggregations

Factory (lucee.transformer.Factory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Resource (lucee.commons.io.res.Resource)1 TemplateException (lucee.runtime.exp.TemplateException)1 Position (lucee.transformer.Position)1 TransformerException (lucee.transformer.TransformerException)1 BytecodeFactory (lucee.transformer.bytecode.BytecodeFactory)1 Page (lucee.transformer.bytecode.Page)1 ForVisitor (lucee.transformer.bytecode.visitor.ForVisitor)1 ExprInt (lucee.transformer.expression.ExprInt)1 AlreadyClassException (lucee.transformer.util.AlreadyClassException)1 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)1