Java Code Examples for org.mozilla.javascript.ast.FunctionNode#getParamAndVarCount()

The following examples show how to use org.mozilla.javascript.ast.FunctionNode#getParamAndVarCount() . 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));
    }
}