Package com.google.caja.parser.js

Examples of com.google.caja.parser.js.Expression


  private static Expression commaOp(
      FilePosition pos, Expression a, Expression b) {
    while (Operation.is(b, Operator.COMMA)) {
      // (a, (b, c)) -> (a, b, c)
      List<? extends Expression> operands = ((Operation) b).children();
      Expression op0 = operands.get(0);
      a = commaOp(a, op0);
      b = operands.get(1);
    }
    return Operation.create(pos, Operator.COMMA, a, b);
  }
View Full Code Here


          break;
        case LESS_THAN:
        case GREATER_THAN:
          if (fact.isTruthy()) {
            // If (a < b) the a != b, and !(a > b).
            Expression left = operands.get(0);
            Expression right = operands.get(1);
            Operator included = op.getOperator() == Operator.LESS_THAN
                ? Operator.LESS_EQUALS : Operator.GREATER_EQUALS;
            addFactInt(Operation.create(UNK, included, left, right), Fact.TRUE);
            addFactInt(
                Operation.create(UNK, Operator.STRICTLY_NOT_EQUAL, left, right),
                Fact.TRUE);
            // Incomparable values like NaN means we can conclude nothing if
            // !(a < b).
          }
          break;
        case LESS_EQUALS:
        case GREATER_EQUALS:
          if (fact.isFalsey()) {
            // if !(a <= b) then a !== b.
            Expression left = operands.get(0);
            Expression right = operands.get(1);
            addFactInt(
                Operation.create(UNK, Operator.STRICTLY_NOT_EQUAL, left, right),
                Fact.TRUE);
          }
          break;
        case INSTANCE_OF:
          // if (x instanceof y) does not throw, then y must be a function.
          // if it's true, then x must be an object.
          // Note: primitives are not instances of their wrapper class.
          addFactInt(
              Operation.create(UNK, Operator.TYPEOF, operands.get(1)),
              Fact.is(StringLiteral.valueOf(UNK, "function")));
          if (fact.isTruthy()) {
            addFactInt(operands.get(0), Fact.TRUTHY);
          }
          break;
        case EQUAL:
          addFactInt(
              Operation.create(
                  UNK, Operator.NOT_EQUAL, operands.get(0), operands.get(1)),
              fact.isTruthy() ? Fact.FALSE : Fact.TRUE);
          break;
        case NOT_EQUAL:
          addFactInt(
              Operation.create(
                  UNK, Operator.EQUAL, operands.get(0), operands.get(1)),
              fact.isTruthy() ? Fact.FALSE : Fact.TRUE);
          break;
        case STRICTLY_EQUAL:
          if (fact.isTruthy()) {
            Expression lhs = operands.get(0);
            Expression rhs = operands.get(1);
            addFactInt(Operation.create(UNK, Operator.EQUAL, lhs, rhs),
                       Fact.TRUE);
            if (rhs instanceof Literal) {
              // TODO(mikesamuel): what do we do about the fact that (0 === -0)?
              // Instead of inferring that the value IS 0, we could infer that
              // it's falsey, and the typeof is number.
              addFactInt(lhs, Fact.is((Literal) rhs));

            // (this.global === this) -> global aliases the global object.
            } else if (isThis(rhs) && lhs instanceof Reference) {
              addFactInt(lhs, Fact.GLOBAL);
            } else {
              String typeOf = rhs.typeOf();
              if (typeOf != null && lhs.typeOf() == null) {
                addFactInt(
                    Operation.create(UNK, Operator.TYPEOF, lhs),
                    Fact.is(StringLiteral.valueOf(UNK, typeOf)));
              }
              // TODO(mikesamuel): Is this useful?  When, in a comparison,
              // do we know that something is truthy or falsey, but not what
              // literal value it is.  The expressions:
              //    x === function () {}
              //    y === [1,2,3]
              //    z === {}
              // are never true, so control wouldn't reach here.
              Boolean truthiness = rhs.conditionResult();
              if (truthiness != null) {
                addFuzzyFact(lhs, truthiness);
              }
            }
          } else {
            Expression lhs = operands.get(0);
            Expression rhs = operands.get(1);
            if (ParseTreeNodes.deepEquals(lhs, rhs)) {
              addFactInt(lhs, Fact.is(new RealLiteral(UNK, Double.NaN)));
            }
          }
          addFactInt(
              Operation.create(
                  UNK, Operator.STRICTLY_NOT_EQUAL,
                  operands.get(0), operands.get(1)),
              fact.isTruthy() ? Fact.FALSE : Fact.TRUE);
          break;
        case STRICTLY_NOT_EQUAL:
          addFactInt(Operation.create(
              UNK, Operator.STRICTLY_EQUAL, operands.get(0), operands.get(1)),
              fact.isTruthy() ? Fact.FALSE : Fact.TRUE);
          break;
        case LOGICAL_AND:
        case LOGICAL_OR:
          boolean isAnd = op.getOperator() == Operator.LOGICAL_AND;
          if (fact.isTruthy() == isAnd) {
            addFuzzyFact(operands.get(0), isAnd);
            addFactInt(operands.get(1), fact)// Second value is result
          }
          break;
        case MEMBER_ACCESS:
        case SQUARE_BRACKET:
          // If foo.bar is truthy, then so is foo.
          if (fact.isTruthy()) {
            addFuzzyFact(operands.get(0), true);
          }
          break;
        case TYPEOF:
          if (fact.type == Fact.Type.IS
              && fact.value instanceof StringLiteral) {
            String s = ((StringLiteral) fact.value).getUnquotedValue();
            Expression op0 = operands.get(0);
            if ("undefined".equals(s)) {
              addFactInt(op0, Fact.UNDEFINED);
            } else {
              if ("function".equals(s)) { addFactInt(op0, Fact.TRUTHY); }
              // undefined is a commonly tested value, so infer its absence
View Full Code Here

      }
    }
    if (!globals.isEmpty()) {
      for (Pair<Expression, Fact> fe : factList) {
        if (fe.b == Fact.GLOBAL) { continue; }
        Expression e = fe.a;
        Operator op = null;
        if (Operation.is(e, Operator.TYPEOF)) {
          op = Operator.TYPEOF;
          e = (Expression) e.children().get(0);
        }
        String topRef = topRef(e);
        if (topRef != null) {
          if (!globals.contains(topRef)) {
            for (String globalAlias : globals) {
              Expression newExpr = withTopRef(e, globalAlias);
              if (op != null) {
                newExpr = Operation.create(UNK, op, newExpr);
              }
              addFactInt(newExpr, fe.b);
            }
          } else if (op == null && e instanceof Operation) {
            // Simplify the fact unless it is falsey and not false.
            // E.g. global.foo   IS   4
            //   -> foo          IS   4
            // but  global.foo   IS   undefined
            //   !> foo          IS   undefined
            // because foo could result in an Error.
            if (fe.b.isFalse() || fe.b.isTruthy()) {
              Expression newExpr = withoutTopRef(e);
              addFactInt(newExpr, fe.b);
            }
          }
        }
      }
View Full Code Here

    } else {
      digest = null;
    }

    if (node instanceof Expression) {
      Expression folded = normNum(((Expression) node).fold(isFn));
      if (folded != node) {
        node = folded;
        digest = optNodeDigest(folded);
      }
    }
View Full Code Here

          ? ((Expression) newChild).conditionResult() : null;
      if (optCond != null || child != newChild) {
        if (newChildren == null) { newChildren = Lists.newArrayList(n); }
        newChildren.addAll(children.subList(nEmitted, i));
        if (optCond != null) {  // The condition is known, so we can remove it.
          Expression sideEffect = ((Expression) newChild)
              .simplifyForSideEffect();
          if (sideEffect == null) {
            if (!optCond) {
              nEmitted = i = i + 2// Skip the condition and its clause
              // if (false) { foo } else if (...) bar  =>  if (...) bar
View Full Code Here

  private void optimizeMemberAccess(
      Scope s, Operation ma, boolean isFuzzy, boolean isLhs,
      boolean throwsOnUndefined, Result out) {
    StringBuilder sb = new StringBuilder();
    sb.append('(');
    Expression obj = ma.children().get(0);
    optimize(s, obj, false, false, false, false, out);
    Reference prop = (Reference) ma.children().get(1);
    if (out.node != obj) {
      ma = Operation.createInfix(
          Operator.MEMBER_ACCESS, (Expression) out.node, prop);
View Full Code Here

      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;
    }
View Full Code Here

  }

  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);
  }
View Full Code Here

      Operation o = (Operation) e;
      List<? extends Expression> children = o.children();
      Expression[] newChildren = null;
      boolean oIsFn = o.getOperator() == Operator.FUNCTION_CALL;
      for (int i = 0, n = children.size(); i < n; ++i) {
        Expression operand = children.get(i);
        Expression newOperand = rfold(operand, oIsFn && i == 0);
        if (operand == newOperand) { continue; }
        if (newChildren == null) {
          newChildren = children.toArray(new Expression[n]);
        }
        newChildren[i] = newOperand;
View Full Code Here

      for (ObjProperty p : ((ObjectConstructor) e).children()) {
        last = liveness(p.children().get(1), last).vars;
      }
    } else {
      for (ParseTreeNode child : e.children()) {
        Expression childE = (Expression) child;
        last = liveness(childE, last).vars;
      }
    }
    return last;
  }
View Full Code Here

TOP

Related Classes of com.google.caja.parser.js.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.