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

The following examples show how to use jdk.nashorn.internal.runtime.JSType#isRepresentableAsLong() . 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: NativeArray.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 2
Source File: LongArrayData.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final double value, final boolean strict) {
    if (JSType.isRepresentableAsLong(value)) {
        array[index] = (long)value;
        setLength(Math.max(index + 1, length()));
        return this;
    }
    return convert(Double.class).set(index, value, strict);
}
 
Example 3
Source File: ArrayIndex.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        final int intKey = (int)key;
        if (intKey >= 0) {
            return intKey;
        }
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 4
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: LongArrayData.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final double value, final boolean strict) {
    if (JSType.isRepresentableAsLong(value)) {
        array[index] = (long)value;
        setLength(Math.max(index + 1, length()));
        return this;
    }
    return convert(Double.class).set(index, value, strict);
}
 
Example 6
Source File: NativeArray.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: LongArrayData.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final double value, final boolean strict) {
    if (JSType.isRepresentableAsLong(value)) {
        array[index] = (long)value;
        setLength(Math.max(index + 1, length()));
        return this;
    }
    return convert(Double.class).set(index, value, strict);
}
 
Example 8
Source File: LongArrayData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final double value, final boolean strict) {
    if (JSType.isRepresentableAsLong(value)) {
        array[index] = (long)value;
        setLength(Math.max(index + 1, length()));
        return this;
    }
    return convert(Double.class).set(index, value, strict);
}
 
Example 9
Source File: Lexer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return value of token given its token descriptor.
 *
 * @param token  Token descriptor.
 * @return JavaScript value.
 */
Object getValueOf(final long token, final boolean strict) {
    final int start = Token.descPosition(token);
    final int len   = Token.descLength(token);

    switch (Token.descType(token)) {
    case DECIMAL:
        return Lexer.valueOf(source.getString(start, len), 10); // number
    case OCTAL:
        return Lexer.valueOf(source.getString(start, len), 8); // number
    case HEXADECIMAL:
        return Lexer.valueOf(source.getString(start + 2, len - 2), 16); // number
    case FLOATING:
        final String str   = source.getString(start, len);
        final double value = Double.valueOf(str);
        if (str.indexOf('.') != -1) {
            return value; //number
        }
        //anything without an explicit decimal point is still subject to a
        //"representable as int or long" check. Then the programmer does not
        //explicitly code something as a double. For example new Color(int, int, int)
        //and new Color(float, float, float) will get ambiguous for cases like
        //new Color(1.0, 1.5, 1.5) if we don't respect the decimal point.
        //yet we don't want e.g. 1e6 to be a double unnecessarily
        if (JSType.isRepresentableAsInt(value) && !JSType.isNegativeZero(value)) {
            return (int)value;
        } else if (JSType.isRepresentableAsLong(value) && !JSType.isNegativeZero(value)) {
            return (long)value;
        }
        return value;
    case STRING:
        return source.getString(start, len); // String
    case ESCSTRING:
        return valueOfString(start, len, strict); // String
    case IDENT:
        return valueOfIdent(start, len); // String
    case REGEX:
        return valueOfPattern(start, len); // RegexToken::LexerToken
    case XML:
        return valueOfXML(start, len); // XMLToken::LexerToken
    case DIRECTIVE_COMMENT:
        return source.getString(start, len);
    default:
        break;
    }

    return null;
}
 
Example 10
Source File: JSONParser.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private Number parseNumber() {
    final int start = pos;
    int c = next();

    if (c == '-') {
        c = next();
    }
    if (!isDigit(c)) {
        throw numberError(start);
    }
    // no more digits allowed after 0
    if (c != '0') {
        skipDigits();
    }

    // fraction
    if (peek() == '.') {
        pos++;
        if (!isDigit(next())) {
            throw numberError(pos - 1);
        }
        skipDigits();
    }

    // exponent
    c = peek();
    if (c == 'e' || c == 'E') {
        pos++;
        c = next();
        if (c == '-' || c == '+') {
            c = next();
        }
        if (!isDigit(c)) {
            throw numberError(pos - 1);
        }
        skipDigits();
    }

    final double d = Double.parseDouble(source.substring(start, pos));
    if (JSType.isRepresentableAsInt(d)) {
        return (int) d;
    } else if (JSType.isRepresentableAsLong(d)) {
        return (long) d;
    }
    return d;
}
 
Example 11
Source File: ArrayIndex.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 12
Source File: ArrayIndex.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 13
Source File: ArrayIndex.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 14
Source File: ArrayIndex.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 15
Source File: ArrayIndex.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 16
Source File: ArrayIndex.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 17
Source File: ArrayIndex.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 18
Source File: ArrayIndex.java    From jdk8u_nashorn with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}
 
Example 19
Source File: ArrayIndex.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a valid index for this double, if it represents one.
 *
 * Doubles that aren't representable exactly as longs/ints aren't working
 * array indexes, however, array[1.1] === array["1.1"] in JavaScript.
 *
 * @param key the key to check
 * @return the array index this double represents or {@code -1} if this isn't a valid index.
 *         Note that negative return values other than {@code -1} are considered valid and can be converted to
 *         the actual index using {@link #toLongIndex(int)}.
 */
public static int getArrayIndex(final double key) {
    if (JSType.isRepresentableAsInt(key)) {
        return getArrayIndex((int) key);
    } else if (JSType.isRepresentableAsLong(key)) {
        return getArrayIndex((long) key);
    }

    return INVALID_ARRAY_INDEX;
}