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

The following examples show how to use jdk.nashorn.internal.runtime.JSType#isRepresentableAsInt() . 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 TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final long[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();
    Class<?> widest = int.class;

    for (int index = 0; index < array.length; index++) {
        final long value = array[index];

        if (widest == int.class && JSType.isRepresentableAsInt(value)) {
            arrayData = arrayData.set(index, (int) value, false);
        } else if (widest != Object.class && JSType.isRepresentableAsDouble(value)) {
            arrayData = arrayData.set(index, (double) value, false);
            widest = double.class;
        } else {
            arrayData = arrayData.set(index, (Object) value, false);
            widest = Object.class;
        }
    }

    this.setArray(arrayData);
}
 
Example 2
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
NativeArray(final long[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();
    Class<?> widest = int.class;

    for (int index = 0; index < array.length; index++) {
        final long value = array[index];

        if (widest == int.class && JSType.isRepresentableAsInt(value)) {
            arrayData = arrayData.set(index, (int) value, false);
        } else if (widest != Object.class && JSType.isRepresentableAsDouble(value)) {
            arrayData = arrayData.set(index, (double) value, false);
            widest = double.class;
        } else {
            arrayData = arrayData.set(index, (Object) value, false);
            widest = Object.class;
        }
    }

    this.setArray(arrayData);
}
 
Example 3
Source File: Lower.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveCaseNode(final CaseNode caseNode) {
    // Try to represent the case test as an integer
    final Node test = caseNode.getTest();
    if (test instanceof LiteralNode) {
        final LiteralNode<?> lit = (LiteralNode<?>)test;
        if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
            if (JSType.isRepresentableAsInt(lit.getNumber())) {
                return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
            }
        }
    }
    return caseNode;
}
 
Example 4
Source File: Lower.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveCaseNode(final CaseNode caseNode) {
    // Try to represent the case test as an integer
    final Node test = caseNode.getTest();
    if (test instanceof LiteralNode) {
        final LiteralNode<?> lit = (LiteralNode<?>)test;
        if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
            if (JSType.isRepresentableAsInt(lit.getNumber())) {
                return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
            }
        }
    }
    return caseNode;
}
 
Example 5
Source File: IntArrayData.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.isRepresentableAsInt(value)) {
        array[index] = (int)(long)value;
        setLength(Math.max(index + 1, length()));
        return this;
    }

    return convert(Double.class).set(index, value, strict);
}
 
Example 6
Source File: IntArrayData.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
    if (JSType.isRepresentableAsInt(value)) {
        return set(index, JSType.toInt32(value), strict);
    } else if (value == ScriptRuntime.UNDEFINED) {
        return new UndefinedArrayFilter(this).set(index, value, strict);
    }

    final ArrayData newData = convert(value == null ? Object.class : value.getClass());
    return newData.set(index, value, strict);
}
 
Example 7
Source File: Lower.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveCaseNode(final CaseNode caseNode) {
    // Try to represent the case test as an integer
    final Node test = caseNode.getTest();
    if (test instanceof LiteralNode) {
        final LiteralNode<?> lit = (LiteralNode<?>)test;
        if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
            if (JSType.isRepresentableAsInt(lit.getNumber())) {
                return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
            }
        }
    }
    return caseNode;
}
 
Example 8
Source File: IntArrayData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ArrayData set(final int index, final Object value, final boolean strict) {
    if (JSType.isRepresentableAsInt(value)) {
        return set(index, JSType.toInt32(value), strict);
    } else if (value == ScriptRuntime.UNDEFINED) {
        return new UndefinedArrayFilter(this).set(index, value, strict);
    }

    final ArrayData newData = convert(value == null ? Object.class : value.getClass());
    return newData.set(index, value, strict);
}
 
Example 9
Source File: Lower.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveCaseNode(final CaseNode caseNode) {
    // Try to represent the case test as an integer
    final Node test = caseNode.getTest();
    if (test instanceof LiteralNode) {
        final LiteralNode<?> lit = (LiteralNode<?>)test;
        if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
            if (JSType.isRepresentableAsInt(lit.getNumber())) {
                return caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
            }
        }
    }
    return caseNode;
}
 
Example 10
Source File: ObjectCreator.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
MethodEmitter loadIndex(final MethodEmitter method, final long index) {
    return JSType.isRepresentableAsInt(index) ? method.load((int) index) : method.load((double) index);
}
 
Example 11
Source File: JSObjectLinker.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static int getIndex(final Number n) {
    final double value = n.doubleValue();
    return JSType.isRepresentableAsInt(value) ? (int)value : -1;
}
 
Example 12
Source File: ObjectCreator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
MethodEmitter loadIndex(final MethodEmitter method, final long index) {
    return JSType.isRepresentableAsInt(index) ? method.load((int) index) : method.load((double) index);
}
 
Example 13
Source File: BrowserJSObjectLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static int getIndex(final Number n) {
    final double value = n.doubleValue();
    return JSType.isRepresentableAsInt(value) ? (int)value : -1;
}
 
Example 14
Source File: JSONParser.java    From TencentKona-8 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;
    }
    return d;
}
 
Example 15
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 16
Source File: Attr.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Node leaveSwitchNode(final SwitchNode switchNode) {
    Type type = Type.UNKNOWN;

    final List<CaseNode> newCases = new ArrayList<>();
    for (final CaseNode caseNode : switchNode.getCases()) {
        final Node test = caseNode.getTest();

        CaseNode newCaseNode = caseNode;
        if (test != null) {
            if (test instanceof LiteralNode) {
                //go down to integers if we can
                final LiteralNode<?> lit = (LiteralNode<?>)test;
                if (lit.isNumeric() && !(lit.getValue() instanceof Integer)) {
                    if (JSType.isRepresentableAsInt(lit.getNumber())) {
                        newCaseNode = caseNode.setTest((Expression)LiteralNode.newInstance(lit, lit.getInt32()).accept(this));
                    }
                }
            } else {
                // the "all integer" case that CodeGenerator optimizes for currently assumes literals only
                type = Type.OBJECT;
            }

            final Type newCaseType = newCaseNode.getTest().getType();
            if (newCaseType.isBoolean()) {
                type = Type.OBJECT; //booleans and integers aren't assignment compatible
            } else {
                type = Type.widest(type, newCaseType);
            }
        }

        newCases.add(newCaseNode);
    }

    //only optimize for all integers
    if (!type.isInteger()) {
        type = Type.OBJECT;
    }

    switchNode.setTag(newInternal(lc.getCurrentFunction().uniqueName(SWITCH_TAG_PREFIX.symbolName()), type));

    end(switchNode);

    return switchNode.setCases(lc, newCases);
}
 
Example 17
Source File: JSObjectLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static int getIndex(final Number n) {
    final double value = n.doubleValue();
    return JSType.isRepresentableAsInt(value) ? (int)value : -1;
}
 
Example 18
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;
}
 
Example 19
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 20
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;
}