org.mozilla.javascript.ast.ScriptNode Java Examples

The following examples show how to use org.mozilla.javascript.ast.ScriptNode. 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: ScriptDialog.java    From hop with Apache License 2.0 6 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
                                         int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example #2
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * compile the string expression
 * 
 * @param expression
 * @param context
 * @return
 * @throws DataException
 */
protected CompiledExpression compileExpression( String expression,
		ScriptContext context ) throws DataException
{
	String exp = "";
	try
	{
		exp = expression;
		if ( exp == null )
			return null;
		
		IDataScriptEngine engine = (IDataScriptEngine) context.getScriptEngine( IDataScriptEngine.ENGINE_NAME );
		
		ScriptNode tree = parse( exp, engine.getJSContext( context ) );
		return processScriptTree( exp, tree, engine.getJSContext( context )  );
	}
	catch ( Exception e )
	{
		DataException dataException = new DataException( ResourceConstants.INVALID_JS_EXPR,
				e,
				exp );
		throw dataException;
	}
}
 
Example #3
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * compile the scriptExpresion to generate a subclass of compiledExpression.
 * 
 * @param exp
 * @param context
 * @return
 */
protected CompiledExpression compileExpression( IScriptExpression baseExpr, ScriptContext context )
		throws DataException
{
	String exp = "";
	try
	{
		this.scriptExpr = baseExpr;
		exp = baseExpr.getText( );
		if ( exp == null || BaseExpression.constantId.equals( baseExpr.getScriptId( ) ) )
			return null;
		IDataScriptEngine engine = (IDataScriptEngine) context.getScriptEngine( IDataScriptEngine.ENGINE_NAME );
		
		ScriptNode tree = parse( exp, engine.getJSContext( context ) );
		return processScriptTree( exp, tree, engine.getJSContext( context ));
	}
	catch ( Exception e )
	{
		DataException dataException = new DataException( ResourceConstants.INVALID_JS_EXPR,
				e,
				exp );
		throw dataException;
	}
}
 
Example #4
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 #5
Source File: Bug782363Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Checks every variable {@code v} in {@code source} is marked as a
 * number-variable iff {@code numbers} contains {@code v}
 */
protected void assertNumberVars(CharSequence source, String... numbers) {
    // wrap source in function
    ScriptNode tree = compile("function f(){" + source + "}");

    FunctionNode fnode = tree.getFunctionNode(0);
    assertNotNull(fnode);
    OptFunctionNode opt = OptFunctionNode.get(fnode);
    assertNotNull(opt);
    assertSame(fnode, opt.fnode);

    for (int i = 0, c = fnode.getParamCount(); i < c; ++i) {
        assertTrue(opt.isParameter(i));
        assertFalse(opt.isNumberVar(i));
    }

    Set<String> set = new HashSet<String>(asList(numbers));
    for (int i = fnode.getParamCount(), c = fnode.getParamAndVarCount(); i < c; ++i) {
        assertFalse(opt.isParameter(i));
        String name = fnode.getParamOrVarName(i);
        String msg = format("{%s -> number? = %b}", name, opt.isNumberVar(i));
        assertEquals(msg, set.contains(name), opt.isNumberVar(i));
    }
}
 
Example #6
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * compile outer column expression
 * @param refNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileOuterColRef( Node refNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	int level = compileOuterColRefExpr( refNode );
	if ( level == -1 )
	{
		compileComplexExpr( refNode, tree, columnExprList );
	}
	else
	{
		Node nextNode = refNode.getLastChild( );
		if ( nextNode.getType( ) == Token.STRING )
		{
			ColumnBinding info = new ColumnBinding( nextNode.getString( ),
					"",
					level );
			columnExprList.add( info );
		}
	}
	return;
}
 
Example #7
Source File: NodeTransformer.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private void transformCompilationUnit(ScriptNode tree, boolean inStrictMode)
{
    loops = new ObjArray();
    loopEnds = new ObjArray();

    // to save against upchecks if no finally blocks are used.
    hasFinally = false;

    // Flatten all only if we are not using scope objects for block scope
    boolean createScopeObjects = tree.getType() != Token.FUNCTION ||
                              ((FunctionNode)tree).requiresActivation();
    tree.flattenSymbolTable(!createScopeObjects);

    //uncomment to print tree before transformation
    if (Token.printTrees) System.out.println(tree.toStringTree(tree));
    transformCompilationUnit_r(tree, tree, tree, createScopeObjects,
                               inStrictMode);
}
 
Example #8
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 #9
Source File: Bug708801Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Checks every variable {@code v} in {@code source} is marked as a
 * number-variable iff {@code numbers} contains {@code v}
 */
protected void assertNumberVars(CharSequence source, String... numbers) {
    // wrap source in function
    ScriptNode tree = compile("function f(o, fn){" + source + "}");

    FunctionNode fnode = tree.getFunctionNode(0);
    assertNotNull(fnode);
    OptFunctionNode opt = OptFunctionNode.get(fnode);
    assertNotNull(opt);
    assertSame(fnode, opt.fnode);

    for (int i = 0, c = fnode.getParamCount(); i < c; ++i) {
        assertTrue(opt.isParameter(i));
        assertFalse(opt.isNumberVar(i));
    }

    Set<String> set = new HashSet<String>(asList(numbers));
    for (int i = fnode.getParamCount(), c = fnode.getParamAndVarCount(); i < c; ++i) {
        assertFalse(opt.isParameter(i));
        String name = fnode.getParamOrVarName(i);
        String msg = format("{%s -> number? = %b}", name, opt.isNumberVar(i));
        assertEquals(msg, set.contains(name), opt.isNumberVar(i));
    }
}
 
Example #10
Source File: CubeQueryUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
private static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		return getScriptObjectName( script, objectName );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #11
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * get the function node index
 * 
 * @param functionName
 * @param tree
 * @return
 */
private int getFunctionIndex( String functionName, ScriptNode tree )
{
	int index = -1;
	for ( int i = 0; i < tree.getFunctionCount( ); i++ )
	{
		if ( tree.getFunctionNode( i )
				.getFunctionName( ).getString()
				.equals( functionName ) )
		{
			index = i;
			break;
		}
	}
	return index;
}
 
Example #12
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * extract arguments from aggregation expression
 * 
 * @param context
 * @param callNode
 * @throws BirtException
 */
private void extractArguments( Node callNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	Node arg = callNode.getFirstChild( ).getNext( );

	while ( arg != null )
	{
		// need to hold on to the next argument because the tree extraction
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext( );
		processChild( arg, tree, columnExprList );

		arg = nextArg;
	}
}
 
Example #13
Source File: NodeTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void transformCompilationUnit(ScriptNode tree)
{
    loops = new ObjArray();
    loopEnds = new ObjArray();

    // to save against upchecks if no finally blocks are used.
    hasFinally = false;

    // Flatten all only if we are not using scope objects for block scope
    boolean createScopeObjects = tree.getType() != Token.FUNCTION ||
                              ((FunctionNode)tree).requiresActivation();
    tree.flattenSymbolTable(!createScopeObjects);

    //uncomment to print tree before transformation
    if (Token.printTrees) System.out.println(tree.toStringTree(tree));
    boolean inStrictMode = tree instanceof AstRoot &&
                           ((AstRoot)tree).isInStrictMode();
    transformCompilationUnit_r(tree, tree, tree, createScopeObjects,
                               inStrictMode);
}
 
Example #14
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Transforms the tree into a lower-level IR suitable for codegen.
 * Optionally generates the encoded source.
 */
public ScriptNode transformTree(AstRoot root) {
    currentScriptOrFn = root;
    this.inUseStrictDirective = root.isInStrictMode();
    int sourceStartOffset = decompiler.getCurrentOffset();

    if (Token.printTrees) {
        System.out.println("IRFactory.transformTree");
        System.out.println(root.debugPrint());
    }
    ScriptNode script = (ScriptNode)transform(root);

    int sourceEndOffset = decompiler.getCurrentOffset();
    script.setEncodedSourceBounds(sourceStartOffset,
                                  sourceEndOffset);

    if (compilerEnv.isGeneratingSource()) {
        script.setEncodedSource(decompiler.getEncodedSource());
    }

    decompiler = null;
    return script;
}
 
Example #15
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the tree to script
 * 
 * @param context
 * @param tree
 * @param expr
 */
protected void compileForBytecodeExpr( Context context, ScriptNode tree,
		CompiledExpression expr )
{
	assert ( expr instanceof BytecodeExpression );
	CompilerEnvirons compilerEnv = getCompilerEnv( context );
	Interpreter compiler = new Interpreter( );
	Object compiledOb = compiler.compile( compilerEnv, tree, null, false );
	Script script = (Script) compiler.createScriptObject( compiledOb, null );
	( (BytecodeExpression) expr ).setScript( script );
}
 
Example #16
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the expression from a script tree
 * 
 * @param expression
 * @param context
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void CompiledExprFromTree( String expression, Context context,
		ScriptNode tree, List columnExprList ) throws BirtException
{
	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		if ( tree.getFirstChild( ).getType( ) == Token.FUNCTION )
		{
			int index = getFunctionIndex( tree.getFirstChild( ).getString( ),
					tree );
			compileFunctionNode( tree.getFunctionNode( index ),
					tree,
					columnExprList );
		}
		else
		{
			// A single expression
			if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
					&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
					&& tree.getFirstChild( ).getType( ) != Token.BLOCK
					&& tree.getFirstChild( ).getType( ) != Token.SCRIPT)
			{
				// This should never happen?
				throw new CoreException( pluginId,
						ResourceConstants.INVALID_EXPRESSION );
			}
			Node exprNode = tree.getFirstChild( );
			processChild( exprNode, tree, columnExprList );
		}
	}
	else
	{
		compileComplexExpr( tree, tree, columnExprList );
	}
}
 
Example #17
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the expression
 * 
 * @param expression
 * @return List contains all column reference
 * @throws BirtException
 */
public static List compileColumnExpression(  ExpressionParserUtility util, String expression, String indicator )
		throws BirtException
{
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return new ArrayList( );
	util.ROW_INDICATOR = indicator;
	List columnExprList = new ArrayList( );
	columnExprList.clear( );
	Context context = Context.enter( );
	try
	{
		ScriptNode tree = util.parse( expression, context );
		util.CompiledExprFromTree( expression,
				context,
				tree,
				columnExprList );
	}
	catch ( Exception ex )
	{
		throw new CoreException( ResourceConstants.INVALID_EXPRESSION,
				expression,
				ex );
	}
	finally
	{
		Context.exit( );
	}
	return columnExprList;
}
 
Example #18
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the expression into a script tree
 * 
 * @param expression
 * @param cx
 * @return
 */
private ScriptNode parse( String expression, Context cx )
{
	CompilerEnvirons compilerEnv = new CompilerEnvirons( );
	Parser p = new Parser( compilerEnv, cx.getErrorReporter( ) );
	AstRoot root = p.parse( expression, null, 0 );
	IRFactory ir = new IRFactory(compilerEnv);
	return ir.transformTree(root);
}
 
Example #19
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param callNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileAggregationFunction( Node callNode,
		ScriptNode tree, List columnExprList ) throws BirtException
{
	Node firstChild = callNode.getFirstChild( );
	if ( firstChild.getType( ) != Token.GETPROP )
		return;

	Node getPropLeftChild = firstChild.getFirstChild( );
	if ( getPropLeftChild.getType( ) == Token.NAME
			&& getPropLeftChild.getString( ).equals( TOTAL ) )
		hasAggregation = true;

	compileComplexExpr( firstChild, tree, columnExprList );
}
 
Example #20
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the complex expression
 * 
 * @param complexNode
 * @throws BirtException
 */
private void compileComplexExpr( Node complexNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	Node child = complexNode.getFirstChild( );
	while ( child != null )
	{
		if ( child.getType( ) == Token.FUNCTION )
		{
			int index = getFunctionIndex( child.getString( ), tree );
			compileFunctionNode( tree.getFunctionNode( index ),
					tree,
					columnExprList );
		}
		// keep reference to next child, since subsequent steps could
		// lose
		// the reference to it
		Node nextChild = child.getNext( );

		// do not include constants into the sub-expression list
		if ( child.getType( ) == Token.NUMBER
				|| child.getType( ) == Token.STRING
				|| child.getType( ) == Token.TRUE
				|| child.getType( ) == Token.FALSE
				|| child.getType( ) == Token.NULL )
		{
			processChild( child, tree, columnExprList );
			child = nextChild;
			continue;
		}

		processChild( child, tree, columnExprList );
		child = nextChild;
	}
}
 
Example #21
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
public static Set<String> getReferencedMeasure( String expr )
{
	if ( expr == null )
		return Collections.emptySet( );
	try
	{
		Set<String> result = new LinkedHashSet<String>( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		getScriptObjectName( script, "measure", result ); //$NON-NLS-1$
		
		return result;
	}
	catch( Exception e )
	{
		return Collections.emptySet( );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #22
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param bindings
 * @param onlyFromDirectReferenceExpr
 * @return
 * @throws DataException
 */
public static Set<IDimLevel> getReferencedDimLevel( String expr )
		throws CoreException
{
	if ( expr == null  )
		return new HashSet<IDimLevel>( );
	try
	{
		Set<IDimLevel> result = new HashSet<IDimLevel>( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		populateDimLevels( null, script, result );
		return result;
	}
	catch( Exception e )
	{
		return Collections.emptySet( );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #23
Source File: ScriptDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example #24
Source File: ScriptValuesModDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example #25
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile row position expression
 * @param refNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileRowPositionRef( Node refNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	Node rowFirstNode = refNode.getFirstChild( );

	if ( rowFirstNode.getType( ) == Token.GETELEM
			|| rowFirstNode.getType( ) == Token.SETELEM )
	{
		Node rowNode = rowFirstNode.getFirstChild( );
		if ( rowNode != null
				&& rowNode.getType( ) == Token.NAME
				&& rowNode.getString( ).equals( ROWS_0_INDICATOR ) )
		{
			Node rowColumn = rowNode.getNext( );
			if ( rowColumn.getDouble( ) == 0.0 )
			{
				rowColumn = rowFirstNode.getNext( );
				if ( rowColumn.getType( ) == Token.STRING
						&& ( refNode.getType( ) == Token.GETELEM || refNode.getType( ) == Token.SETELEM ) )
				{
					ColumnBinding binding = new ColumnBinding( rowColumn.getString( ),
							ExpressionUtil.createJSDataSetRowExpression( rowColumn.getString( ) ),
							1 );
					columnExprList.add( binding );;
				}
			}
		}
	}
}
 
Example #26
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the expression to script tree
 * 
 * @param expression
 * @param cx
 * @return
 * @throws DataException
 */
protected ScriptNode parse( String expression, Context cx )
		throws DataException
{
	if ( expression == null )
		throw new DataException( ResourceConstants.EXPRESSION_CANNOT_BE_NULL_OR_BLANK );

	CompilerEnvirons compilerEnv = getCompilerEnv( cx );
	Parser p = new Parser( compilerEnv, cx.getErrorReporter( ) );
	AstRoot root = p.parse( expression, null, 0 );
	IRFactory ir = new IRFactory(compilerEnv );
	ScriptNode script = ir.transformTree(root);
	return script;
}
 
Example #27
Source File: NodeTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public final void transform(ScriptNode tree)
{
    transformCompilationUnit(tree);
    for (int i = 0; i != tree.getFunctionCount(); ++i) {
        FunctionNode fn = tree.getFunctionNode(i);
        transform(fn);
    }
}
 
Example #28
Source File: ScriptValuesModDialog.java    From hop with Apache License 2.0 5 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
                                         int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example #29
Source File: Optimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
void optimize(ScriptNode scriptOrFn)
{
    //  run on one function at a time for now
    int functionCount = scriptOrFn.getFunctionCount();
    for (int i = 0; i != functionCount; ++i) {
        OptFunctionNode f = OptFunctionNode.get(scriptOrFn, i);
        optimizeFunction(f);
    }
}
 
Example #30
Source File: CodeGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public InterpreterData compile(CompilerEnvirons compilerEnv,
                               ScriptNode tree,
                               String encodedSource,
                               boolean returnFunction)
{
    this.compilerEnv = compilerEnv;

    if (Token.printTrees) {
        System.out.println("before transform:");
        System.out.println(tree.toStringTree(tree));
    }

    new NodeTransformer().transform(tree);

    if (Token.printTrees) {
        System.out.println("after transform:");
        System.out.println(tree.toStringTree(tree));
    }

    if (returnFunction) {
        scriptOrFn = tree.getFunctionNode(0);
    } else {
        scriptOrFn = tree;
    }
    itsData = new InterpreterData(compilerEnv.getLanguageVersion(),
                                  scriptOrFn.getSourceName(),
                                  encodedSource,
                                  ((AstRoot)tree).isInStrictMode());
    itsData.topLevel = true;

    if (returnFunction) {
        generateFunctionICode();
    } else {
        generateICodeFromTree(scriptOrFn);
    }
    return itsData;
}