Java Code Examples for com.google.javascript.rhino.IR#number()

The following examples show how to use com.google.javascript.rhino.IR#number() . 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: Cardumen_00200_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 2
Source File: jMutRepair_003_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 3
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 4
Source File: Cardumen_0087_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 5
Source File: Cardumen_0087_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 6
Source File: Cardumen_0014_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 7
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 8
Source File: Cardumen_00149_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 9
Source File: Cardumen_00149_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @return An appropriate AST node for the double value.
 */
static Node numberNode(double value, Node srcref) {
  Node result;
  if (Double.isNaN(value)) {
    result = IR.name("NaN");
  } else if (value == Double.POSITIVE_INFINITY) {
    result = IR.name("Infinity");
  } else if (value == Double.NEGATIVE_INFINITY) {
    result = IR.neg(IR.name("Infinity"));
  } else {
    result = IR.number(value);
  }
  if (srcref != null) {
    result.srcrefTree(srcref);
  }
  return result;
}
 
Example 10
Source File: ProcessTweaks.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
Node createDefaultValueNode() {
  switch (this) {
    case REGISTER_BOOLEAN:
      return IR.falseNode();
    case REGISTER_NUMBER:
      return IR.number(0);
    case REGISTER_STRING:
      return IR.string("");
    default:
      throw new IllegalStateException();
  }
}
 
Example 11
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 12
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 13
Source File: PeepholeReplaceKnownMethods.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to evaluate String.indexOf/lastIndexOf:
 *     "abcdef".indexOf("bc") -> 1
 *     "abcdefbc".indexOf("bc", 3) -> 6
 */
private Node tryFoldStringIndexOf(
    Node n, String functionName, Node lstringNode, Node firstArg) {
  Preconditions.checkArgument(n.isCall());
  Preconditions.checkArgument(lstringNode.isString());

  String lstring = NodeUtil.getStringValue(lstringNode);
  boolean isIndexOf = functionName.equals("indexOf");
  Node secondArg = firstArg.getNext();
  String searchValue = NodeUtil.getStringValue(firstArg);
  // searchValue must be a valid string.
  if (searchValue == null) {
    return n;
  }
  int fromIndex = isIndexOf ? 0 : lstring.length();
  if (secondArg != null) {
    // Third-argument and non-numeric second arg are problematic. Discard.
    if (secondArg.getNext() != null || !secondArg.isNumber()) {
      return n;
    } else {
      fromIndex = (int) secondArg.getDouble();
    }
  }
  int indexVal = isIndexOf ? lstring.indexOf(searchValue, fromIndex)
                           : lstring.lastIndexOf(searchValue, fromIndex);
  Node newNode = IR.number(indexVal);
  n.getParent().replaceChild(n, newNode);

  reportCodeChange();

  return newNode;
}
 
Example 14
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 15
Source File: PeepholeReplaceKnownMethods.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to fold .charCodeAt() calls on strings
 */
private Node tryFoldStringCharCodeAt(Node n, Node stringNode, Node arg1) {
  Preconditions.checkArgument(n.isCall());
  Preconditions.checkArgument(stringNode.isString());

  int index;
  String stringAsString = stringNode.getString();

  if (arg1 != null && arg1.isNumber()
      && arg1.getNext() == null) {
    index = (int) arg1.getDouble();
  } else {
    return n;
  }

  if (index < 0 || stringAsString.length() <= index) {
    // http://es5.github.com/#x15.5.4.5 says NaN is returned when index is
    // out of bounds but we bail.
    return n;
  }

  Node resultNode = IR.number(stringAsString.charAt(index));
  Node parent = n.getParent();
  parent.replaceChild(n, resultNode);
  reportCodeChange();
  return resultNode;
}
 
Example 16
Source File: Closure_23_PeepholeFoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to fold array-length. e.g [1, 2, 3].length ==> 3, [x, y].length ==> 2
 */
private Node tryFoldGetProp(Node n, Node left, Node right) {
  Preconditions.checkArgument(n.isGetProp());

  if (left.isObjectLit()) {
    return tryFoldObjectPropAccess(n, left, right);
  }

  if (right.isString() &&
      right.getString().equals("length")) {
    int knownLength = -1;
    switch (left.getType()) {
      case Token.ARRAYLIT:
        if (mayHaveSideEffects(left)) {
          // Nope, can't fold this, without handling the side-effects.
          return n;
        }
        knownLength = left.getChildCount();
        break;
      case Token.STRING:
        knownLength = left.getString().length();
        break;
      default:
        // Not a foldable case, forget it.
        return n;
    }

    Preconditions.checkState(knownLength != -1);
    Node lengthNode = IR.number(knownLength);
    n.getParent().replaceChild(n, lengthNode);
    reportCodeChange();

    return lengthNode;
  }

  return n;
}
 
Example 17
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 18
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 19
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Node tryFoldUnaryOperator(Node n) {
  Preconditions.checkState(n.hasOneChild());

  Node left = n.getFirstChild();
  Node parent = n.getParent();

  if (left == null) {
    return n;
  }

  TernaryValue leftVal = NodeUtil.getPureBooleanValue(left);
  if (leftVal == TernaryValue.UNKNOWN) {
    return n;
  }

  switch (n.getType()) {
    case Token.NOT:
      // Don't fold !0 and !1 back to false.
      if (late && left.isNumber()) {
        double numValue = left.getDouble();
        if (numValue == 0 || numValue == 1) {
          return n;
        }
      }
      Node replacementNode = NodeUtil.booleanNode(!leftVal.toBoolean(true));
      parent.replaceChild(n, replacementNode);
      reportCodeChange();
      return replacementNode;
    case Token.POS:
      if (NodeUtil.isNumericResult(left)) {
        // POS does nothing to numeric values.
        parent.replaceChild(n, left.detachFromParent());
        reportCodeChange();
        return left;
      }
      return n;
    case Token.NEG:
      if (left.isName()) {
        if (left.getString().equals("Infinity")) {
          // "-Infinity" is valid and a literal, don't modify it.
          return n;
        } else if (left.getString().equals("NaN")) {
          // "-NaN" is "NaN".
          n.removeChild(left);
          parent.replaceChild(n, left);
          reportCodeChange();
          return left;
        }
      }

      if (left.isNumber()) {
        double negNum = -left.getDouble();

        Node negNumNode = IR.number(negNum);
        parent.replaceChild(n, negNumNode);
        reportCodeChange();
        return negNumNode;
      } else {
        // left is not a number node, so do not replace, but warn the
        // user because they can't be doing anything good
        report(NEGATING_A_NON_NUMBER_ERROR, left);
        return n;
      }
    case Token.BITNOT:
      try {
        double val = left.getDouble();
        if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) {
          int intVal = (int) val;
          if (intVal == val) {
            Node notIntValNode = IR.number(~intVal);
            parent.replaceChild(n, notIntValNode);
            reportCodeChange();
            return notIntValNode;
          } else {
            report(FRACTIONAL_BITWISE_OPERAND, left);
            return n;
          }
        } else {
          report(BITWISE_OPERAND_OUT_OF_RANGE, left);
          return n;
        }
      } catch (UnsupportedOperationException ex) {
        // left is not a number node, so do not replace, but warn the
        // user because they can't be doing anything good
        report(NEGATING_A_NON_NUMBER_ERROR, left);
        return n;
      }
      default:
        return n;
  }
}
 
Example 20
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Try to fold shift operations
 */
private Node tryFoldShift(Node n, Node left, Node right) {
  if (left.isNumber() &&
      right.isNumber()) {

    double result;
    double lval = left.getDouble();
    double rval = right.getDouble();

    // check ranges.  We do not do anything that would clip the double to
    // a 32-bit range, since the user likely does not intend that.
    if (!(lval >= Integer.MIN_VALUE && lval <= Integer.MAX_VALUE)) {
      report(BITWISE_OPERAND_OUT_OF_RANGE, left);
      return n;
    }

    // only the lower 5 bits are used when shifting, so don't do anything
    // if the shift amount is outside [0,32)
    if (!(rval >= 0 && rval < 32)) {
      report(SHIFT_AMOUNT_OUT_OF_BOUNDS, right);
      return n;
    }

    // Convert the numbers to ints
    int lvalInt = (int) lval;
    if (lvalInt != lval) {
      report(FRACTIONAL_BITWISE_OPERAND, left);
      return n;
    }

    int rvalInt = (int) rval;
    if (rvalInt != rval) {
      report(FRACTIONAL_BITWISE_OPERAND, right);
      return n;
    }

    switch (n.getType()) {
      case Token.LSH:
        result = lvalInt << rvalInt;
        break;
      case Token.RSH:
        result = lvalInt >> rvalInt;
        break;
      case Token.URSH:
        // JavaScript handles zero shifts on signed numbers differently than
        // Java as an Java int can not represent the unsigned 32-bit number
        // where JavaScript can so use a long here.
        long lvalLong = lvalInt & 0xffffffffL;
        result = lvalLong >>> rvalInt;
        break;
      default:
        throw new AssertionError("Unknown shift operator: " +
            Token.name(n.getType()));
    }

    Node newNumber = IR.number(result);
    n.getParent().replaceChild(n, newNumber);
    reportCodeChange();

    return newNumber;
  }

  return n;
}