Package com.google.caja.parser.js

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


    return "number".equals(e.typeOf());
  }

  private static void optimize(ParseTreeNode node, ScopeTree t) {
    if (node instanceof Operation) {
      Operation op = (Operation) node;
      if (op.getOperator() == Operator.SQUARE_BRACKET) {
        Expression index = op.children().get(1);
        Set<String> expanding = new HashSet<String>();
        if (isVisiblePropertyExpr(index, t, expanding)) {
          Operation numIndex = Operation.create(
              index.getFilePosition(), Operator.TO_NUMBER, index);
          numIndex.setFilePosition(index.getFilePosition());
          op.replaceChild(numIndex, index);
        }
      }
    }
    for (ParseTreeNode child : node.children()) {
View Full Code Here


          return false;
        }
        // If it's the LHS of an assignment, check the RHS
        if (gp.node instanceof Operation
            && use.parent.node == gp.node.children().get(0)) {
          Operation operation = gp.cast(Operation.class).node;
          Operator op = operation.getOperator();
          if (op == Operator.ASSIGN) {
            if (!isVisiblePropertyExpr(
                    operation.children().get(1), scopeTree,
                    identifiersExpanding)) {
              return false;
            }
          } else if (op.getAssignmentDelegate() != null) {
            if (!isNumberOrUndefOperator(op.getAssignmentDelegate())) {
View Full Code Here

   */
  static boolean isVisiblePropertyExpr(
      Expression e, ScopeTree scopeTree, Set<String> identifiersExpanding) {
    if (e instanceof NumberLiteral) { return true; }
    if (e instanceof Operation) {
      Operation op = (Operation) e;
      switch (op.getOperator()) {
        case COMMA:
          Expression last = op.children().get(1);
          return isVisiblePropertyExpr(last, scopeTree, identifiersExpanding);
        // || and && pass through one of their operands unchanged.
        // The addition operator works as follows:
        // 11.6.1 Additive Operator
        //   ...
        //   4. Call GetValue(Result(3)).
        //   5. Call ToPrimitive(Result(2)).
        //   6. Call ToPrimitive(Result(4)).
        //   7. If Type(Result(5)) is String or Type(Result(6)) is String, go to
        //      step 12. (Note that this step differs from step 3 in the
        //      comparison algorithm for the relational operators, by using or
        //      instead of and.)
        //   8. Call ToNumber(Result(5)).
        //   9. Call ToNumber(Result(6)).
        //   ...
        // which means that (undefined + undefined) is a number, and so if both
        // operands are undefined or numeric, the result is guaranteed to be
        // numeric.
        case LOGICAL_OR: case LOGICAL_AND: case ADDITION:
          return isVisiblePropertyExpr(
              op.children().get(0), scopeTree, identifiersExpanding)
              && isVisiblePropertyExpr(
              op.children().get(1), scopeTree, identifiersExpanding);
        case TERNARY:
          return isVisiblePropertyExpr(
              op.children().get(1), scopeTree, identifiersExpanding)
              && isVisiblePropertyExpr(
              op.children().get(2), scopeTree, identifiersExpanding);
        case ASSIGN:
          return isVisiblePropertyExpr(
              op.children().get(1), scopeTree, identifiersExpanding);
        default:
          if (isNumberOrUndefOperator(op.getOperator())) {
            return true;
          }
          break;
      }
    } else if (e instanceof Reference) {
View Full Code Here

            RewriterMessageType.CANNOT_ASSIGN_TO_FREE_VARIABLE,
            operand.getFilePosition(), this, operand);
      }
      return sideEffectlessReadAssignOperand(operand, scope);
    } else if (operand instanceof Operation) {
      Operation op = (Operation) operand;
      switch (op.getOperator()) {
        case SQUARE_BRACKET:
          return sideEffectingReadAssignOperand(
              op.children().get(0), op.children().get(1), scope);
        case MEMBER_ACCESS:
          return sideEffectingReadAssignOperand(
              op.children().get(0), toStringLiteral(op.children().get(1)),
              scope);
        default: break;
      }
    }
    throw new IllegalArgumentException("Not an lvalue : " + operand);
View Full Code Here

          "@tmpVar = @right;",
          "tmpVar", tmpVar,
          "right", rightExpanded));
    }

    Operation propertyAccess = null;
    if (key instanceof StringLiteral) {
      // Make sure that cases like
      //   arr.length -= 1
      // optimize arr.length in the right-hand-side usage.
      // See the array length case in testSetReadModifyWriteLocalVar.
View Full Code Here

      return temporaries.isEmpty() && cajoled.isLeftHandSide()
          && cajoled instanceof Reference;
    }

    public Operation makeAssignment(Expression rhs) {
      Operation e = Operation.createInfix(Operator.ASSIGN, this.uncajoled, rhs);
      rewriter.setTaint(e);
      return e;
    }
View Full Code Here

    if (!(ac.parent.node instanceof Reference)) { return null; }
    AncestorChain<?> grandparent = ac.parent.parent;
    if (grandparent == null) { return null; }
    if (grandparent.node instanceof Operation) {
      // Handles ++ac, ac += ..., ac = ...
      Operation op = grandparent.cast(Operation.class).node;
      Operator operator = op.getOperator();
      return (operator.getCategory() == OperatorCategory.ASSIGNMENT
              && ac.parent.node == op.children().get(0)) ? operator : null;
    } else if (grandparent.node instanceof ExpressionStmt
               && grandparent.parent != null
               && grandparent.parent.node instanceof ForEachLoop) {
      // Handle
      //    for (k in obj) { ... }
View Full Code Here

    // Normalize "foo" + "bar" -> "foo bar"
    tests.acceptPostOrder(new Visitor() {
      @Override
      public boolean visit(AncestorChain<?> chain) {
        if (Operation.is(chain.node, Operator.ADDITION)) {
          Operation op = chain.cast(Operation.class).node;
          Expression left = op.children().get(0);
          Expression right = op.children().get(1);
          if (left instanceof StringLiteral && right instanceof StringLiteral) {
            StringLiteral concatenation = StringLiteral.valueOf(
                FilePosition.span(
                    left.getFilePosition(), right.getFilePosition()),
                ((StringLiteral) left).getUnquotedValue()
                + ((StringLiteral) right).getUnquotedValue());
            chain.parent.cast(MutableParseTreeNode.class).node.replaceChild(
                concatenation, op);
          }
        }
        return true;
      }
    }, null);

    AssertionFailedError failure = null;

    // InputSource for file positions in error message goldens.
    is = new InputSource(new URI("http://example.org/test"));

    // Extract the JSON style-object from the call.
    assertTrue(render(tests), Operation.is(tests, Operator.FUNCTION_CALL));
    Operation call = (Operation) tests;
    assertEquals(2, call.children().size());
    Expression testArray = call.children().get(1);
    // testArray is an array like
    // [{ test_name: ..., tests: [] }]
    for (Expression test : ((ArrayConstructor) testArray).children()) {
      ObjectConstructor obj = (ObjectConstructor) test;
      String name = (String)
View Full Code Here

TOP

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

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.