本文整理汇总了Java中com.google.javascript.rhino.Node.getDouble方法的典型用法代码示例。如果您正苦于以下问题:Java Node.getDouble方法的具体用法?Java Node.getDouble怎么用?Java Node.getDouble使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.rhino.Node
的用法示例。
在下文中一共展示了Node.getDouble方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBooleanValue
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Gets the boolean value of a node that represents a literal. This method
* effectively emulates the <code>Boolean()</code> JavaScript cast function.
*
* @throws IllegalArgumentException If {@code n} is not a literal value
*/
static boolean getBooleanValue(Node n) {
switch (n.getType()) {
case Token.STRING:
return n.getString().length() > 0;
case Token.NUMBER:
return n.getDouble() != 0;
case Token.NULL:
case Token.FALSE:
case Token.VOID:
return 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 false;
} else if ("Infinity".equals(name)) {
return true;
}
break;
case Token.TRUE:
case Token.ARRAYLIT:
case Token.OBJECTLIT:
case Token.REGEXP:
return true;
}
throw new IllegalArgumentException("Non-literal value: " + n);
}
示例2: getStringValue
import com.google.javascript.rhino.Node; //导入方法依赖的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): Convert constant array, object, and regex literals as well.
switch (n.getType()) {
case Token.NAME:
case Token.STRING:
return n.getString();
case Token.NUMBER:
double value = n.getDouble();
long longValue = (long) value;
// Return "1" instead of "1.0"
if (longValue == value) {
return Long.toString(longValue);
} else {
return Double.toString(n.getDouble());
}
case Token.FALSE:
case Token.TRUE:
case Token.NULL:
return Node.tokenToName(n.getType());
case Token.VOID:
return "undefined";
}
return null;
}
示例3: tryFoldArithmetic
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to fold arithmetic binary operators
*/
void tryFoldArithmetic(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
double result;
double lval = left.getDouble();
double rval = right.getDouble();
switch (n.getType()) {
case Token.ADD:
result = lval + rval;
break;
case Token.SUB:
result = lval - rval;
break;
case Token.MUL:
result = lval * rval;
break;
case Token.DIV:
if (rval == 0) {
error(t, DIVIDE_BY_0_ERROR, right);
return;
}
result = lval / rval;
break;
default:
throw new Error("Unknown arithmetic operator");
}
// length of the left and right value plus 1 byte for the operator.
if (String.valueOf(result).length() <=
String.valueOf(lval).length() + String.valueOf(rval).length() + 1) {
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
}
示例4: tryFoldBitAndOr
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to fold arithmetic binary operators
*/
void tryFoldBitAndOr(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
double result;
double lval = left.getDouble();
double rval = right.getDouble();
// For now, we are being extra conservative, and only folding ints in
// the range MIN_VALUE-MAX_VALUE
if (lval < Integer.MIN_VALUE || lval > Integer.MAX_VALUE ||
rval < Integer.MIN_VALUE || rval > Integer.MAX_VALUE) {
// Fall back through and let the javascript use the larger values
return;
}
// Convert the numbers to ints
int lvalInt = (int) lval;
if (lvalInt != lval) {
return;
}
int rvalInt = (int) rval;
if (rvalInt != rval) {
return;
}
switch (n.getType()) {
case Token.BITAND:
result = lvalInt & rvalInt;
break;
case Token.BITOR:
result = lvalInt | rvalInt;
break;
default:
throw new Error("Unknown bitwise operator");
}
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
示例5: tryFoldShift
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to fold shift operations
*/
void tryFoldShift(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.NUMBER &&
right.getType() == Token.NUMBER) {
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)) {
error(t, BITWISE_OPERAND_OUT_OF_RANGE, left);
return;
}
// 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)) {
error(t, SHIFT_AMOUNT_OUT_OF_BOUNDS, right);
return;
}
// Convert the numbers to ints
int lvalInt = (int) lval;
if (lvalInt != lval) {
error(t, FRACTIONAL_BITWISE_OPERAND, left);
return;
}
int rvalInt = (int) rval;
if (rvalInt != rval) {
error(t, FRACTIONAL_BITWISE_OPERAND, right);
return;
}
switch (n.getType()) {
case Token.LSH:
result = lvalInt << rvalInt;
break;
case Token.RSH:
result = lvalInt >> rvalInt;
break;
case Token.URSH:
result = lvalInt >>> rvalInt;
break;
default:
throw new AssertionError("Unknown shift operator: " +
Node.tokenToName(n.getType()));
}
parent.replaceChild(n, Node.newNumber(result));
t.getCompiler().reportCodeChange();
}
}
示例6: tryFoldStringIndexOf
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to evaluate String.indexOf/lastIndexOf:
* "abcdef".indexOf("bc") -> 1
* "abcdefbc".indexOf("bc", 3) -> 6
*/
void tryFoldStringIndexOf(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (!NodeUtil.isGetProp(left) || !NodeUtil.isImmutableValue(right)) {
return;
}
Node lstringNode = left.getFirstChild();
Node functionName = lstringNode.getNext();
if ((lstringNode.getType() != Token.STRING) ||
(!functionName.getString().equals("indexOf") &&
!functionName.getString().equals("lastIndexOf"))) {
return;
}
String lstring = NodeUtil.getStringValue(lstringNode);
boolean isIndexOf = functionName.getString().equals("indexOf");
Node firstArg = right;
Node secondArg = right.getNext();
String searchValue = NodeUtil.getStringValue(firstArg);
// searchValue must be a valid string.
if (searchValue == null) {
return;
}
int fromIndex = isIndexOf ? 0 : lstring.length();
if (secondArg != null) {
// Third-argument and non-numeric second arg are problematic. Discard.
if ((secondArg.getNext() != null) ||
(secondArg.getType() != Token.NUMBER)) {
return;
} else {
fromIndex = (int) secondArg.getDouble();
}
}
int indexVal = isIndexOf ? lstring.indexOf(searchValue, fromIndex)
: lstring.lastIndexOf(searchValue, fromIndex);
Node newNode = Node.newNumber(indexVal);
parent.replaceChild(n, newNode);
t.getCompiler().reportCodeChange();
}
示例7: tryFoldGetElem
import com.google.javascript.rhino.Node; //导入方法依赖的package包/类
/**
* Try to fold array-element. e.g [1, 2, 3][10];
*/
void tryFoldGetElem(NodeTraversal t, Node n, Node left, Node right,
Node parent) {
if (left.getType() == Token.ARRAYLIT) {
if (right.getType() != Token.NUMBER) {
// Sometimes people like to use complex expressions to index into
// arrays, or strings to index into array methods.
return;
}
double index = right.getDouble();
int intIndex = (int) index;
if (intIndex != index) {
t.getCompiler().report(JSError.make(t, right,
INVALID_GETELEM_INDEX_ERROR, String.valueOf(index)));
return;
}
if (intIndex < 0) {
t.getCompiler().report(JSError.make(t, n, INDEX_OUT_OF_BOUNDS_ERROR,
String.valueOf(intIndex)));
return;
}
Node elem = left.getFirstChild();
for (int i = 0; elem != null && i < intIndex; i++) {
elem = elem.getNext();
}
if (elem == null) {
t.getCompiler().report(JSError.make(t, n, INDEX_OUT_OF_BOUNDS_ERROR,
String.valueOf(intIndex)));
return;
}
// Replace the entire GETELEM with the value
left.removeChild(elem);
parent.replaceChild(n, elem);
t.getCompiler().reportCodeChange();
}
}