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

The following examples show how to use com.google.javascript.rhino.jstype.TernaryValue#TRUE . 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: jMutRepair_003_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 2
Source File: Cardumen_0014_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: jKali_003_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: 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 5
Source File: jKali_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 6
Source File: jKali_003_t.java    From coming with MIT License 5 votes vote down vote up
static String trimJsWhiteSpace(String s) {
  int start = 0;
  int end = s.length();
  while (end > 0
      && isStrWhiteSpaceChar(s.charAt(end - 1)) == TernaryValue.TRUE) {
    end--;
  }
  while (start < end
      && isStrWhiteSpaceChar(s.charAt(start)) == TernaryValue.TRUE) {
    start++;
  }
  return s.substring(start, end);
}
 
Example 7
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove always true loop conditions.
 */
private void tryFoldForCondition(Node forCondition) {
  if (NodeUtil.getPureBooleanValue(forCondition) == TernaryValue.TRUE) {
    forCondition.getParent().replaceChild(forCondition,
        IR.empty());
    reportCodeChange();
  }
}
 
Example 8
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
static String trimJsWhiteSpace(String s) {
  int start = 0;
  int end = s.length();
  while (end > 0
      && isStrWhiteSpaceChar(s.charAt(end - 1)) == TernaryValue.TRUE) {
    end--;
  }
  while (start < end
      && isStrWhiteSpaceChar(s.charAt(start)) == TernaryValue.TRUE) {
    start++;
  }
  return s.substring(start, end);
}
 
Example 9
Source File: Cardumen_00149_t.java    From coming with MIT License 5 votes vote down vote up
static String trimJsWhiteSpace(String s) {
  int start = 0;
  int end = s.length();
  while (end > 0
      && isStrWhiteSpaceChar(s.charAt(end - 1)) == TernaryValue.TRUE) {
    end--;
  }
  while (start < end
      && isStrWhiteSpaceChar(s.charAt(start)) == TernaryValue.TRUE) {
    start++;
  }
  return s.substring(start, end);
}
 
Example 10
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 5 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.
 */
static TernaryValue getBooleanValue(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 getBooleanValue(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.ARRAYLIT:
    case Token.OBJECTLIT:
    case Token.REGEXP:
      return TernaryValue.TRUE;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 11
Source File: Cardumen_0087_t.java    From coming with MIT License 5 votes vote down vote up
static String trimJsWhiteSpace(String s) {
  int start = 0;
  int end = s.length();
  while (end > 0
      && isStrWhiteSpaceChar(s.charAt(end - 1)) == TernaryValue.TRUE) {
    end--;
  }
  while (start < end
      && isStrWhiteSpaceChar(s.charAt(start)) == TernaryValue.TRUE) {
    start++;
  }
  return s.substring(start, end);
}
 
Example 12
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 5 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.
 */
static TernaryValue getBooleanValue(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 getBooleanValue(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.ARRAYLIT:
    case Token.OBJECTLIT:
    case Token.REGEXP:
      return TernaryValue.TRUE;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 13
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 5 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.
 */
static TernaryValue getBooleanValue(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.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.ARRAYLIT:
    case Token.OBJECTLIT:
    case Token.REGEXP:
      return TernaryValue.TRUE;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 14
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 5 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.
 */
static TernaryValue getBooleanValue(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.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.ARRAYLIT:
    case Token.OBJECTLIT:
    case Token.REGEXP:
      return TernaryValue.TRUE;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 15
Source File: Closure_60_NodeUtil_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:
        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;
      }
      break;
  }

  return TernaryValue.UNKNOWN;
}
 
Example 16
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 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
 * getImpureBooleanValue 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: 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 18
Source File: jKali_003_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 19
Source File: Cardumen_0014_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 20
Source File: jMutRepair_003_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;
}