本文整理汇总了Java中jdk.nashorn.internal.runtime.JSType.toNumber方法的典型用法代码示例。如果您正苦于以下问题:Java JSType.toNumber方法的具体用法?Java JSType.toNumber怎么用?Java JSType.toNumber使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.nashorn.internal.runtime.JSType
的用法示例。
在下文中一共展示了JSType.toNumber方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setYear
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* ECMA B.2.5 Date.prototype.setYear (year)
*
* @param self self reference
* @param year year
* @return NativeDate
*/
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double setYear(final Object self, final Object year) {
final NativeDate nd = getNativeDate(self);
if (isNaN(nd.getTime())) {
nd.setTime(utc(0, nd.getTimeZone()));
}
final double yearNum = JSType.toNumber(year);
if (isNaN(yearNum)) {
nd.setTime(NaN);
return nd.getTime();
}
int yearInt = (int)yearNum;
if (0 <= yearInt && yearInt <= 99) {
yearInt += 1900;
}
setFields(nd, YEAR, new Object[] {yearInt}, true);
return nd.getTime();
}
示例2: convertCtorArgs
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
private static double[] convertCtorArgs(final Object[] args) {
final double[] d = new double[7];
boolean nullReturn = false;
// should not bailout on first NaN or infinite. Need to convert all
// subsequent args for possible side-effects via valueOf/toString overrides
// on argument objects.
for (int i = 0; i < d.length; i++) {
if (i < args.length) {
final double darg = JSType.toNumber(args[i]);
if (isNaN(darg) || isInfinite(darg)) {
nullReturn = true;
}
d[i] = (long)darg;
} else {
d[i] = i == 2 ? 1 : 0; // day in month defaults to 1
}
}
if (0 <= d[0] && d[0] <= 99) {
d[0] += 1900;
}
return nullReturn? null : d;
}
示例3: max
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* ECMA 15.8.2.11 max(x)
*
* @param self self reference
* @param args arguments
*
* @return the largest of the arguments, {@link Double#NEGATIVE_INFINITY} if no args given, or identity if one arg is given
*/
@Function(arity = 2, attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static double max(final Object self, final Object... args) {
switch (args.length) {
case 0:
return Double.NEGATIVE_INFINITY;
case 1:
return JSType.toNumber(args[0]);
default:
double res = JSType.toNumber(args[0]);
for (int i = 1; i < args.length; i++) {
res = Math.max(res, JSType.toNumber(args[i]));
}
return res;
}
}
示例4: min
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* ECMA 15.8.2.12 min(x)
*
* @param self self reference
* @param args arguments
*
* @return the smallest of the arguments, {@link Double#NEGATIVE_INFINITY} if no args given, or identity if one arg is given
*/
@Function(arity = 2, attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static double min(final Object self, final Object... args) {
switch (args.length) {
case 0:
return Double.POSITIVE_INFINITY;
case 1:
return JSType.toNumber(args[0]);
default:
double res = JSType.toNumber(args[0]);
for (int i = 1; i < args.length; i++) {
res = Math.min(res, JSType.toNumber(args[i]));
}
return res;
}
}
示例5: toDouble
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private static Double toDouble(final Object obj0) {
// TODO - Order tests for performance.
for (Object obj = obj0; ;) {
if (obj == null) {
return null;
} else if (obj instanceof Double) {
return (Double) obj;
} else if (obj instanceof Number) {
return ((Number)obj).doubleValue();
} else if (obj instanceof String) {
return JSType.toNumber((String) obj);
} else if (obj instanceof ConsString) {
return JSType.toNumber(obj.toString());
} else if (obj instanceof Boolean) {
return (Boolean) obj ? 1 : +0.0;
} else if (obj instanceof ScriptObject) {
obj = JSType.toPrimitive(obj, Number.class);
continue;
} else if (obj == UNDEFINED) {
return Double.NaN;
}
throw assertUnexpectedType(obj);
}
}
示例6: getDouble
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
@Override
public double getDouble(final int index) {
if (index >= length()) {
return JSType.toNumber(get(index));
}
return underlying.getDouble(index);
}
示例7: validLength
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
static long validLength(final Object length, final boolean reject) {
final double doubleLength = JSType.toNumber(length);
if (!Double.isNaN(doubleLength) && JSType.isRepresentableAsLong(doubleLength)) {
final long len = (long) doubleLength;
if (len >= 0 && len <= JSType.MAX_UINT) {
return len;
}
}
if (reject) {
throw rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length));
}
return -1;
}
示例8: convertArgs
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
private static double[] convertArgs(final Object[] args, final double time, final int fieldId, final int start, final int length) {
final double[] d = new double[length];
boolean nullReturn = false;
// Need to call toNumber on all args for side-effects - even if an argument
// fails to convert to number, subsequent toNumber calls needed for possible
// side-effects via valueOf/toString overrides.
for (int i = start; i < start + length; i++) {
if (fieldId <= i && i < fieldId + args.length) {
final double darg = JSType.toNumber(args[i - fieldId]);
if (isNaN(darg) || isInfinite(darg)) {
nullReturn = true;
}
d[i - start] = (long) darg;
} else {
// Date.prototype.set* methods require first argument to be defined
if (i == fieldId) {
nullReturn = true;
}
if (!nullReturn && !isNaN(time)) {
d[i - start] = valueFromTime(i, time);
}
}
}
return nullReturn ? null : d;
}
示例9: getDouble
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
@Override
public double getDouble(final int index) {
if (index >= 0 && index < maxDenseLength) {
return underlying.getDouble(index);
}
return JSType.toNumber(sparseMap.get(indexToKey(index)));
}
示例10: round
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* ECMA 15.8.2.15 round(x)
*
* @param self self reference
* @param x argument
*
* @return x rounded
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static double round(final Object self, final Object x) {
final double d = JSType.toNumber(x);
if (Math.getExponent(d) >= 52) {
return d;
}
return Math.copySign(Math.floor(d + 0.5), d);
}
示例11: validLength
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
static long validLength(final Object length) {
// ES5 15.4.5.1, steps 3.c and 3.d require two ToNumber conversions here
final double doubleLength = JSType.toNumber(length);
if (doubleLength != JSType.toUint32(length)) {
throw rangeError("inappropriate.array.length", ScriptRuntime.safeToString(length));
}
return (long) doubleLength;
}
示例12: getDouble
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
@Override
public double getDouble(final int index) {
return JSType.toNumber(array[index]);
}
示例13: constructor
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* ECMA 15.7.2 - The Number constructor
*
* @param newObj is this Number instantiated with the new operator
* @param self self reference
* @param args value of number
* @return the Number instance (internally represented as a {@code NativeNumber})
*/
@Constructor(arity = 1)
public static Object constructor(final boolean newObj, final Object self, final Object... args) {
final double num = (args.length > 0) ? JSType.toNumber(args[0]) : 0.0;
return newObj? new NativeNumber(num) : num;
}
示例14: getNumber
import jdk.nashorn.internal.runtime.JSType; //导入方法依赖的package包/类
/**
* Fetch double value of node.
*
* @return double value of node.
*/
public double getNumber() {
return JSType.toNumber(value);
}