本文整理汇总了Java中com.google.javascript.rhino.jstype.TernaryValue.toBoolean方法的典型用法代码示例。如果您正苦于以下问题:Java TernaryValue.toBoolean方法的具体用法?Java TernaryValue.toBoolean怎么用?Java TernaryValue.toBoolean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.jstype.TernaryValue
的用法示例。
在下文中一共展示了TernaryValue.toBoolean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: apply
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
@Override
public boolean apply(EdgeTuple<Node, Branch> input) {
Branch branch = input.edge;
if (!branch.isConditional()) {
return true;
}
Node predecessor = input.sourceNode;
Node condition = NodeUtil.getConditionExpression(predecessor);
// TODO(user): Handle more complicated expression like true == true,
// etc....
if (condition != null) {
TernaryValue val = NodeUtil.getBooleanValue(condition);
if (val != TernaryValue.UNKNOWN) {
return val.toBoolean(true) == (branch == Branch.ON_TRUE);
}
}
return true;
}
示例2: apply
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
public boolean apply(DiGraphEdge<Node, ControlFlowGraph.Branch> input) {
// First skill all exceptions.
Branch branch = input.getValue();
if (branch == Branch.ON_EX) {
return false;
} else if (branch.isConditional()) {
Node condition = NodeUtil.getConditionExpression(
input.getSource().getValue());
// TODO(user): We CAN make this bit smarter just looking at
// constants. We DO have a full blown ReverseAbstractInterupter and
// type system that can evaluate some impressions' boolean value but
// for now we will keep this pass lightweight.
if (condition != null) {
TernaryValue val = NodeUtil.getBooleanValue(condition);
if (val != TernaryValue.UNKNOWN) {
return val.toBoolean(true) == (Branch.ON_TRUE == branch);
}
}
}
return true;
}
示例3: apply
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
@Override
public boolean apply(EdgeTuple<Node, Branch> input) {
Branch branch = input.edge;
if (!branch.isConditional()) {
return true;
}
Node predecessor = input.sourceNode;
Node condition = NodeUtil.getConditionExpression(predecessor);
// TODO(user): Handle more complicated expression like true == true,
// etc....
if (condition != null) {
TernaryValue val = NodeUtil.getImpureBooleanValue(condition);
if (val != TernaryValue.UNKNOWN) {
return val.toBoolean(true) == (branch == Branch.ON_TRUE);
}
}
return true;
}
示例4: apply
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
@Override
public boolean apply(DiGraphEdge<Node, ControlFlowGraph.Branch> input) {
// First skill all exceptions.
Branch branch = input.getValue();
if (branch == Branch.ON_EX) {
return false;
} else if (branch.isConditional()) {
Node condition = NodeUtil.getConditionExpression(
input.getSource().getValue());
// TODO(user): We CAN make this bit smarter just looking at
// constants. We DO have a full blown ReverseAbstractInterupter and
// type system that can evaluate some impressions' boolean value but
// for now we will keep this pass lightweight.
if (condition != null) {
TernaryValue val = NodeUtil.getImpureBooleanValue(condition);
if (val != TernaryValue.UNKNOWN) {
return val.toBoolean(true) == (Branch.ON_TRUE == branch);
}
}
}
return true;
}
示例5: tryFoldHook
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* Try folding HOOK (?:) if the condition results of the condition is known.
* @return the replacement node, if changed, or the original if not
*/
private Node tryFoldHook(Node n) {
Preconditions.checkState(n.getType() == Token.HOOK);
Node parent = n.getParent();
Preconditions.checkNotNull(parent);
Node cond = n.getFirstChild();
Node thenBody = cond.getNext();
Node elseBody = thenBody.getNext();
TernaryValue condValue = NodeUtil.getExpressionBooleanValue(cond);
if (condValue == TernaryValue.UNKNOWN) {
return n; // We can't remove branches otherwise!
}
// Transform "(a = 2) ? x =2 : y" into "a=2,x=2"
n.detachChildren();
Node branchToKeep = condValue.toBoolean(true) ? thenBody : elseBody;
Node replacement;
if (mayHaveSideEffects(cond)) {
replacement = new Node(Token.COMMA).copyInformationFrom(n);
replacement.addChildToFront(cond);
replacement.addChildToBack(branchToKeep);
} else {
replacement = branchToKeep;
}
parent.replaceChild(n, replacement);
reportCodeChange();
return replacement;
}
示例6: tryFoldHook
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* Try folding HOOK (?:) if the condition results of the condition is known.
* @return the replacement node, if changed, or the original if not
*/
private Node tryFoldHook(Node n) {
Preconditions.checkState(n.isHook());
Node parent = n.getParent();
Preconditions.checkNotNull(parent);
Node cond = n.getFirstChild();
Node thenBody = cond.getNext();
Node elseBody = thenBody.getNext();
TernaryValue condValue = NodeUtil.getImpureBooleanValue(cond);
if (condValue == TernaryValue.UNKNOWN) {
// If the result nodes are equivalent, then one of the nodes can be
// removed and it doesn't matter which.
if (!areNodesEqualForInlining(thenBody, elseBody)) {
return n; // We can't remove branches otherwise!
}
}
// Transform "(a = 2) ? x =2 : y" into "a=2,x=2"
n.detachChildren();
Node branchToKeep = condValue.toBoolean(true) ? thenBody : elseBody;
Node replacement;
if (mayHaveSideEffects(cond)) {
replacement = IR.comma(cond, branchToKeep).srcref(n);
} else {
replacement = branchToKeep;
}
parent.replaceChild(n, replacement);
reportCodeChange();
return replacement;
}
示例7: tryFoldAndOr
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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.getImpureBooleanValue(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 if (!mayHaveSideEffects(left)) {
// (FALSE || x) => x
// (TRUE && x) => x
result = right;
}
}
// Note: Right hand side folding is handled by
// PeepholeSubstituteAlternateSyntax#tryMinimizeCondition
if (result != null) {
// Fold it!
n.removeChild(result);
parent.replaceChild(n, result);
reportCodeChange();
return result;
} else {
return n;
}
}
示例8: tryFoldHook
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* Try folding HOOK (?:) if the condition results of the condition is known.
* @return the replacement node, if changed, or the original if not
*/
private Node tryFoldHook(Node n) {
Preconditions.checkState(n.isHook());
Node parent = n.getParent();
Preconditions.checkNotNull(parent);
Node cond = n.getFirstChild();
Node thenBody = cond.getNext();
Node elseBody = thenBody.getNext();
TernaryValue condValue = NodeUtil.getImpureBooleanValue(cond);
if (condValue == TernaryValue.UNKNOWN) {
// If the result nodes are equivalent, then one of the nodes can be
// removed and it doesn't matter which.
if (!areNodesEqualForInlining(thenBody, elseBody)) {
return n; // We can't remove branches otherwise!
}
}
// Transform "(a = 2) ? x =2 : y" into "a=2,x=2"
Node branchToKeep = condValue.toBoolean(true) ? thenBody : elseBody;
Node replacement;
boolean condHasSideEffects = mayHaveSideEffects(cond);
// Must detach after checking for side effects, to ensure that the parents
// of nodes are set correctly.
n.detachChildren();
if (condHasSideEffects) {
replacement = IR.comma(cond, branchToKeep).srcref(n);
} else {
replacement = branchToKeep;
}
parent.replaceChild(n, replacement);
reportCodeChange();
return replacement;
}
示例9: tryFoldAndOr
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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.getImpureBooleanValue(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 if (!mayHaveSideEffects(left)) {
// (FALSE || x) => x
// (TRUE && x) => x
result = right;
}
}
// Note: Right hand side folding is handled by
// PeepholeMinimizeConditions#tryMinimizeCondition
if (result != null) {
// Fold it!
n.removeChild(result);
parent.replaceChild(n, result);
reportCodeChange();
return result;
} else {
return n;
}
}
示例10: tryOptimizeConditionalAfterAssign
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* Attempt to replace the condition of if or hook immediately that is a
* reference to a name that is assigned immediately before.
*/
private void tryOptimizeConditionalAfterAssign(Node n) {
Node next = n.getNext();
// Look for patterns like the following and replace the if-condition with
// a constant value so it can later be folded:
// var a = /a/;
// if (a) {foo(a)}
// or
// a = 0;
// a ? foo(a) : c;
// or
// a = 0;
// a || foo(a);
// or
// a = 0;
// a && foo(a)
//
// TODO(johnlenz): This would be better handled by control-flow sensitive
// constant propagation. As the other case that I want to handle is:
// i=0; for(;i<0;i++){}
// as right now nothing facilitates removing a loop like that.
// This is here simply to remove the cruft left behind goog.userAgent and
// similar cases.
if (isSimpleAssignment(n) && isConditionalStatement(next)) {
Node lhsAssign = getSimpleAssignmentName(n);
Node condition = getConditionalStatementCondition(next);
if (NodeUtil.isName(lhsAssign) && NodeUtil.isName(condition)
&& lhsAssign.getString().equals(condition.getString())) {
Node rhsAssign = getSimpleAssignmentValue(n);
TernaryValue value = NodeUtil.getExpressionBooleanValue(rhsAssign);
if (value != TernaryValue.UNKNOWN) {
int replacementConditionNodeType =
(value.toBoolean(true)) ? Token.TRUE : Token.FALSE;
condition.getParent().replaceChild(condition,
new Node(replacementConditionNodeType));
reportCodeChange();
}
}
}
}
示例11: tryFoldAndOr
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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 (!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;
}
}
示例12: getStringValue
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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;
}
示例13: getNumberValue
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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;
}
示例14: getNumberValue
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* 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.
*
* @param n The node.
* @return The value of a node as a Number, or null if it cannot be converted.
*/
static Double getNumberValue(Node n) {
switch (n.getToken()) {
case TRUE:
return 1.0;
case FALSE:
case NULL:
return 0.0;
case NUMBER:
return n.getDouble();
case VOID:
if (mayHaveSideEffects(n.getFirstChild())) {
return null;
} else {
return Double.NaN;
}
case 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 NEG:
if (n.hasOneChild() && n.getFirstChild().isName()
&& n.getFirstChild().getString().equals("Infinity")) {
return Double.NEGATIVE_INFINITY;
}
return null;
case NOT:
TernaryValue child = getPureBooleanValue(n.getFirstChild());
if (child != TernaryValue.UNKNOWN) {
return child.toBoolean(true) ? 0.0 : 1.0; // reversed.
}
break;
case TEMPLATELIT:
String string = getStringValue(n);
if (string == null) {
return null;
}
return getStringNumberValue(string);
case STRING:
return getStringNumberValue(n.getString());
case ARRAYLIT:
case OBJECTLIT:
String value = getStringValue(n);
return value != null ? getStringNumberValue(value) : null;
default:
break;
}
return null;
}
示例15: tryFoldHook
import com.google.javascript.rhino.jstype.TernaryValue; //导入方法依赖的package包/类
/**
* Try folding HOOK (?:) if the condition results of the condition is known.
* @return the replacement node, if changed, or the original if not
*/
private Node tryFoldHook(Node n) {
checkState(n.isHook(), n);
Node parent = n.getParent();
checkNotNull(parent);
Node cond = n.getFirstChild();
Node thenBody = cond.getNext();
Node elseBody = thenBody.getNext();
TernaryValue condValue = NodeUtil.getImpureBooleanValue(cond);
if (condValue == TernaryValue.UNKNOWN) {
// If the result nodes are equivalent, then one of the nodes can be
// removed and it doesn't matter which.
if (!areNodesEqualForInlining(thenBody, elseBody)) {
return n; // We can't remove branches otherwise!
}
}
// Transform "(a = 2) ? x =2 : y" into "a=2,x=2"
Node branchToKeep;
Node branchToRemove;
if (condValue.toBoolean(true)) {
branchToKeep = thenBody;
branchToRemove = elseBody;
} else {
branchToKeep = elseBody;
branchToRemove = thenBody;
}
Node replacement;
boolean condHasSideEffects = mayHaveSideEffects(cond);
// Must detach after checking for side effects, to ensure that the parents
// of nodes are set correctly.
n.detachChildren();
if (condHasSideEffects) {
replacement = IR.comma(cond, branchToKeep).srcref(n);
} else {
replacement = branchToKeep;
NodeUtil.markFunctionsDeleted(cond, compiler);
}
parent.replaceChild(n, replacement);
compiler.reportChangeToEnclosingScope(replacement);
NodeUtil.markFunctionsDeleted(branchToRemove, compiler);
return replacement;
}