Java Code Examples for jdk.nashorn.internal.runtime.JSType#toInteger()

The following examples show how to use jdk.nashorn.internal.runtime.JSType#toInteger() . 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: NativeNumber.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.6 Number.prototype.toExponential (fractionDigits)
 *
 * @param self           self reference
 * @param fractionDigits how many digital should be after the significand's decimal point. If undefined, use as many as necessary to uniquely specify number.
 *
 * @return number in decimal exponential notation
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toExponential(final Object self, final Object fractionDigits) {
    final double  x         = getNumberValue(self);
    final boolean trimZeros = fractionDigits == UNDEFINED;
    final int     f         = trimZeros ? 16 : JSType.toInteger(fractionDigits);

    if (Double.isNaN(x)) {
        return "NaN";
    } else if (Double.isInfinite(x)) {
        return x > 0? "Infinity" : "-Infinity";
    }

    if (fractionDigits != UNDEFINED && (f < 0 || f > 20)) {
        throw rangeError("invalid.fraction.digits", "toExponential");
    }

    final String res = String.format(Locale.US, "%1." + f + "e", x);
    return fixExponent(res, trimZeros);
}
 
Example 2
Source File: NativeNumber.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.5 Number.prototype.toFixed (fractionDigits)
 *
 * @param self           self reference
 * @param fractionDigits how many digits should be after the decimal point, 0 if undefined
 *
 * @return number in decimal fixed point notation
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toFixed(final Object self, final Object fractionDigits) {
    final int f = JSType.toInteger(fractionDigits);
    if (f < 0 || f > 20) {
        throw rangeError("invalid.fraction.digits", "toFixed");
    }

    final double x = getNumberValue(self);
    if (Double.isNaN(x)) {
        return "NaN";
    }

    if (Math.abs(x) >= 1e21) {
        return JSType.toString(x);
    }

    final NumberFormat format = NumberFormat.getNumberInstance(Locale.US);
    format.setMinimumFractionDigits(f);
    format.setMaximumFractionDigits(f);
    format.setGroupingUsed(false);

    return format.format(x);
}
 
Example 3
Source File: NativeNumber.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.6 Number.prototype.toExponential (fractionDigits)
 *
 * @param self           self reference
 * @param fractionDigits how many digital should be after the significand's decimal point. If undefined, use as many as necessary to uniquely specify number.
 *
 * @return number in decimal exponential notation
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toExponential(final Object self, final Object fractionDigits) {
    final double  x         = getNumberValue(self);
    final boolean trimZeros = fractionDigits == UNDEFINED;
    final int     f         = trimZeros ? 16 : JSType.toInteger(fractionDigits);

    if (Double.isNaN(x)) {
        return "NaN";
    } else if (Double.isInfinite(x)) {
        return x > 0? "Infinity" : "-Infinity";
    }

    if (fractionDigits != UNDEFINED && (f < 0 || f > 20)) {
        throw rangeError("invalid.fraction.digits", "toExponential");
    }

    final String res = String.format(Locale.US, "%1." + f + "e", x);
    return fixExponent(res, trimZeros);
}
 
Example 4
Source File: NativeNumber.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.7.4.6 Number.prototype.toExponential (fractionDigits)
 *
 * @param self           self reference
 * @param fractionDigits how many digital should be after the significand's decimal point. If undefined, use as many as necessary to uniquely specify number.
 *
 * @return number in decimal exponential notation
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toExponential(final Object self, final Object fractionDigits) {
    final double  x         = getNumberValue(self);
    final boolean trimZeros = fractionDigits == UNDEFINED;
    final int     f         = trimZeros ? 16 : JSType.toInteger(fractionDigits);

    if (Double.isNaN(x)) {
        return "NaN";
    } else if (Double.isInfinite(x)) {
        return x > 0? "Infinity" : "-Infinity";
    }

    if (fractionDigits != UNDEFINED && (f < 0 || f > 20)) {
        throw rangeError("invalid.fraction.digits", "toExponential");
    }

    final String res = String.format(Locale.US, "%1." + f + "e", x);
    return fixExponent(res, trimZeros);
}
 
Example 5
Source File: NativeNumber.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 6
Source File: NativeString.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.5 String.prototype.charCodeAt (pos)
 * @param self self reference
 * @param pos  position in string
 * @return number representing charcode at position
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double charCodeAt(final Object self, final Object pos) {
    final String str = checkObjectToString(self);
    final int    idx = JSType.toInteger(pos);
    return idx < 0 || idx >= str.length() ? Double.NaN : str.charAt(idx);
}
 
Example 7
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.5 String.prototype.charCodeAt (pos)
 * @param self self reference
 * @param pos  position in string
 * @return number representing charcode at position
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double charCodeAt(final Object self, final Object pos) {
    final String str = checkObjectToString(self);
    final int    idx = JSType.toInteger(pos);
    return idx < 0 || idx >= str.length() ? Double.NaN : str.charAt(idx);
}
 
Example 8
Source File: NativeNumber.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 9
Source File: NativeNumber.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 10
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) {
    try {
        //check that it's a char sequence or throw cce
        final CharSequence cs = (CharSequence)self;
        //check that the index, representable as an int, is inside the array
        final int intIndex = JSType.toInteger(request.getArguments()[2]);
        return intIndex >= 0 && intIndex < cs.length(); //can link
    } catch (final ClassCastException | IndexOutOfBoundsException e) {
        //fallthru
    }
    return false;
}
 
Example 11
Source File: NativeString.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA B.2.3 String.prototype.substr (start, length)
 *
 * @param self   self reference
 * @param start  start position
 * @param length length of section
 * @return substring given start and length of section
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String substr(final Object self, final Object start, final Object length) {
    final String str       = JSType.toString(self);
    final int    strLength = str.length();

    int intStart = JSType.toInteger(start);
    if (intStart < 0) {
        intStart = Math.max(intStart + strLength, 0);
    }

    final int intLen = Math.min(Math.max(length == UNDEFINED ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart);

    return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen);
}
 
Example 12
Source File: NativeNumber.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 13
Source File: NativeString.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA B.2.3 String.prototype.substr (start, length)
 *
 * @param self   self reference
 * @param start  start position
 * @param length length of section
 * @return substring given start and length of section
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object substr(final Object self, final Object start, final Object length) {
    final String str       = JSType.toString(self);
    final int    strLength = str.length();

    int intStart = JSType.toInteger(start);
    if (intStart < 0) {
        intStart = Math.max(intStart + strLength, 0);
    }

    final int intLen = Math.min(Math.max((length == UNDEFINED) ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart);

    return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen);
}
 
Example 14
Source File: NativeNumber.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 15
Source File: NativeNumber.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 16
Source File: NativeNumber.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 17
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA B.2.3 String.prototype.substr (start, length)
 *
 * @param self   self reference
 * @param start  start position
 * @param length length of section
 * @return substring given start and length of section
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String substr(final Object self, final Object start, final Object length) {
    final String str       = JSType.toString(self);
    final int    strLength = str.length();

    int intStart = JSType.toInteger(start);
    if (intStart < 0) {
        intStart = Math.max(intStart + strLength, 0);
    }

    final int intLen = Math.min(Math.max(length == UNDEFINED ? Integer.MAX_VALUE : JSType.toInteger(length), 0), strLength - intStart);

    return intLen <= 0 ? "" : str.substring(intStart, intStart + intLen);
}
 
Example 18
Source File: NativeNumber.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.2 Number.prototype.toString ( [ radix ] )
 *
 * @param self  self reference
 * @param radix radix to use for string conversion
 * @return string representation of this Number in the given radix
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self, final Object radix) {
    if (radix != UNDEFINED) {
        final int intRadix = JSType.toInteger(radix);
        if (intRadix != 10) {
            if (intRadix < 2 || intRadix > 36) {
                throw rangeError("invalid.radix");
            }
            return JSType.toString(getNumberValue(self), intRadix);
        }
    }

    return JSType.toString(getNumberValue(self));
}
 
Example 19
Source File: NativeRegExp.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fast lastIndex getter
 * @return last index property as int
 */
public int getLastIndex() {
    return JSType.toInteger(lastIndex);
}
 
Example 20
Source File: NativeRegExp.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fast lastIndex getter
 * @return last index property as int
 */
public int getLastIndex() {
    return JSType.toInteger(lastIndex);
}