Java Code Examples for jdk.nashorn.internal.runtime.arrays.ArrayData#indexIterator()

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayData#indexIterator() . 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 5 votes vote down vote up
/**
 * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 2
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 3
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 4
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
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.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 6
Source File: ScriptObject.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 7
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.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 8
Source File: ScriptObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 9
Source File: NativeArray.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 10
Source File: ScriptObject.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param <T> the type returned keys.
 * @param type the type of keys to return, either {@code String.class} or {@code Symbol.class}.
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already. Used to
 *                      filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
@SuppressWarnings("unchecked")
protected <T> T[] getOwnKeys(final Class<T> type, final boolean all, final Set<T> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    if (type == String.class) {
        for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
            keys.add(JSType.toString(iter.next().longValue()));
        }
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final Object key = property.getKey();
        if (!type.isInstance(key)) {
            continue;
        }
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add((T) key);
            }
        }
    }

    return keys.toArray((T[]) Array.newInstance(type, keys.size()));
}
 
Example 11
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 12
Source File: ScriptObject.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 13
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.11 Array.prototype.sort ( comparefn )
 *
 * @param self       self reference
 * @param comparefn  element comparison function
 * @return sorted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject sort(final Object self, final Object comparefn) {
    try {
        final ScriptObject sobj    = (ScriptObject) self;
        final long         len     = JSType.toUint32(sobj.getLength());
        ArrayData          array   = sobj.getArray();

        if (len > 1) {
            // Get only non-missing elements. Missing elements go at the end
            // of the sorted array. So, just don't copy these to sort input.
            final ArrayList<Object> src = new ArrayList<>();

            for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
                final long index = iter.next();
                if (index >= len) {
                    break;
                }
                src.add(array.getObject((int)index));
            }

            final Object[] sorted = sort(src.toArray(), comparefn);

            for (int i = 0; i < sorted.length; i++) {
                array = array.set(i, sorted[i], true);
            }

            // delete missing elements - which are at the end of sorted array
            if (sorted.length != len) {
                array = array.delete(sorted.length, len - 1);
            }

            sobj.setArray(array);
        }

        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 14
Source File: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * return an array of own property keys associated with the object.
 *
 * @param all True if to include non-enumerable keys.
 * @param nonEnumerable set of non-enumerable properties seen already.Used
   to filter out shadowed, but enumerable properties from proto children.
 * @return Array of keys.
 */
protected String[] getOwnKeys(final boolean all, final Set<String> nonEnumerable) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();

    for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) {
        keys.add(JSType.toString(iter.next().longValue()));
    }

    for (final Property property : selfMap.getProperties()) {
        final boolean enumerable = property.isEnumerable();
        final String key = property.getKey();
        if (all) {
            keys.add(key);
        } else if (enumerable) {
            // either we don't have non-enumerable filter set or filter set
            // does not contain the current property.
            if (nonEnumerable == null || !nonEnumerable.contains(key)) {
                keys.add(key);
            }
        } else {
            // store this non-enumerable property for later proto walk
            if (nonEnumerable != null) {
                nonEnumerable.add(key);
            }
        }
    }

    return keys.toArray(new String[keys.size()]);
}