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

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayIndex#isValidArrayIndex() . 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 long key, final int 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 void set(final Object 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(key, JSType.toObject(value), strict);
}
 
Example 3
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final Object key, final int 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(key, JSType.toObject(value), strict);
}
 
Example 4
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final long key, final double 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 5
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean has(final Object 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 6
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean has(final int 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 7
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 8
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final Object key, final Object 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;
    }

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

    setObject(find, strict, propName, value);
}
 
Example 9
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final double key, final int 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 10
Source File: ScriptObject.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void set(final long 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 11
Source File: JSONFunctions.java    From hottub 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 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, 0);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 12
Source File: JSONFunctions.java    From openjdk-jdk8u 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 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, 0);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 13
Source File: JSONFunctions.java    From jdk8u_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 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, 0);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 14
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 15
Source File: JSONFunctions.java    From jdk8u60 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 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, 0);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 16
Source File: JSONFunctions.java    From TencentKona-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 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, 0);
    } else {
        // add new property
        sobj.addOwnProperty(name, Property.WRITABLE_ENUMERABLE_CONFIGURABLE, value);
    }
}
 
Example 17
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);
}
 
Example 18
Source File: NativeArray.java    From openjdk-jdk8u 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 19
Source File: NativeArray.java    From jdk8u_nashorn 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 20
Source File: NativeArray.java    From jdk8u60 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);
}