Java Code Examples for jdk.nashorn.internal.runtime.PropertyDescriptor#has()

The following examples show how to use jdk.nashorn.internal.runtime.PropertyDescriptor#has() . 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: GenericPropertyDescriptor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasAndEquals(final PropertyDescriptor other) {
    if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
        if (isConfigurable() != other.isConfigurable()) {
            return false;
        }
    }

    if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
        if (isEnumerable() != other.isEnumerable()) {
            return false;
        }
    }

    return true;
}
 
Example 2
Source File: GenericPropertyDescriptor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasAndEquals(final PropertyDescriptor other) {
    if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
        if (isConfigurable() != other.isConfigurable()) {
            return false;
        }
    }

    if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
        if (isEnumerable() != other.isEnumerable()) {
            return false;
        }
    }

    return true;
}
 
Example 3
Source File: GenericPropertyDescriptor.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasAndEquals(final PropertyDescriptor other) {
    if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
        if (isConfigurable() != other.isConfigurable()) {
            return false;
        }
    }

    if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
        if (isEnumerable() != other.isEnumerable()) {
            return false;
        }
    }

    return true;
}
 
Example 4
Source File: GenericPropertyDescriptor.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean hasAndEquals(final PropertyDescriptor other) {
    if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
        if (isConfigurable() != other.isConfigurable()) {
            return false;
        }
    }

    if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
        if (isEnumerable() != other.isEnumerable()) {
            return false;
        }
    }

    return true;
}
 
Example 5
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 6
Source File: NativeArguments.java    From hottub 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 7
Source File: NativeArguments.java    From openjdk-8 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 8
Source File: NativeArguments.java    From TencentKona-8 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 9
Source File: NativeArguments.java    From openjdk-8-source 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: NativeArguments.java    From nashorn 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 11
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 12
Source File: NativeArray.java    From openjdk-jdk9 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 Object 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.toString(), 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 13
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 14
Source File: NativeArray.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
    // Step 3a
    if (!desc.has(VALUE)) {
        return super.defineOwnProperty("length", desc, reject);
    }

    // Step 3b
    final PropertyDescriptor newLenDesc = desc;

    // Step 3c and 3d - get new length and convert to long
    final long newLen = NativeArray.validLength(newLenDesc.getValue());

    // Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
    newLenDesc.setValue(JSType.toNarrowestNumber(newLen));

    // Step 3f
    // increasing array length - just need to set new length value (and attributes if any) and return
    if (newLen >= oldLen) {
        return super.defineOwnProperty("length", newLenDesc, reject);
    }

    // Step 3g
    if (!oldLenDesc.isWritable()) {
        if (reject) {
            throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
        }
        return false;
    }

    // Step 3h and 3i
    final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
    if (!newWritable) {
        newLenDesc.setWritable(true);
    }

    // Step 3j and 3k
    final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
    if (!succeeded) {
        return false;
    }

    // Step 3l
    // make sure that length is set till the point we can delete the old elements
    long o = oldLen;
    while (newLen < o) {
        o--;
        final boolean deleteSucceeded = delete(o, false);
        if (!deleteSucceeded) {
            newLenDesc.setValue(o + 1);
            if (!newWritable) {
                newLenDesc.setWritable(false);
            }
            super.defineOwnProperty("length", newLenDesc, false);
            if (reject) {
                throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
            }
            return false;
        }
    }

    // Step 3m
    if (!newWritable) {
        // make 'length' property not writable
        final ScriptObject newDesc = Global.newEmptyInstance();
        newDesc.set(WRITABLE, false, 0);
        return super.defineOwnProperty("length", newDesc, false);
    }

    return true;
}
 
Example 15
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
    // Step 3a
    if (!desc.has(VALUE)) {
        return super.defineOwnProperty("length", desc, reject);
    }

    // Step 3b
    final PropertyDescriptor newLenDesc = desc;

    // Step 3c and 3d - get new length and convert to long
    final long newLen = NativeArray.validLength(newLenDesc.getValue());

    // Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
    newLenDesc.setValue(JSType.toNarrowestNumber(newLen));

    // Step 3f
    // increasing array length - just need to set new length value (and attributes if any) and return
    if (newLen >= oldLen) {
        return super.defineOwnProperty("length", newLenDesc, reject);
    }

    // Step 3g
    if (!oldLenDesc.isWritable()) {
        if (reject) {
            throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
        }
        return false;
    }

    // Step 3h and 3i
    final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
    if (!newWritable) {
        newLenDesc.setWritable(true);
    }

    // Step 3j and 3k
    final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
    if (!succeeded) {
        return false;
    }

    // Step 3l
    // make sure that length is set till the point we can delete the old elements
    long o = oldLen;
    while (newLen < o) {
        o--;
        final boolean deleteSucceeded = delete(o, false);
        if (!deleteSucceeded) {
            newLenDesc.setValue(o + 1);
            if (!newWritable) {
                newLenDesc.setWritable(false);
            }
            super.defineOwnProperty("length", newLenDesc, false);
            if (reject) {
                throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
            }
            return false;
        }
    }

    // Step 3m
    if (!newWritable) {
        // make 'length' property not writable
        final ScriptObject newDesc = Global.newEmptyInstance();
        newDesc.set(WRITABLE, false, 0);
        return super.defineOwnProperty("length", newDesc, false);
    }

    return true;
}
 
Example 16
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 17
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
    // Step 3a
    if (!desc.has(VALUE)) {
        return super.defineOwnProperty("length", desc, reject);
    }

    // Step 3b
    final PropertyDescriptor newLenDesc = desc;

    // Step 3c and 3d - get new length and convert to long
    final long newLen = NativeArray.validLength(newLenDesc.getValue());

    // Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
    newLenDesc.setValue(JSType.toNarrowestNumber(newLen));

    // Step 3f
    // increasing array length - just need to set new length value (and attributes if any) and return
    if (newLen >= oldLen) {
        return super.defineOwnProperty("length", newLenDesc, reject);
    }

    // Step 3g
    if (!oldLenDesc.isWritable()) {
        if (reject) {
            throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
        }
        return false;
    }

    // Step 3h and 3i
    final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
    if (!newWritable) {
        newLenDesc.setWritable(true);
    }

    // Step 3j and 3k
    final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
    if (!succeeded) {
        return false;
    }

    // Step 3l
    // make sure that length is set till the point we can delete the old elements
    long o = oldLen;
    while (newLen < o) {
        o--;
        final boolean deleteSucceeded = delete(o, false);
        if (!deleteSucceeded) {
            newLenDesc.setValue(o + 1);
            if (!newWritable) {
                newLenDesc.setWritable(false);
            }
            super.defineOwnProperty("length", newLenDesc, false);
            if (reject) {
                throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
            }
            return false;
        }
    }

    // Step 3m
    if (!newWritable) {
        // make 'length' property not writable
        final ScriptObject newDesc = Global.newEmptyInstance();
        newDesc.set(WRITABLE, false, 0);
        return super.defineOwnProperty("length", newDesc, false);
    }

    return true;
}
 
Example 18
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);
}
 
Example 19
Source File: NativeArray.java    From TencentKona-8 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 TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private boolean defineLength(final long oldLen, final PropertyDescriptor oldLenDesc, final PropertyDescriptor desc, final boolean reject) {
    // Step 3a
    if (!desc.has(VALUE)) {
        return super.defineOwnProperty("length", desc, reject);
    }

    // Step 3b
    final PropertyDescriptor newLenDesc = desc;

    // Step 3c and 3d - get new length and convert to long
    final long newLen = NativeArray.validLength(newLenDesc.getValue());

    // Step 3e - note that we need to convert to int or double as long is not considered a JS number type anymore
    newLenDesc.setValue(JSType.toNarrowestNumber(newLen));

    // Step 3f
    // increasing array length - just need to set new length value (and attributes if any) and return
    if (newLen >= oldLen) {
        return super.defineOwnProperty("length", newLenDesc, reject);
    }

    // Step 3g
    if (!oldLenDesc.isWritable()) {
        if (reject) {
            throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
        }
        return false;
    }

    // Step 3h and 3i
    final boolean newWritable = !newLenDesc.has(WRITABLE) || newLenDesc.isWritable();
    if (!newWritable) {
        newLenDesc.setWritable(true);
    }

    // Step 3j and 3k
    final boolean succeeded = super.defineOwnProperty("length", newLenDesc, reject);
    if (!succeeded) {
        return false;
    }

    // Step 3l
    // make sure that length is set till the point we can delete the old elements
    long o = oldLen;
    while (newLen < o) {
        o--;
        final boolean deleteSucceeded = delete(o, false);
        if (!deleteSucceeded) {
            newLenDesc.setValue(o + 1);
            if (!newWritable) {
                newLenDesc.setWritable(false);
            }
            super.defineOwnProperty("length", newLenDesc, false);
            if (reject) {
                throw typeError("property.not.writable", "length", ScriptRuntime.safeToString(this));
            }
            return false;
        }
    }

    // Step 3m
    if (!newWritable) {
        // make 'length' property not writable
        final ScriptObject newDesc = Global.newEmptyInstance();
        newDesc.set(WRITABLE, false, 0);
        return super.defineOwnProperty("length", newDesc, false);
    }

    return true;
}