Java Code Examples for com.google.javascript.rhino.jstype.TernaryValue#UNKNOWN

The following examples show how to use com.google.javascript.rhino.jstype.TernaryValue#UNKNOWN . 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: Closure_60_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Copied from Rhino's ScriptRuntime
 */
static TernaryValue isStrWhiteSpaceChar(int c) {
  switch (c) {
    case '\u000B': // <VT>
      return TernaryValue.UNKNOWN;  // IE says "no", EcmaScript says "yes"
    case ' ': // <SP>
    case '\n': // <LF>
    case '\r': // <CR>
    case '\t': // <TAB>
    case '\u00A0': // <NBSP>
    case '\u000C': // <FF>
    case '\u2028': // <LS>
    case '\u2029': // <PS>
    case '\uFEFF': // <BOM>
      return TernaryValue.TRUE;
    default:
      return (Character.getType(c) == Character.SPACE_SEPARATOR)
          ? TernaryValue.TRUE : TernaryValue.FALSE;
  }
}
 
Example 2
Source File: jMutRepair_003_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Copied from Rhino's ScriptRuntime
 */
public static TernaryValue isStrWhiteSpaceChar(int c) {
  switch (c) {
    case '\u000B': // <VT>
      return TernaryValue.UNKNOWN;  // IE says "no", ECMAScript says "yes"
    case ' ': // <SP>
    case '\n': // <LF>
    case '\r': // <CR>
    case '\t': // <TAB>
    case '\u00A0': // <NBSP>
    case '\u000C': // <FF>
    case '\u2028': // <LS>
    case '\u2029': // <PS>
    case '\uFEFF': // <BOM>
      return TernaryValue.TRUE;
    default:
      return (Character.getType(c) == Character.SPACE_SEPARATOR)
          ? TernaryValue.TRUE : TernaryValue.FALSE;
  }
}
 
Example 3
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Copied from Rhino's ScriptRuntime
 */
public static TernaryValue isStrWhiteSpaceChar(int c) {
  switch (c) {
    case '\u000B': // <VT>
      return TernaryValue.UNKNOWN;  // IE says "no", ECMAScript says "yes"
    case ' ': // <SP>
    case '\n': // <LF>
    case '\r': // <CR>
    case '\t': // <TAB>
    case '\u00A0': // <NBSP>
    case '\u000C': // <FF>
    case '\u2028': // <LS>
    case '\u2029': // <PS>
    case '\uFEFF': // <BOM>
      return TernaryValue.TRUE;
    default:
      return (Character.getType(c) == Character.SPACE_SEPARATOR)
          ? TernaryValue.TRUE : TernaryValue.FALSE;
  }
}
 
Example 4
Source File: Cardumen_00200_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Copied from Rhino's ScriptRuntime
 */
public static TernaryValue isStrWhiteSpaceChar(int c) {
  switch (c) {
    case '\u000B': // <VT>
      return TernaryValue.UNKNOWN;  // IE says "no", ECMAScript says "yes"
    case ' ': // <SP>
    case '\n': // <LF>
    case '\r': // <CR>
    case '\t': // <TAB>
    case '\u00A0': // <NBSP>
    case '\u000C': // <FF>
    case '\u2028': // <LS>
    case '\u2029': // <PS>
    case '\uFEFF': // <BOM>
      return TernaryValue.TRUE;
    default:
      return (Character.getType(c) == Character.SPACE_SEPARATOR)
          ? TernaryValue.TRUE : TernaryValue.FALSE;
  }
}
 
Example 5
Source File: Cardumen_0014_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
    case Token.STRING_KEY:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 6
Source File: Cardumen_00200_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a Number, or null if it cannot be converted.
 * When it returns a non-null Double, this method effectively emulates the
 * <code>Number()</code> JavaScript cast function.
 */
static Double getNumberValue(Node n) {
  switch (n.getType()) {
    case Token.TRUE:
      return 1.0;

    case Token.FALSE:
    case Token.NULL:
      return 0.0;

    case Token.NUMBER:
      return n.getDouble();

    case Token.VOID:
      if (mayHaveSideEffects(n.getFirstChild())) {
        return null;
      } else {
        return Double.NaN;
      }

    case Token.NAME:
      // Check for known constants
      String name = n.getString();
      if (name.equals("undefined")) {
        return Double.NaN;
      }
      if (name.equals("NaN")) {
        return Double.NaN;
      }
      if (name.equals("Infinity")) {
        return Double.POSITIVE_INFINITY;
      }
      return null;

    case Token.NEG:
      if (n.getChildCount() == 1 && n.getFirstChild().isName()
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
      }
      break;

    case Token.STRING:
      return getStringNumberValue(n.getString());

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      String value = getStringValue(n);
      return value != null ? getStringNumberValue(value) : null;
  }

  return null;
}
 
Example 7
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 8
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
    case Token.STRING_KEY:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 9
Source File: Cardumen_00200_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
    case Token.STRING_KEY:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 10
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
    case Token.TRUE:
    case Token.NULL:
      return Node.tokenToName(n.getType());

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 11
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets the value of a node as a Number, or null if it cannot be converted.
 * When it returns a non-null Double, this method effectively emulates the
 * <code>Number()</code> JavaScript cast function.
 */
static Double getNumberValue(Node n) {
  switch (n.getType()) {
    case Token.TRUE:
      return 1.0;

    case Token.FALSE:
    case Token.NULL:
      return 0.0;

    case Token.NUMBER:
      return n.getDouble();

    case Token.VOID:
      if (mayHaveSideEffects(n.getFirstChild())) {
        return null;
      } else {
        return Double.NaN;
      }

    case Token.NAME:
      // Check for known constants
      String name = n.getString();
      if (name.equals("undefined")) {
        return Double.NaN;
      }
      if (name.equals("NaN")) {
        return Double.NaN;
      }
      if (name.equals("Infinity")) {
        return Double.POSITIVE_INFINITY;
      }
      return null;

    case Token.NEG:
      if (n.getChildCount() == 1 && n.getFirstChild().getType() == Token.NAME
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
      }
      break;

    case Token.STRING:
      return getStringNumberValue(n.getString());

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      String value = getStringValue(n);
      return value != null ? getStringNumberValue(value) : null;
  }

  return null;
}
 
Example 12
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the boolean value of a node that represents a literal. This method
 * effectively emulates the <code>Boolean()</code> JavaScript cast function
 * except it return UNKNOWN for known values with side-effects, use
 * getExpressionBooleanValue if you don't care about side-effects.
 */
static TernaryValue getPureBooleanValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
      return TernaryValue.forBoolean(n.getString().length() > 0);

    case Token.NUMBER:
      return TernaryValue.forBoolean(n.getDouble() != 0);

    case Token.NOT:
      return getPureBooleanValue(n.getLastChild()).not();

    case Token.NULL:
    case Token.FALSE:
      return TernaryValue.FALSE;

    case Token.VOID:
      if (!mayHaveSideEffects(n.getFirstChild())) {
        return TernaryValue.FALSE;
      }
      break;

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "NaN".equals(name)) {
        // We assume here that programs don't change the value of the keyword
        // undefined to something other than the value undefined.
        return TernaryValue.FALSE;
      } else if ("Infinity".equals(name)) {
        return TernaryValue.TRUE;
      }
      break;

    case Token.TRUE:
    case Token.REGEXP:
      return TernaryValue.TRUE;

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      if (!mayHaveSideEffects(n)) {
        return TernaryValue.TRUE;
      }
      break;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 13
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
    case Token.STRING_KEY:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 14
Source File: Closure_97_PeepholeFoldConstants_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  } else {
    TernaryValue rightVal = NodeUtil.getBooleanValue(right);
    if (rightVal != TernaryValue.UNKNOWN) {

    // Note: We cannot always fold when the constant is on the
    // right, because the typed value of the expression will depend
    // on the type of the constant on the right, even if the boolean
    // equivalent of the value does not. Specifically, in "a = x ||
    // 0", a will be numeric 0 if x is undefined (and hence is
    // e.g. a valid array index). However, it is safe to fold
    // e.g. "if (x || true)" because 'if' doesn't care if the
    // expression is 'true' or '3'.
    int pt = parent.getType();
    if (pt == Token.IF || pt == Token.WHILE || pt == Token.DO ||
        (pt == Token.FOR && NodeUtil.getConditionExpression(parent) == n) ||
        (pt == Token.HOOK && parent.getFirstChild() == n)) {
      boolean rval = rightVal.toBoolean(true);

      // (x || FALSE) => x
      // (x && TRUE) => x
      if (type == Token.OR && !rval ||
          type == Token.AND && rval) {
        result = left;
      } else {
        // If x has no side-effects:
        //   (x || TRUE) => TRUE
        //   (x && FALSE) => FALSE
        if (!NodeUtil.mayHaveSideEffects(left)) {
          result = right;
        }
      }
      }
    }
  }

  // Note: The parser parses 'x && FALSE && y' as 'x && (FALSE && y)', so
  // there is not much need to worry about const values on left's
  // right child.

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 15
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Remove useless switches and cases.
 */
private Node tryOptimizeSwitch(Node n) {
  Preconditions.checkState(n.isSwitch());

  Node defaultCase = tryOptimizeDefaultCase(n);

  // Removing cases when there exists a default case is not safe.
  if (defaultCase == null) {
    Node cond = n.getFirstChild(), prev = null, next = null, cur;

    for (cur = cond.getNext(); cur != null; cur = next) {
      next = cur.getNext();
      if (!mayHaveSideEffects(cur.getFirstChild()) &&
          isUselessCase(cur, prev)) {
        removeCase(n, cur);
      } else {
        prev = cur;
      }
    }

    // Optimize switches with constant condition
    if (NodeUtil.isLiteralValue(cond, false)) {
      Node caseLabel;
      TernaryValue caseMatches = TernaryValue.TRUE;
      // Remove cases until you find one that may match
      for (cur = cond.getNext(); cur != null; cur = next) {
        next = cur.getNext();
        caseLabel = cur.getFirstChild();
        caseMatches = PeepholeFoldConstants.evaluateComparison(
            Token.SHEQ, cond, caseLabel);
        if (caseMatches == TernaryValue.TRUE) {
          break;
        } else if (caseMatches == TernaryValue.UNKNOWN) {
          break;
        } else {
          removeCase(n, cur);
        }
      }
      if (caseMatches != TernaryValue.UNKNOWN) {
        Node block, lastStm;
        // Skip cases until you find one whose last stm is a break
        while (cur != null) {
          block = cur.getLastChild();
          lastStm = block.getLastChild();
          cur = cur.getNext();
          if (lastStm != null && lastStm.isBreak()) {
            block.removeChild(lastStm);
            reportCodeChange();
            break;
          }
        }
        // Remove any remaining cases
        for (; cur != null; cur = next) {
          next = cur.getNext();
          removeCase(n, cur);
        }
        // If there is one case left, we may be able to fold it
        cur = cond.getNext();
        if (cur != null && cur.getNext() == null) {
          block = cur.getLastChild();
          if (!(NodeUtil.containsType(block, Token.BREAK,
              NodeUtil.MATCH_NOT_FUNCTION))) {
            cur.removeChild(block);
            n.getParent().replaceChild(n, block);
            reportCodeChange();
            return block;
          }
        }
      }
    }
  }

  // Remove the switch if there are no remaining cases.
  if (n.hasOneChild()) {
    Node condition = n.removeFirstChild();
    Node replacement = IR.exprResult(condition).srcref(n);
    n.getParent().replaceChild(n, replacement);
    reportCodeChange();
    return replacement;
  }

  return null;
}
 
Example 16
Source File: Cardumen_0087_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the boolean value of a node that represents a literal. This method
 * effectively emulates the <code>Boolean()</code> JavaScript cast function
 * except it return UNKNOWN for known values with side-effects, use
 * getExpressionBooleanValue if you don't care about side-effects.
 */
static TernaryValue getPureBooleanValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
      return TernaryValue.forBoolean(n.getString().length() > 0);

    case Token.NUMBER:
      return TernaryValue.forBoolean(n.getDouble() != 0);

    case Token.NOT:
      return getPureBooleanValue(n.getLastChild()).not();

    case Token.NULL:
    case Token.FALSE:
      return TernaryValue.FALSE;

    case Token.VOID:
      if (!mayHaveSideEffects(n.getFirstChild())) {
        return TernaryValue.FALSE;
      }
      break;

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "NaN".equals(name)) {
        // We assume here that programs don't change the value of the keyword
        // undefined to something other than the value undefined.
        return TernaryValue.FALSE;
      } else if ("Infinity".equals(name)) {
        return TernaryValue.TRUE;
      }
      break;

    case Token.TRUE:
    case Token.REGEXP:
      return TernaryValue.TRUE;

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      if (!mayHaveSideEffects(n)) {
        return TernaryValue.TRUE;
      }
      break;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 17
Source File: Cardumen_00149_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a String, or null if it cannot be converted.
 * When it returns a non-null String, this method effectively emulates the
 * <code>String()</code> JavaScript cast function.
 */
static String getStringValue(Node n) {
  // TODO(user): regex literals as well.
  switch (n.getType()) {
    case Token.STRING:
    case Token.STRING_KEY:
      return n.getString();

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "Infinity".equals(name)
          || "NaN".equals(name)) {
        return name;
      }
      break;

    case Token.NUMBER:
      return getStringValue(n.getDouble());

    case Token.FALSE:
      return "false";

    case Token.TRUE:
      return "true";

    case Token.NULL:
      return "null";

    case Token.VOID:
      return "undefined";

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? "false" : "true"; // reversed.
      }
      break;

    case Token.ARRAYLIT:
      return arrayToString(n);

    case Token.OBJECTLIT:
      return "[object Object]";
  }
  return null;
}
 
Example 18
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the value of a node as a Number, or null if it cannot be converted.
 * When it returns a non-null Double, this method effectively emulates the
 * <code>Number()</code> JavaScript cast function.
 */
static Double getNumberValue(Node n) {
  switch (n.getType()) {
    case Token.TRUE:
      return 1.0;

    case Token.FALSE:
    case Token.NULL:
      return 0.0;

    case Token.NUMBER:
      return n.getDouble();

    case Token.VOID:
      if (mayHaveSideEffects(n.getFirstChild())) {
        return null;
      } else {
        return Double.NaN;
      }

    case Token.NAME:
      // Check for known constants
      String name = n.getString();
      if (name.equals("undefined")) {
        return Double.NaN;
      }
      if (name.equals("NaN")) {
        return Double.NaN;
      }
      if (name.equals("Infinity")) {
        return Double.POSITIVE_INFINITY;
      }
      return null;

    case Token.NEG:
      if (n.getChildCount() == 1 && n.getFirstChild().getType() == Token.NAME
          && n.getFirstChild().getString().equals("Infinity")) {
        return Double.NEGATIVE_INFINITY;
      }
      return null;

    case Token.NOT:
      TernaryValue child = getPureBooleanValue(n.getFirstChild());
      if (child != TernaryValue.UNKNOWN) {
        return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
      }
      break;

    case Token.STRING:
      return getStringNumberValue(n.getString());

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      String value = getStringValue(n);
      return value != null ? getStringNumberValue(value) : null;
  }

  return null;
}
 
Example 19
Source File: Closure_61_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the boolean value of a node that represents a literal. This method
 * effectively emulates the <code>Boolean()</code> JavaScript cast function
 * except it return UNKNOWN for known values with side-effects, use
 * getExpressionBooleanValue if you don't care about side-effects.
 */
static TernaryValue getPureBooleanValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
      return TernaryValue.forBoolean(n.getString().length() > 0);

    case Token.NUMBER:
      return TernaryValue.forBoolean(n.getDouble() != 0);

    case Token.NOT:
      return getPureBooleanValue(n.getLastChild()).not();

    case Token.NULL:
    case Token.FALSE:
    case Token.VOID:
      return TernaryValue.FALSE;

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "NaN".equals(name)) {
        // We assume here that programs don't change the value of the keyword
        // undefined to something other than the value undefined.
        return TernaryValue.FALSE;
      } else if ("Infinity".equals(name)) {
        return TernaryValue.TRUE;
      }
      break;

    case Token.TRUE:
    case Token.REGEXP:
      return TernaryValue.TRUE;

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      if (!mayHaveSideEffects(n)) {
        return TernaryValue.TRUE;
      }
  }

  return TernaryValue.UNKNOWN;
}
 
Example 20
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Gets the boolean value of a node that represents a literal. This method
 * effectively emulates the <code>Boolean()</code> JavaScript cast function
 * except it return UNKNOWN for known values with side-effects, use
 * getExpressionBooleanValue if you don't care about side-effects.
 */
static TernaryValue getPureBooleanValue(Node n) {
  switch (n.getType()) {
    case Token.STRING:
      return TernaryValue.forBoolean(n.getString().length() > 0);

    case Token.NUMBER:
      return TernaryValue.forBoolean(n.getDouble() != 0);

    case Token.NOT:
      return getPureBooleanValue(n.getLastChild()).not();

    case Token.NULL:
    case Token.FALSE:
      return TernaryValue.FALSE;

    case Token.VOID:
      if (!mayHaveSideEffects(n.getFirstChild())) {
        return TernaryValue.FALSE;
      }
      break;

    case Token.NAME:
      String name = n.getString();
      if ("undefined".equals(name)
          || "NaN".equals(name)) {
        // We assume here that programs don't change the value of the keyword
        // undefined to something other than the value undefined.
        return TernaryValue.FALSE;
      } else if ("Infinity".equals(name)) {
        return TernaryValue.TRUE;
      }
      break;

    case Token.TRUE:
    case Token.REGEXP:
      return TernaryValue.TRUE;

    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      if (!mayHaveSideEffects(n)) {
        return TernaryValue.TRUE;
      }
      break;
  }

  return TernaryValue.UNKNOWN;
}