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

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayData#length() . 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 nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...)
 *
 * @param self self reference
 * @param args arguments to push
 * @return array after pushes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object push(final Object self, final Object... args) {
    try {
        final ScriptObject sobj   = (ScriptObject)self;

        if (bulkable(sobj)) {
            if (sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
                final ArrayData newData = sobj.getArray().push(true, args);
                sobj.setArray(newData);
                return newData.length();
            }
            //fallthru
        }

        long len = JSType.toUint32(sobj.getLength());
        for (final Object element : args) {
            sobj.set(len++, element, true);
        }
        sobj.set("length", len, true);

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 2
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static double push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 3
Source File: NativeArray.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...)
 *
 * @param self self reference
 * @param args arguments to push
 * @return array after pushes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object push(final Object self, final Object... args) {
    try {
        final ScriptObject sobj   = (ScriptObject)self;

        if (bulkable(sobj)) {
            if (sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
                final ArrayData newData = sobj.getArray().push(true, args);
                sobj.setArray(newData);
                return newData.length();
            }
            //fallthru
        }

        long len = JSType.toUint32(sobj.getLength());
        for (final Object element : args) {
            sobj.set(len++, element, true);
        }
        sobj.set("length", len, true);

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 4
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static long push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 5
Source File: ScriptObject.java    From openjdk-8 with GNU General Public License v2.0 6 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.
 * @return Array of keys.
 */
public String[] getOwnKeys(final boolean all) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();
    final long length      = array.length();

    for (long i = 0; i < length; i = array.nextIndex(i)) {
        if (array.has((int)i)) {
            keys.add(JSType.toString(i));
        }
    }

    for (final Property property : selfMap.getProperties()) {
        if (all || property.isEnumerable()) {
            keys.add(property.getKey());
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 6
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static double push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 7
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...) specialized for single object argument
 *
 * @param self self reference
 * @param arg argument to push
 * @return array after pushes
 */
@SpecializedFunction
public static double push(final Object self, final Object arg) {
    try {
        final ScriptObject sobj = (ScriptObject)self;
        final ArrayData arrayData = sobj.getArray();
        final long length = arrayData.length();
        if (bulkable(sobj) && length < JSType.MAX_UINT) {
            sobj.setArray(arrayData.push(true, arg));
            return length + 1;
        }

        long len = JSType.toUint32(sobj.getLength());
        sobj.set(len++, arg, CALLSITE_STRICT);
        sobj.set("length", len, CALLSITE_STRICT);
        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 8
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 6 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.
 * @return Array of keys.
 */
public String[] getOwnKeys(final boolean all) {
    final List<Object> keys    = new ArrayList<>();
    final PropertyMap  selfMap = this.getMap();

    final ArrayData array  = getArray();
    final long length      = array.length();

    for (long i = 0; i < length; i = array.nextIndex(i)) {
        if (array.has((int)i)) {
            keys.add(JSType.toString(i));
        }
    }

    for (final Property property : selfMap.getProperties()) {
        if (all || property.isEnumerable()) {
            keys.add(property.getKey());
        }
    }

    return keys.toArray(new String[keys.size()]);
}
 
Example 9
Source File: JSONParser.java    From openjdk-jdk8u-backup 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 10
Source File: ScriptObject.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Numeric length setter for length property
  *
  * @param newLength new length to set
  */
public final void setLength(final long newLength) {
    ArrayData data = getArray();
    final long arrayLength = data.length();
    if (newLength == arrayLength) {
        return;
    }

    if (newLength > arrayLength) {
        setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false));
        return;
    }

    if (newLength < arrayLength) {
       long actualLength = newLength;

       // Check for numeric keys in property map and delete them or adjust length, depending on whether
       // they're defined as configurable. See ES5 #15.4.5.2
       if (getMap().containsArrayKeys()) {

           for (long l = arrayLength - 1; l >= newLength; l--) {
               final FindProperty find = findProperty(JSType.toString(l), false);

               if (find != null) {

                   if (find.getProperty().isConfigurable()) {
                       deleteOwnProperty(find.getProperty());
                   } else {
                       actualLength = l + 1;
                       break;
                   }
               }
           }
       }

       setArray(data.shrink(actualLength));
       data.setLength(actualLength);
   }
}
 
Example 11
Source File: JSONParser.java    From hottub 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: ScriptObject.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Numeric length setter for length property
  *
  * @param newLength new length to set
  */
public final void setLength(final long newLength) {
    ArrayData data = getArray();
    final long arrayLength = data.length();
    if (newLength == arrayLength) {
        return;
    }

    if (newLength > arrayLength) {
        setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false));
        return;
    }

    if (newLength < arrayLength) {
       long actualLength = newLength;

       // Check for numeric keys in property map and delete them or adjust length, depending on whether
       // they're defined as configurable. See ES5 #15.4.5.2
       if (getMap().containsArrayKeys()) {

           for (long l = arrayLength - 1; l >= newLength; l--) {
               final FindProperty find = findProperty(JSType.toString(l), false);

               if (find != null) {

                   if (find.getProperty().isConfigurable()) {
                       deleteOwnProperty(find.getProperty());
                   } else {
                       actualLength = l + 1;
                       break;
                   }
               }
           }
       }

       setArray(data.shrink(actualLength));
       data.setLength(actualLength);
   }
}
 
Example 13
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 14
Source File: JSONParser.java    From TencentKona-8 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 15
Source File: ScriptObject.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Numeric length setter for length property
  *
  * @param newLength new length to set
  */
public final void setLength(final long newLength) {
    ArrayData data = getArray();
    final long arrayLength = data.length();
    if (newLength == arrayLength) {
        return;
    }

    if (newLength > arrayLength) {
        setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false));
        return;
    }

    if (newLength < arrayLength) {
       long actualLength = newLength;

       // Check for numeric keys in property map and delete them or adjust length, depending on whether
       // they're defined as configurable. See ES5 #15.4.5.2
       if (getMap().containsArrayKeys()) {

           for (long l = arrayLength - 1; l >= newLength; l--) {
               final FindProperty find = findProperty(JSType.toString(l), false);

               if (find != null) {

                   if (find.getProperty().isConfigurable()) {
                       deleteOwnProperty(find.getProperty());
                   } else {
                       actualLength = l + 1;
                       break;
                   }
               }
           }
       }

       setArray(data.shrink(actualLength));
       data.setLength(actualLength);
   }
}
 
Example 16
Source File: JSONParser.java    From openjdk-jdk8u 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 17
Source File: JSONParser.java    From jdk8u60 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 18
Source File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Numeric length setter for length property
  *
  * @param newLength new length to set
  */
public final void setLength(final long newLength) {
    ArrayData data = getArray();
    final long arrayLength = data.length();
    if (newLength == arrayLength) {
        return;
    }

    if (newLength > arrayLength) {
        setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false));
        return;
    }

    if (newLength < arrayLength) {
       long actualLength = newLength;

       // Check for numeric keys in property map and delete them or adjust length, depending on whether
       // they're defined as configurable. See ES5 #15.4.5.2
       if (getMap().containsArrayKeys()) {

           for (long l = arrayLength - 1; l >= newLength; l--) {
               final FindProperty find = findProperty(JSType.toString(l), false);

               if (find != null) {

                   if (find.getProperty().isConfigurable()) {
                       deleteOwnProperty(find.getProperty());
                   } else {
                       actualLength = l + 1;
                       break;
                   }
               }
           }
       }

       setArray(data.shrink(actualLength));
       data.setLength(actualLength);
   }
}
 
Example 19
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if this ScriptObject has array entries. This means that someone has
 * set values with numeric keys in the object.
 *
 * Note: this can be O(n) up to the array length
 *
 * @return true if array entries exists.
 */
public boolean hasArrayEntries() {
    final ArrayData array = getArray();
    final long length = array.length();

    for (long i = 0; i < length; i++) {
        if (array.has((int)i)) {
            return true;
        }
    }

    return false;
}
 
Example 20
Source File: ScriptObject.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
  * Numeric length setter for length property
  *
  * @param newLength new length to set
  */
public final void setLength(final long newLength) {
    ArrayData data = getArray();
    final long arrayLength = data.length();
    if (newLength == arrayLength) {
        return;
    }

    if (newLength > arrayLength) {
        data = data.ensure(newLength - 1);
        if (data.canDelete(arrayLength, newLength - 1, false)) {
           data = data.delete(arrayLength, newLength - 1);
        }
        setArray(data);
        return;
    }

    if (newLength < arrayLength) {
       long actualLength = newLength;

       // Check for numeric keys in property map and delete them or adjust length, depending on whether
       // they're defined as configurable. See ES5 #15.4.5.2
       if (getMap().containsArrayKeys()) {

           for (long l = arrayLength - 1; l >= newLength; l--) {
               final FindProperty find = findProperty(JSType.toString(l), false);

               if (find != null) {

                   if (find.getProperty().isConfigurable()) {
                       deleteOwnProperty(find.getProperty());
                   } else {
                       actualLength = l + 1;
                       break;
                   }
               }
           }
       }

       setArray(data.shrink(actualLength));
       data.setLength(actualLength);
   }
}