Java Code Examples for org.mozilla.javascript.ast.ScriptNode#getFunctionCount()

The following examples show how to use org.mozilla.javascript.ast.ScriptNode#getFunctionCount() . 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: 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 2
Source File: NodeTransformer.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public final void transform(ScriptNode tree, boolean inStrictMode)
{
    inStrictMode = inStrictMode || tree.isInStrictMode();
    transformCompilationUnit(tree, inStrictMode);
    for (int i = 0; i != tree.getFunctionCount(); ++i) {
        FunctionNode fn = tree.getFunctionNode(i);
        transform(fn, inStrictMode);
    }
}
 
Example 3
Source File: Optimizer.java    From JsDroidCmd with Mozilla Public License 2.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 4
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 5
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 6
Source File: ClassCompiler.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Compile JavaScript source into one or more Java class files.
 * The first compiled class will have name mainClassName.
 * If the results of {@link #getTargetExtends()} or
 * {@link #getTargetImplements()} are not null, then the first compiled
 * class will extend the specified super class and implement
 * specified interfaces.
 *
 * @return array where elements with even indexes specifies class name
 *         and the following odd index gives class file body as byte[]
 *         array. The initial element of the array always holds
 *         mainClassName and array[1] holds its byte code.
 */
public Object[] compileToClassFiles(String source,
                                    String sourceLocation,
                                    int lineno,
                                    String mainClassName)
{
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source, sourceLocation, lineno);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    // release reference to original parse tree & parser
    irf = null;
    ast = null;
    p = null;

    Class<?> superClass = getTargetExtends();
    Class<?>[] interfaces = getTargetImplements();
    String scriptClassName;
    boolean isPrimary = (interfaces == null && superClass == null);
    if (isPrimary) {
        scriptClassName = mainClassName;
    } else {
        scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
    }

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

    if (isPrimary) {
        return new Object[] { scriptClassName, scriptClassBytes };
    }
    int functionCount = tree.getFunctionCount();
    ObjToIntMap functionNames = new ObjToIntMap(functionCount);
    for (int i = 0; i != functionCount; ++i) {
        FunctionNode ofn = tree.getFunctionNode(i);
        String name = ofn.getName();
        if (name != null && name.length() != 0) {
            functionNames.put(name, ofn.getParamCount());
        }
    }
    if (superClass == null) {
        superClass = ScriptRuntime.ObjectClass;
    }
    byte[] mainClassBytes
        = JavaAdapter.createAdapterCode(
            functionNames, mainClassName,
            superClass, interfaces, scriptClassName);

    return new Object[] { mainClassName, mainClassBytes,
                          scriptClassName, scriptClassBytes };
}
 
Example 7
Source File: ClassCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compile JavaScript source into one or more Java class files.
 * The first compiled class will have name mainClassName.
 * If the results of {@link #getTargetExtends()} or
 * {@link #getTargetImplements()} are not null, then the first compiled
 * class will extend the specified super class and implement
 * specified interfaces.
 *
 * @return array where elements with even indexes specifies class name
 *         and the following odd index gives class file body as byte[]
 *         array. The initial element of the array always holds
 *         mainClassName and array[1] holds its byte code.
 */
public Object[] compileToClassFiles(String source,
                                    String sourceLocation,
                                    int lineno,
                                    String mainClassName)
{
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source, sourceLocation, lineno);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    // release reference to original parse tree & parser
    irf = null;
    ast = null;
    p = null;

    Class<?> superClass = getTargetExtends();
    Class<?>[] interfaces = getTargetImplements();
    String scriptClassName;
    boolean isPrimary = (interfaces == null && superClass == null);
    if (isPrimary) {
        scriptClassName = mainClassName;
    } else {
        scriptClassName = makeAuxiliaryClassName(mainClassName, "1");
    }

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

    if (isPrimary) {
        return new Object[] { scriptClassName, scriptClassBytes };
    }
    int functionCount = tree.getFunctionCount();
    ObjToIntMap functionNames = new ObjToIntMap(functionCount);
    for (int i = 0; i != functionCount; ++i) {
        FunctionNode ofn = tree.getFunctionNode(i);
        String name = ofn.getName();
        if (name != null && name.length() != 0) {
            functionNames.put(name, ofn.getParamCount());
        }
    }
    if (superClass == null) {
        superClass = ScriptRuntime.ObjectClass;
    }
    byte[] mainClassBytes
        = JavaAdapter.createAdapterCode(
            functionNames, mainClassName,
            superClass, interfaces, scriptClassName);

    return new Object[] { mainClassName, mainClassBytes,
                          scriptClassName, scriptClassBytes };
}