com.google.caja.parser.js.Operation

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

1. Linter#shouldBeEvaluatedForValue()

Project: caja
Source File: Linter.java
View license
/**
   * A heuristic that identifies expressions that should not appear in a place
   * where their value cannot be used.  This identifies expressions that don't
   * have a side effect, or that are overly complicated.
   *
   * <p>
   * E.g. the expression {@code [1, 2, 3]} has no side effect and so should not
   * appear where its value would be ignored.
   *
   * <p>
   * The expression {@code +f()} might have a side effect, but the {@code +}
   * operator is redundant, and so the expression should not be ignored.
   *
   * <p>
   * Expressions like function calls and assignments are considered side effects
   * and can reasonably appear where their value is not used.
   *
   * <p>
   * Member access operations {@code a.b} could have a useful side-effect, but
   * are unlikely to be used that way.
   *
   * <p>
   * To convince this method that an operations value is being purposely ignored
   * use the {@code void} operator.
   *
   * @return true for any expression that is likely to be used for its value.
   */
private static boolean shouldBeEvaluatedForValue(Expression e) {
    // A literal or value constructor
    if (e instanceof Reference || e instanceof Literal || e instanceof ArrayConstructor || e instanceof ObjectConstructor || e instanceof FunctionConstructor) {
        return true;
    }
    if (!(e instanceof Operation)) {
        return false;
    }
    Operation op = (Operation) e;
    switch(op.getOperator()) {
        case ASSIGN:
        case DELETE:
        case POST_DECREMENT:
        case POST_INCREMENT:
        case PRE_DECREMENT:
        case PRE_INCREMENT:
        case // indicates value purposely ignored
        VOID:
            return false;
        case FUNCTION_CALL:
            return false;
        case CONSTRUCTOR:
            return true;
        case LOGICAL_AND:
        case LOGICAL_OR:
            return shouldBeEvaluatedForValue(op.children().get(1));
        case TERNARY:
            return shouldBeEvaluatedForValue(op.children().get(1)) && shouldBeEvaluatedForValue(op.children().get(2));
        case COMMA:
        // $FALL-THROUGH
        default:
            return op.getOperator().getCategory() != OperatorCategory.ASSIGNMENT;
    }
}

2. Linter#isCommaOperationNotEvaluatedForValue()

Project: caja
Source File: Linter.java
View license
private static boolean isCommaOperationNotEvaluatedForValue(Expression e) {
    if (!(e instanceof Operation)) {
        return false;
    }
    Operation op = (Operation) e;
    if (op.getOperator() != Operator.COMMA) {
        return false;
    }
    Expression left = op.children().get(0), right = op.children().get(1);
    return !shouldBeEvaluatedForValue(right) && (!shouldBeEvaluatedForValue(left) || isCommaOperationNotEvaluatedForValue(left));
}

3. ParseTreeKB#foldComparisonToFalsey()

Project: caja
Source File: ParseTreeKB.java
View license
private Fact foldComparisonToFalsey(ParseTreeNode n) {
    if (!(n instanceof Operation)) {
        return null;
    }
    Operation op = (Operation) n;
    Operator o = op.getOperator();
    boolean eq;
    boolean strict;
    switch(o) {
        case EQUAL:
        case STRICTLY_EQUAL:
            eq = true;
            break;
        case NOT_EQUAL:
        case STRICTLY_NOT_EQUAL:
            eq = false;
            break;
        default:
            return null;
    }
    strict = o == Operator.STRICTLY_EQUAL || o == Operator.STRICTLY_NOT_EQUAL;
    List<? extends Expression> operands = op.children();
    Expression a = operands.get(0);
    Expression b = operands.get(1);
    if (strict ? isUndefOrLiteral(a) : isNullOrUndef(a)) {
    // continue to check
    } else if (strict ? isUndefOrLiteral(b) : isNullOrUndef(b)) {
        Expression t = a;
        a = b;
        b = t;
    } else {
        return null;
    }
    Pair<Expression, Fact> fe = facts.get(this.optNodeDigest(b));
    if (fe == null) {
        return null;
    }
    Boolean bool = a.conditionResult();
    if (bool == null || bool.booleanValue() == fe.b.isTruthy()) {
        return null;
    }
    return eq ? Fact.FALSE : Fact.TRUE;
}

4. ParseTreeKB#withoutTopRef()

Project: caja
Source File: ParseTreeKB.java
View license
private static Expression withoutTopRef(Expression e) {
    Operation op = (Operation) e;
    List<? extends Expression> operands = op.children();
    Expression obj = operands.get(0), prop = operands.get(1);
    if (obj instanceof Reference) {
        return prop;
    }
    return Operation.create(e.getFilePosition(), op.getOperator(), withoutTopRef(obj), prop);
}

5. StatementSimplifier#optimizeExpressionFlow()

Project: caja
Source File: StatementSimplifier.java
View license
static Expression optimizeExpressionFlow(Expression e) {
    if (!(e instanceof Operation)) {
        return e;
    }
    Operation op = (Operation) e;
    List<? extends Expression> operands = op.children();
    Expression[] newOperands = null;
    int n = operands.size();
    for (int i = 0; i < n; ++i) {
        Expression operand = operands.get(i);
        Expression newOperand = optimizeExpressionFlow(operand);
        if (operand != newOperand) {
            if (newOperands == null) {
                newOperands = operands.toArray(new Expression[n]);
            }
            newOperands[i] = newOperand;
        }
    }
    Operator oper = op.getOperator();
    FilePosition pos = e.getFilePosition();
    if (oper != Operator.TERNARY) {
        return newOperands == null ? e : Operation.create(pos, oper, newOperands);
    }
    // (c ? x,z : y,z) -> (c ? x:y),z
    Expression[] ternaryOperands = newOperands != null ? newOperands : operands.toArray(new Expression[3]);
    Expression c = ternaryOperands[0];
    Expression x = ternaryOperands[1];
    Expression y = ternaryOperands[2];
    while (Operation.is(c, Operator.NOT)) {
        c = ((Operation) c).children().get(0);
        Expression t = x;
        x = y;
        y = t;
    }
    if (ParseTreeNodes.deepEquals(x, y)) {
        if (c.simplifyForSideEffect() == null) {
            return x;
        }
        return commaOp(pos, c, x);
    }
    if (isSimple(c)) {
        // control would never reach the second identical expression.
        if (ParseTreeNodes.deepEquals(c, x)) {
            // (c ? c : y) -> c || y     if c not side effecting
            return Operation.create(pos, Operator.LOGICAL_OR, c, y);
        } else if (ParseTreeNodes.deepEquals(c, y)) {
            // (c ? x : c) -> c && x     if c not side effecting
            return Operation.create(pos, Operator.LOGICAL_AND, c, x);
        }
    }
    // TODO(mikesamuel): if c is simple and not a global reference, optimize
    // out the common head as well.
    CommaCommonalities opt = commaCommonalities(x, y);
    if (opt != null) {
        // x and y are structurally identical.
        if (opt.aReduced == null) {
            // (c ? z: y,z)  ->  (c || y),z
            return commaOp(pos, Operation.createInfix(Operator.LOGICAL_OR, c, opt.bReduced), opt.commonTail);
        } else if (opt.bReduced == null) {
            // (c ? x,z : z)  ->  (c && x),z
            return commaOp(pos, Operation.createInfix(Operator.LOGICAL_AND, c, opt.aReduced), opt.commonTail);
        } else {
            // (c ? x,z : y,z) -> (c ? x : y),z
            return commaOp(pos, optimizeExpressionFlow(Operation.createTernary(c, opt.aReduced, opt.bReduced)), opt.commonTail);
        }
    }
    ternaryOperands[0] = c;
    ternaryOperands[1] = x;
    ternaryOperands[2] = y;
    if (x instanceof Operation && y instanceof Operation) {
        Operation xop = (Operation) x;
        Operation yop = (Operation) y;
        Operator xoper = xop.getOperator();
        if (xoper == yop.getOperator()) {
            List<? extends Expression> xoperands = xop.children();
            List<? extends Expression> yoperands = yop.children();
            int nOperands = xoperands.size();
            if (nOperands == yoperands.size()) {
                Expression xoperand0 = xoperands.get(0);
                // evaluated last regardless.
                if (nOperands == 2 && ParseTreeNodes.deepEquals(xoperands.get(1), yoperands.get(1)) && xoper.getCategory() != OperatorCategory.ASSIGNMENT && (xoper != Operator.FUNCTION_CALL || !(Operation.is(xoperand0, Operator.MEMBER_ACCESS) || Operation.is(xoperand0, Operator.SQUARE_BRACKET)))) {
                    // c ? foo(myNode) : bar(myNode)  =>  (c ? foo : bar)(myNode)
                    return Operation.create(pos, xoper, optimizeExpressionFlow(Operation.createTernary(c, xoperands.get(0), yoperands.get(0))), xoperands.get(1));
                }
                if (// Switching order of evaluation doesn't matter
                isSimple(xoperands.get(0)) && isSimple(c) && ParseTreeNodes.deepEquals(xoperands.get(0), yoperands.get(0))) {
                    // c ? (x + 1 : x + 2)  ->  x + (c ? 1 : 2)
                    if (xoper != Operator.MEMBER_ACCESS) {
                        return Operation.create(pos, xoper, xoperands.get(0), optimizeExpressionFlow(Operation.createTernary(c, xoperands.get(1), yoperands.get(1))));
                    } else {
                        Reference xref = (Reference) xoperands.get(1);
                        Reference yref = (Reference) yoperands.get(1);
                        StringLiteral xname = StringLiteral.valueOf(xref.getFilePosition(), xref.getIdentifierName());
                        StringLiteral yname = StringLiteral.valueOf(yref.getFilePosition(), yref.getIdentifierName());
                        return Operation.create(pos, Operator.SQUARE_BRACKET, xoperands.get(0), optimizeExpressionFlow(Operation.createTernary(c, xname, yname)));
                    }
                }
            }
        }
    }
    if (operands.equals(Arrays.asList(ternaryOperands))) {
        return e;
    }
    return Operation.create(pos, Operator.TERNARY, ternaryOperands);
}