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

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayData#delete() . 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 Object[] array) {
    this(ArrayData.allocate(array.length));

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();
    arrayData.ensure(array.length - 1);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();
    arrayData.ensure(array.length - 1);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

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

    ArrayData arrayData = this.getArray();

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        } else {
            arrayData = arrayData.set(index, value, false);
        }
    }

    this.setArray(arrayData);
}
 
Example 9
Source File: Global.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 10
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 11
Source File: JSONParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static ArrayData addArrayElement(final ArrayData arrayData, final int index, final Object value) {
    final long oldLength = arrayData.length();
    final long longIndex = ArrayIndex.toLongIndex(index);
    ArrayData newArrayData = arrayData;
    if (longIndex >= oldLength) {
        newArrayData = newArrayData.ensure(longIndex);
        if (longIndex > oldLength) {
            newArrayData = newArrayData.delete(oldLength, longIndex - 1);
        }
    }
    return newArrayData.set(index, value, false);
}
 
Example 12
Source File: Global.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 13
Source File: NativeArray.java    From openjdk-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 Object 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 (long i = 0; i < len; i = array.nextIndex(i)) {
                if (array.has((int) i)) {
                    src.add(array.getObject((int) i));
                }
            }

            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: Global.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 15
Source File: JSONParser.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static ArrayData addArrayElement(final ArrayData arrayData, final int index, final Object value) {
    final long oldLength = arrayData.length();
    final long longIndex = ArrayIndex.toLongIndex(index);
    ArrayData newArrayData = arrayData;
    if (longIndex >= oldLength) {
        newArrayData = newArrayData.ensure(longIndex);
        if (longIndex > oldLength) {
            newArrayData = newArrayData.delete(oldLength, longIndex - 1);
        }
    }
    return newArrayData.set(index, value, false);
}
 
Example 16
Source File: Global.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 17
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 18
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 19
Source File: Global.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allocate a new object array.
 *
 * @param initial object values.
 * @return the new array
 */
public static NativeArray allocate(final Object[] initial) {
    ArrayData arrayData = ArrayData.allocate(initial);

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

        if (value == ScriptRuntime.EMPTY) {
            arrayData = arrayData.delete(index);
        }
    }

    return new NativeArray(arrayData);
}
 
Example 20
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle when an array doesn't have a slot - possibly grow and/or convert array.
 *
 * @param index  key as index
 * @param value  element value
 * @param strict are we in strict mode
 */
private void doesNotHave(final int index, final Object value, final boolean strict) {
    final long oldLength = getArray().length();
    final long longIndex = ArrayIndex.toLongIndex(index);

    if (getMap().containsArrayKeys()) {
        final String key = JSType.toString(longIndex);
        final FindProperty find = findProperty(key, true);

        if (find != null) {
            setObject(find, strict, key, value);
            return;
        }
    }

    if (longIndex >= oldLength) {
        if (!isExtensible()) {
            if (strict) {
                throw typeError("object.non.extensible", JSType.toString(index), ScriptRuntime.safeToString(this));
            }
            return;
        }
        setArray(getArray().ensure(longIndex));
    }

    if (value instanceof Integer) {
        setArray(getArray().set(index, (int)value, strict));
    } else if (value instanceof Long) {
        setArray(getArray().set(index, (long)value, strict));
    } else if (value instanceof Double) {
        setArray(getArray().set(index, (double)value, strict));
    } else {
        setArray(getArray().set(index, value, strict));
    }

    if (longIndex > oldLength) {
        ArrayData array = getArray();

        if (array.canDelete(oldLength, (longIndex - 1), strict)) {
            array = array.delete(oldLength, (longIndex - 1));
        }

        setArray(array);
    }
}