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

The following examples show how to use org.mozilla.javascript.ast.ScriptNode#getFunctionNode() . 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
/**
 * 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 2
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 3
Source File: Node.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private static void toStringTreeHelper(ScriptNode treeTop, Node n,
                                       ObjToIntMap printIds,
                                       int level, StringBuilder sb)
{
    if (Token.printTrees) {
        if (printIds == null) {
            printIds = new ObjToIntMap();
            generatePrintIds(treeTop, printIds);
        }
        for (int i = 0; i != level; ++i) {
            sb.append("    ");
        }
        n.toString(printIds, sb);
        sb.append('\n');
        for (Node cursor = n.getFirstChild(); cursor != null;
             cursor = cursor.getNext())
        {
            if (cursor.getType() == Token.FUNCTION) {
                int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP);
                FunctionNode fn = treeTop.getFunctionNode(fnIndex);
                toStringTreeHelper(fn, fn, null, level + 1, sb);
            } else {
                toStringTreeHelper(treeTop, cursor, printIds, level+1, sb);
            }
        }
    }
}
 
Example 4
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 5
Source File: CodeGenerator.java    From JsDroidCmd with Mozilla Public License 2.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,
                                  scriptOrFn.isInStrictMode());
    itsData.topLevel = true;

    if (returnFunction) {
        generateFunctionICode();
    } else {
        generateICodeFromTree(scriptOrFn);
    }
    return itsData;
}
 
Example 6
Source File: Node.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static void toStringTreeHelper(ScriptNode treeTop, Node n,
                                       ObjToIntMap printIds,
                                       int level, StringBuffer sb)
{
    if (Token.printTrees) {
        if (printIds == null) {
            printIds = new ObjToIntMap();
            generatePrintIds(treeTop, printIds);
        }
        for (int i = 0; i != level; ++i) {
            sb.append("    ");
        }
        n.toString(printIds, sb);
        sb.append('\n');
        for (Node cursor = n.getFirstChild(); cursor != null;
             cursor = cursor.getNext())
        {
            if (cursor.getType() == Token.FUNCTION) {
                int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP);
                FunctionNode fn = treeTop.getFunctionNode(fnIndex);
                toStringTreeHelper(fn, fn, null, level + 1, sb);
            } else {
                toStringTreeHelper(treeTop, cursor, printIds, level+1, sb);
            }
        }
    }
}
 
Example 7
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 8
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;
}
 
Example 9
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 10
Source File: OptFunctionNode.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public static OptFunctionNode get(ScriptNode scriptOrFn, int i)
{
    FunctionNode fnode = scriptOrFn.getFunctionNode(i);
    return (OptFunctionNode)fnode.getCompilerData();
}
 
Example 11
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 };
}
 
Example 12
Source File: OptFunctionNode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static OptFunctionNode get(ScriptNode scriptOrFn, int i)
{
    FunctionNode fnode = scriptOrFn.getFunctionNode(i);
    return (OptFunctionNode)fnode.getCompilerData();
}