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

The following examples show how to use jdk.nashorn.internal.runtime.JSType#toLong() . 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: JavaArgumentConverters.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (obj instanceof String || obj instanceof ConsString) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 2
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
 *
 * @param self           self reference
 * @param searchElement  element to search for
 * @param fromIndex      start index of search
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object indexOf(final Object self, final Object searchElement, final Object fromIndex) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());
        final long         n    = JSType.toLong(fromIndex);

        if (len == 0 || n >= len) {
            return -1;
        }

        for (long k = Math.max(0, (n < 0) ? (len - Math.abs(n)) : n); k < len; k++) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        //fallthru
    }

    return -1;
}
 
Example 3
Source File: NativeArray.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.10 Array.prototype.slice ( start [ , end ] )
 *
 * @param self  self reference
 * @param start start of slice (inclusive)
 * @param end   end of slice (optional, exclusive)
 * @return sliced array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object slice(final Object self, final Object start, final Object end) {
    final Object       obj                 = Global.toObject(self);
    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);
    final long         relativeEnd         = (end == ScriptRuntime.UNDEFINED) ? len : JSType.toLong(end);

    long k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long finale = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);

    if (k >= finale) {
        return new NativeArray(0);
    }

    if (bulkable(sobj)) {
        return new NativeArray(sobj.getArray().slice(k, finale));
    }

    // Construct array with proper length to have a deleted filter on undefined elements
    final NativeArray copy = new NativeArray(finale - k);
    for (long n = 0; k < finale; n++, k++) {
        if (sobj.has(k)) {
            copy.defineOwnProperty(ArrayIndex.getArrayIndex(n), sobj.get(k));
        }
    }

    return copy;
}
 
Example 4
Source File: NativeArray.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.10 Array.prototype.slice ( start [ , end ] )
 *
 * @param self  self reference
 * @param start start of slice (inclusive)
 * @param end   end of slice (optional, exclusive)
 * @return sliced array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object slice(final Object self, final Object start, final Object end) {
    final Object       obj                 = Global.toObject(self);
    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);
    final long         relativeEnd         = (end == ScriptRuntime.UNDEFINED) ? len : JSType.toLong(end);

    long k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long finale = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);

    if (k >= finale) {
        return new NativeArray(0);
    }

    if (bulkable(sobj)) {
        return new NativeArray(sobj.getArray().slice(k, finale));
    }

    // Construct array with proper length to have a deleted filter on undefined elements
    final NativeArray copy = new NativeArray(finale - k);
    for (long n = 0; k < finale; n++, k++) {
        if (sobj.has(k)) {
            copy.defineOwnProperty(ArrayIndex.getArrayIndex(n), sobj.get(k));
        }
    }

    return copy;
}
 
Example 5
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
 *
 * @param self self reference
 * @param args arguments: element to search for and optional from index
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double lastIndexOf(final Object self, final Object... args) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());

        if (len == 0) {
            return -1;
        }

        final Object searchElement = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
        final long   n             = args.length > 1 ? JSType.toLong(args[1]) : len - 1;

        for (long k = n < 0 ? len - Math.abs(n) : Math.min(n, len - 1); k >= 0; k--) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }

    return -1;
}
 
Example 6
Source File: NativeArray.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
 *
 * @param self self reference
 * @param args arguments: element to search for and optional from index
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double lastIndexOf(final Object self, final Object... args) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());

        if (len == 0) {
            return -1;
        }

        final Object searchElement = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
        final long   n             = args.length > 1 ? JSType.toLong(args[1]) : len - 1;

        for (long k = n < 0 ? len - Math.abs(n) : Math.min(n, len - 1); k >= 0; k--) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }

    return -1;
}
 
Example 7
Source File: JavaArgumentConverters.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d.doubleValue())) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f.floatValue())) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (isString(obj)) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 8
Source File: SparseArrayData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(final int index) {
    if (index >= 0 && index < maxDenseLength) {
        return underlying.getLong(index);
    }
    return JSType.toLong(sparseMap.get(indexToKey(index)));
}
 
Example 9
Source File: JavaArgumentConverters.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d.doubleValue())) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f.floatValue())) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (obj instanceof String || obj instanceof ConsString) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 10
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
 *
 * @param self self reference
 * @param args arguments: element to search for and optional from index
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double lastIndexOf(final Object self, final Object... args) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());

        if (len == 0) {
            return -1;
        }

        final Object searchElement = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
        final long   n             = args.length > 1 ? JSType.toLong(args[1]) : len - 1;

        for (long k = n < 0 ? len - Math.abs(n) : Math.min(n, len - 1); k >= 0; k--) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }

    return -1;
}
 
Example 11
Source File: JavaArgumentConverters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Long toLong(final Object obj0) {
    // TODO - Order tests for performance.
    for (Object obj = obj0; ;) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Long) {
            return (Long) obj;
        } else if (obj instanceof Integer) {
            return ((Integer)obj).longValue();
        } else if (obj instanceof Double) {
            final Double d = (Double)obj;
            if(Double.isInfinite(d.doubleValue())) {
                return 0L;
            }
            return d.longValue();
        } else if (obj instanceof Float) {
            final Float f = (Float)obj;
            if(Float.isInfinite(f.floatValue())) {
                return 0L;
            }
            return f.longValue();
        } else if (obj instanceof Number) {
            return ((Number)obj).longValue();
        } else if (isString(obj)) {
            return JSType.toLong(obj);
        } else if (obj instanceof Boolean) {
            return (Boolean)obj ? 1L : 0L;
        } else if (obj instanceof ScriptObject) {
            obj = JSType.toPrimitive(obj, Number.class);
            continue;
        } else if (obj == UNDEFINED) {
            return null; // null or 0L?
        }
        throw assertUnexpectedType(obj);
    }
}
 
Example 12
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
 *
 * @param self self reference
 * @param args arguments: element to search for and optional from index
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object lastIndexOf(final Object self, final Object... args) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());

        if (len == 0) {
            return -1;
        }

        final Object searchElement = (args.length > 0) ? args[0] : ScriptRuntime.UNDEFINED;
        final long   n             = (args.length > 1) ? JSType.toLong(args[1]) : (len - 1);

        for (long k = (n < 0) ? (len - Math.abs(n)) : Math.min(n, len - 1); k >= 0; k--) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }

    return -1;
}
 
Example 13
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
 *
 * @param self           self reference
 * @param searchElement  element to search for
 * @param fromIndex      start index of search
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double indexOf(final Object self, final Object searchElement, final Object fromIndex) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());
        if (len == 0) {
            return -1;
        }

        final long         n = JSType.toLong(fromIndex);
        if (n >= len) {
            return -1;
        }


        for (long k = Math.max(0, n < 0 ? len - Math.abs(n) : n); k < len; k++) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        //fallthru
    }

    return -1;
}
 
Example 14
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.4.12 Array.prototype.splice ( start, deleteCount [ item1 [ , item2 [ , ... ] ] ] )
 *
 * @param self self reference
 * @param args arguments
 * @return result of splice
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object splice(final Object self, final Object... args) {
    final Object obj = Global.toObject(self);

    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final Object start = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
    final Object deleteCount = args.length > 1 ? args[1] : ScriptRuntime.UNDEFINED;

    Object[] items;

    if (args.length > 2) {
        items = new Object[args.length - 2];
        System.arraycopy(args, 2, items, 0, items.length);
    } else {
        items = ScriptRuntime.EMPTY_ARRAY;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);

    final long actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long actualDeleteCount = Math.min(Math.max(JSType.toLong(deleteCount), 0), len - actualStart);

    NativeArray returnValue;

    if (actualStart <= Integer.MAX_VALUE && actualDeleteCount <= Integer.MAX_VALUE && bulkable(sobj)) {
        try {
            returnValue =  new NativeArray(sobj.getArray().fastSplice((int)actualStart, (int)actualDeleteCount, items.length));

            // Since this is a dense bulkable array we can use faster defineOwnProperty to copy new elements
            int k = (int) actualStart;
            for (int i = 0; i < items.length; i++, k++) {
                sobj.defineOwnProperty(k, items[i]);
            }
        } catch (final UnsupportedOperationException uoe) {
            returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
        }
    } else {
        returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
    }

    return returnValue;
}
 
Example 15
Source File: NativeJSAdapter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private long callAdapteeLong(final String name, final Object... args) {
    return JSType.toLong(callAdaptee(name, args));
}
 
Example 16
Source File: NativeArray.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.4.12 Array.prototype.splice ( start, deleteCount [ item1 [ , item2 [ , ... ] ] ] )
 *
 * @param self self reference
 * @param args arguments
 * @return result of splice
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object splice(final Object self, final Object... args) {
    final Object obj = Global.toObject(self);

    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final Object start = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
    final Object deleteCount = args.length > 1 ? args[1] : ScriptRuntime.UNDEFINED;

    Object[] items;

    if (args.length > 2) {
        items = new Object[args.length - 2];
        System.arraycopy(args, 2, items, 0, items.length);
    } else {
        items = ScriptRuntime.EMPTY_ARRAY;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);

    final long actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long actualDeleteCount = Math.min(Math.max(JSType.toLong(deleteCount), 0), len - actualStart);

    NativeArray returnValue;

    if (actualStart <= Integer.MAX_VALUE && actualDeleteCount <= Integer.MAX_VALUE && bulkable(sobj)) {
        try {
            returnValue =  new NativeArray(sobj.getArray().fastSplice((int)actualStart, (int)actualDeleteCount, items.length));

            // Since this is a dense bulkable array we can use faster defineOwnProperty to copy new elements
            int k = (int) actualStart;
            for (int i = 0; i < items.length; i++, k++) {
                sobj.defineOwnProperty(k, items[i]);
            }
        } catch (final UnsupportedOperationException uoe) {
            returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
        }
    } else {
        returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
    }

    return returnValue;
}
 
Example 17
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.4.12 Array.prototype.splice ( start, deleteCount [ item1 [ , item2 [ , ... ] ] ] )
 *
 * @param self self reference
 * @param args arguments
 * @return result of splice
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object splice(final Object self, final Object... args) {
    final Object obj = Global.toObject(self);

    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final Object start = args.length > 0 ? args[0] : ScriptRuntime.UNDEFINED;
    final Object deleteCount = args.length > 1 ? args[1] : ScriptRuntime.UNDEFINED;

    Object[] items;

    if (args.length > 2) {
        items = new Object[args.length - 2];
        System.arraycopy(args, 2, items, 0, items.length);
    } else {
        items = ScriptRuntime.EMPTY_ARRAY;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);

    final long actualStart = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long actualDeleteCount = Math.min(Math.max(JSType.toLong(deleteCount), 0), len - actualStart);

    NativeArray returnValue;

    if (actualStart <= Integer.MAX_VALUE && actualDeleteCount <= Integer.MAX_VALUE && bulkable(sobj)) {
        try {
            returnValue =  new NativeArray(sobj.getArray().fastSplice((int)actualStart, (int)actualDeleteCount, items.length));

            // Since this is a dense bulkable array we can use faster defineOwnProperty to copy new elements
            int k = (int) actualStart;
            for (int i = 0; i < items.length; i++, k++) {
                sobj.defineOwnProperty(k, items[i]);
            }
        } catch (final UnsupportedOperationException uoe) {
            returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
        }
    } else {
        returnValue = slowSplice(sobj, actualStart, actualDeleteCount, items, len);
    }

    return returnValue;
}
 
Example 18
Source File: LiteralNode.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch long value of node
 *
 * @return long value of node
 */
public long getLong() {
    return JSType.toLong(value);
}
 
Example 19
Source File: LiteralNode.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch long value of node
 *
 * @return long value of node
 */
public long getLong() {
    return JSType.toLong(value);
}
 
Example 20
Source File: LiteralNode.java    From nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch long value of node
 *
 * @return long value of node
 */
public long getLong() {
    return JSType.toLong(value);
}