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

The following examples show how to use jdk.nashorn.internal.runtime.JSType#isStrictlyRepresentableAsInt() . 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: Lexer.java    From TencentKona-8 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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 2
Source File: Lexer.java    From openjdk-jdk8u 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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 3
Source File: Lexer.java    From openjdk-jdk8u-backup 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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 4
Source File: Lexer.java    From openjdk-jdk9 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 HEXADECIMAL:
        return Lexer.valueOf(source.getString(start + 2, len - 2), 16); // number
    case OCTAL_LEGACY:
        return Lexer.valueOf(source.getString(start, len), 8); // number
    case OCTAL:
        return Lexer.valueOf(source.getString(start + 2, len - 2), 8); // number
    case BINARY_NUMBER:
        return Lexer.valueOf(source.getString(start + 2, len - 2), 2); // 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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 TEMPLATE:
    case TEMPLATE_HEAD:
    case TEMPLATE_MIDDLE:
    case TEMPLATE_TAIL:
        return valueOfString(start, len, true); // String
    case XML:
        return valueOfXML(start, len); // XMLToken::LexerToken
    case DIRECTIVE_COMMENT:
        return source.getString(start, len);
    default:
        break;
    }

    return null;
}
 
Example 5
Source File: Lexer.java    From hottub 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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 6
Source File: Lexer.java    From jdk8u_nashorn 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.isStrictlyRepresentableAsInt(value)) {
            return (int)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;
}