Java Code Examples for org.mozilla.javascript.CompilerEnvirons#initFromContext()

The following examples show how to use org.mozilla.javascript.CompilerEnvirons#initFromContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bug782363Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(),
            false);

    return tree;
}
 
Example 2
Source File: Bug708801Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv
            .getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree,
            tree.getEncodedSource(), false);

    return tree;
}
 
Example 3
Source File: Bug688021Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 4
Source File: Bug688018Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 5
Source File: Bug687669Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 6
Source File: Bug688023Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 7
Source File: Bug689308Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 8
Source File: Bug689314Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 9
Source File: Bug689314Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 10
Source File: Bug688021Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 11
Source File: Bug688018Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 12
Source File: Bug687669Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 13
Source File: Bug689308Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private AstRoot parse(CharSequence cs) {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    return p.parse(cs.toString(), "<eval>", 1);
}
 
Example 14
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * 
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( compiledExprCache.containsKey( expression ) )
		return ( (Boolean) compiledExprCache.get( expression ) ).booleanValue( );
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		compiledExprCache.put( expression, Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}

	compiledExprCache.put( expression, Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 15
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 16
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 17
Source File: JavaScriptClassCompiler.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath,
		File tempDirFile) throws JRException
{
	Context context = ContextFactory.getGlobal().enterContext();
	try
	{
		JRPropertiesUtil properties = JRPropertiesUtil.getInstance(jasperReportsContext);
		int expressionsPerScript = properties.getIntegerProperty(PROPERTY_EXPRESSIONS_PER_SCRIPT);
		int scriptMaxLength = properties.getIntegerProperty(PROPERTY_SCRIPT_MAX_SIZE);
		
		int optimizationLevel = properties.getIntegerProperty(PROPERTY_OPTIMIZATION_LEVEL);
		context.setOptimizationLevel(optimizationLevel);
		context.getWrapFactory().setJavaPrimitiveWrap(false);

		Errors errors = new Errors();
		
		CompilerEnvirons compilerEnv = new CompilerEnvirons();
		compilerEnv.initFromContext(context);
		
		// we're using the context to compile the expressions in interpreted mode to catch syntax errors 
		context.setOptimizationLevel(-1);
		
		for (int i = 0; i < units.length; i++)
		{
			JRCompilationUnit unit = units[i];
			CompileSources compileSources = new CompileSources(expressionsPerScript, scriptMaxLength);
			JavaScriptCompiledData compiledData = new JavaScriptCompiledData();
			
			JRSourceCompileTask compileTask = unit.getCompileTask();
			for (Iterator<JRExpression> it = compileTask.getExpressions().iterator(); it.hasNext();)
			{
				JRExpression expr = it.next();
				int id = compileTask.getExpressionId(expr);
				
				ScriptExpressionVisitor defaultVisitor = defaultExpressionCreator();
				JRExpressionUtil.visitChunks(expr, defaultVisitor);
				String defaultExpression = defaultVisitor.getScript();
				
				//compile the default expression to catch syntax errors
				try
				{
					context.compileString(defaultExpression, "expression", 0, null);
				}
				catch (EvaluatorException e)
				{
					errors.addError(e);
				}

				if (!errors.hasErrors())
				{
					ScriptExpressionVisitor oldVisitor = oldExpressionCreator();
					ScriptExpressionVisitor estimatedVisitor = estimatedExpressionCreator();
					JRExpressionUtil.visitChunks(expr, new CompositeExpressionChunkVisitor(oldVisitor, estimatedVisitor));
					
					int defaultExpressionIdx = compileSources.addExpression(defaultExpression);
					int oldExpressionIdx = compileSources.addExpression(oldVisitor.getScript());
					int estimatedExpressionIdx = compileSources.addExpression(estimatedVisitor.getScript());
					
					compiledData.addExpression(id, defaultExpressionIdx, oldExpressionIdx, estimatedExpressionIdx);
				}
			}

			if (!errors.hasErrors())
			{
				compileScripts(unit, compilerEnv, compileSources, compiledData);
				unit.setCompileData(compiledData);
			}
		}

		return errors.errorMessage();
	}
	finally
	{
		Context.exit();
	}
}