Java Code Examples for jdk.nashorn.internal.runtime.arrays.ArrayIndex#getArrayIndex()

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayIndex#getArrayIndex() . 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: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final int key, final long value, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);

    if (ArrayIndex.isValidArrayIndex(index)) {
        if (getArray().has(index)) {
            setArray(getArray().set(index, value, strict));
        } else {
            doesNotHave(index, value, strict);
        }

        return;
    }

    set(JSType.toObject(key), JSType.toObject(value), strict);
}
 
Example 2
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean has(final double key) {
    final int index = ArrayIndex.getArrayIndex(key);

    if (ArrayIndex.isValidArrayIndex(index)) {
        for (ScriptObject self = this; self != null; self = self.getProto()) {
            if (self.getArray().has(index)) {
                return true;
            }
        }
    }

    final FindProperty find = findProperty(JSType.toString(key), true);

    return find != null;
}
 
Example 3
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object get(final Object key) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    if (index >= 0 && index < value.length()) {
        return String.valueOf(value.charAt(index));
    }
    return super.get(primitiveKey);
}
 
Example 4
Source File: JSONFunctions.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void setPropertyValue(final ScriptObject sobj, final String name, final Object value, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(name);
    if (ArrayIndex.isValidArrayIndex(index)) {
        // array index key
        sobj.defineOwnProperty(index, value);
    } else if (sobj.getMap().findProperty(name) != null) {
        // pre-existing non-inherited property, call set
        sobj.set(name, value, strict);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 5
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final long key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getInt(index);
    }

    return getInt(index, JSType.toString(key));
}
 
Example 6
Source File: NativeString.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getOwnPropertyDescriptor(final String key) {
    final int index = ArrayIndex.getArrayIndex(key);
    if (index >= 0 && index < value.length()) {
        final Global global = Global.instance();
        return global.newDataDescriptor(String.valueOf(value.charAt(index)), false, true, false);
    }

    return super.getOwnPropertyDescriptor(key);
}
 
Example 7
Source File: JSONFunctions.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void setPropertyValue(final ScriptObject sobj, final String name, final Object value, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(name);
    if (ArrayIndex.isValidArrayIndex(index)) {
        // array index key
        sobj.defineOwnProperty(index, value);
    } else if (sobj.getMap().findProperty(name) != null) {
        // pre-existing non-inherited property, call set
        sobj.set(name, value, strict);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 8
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean delete(final Object key, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        if (array.canDelete(index, strict)) {
            setArray(array.delete(index));
            return true;
        }
        return false;
    }

    return deleteObject(key, strict);
}
 
Example 9
Source File: NativeArguments.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw ) as specialized in
 * ECMA 10.6 for Arguments object.
 */
@Override
public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
    final int index = ArrayIndex.getArrayIndex(key);
    if (index >= 0) {
        final boolean isMapped = isMapped(index);
        final Object oldValue = isMapped ? getArray().getObject(index) : null;

        if (!super.defineOwnProperty(key, propertyDesc, false)) {
            if (reject) {
                throw typeError("cant.redefine.property",  key, ScriptRuntime.safeToString(this));
            }
            return false;
        }

        if (isMapped) {
            // When mapped argument is redefined, if new descriptor is accessor property
            // or data-non-writable property, we have to "unmap" (unlink).
            final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);
            if (desc.type() == PropertyDescriptor.ACCESSOR) {
                setDeleted(index, oldValue);
            } else if (desc.has(PropertyDescriptor.WRITABLE) && !desc.isWritable()) {
                // delete and set value from new descriptor if it has one, otherwise use old value
                setDeleted(index, desc.has(PropertyDescriptor.VALUE) ? desc.getValue() : oldValue);
            } else if (desc.has(PropertyDescriptor.VALUE)) {
                setArray(getArray().set(index, desc.getValue(), false));
            }
        }

        return true;
    }

    return super.defineOwnProperty(key, propertyDesc, reject);
}
 
Example 10
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getLong(final Object key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getLong(index);
    }

    return getLong(index, JSType.toString(key));
}
 
Example 11
Source File: NativeString.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Object get(final Object self, final Object key) {
    final CharSequence cs = JSType.toCharSequence(self);
    final int index = ArrayIndex.getArrayIndex(key);
    if (index >= 0 && index < cs.length()) {
        return String.valueOf(cs.charAt(index));
    }
    return ((ScriptObject) Global.toObject(self)).get(key);
}
 
Example 12
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getInt(final Object key) {
    final int index = ArrayIndex.getArrayIndex(key);
    final ArrayData array = getArray();

    if (array.has(index)) {
        return array.getInt(index);
    }

    return getInt(index, JSType.toString(key));
}
 
Example 13
Source File: NativeArguments.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final double key, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
}
 
Example 14
Source File: NativeArguments.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final double key, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
}
 
Example 15
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw )
 */
@Override
public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
    final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);

    // never be undefined as "length" is always defined and can't be deleted for arrays
    // Step 1
    final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length");

    // Step 2
    // get old length and convert to long. Always a Long/Uint32 but we take the safe road.
    final long oldLen = JSType.toUint32(oldLenDesc.getValue());

    // Step 3
    if ("length".equals(key)) {
        // check for length being made non-writable
        final boolean result = defineLength(oldLen, oldLenDesc, desc, reject);
        if (desc.has(WRITABLE) && !desc.isWritable()) {
            setIsLengthNotWritable();
        }
        return result;
    }

    // Step 4a
    final int index = ArrayIndex.getArrayIndex(key);
    if (ArrayIndex.isValidArrayIndex(index)) {
        final long longIndex = ArrayIndex.toLongIndex(index);
        // Step 4b
        // setting an element beyond current length, but 'length' is not writable
        if (longIndex >= oldLen && !oldLenDesc.isWritable()) {
            if (reject) {
                throw typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));
            }
            return false;
        }

        // Step 4c
        // set the new array element
        final boolean succeeded = super.defineOwnProperty(key, desc, false);

        // Step 4d
        if (!succeeded) {
            if (reject) {
                throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
            }
            return false;
        }

        // Step 4e -- adjust new length based on new element index that is set
        if (longIndex >= oldLen) {
            oldLenDesc.setValue(longIndex + 1);
            super.defineOwnProperty("length", oldLenDesc, false);
        }

        // Step 4f
        return true;
    }

    // not an index property
    return super.defineOwnProperty(key, desc, reject);
}
 
Example 16
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean hasOwnProperty(final double key) {
    final int index = ArrayIndex.getArrayIndex(key);
    return isValidStringIndex(index) || super.hasOwnProperty(key);
}
 
Example 17
Source File: NativeArguments.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final int key, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
}
 
Example 18
Source File: NativeArguments.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final Object key, final boolean strict) {
    final Object primitiveKey = JSType.toPrimitive(key, String.class);
    final int index = ArrayIndex.getArrayIndex(primitiveKey);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(primitiveKey, strict);
}
 
Example 19
Source File: NativeArguments.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean delete(final int key, final boolean strict) {
    final int index = ArrayIndex.getArrayIndex(key);
    return isMapped(index) ? deleteMapped(index, strict) : super.delete(key, strict);
}
 
Example 20
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.5.1 [[DefineOwnProperty]] ( P, Desc, Throw )
 */
@Override
public boolean defineOwnProperty(final String key, final Object propertyDesc, final boolean reject) {
    final PropertyDescriptor desc = toPropertyDescriptor(Global.instance(), propertyDesc);

    // never be undefined as "length" is always defined and can't be deleted for arrays
    // Step 1
    final PropertyDescriptor oldLenDesc = (PropertyDescriptor) super.getOwnPropertyDescriptor("length");

    // Step 2
    // get old length and convert to long. Always a Long/Uint32 but we take the safe road.
    final long oldLen = JSType.toUint32(oldLenDesc.getValue());

    // Step 3
    if ("length".equals(key)) {
        // check for length being made non-writable
        final boolean result = defineLength(oldLen, oldLenDesc, desc, reject);
        if (desc.has(WRITABLE) && !desc.isWritable()) {
            setIsLengthNotWritable();
        }
        return result;
    }

    // Step 4a
    final int index = ArrayIndex.getArrayIndex(key);
    if (ArrayIndex.isValidArrayIndex(index)) {
        final long longIndex = ArrayIndex.toLongIndex(index);
        // Step 4b
        // setting an element beyond current length, but 'length' is not writable
        if (longIndex >= oldLen && !oldLenDesc.isWritable()) {
            if (reject) {
                throw typeError("property.not.writable", Long.toString(longIndex), ScriptRuntime.safeToString(this));
            }
            return false;
        }

        // Step 4c
        // set the new array element
        final boolean succeeded = super.defineOwnProperty(key, desc, false);

        // Step 4d
        if (!succeeded) {
            if (reject) {
                throw typeError("cant.redefine.property", key, ScriptRuntime.safeToString(this));
            }
            return false;
        }

        // Step 4e -- adjust new length based on new element index that is set
        if (longIndex >= oldLen) {
            oldLenDesc.setValue(longIndex + 1);
            super.defineOwnProperty("length", oldLenDesc, false);
        }

        // Step 4f
        return true;
    }

    // not an index property
    return super.defineOwnProperty(key, desc, reject);
}