com.google.caja.parser.js.FunctionConstructor

Here are the examples of the java api class com.google.caja.parser.js.FunctionConstructor taken from open source projects.

1. VariableLiveness#processImmediatelyCalledFunction()

Project: caja
Source File: VariableLiveness.java
View license
private static LiveSet processImmediatelyCalledFunction(Operation op, LiveSet onEntry) {
    List<? extends Expression> operands = op.children();
    FunctionConstructor fn = (FunctionConstructor) operands.get(0);
    // operands are resolved before the function body is executed
    LiveSet onExit = onEntry;
    for (Expression actual : operands.subList(1, operands.size())) {
        onExit = liveness(actual, onExit).vars;
    }
    LiveSet asAResultOfCallingFn = processFunctionConstructor(fn);
    return onExit.union(asAResultOfCallingFn);
}

2. ScopeTest#testMaskedExceptionVariablesErrorB()

Project: caja
Source File: ScopeTest.java
View license
public final void testMaskedExceptionVariablesErrorB() throws Exception {
    Block n = js(fromString("try { } catch (e) { function foo() { var e; } }"));
    TryStmt t = (TryStmt) n.children().get(0);
    CatchStmt c = (CatchStmt) t.children().get(1);
    Declaration d = findNodeWithIdentifier(n, Declaration.class, "foo");
    FunctionConstructor fc = (FunctionConstructor) d.getInitializer();
    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromCatchStmt(s0, c);
    Scope.fromFunctionConstructor(s1, fc);
    assertEquals(1, mq.getMessages().size());
    assertMsgType(MessageType.MASKING_SYMBOL, mq.getMessages().get(0));
    assertMsgLevel(MessageLevel.LINT, mq.getMessages().get(0));
}

3. ScopeTest#testStartStatementsForFunctionConstructor()

Project: caja
Source File: ScopeTest.java
View license
public final void testStartStatementsForFunctionConstructor() throws Exception {
    Scope s0 = fromProgram(js(fromString("{}")));
    Block block = js(fromString("function() {};"));
    FunctionConstructor fc = (FunctionConstructor) block.children().get(0).children().get(0);
    Scope s1 = Scope.fromFunctionConstructor(s0, fc);
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(0, s1.getStartStatements().size());
    s1.addStartStatement(js(fromString("{}")));
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(1, s1.getStartStatements().size());
    s1.addStartOfScopeStatement(js(fromString("{}")));
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(2, s1.getStartStatements().size());
    s1.declareStartOfScopeTempVariable();
    assertEquals(0, s0.getStartStatements().size());
    assertEquals(3, s1.getStartStatements().size());
}

4. VarCollector#optimize()

Project: caja
Source File: VarCollector.java
View license
private static void optimize(Block body, Set<Identifier> alreadyDeclared) {
    Set<Identifier> locals = newIdentSet();
    List<FunctionConstructor> inners = Lists.newArrayList();
    unvar(body, locals, inners);
    Set<Identifier> needed = newIdentSet(locals);
    needed.removeAll(alreadyDeclared);
    if (!needed.isEmpty()) {
        List<Operation> topAssignments = extractAssignments(body, newIdentSet(locals));
        List<Declaration> decls = Lists.newArrayList();
        for (Operation topAssign : topAssignments) {
            Identifier id = ((Reference) topAssign.children().get(0)).getIdentifier();
            decls.add(new Declaration(topAssign.getFilePosition(), id, topAssign.children().get(1)));
            needed.remove(id);
        }
        for (Identifier name : needed) {
            decls.add(new Declaration(name.getFilePosition(), name, null));
        }
        Statement decl;
        if (decls.size() == 1) {
            decl = decls.get(0);
        } else {
            decl = new MultiDeclaration(FilePosition.startOf(body.getFilePosition()), decls);
        }
        Statement declFollower = null;
        List<? extends Statement> bodyChildren = body.children();
        if (!bodyChildren.isEmpty()) {
            declFollower = bodyChildren.get(0);
        }
        if (declFollower instanceof DirectivePrologue) {
            declFollower = bodyChildren.size() == 1 ? null : bodyChildren.get(1);
        }
        body.insertBefore(decl, declFollower);
    }
    for (FunctionConstructor inner : inners) {
        Set<Identifier> formals = newIdentSet();
        for (FormalParam p : inner.getParams()) {
            formals.add(p.getIdentifier());
        }
        optimize(inner.getBody(), formals);
    }
}