Java Code Examples for jdk.nashorn.internal.runtime.JSType#toUint32()

The following examples show how to use jdk.nashorn.internal.runtime.JSType#toUint32() . 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 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 2
Source File: NativeArray.java    From TencentKona-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 length 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) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
            final ArrayData newData = sobj.getArray().push(true, args);
            sobj.setArray(newData);
            return JSType.toNarrowestNumber(newData.length());
        }

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

        return JSType.toNarrowestNumber(len);
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 3
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...)
 *
 * @param self self reference
 * @param args arguments to push
 * @return array length 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) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
            final ArrayData newData = sobj.getArray().push(true, args);
            sobj.setArray(newData);
            return newData.length();
        }

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

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 4
Source File: NativeRegExpExecResult.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Length getter
 * @param self self reference
 * @return length property value
 */
@Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_CONFIGURABLE)
public static Object length(final Object self) {
    if (self instanceof ScriptObject) {
        return (double) JSType.toUint32(((ScriptObject)self).getArray().length());
    }

    return 0;
}
 
Example 5
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.8 Array.prototype.reverse ()
 *
 * @param self self reference
 * @return reversed array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object reverse(final Object self) {
    try {
        final ScriptObject sobj   = (ScriptObject)self;
        final long         len    = JSType.toUint32(sobj.getLength());
        final long         middle = len / 2;

        for (long lower = 0; lower != middle; lower++) {
            final long    upper       = len - lower - 1;
            final Object  lowerValue  = sobj.get(lower);
            final Object  upperValue  = sobj.get(upper);
            final boolean lowerExists = sobj.has(lower);
            final boolean upperExists = sobj.has(upper);

            if (lowerExists && upperExists) {
                sobj.set(lower, upperValue, true);
                sobj.set(upper, lowerValue, true);
            } else if (!lowerExists && upperExists) {
                sobj.set(lower, upperValue, true);
                sobj.delete(upper, true);
            } else if (lowerExists && !upperExists) {
                sobj.delete(lower, true);
                sobj.set(upper, lowerValue, true);
            }
        }
        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 6
Source File: NativeArray.java    From jdk8u60 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 7
Source File: NativeArray.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.2.2 new Array (len)
 *
 * Specialized constructor for one double argument (length)
 *
 * @param newObj was the new operator used to instantiate this array
 * @param self   self reference
 * @param length array length
 * @return the new NativeArray
 */
@SpecializedFunction(isConstructor=true)
public static NativeArray construct(final boolean newObj, final Object self, final double length) {
    final long uint32length = JSType.toUint32(length);

    if (uint32length == length) {
        return new NativeArray(uint32length);
    }

    return construct(newObj, self, new Object[]{length});
}
 
Example 8
Source File: ScriptObjectIterator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
ScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) {
    super(includeUndefined);
    this.obj    = obj;
    this.length = JSType.toUint32(obj.getLength());
    this.index  = 0;
}
 
Example 9
Source File: JSObjectIterator.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
JSObjectIterator(final JSObject obj, final boolean includeUndefined) {
    super(includeUndefined);
    this.obj    = obj;
    this.length = JSType.toUint32(obj.hasMember("length")? obj.getMember("length") : 0);
    this.index  = 0;
}
 
Example 10
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.4.13 Array.prototype.unshift ( [ item1 [ , item2 [ , ... ] ] ] )
 *
 * @param self  self reference
 * @param items items for unshift
 * @return unshifted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object unshift(final Object self, final Object... items) {
    final Object obj = Global.toObject(self);

    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final ScriptObject sobj   = (ScriptObject)obj;
    final long         len    = JSType.toUint32(sobj.getLength());

    if (items == null) {
        return ScriptRuntime.UNDEFINED;
    }

    if (bulkable(sobj)) {
        sobj.getArray().shiftRight(items.length);

        for (int j = 0; j < items.length; j++) {
            sobj.setArray(sobj.getArray().set(j, items[j], true));
        }
    } else {
        for (long k = len; k > 0; k--) {
            final long from = k - 1;
            final long to = k + items.length - 1;

            if (sobj.has(from)) {
                final Object fromValue = sobj.get(from);
                sobj.set(to, fromValue, CALLSITE_STRICT);
            } else {
                sobj.delete(to, true);
            }
        }

        for (int j = 0; j < items.length; j++) {
            sobj.set(j, items[j], CALLSITE_STRICT);
        }
    }

    final long newLength = len + items.length;
    sobj.set("length", newLength, CALLSITE_STRICT);

    return JSType.toNarrowestNumber(newLength);
}
 
Example 11
Source File: NativeUint32Array.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private double getElem(final int index) {
    return JSType.toUint32(getRawElem(index));
}
 
Example 12
Source File: ReverseJSObjectIterator.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
ReverseJSObjectIterator(final JSObject obj, final boolean includeUndefined) {
    super(obj, includeUndefined);
    this.index = JSType.toUint32(obj.hasMember("length")? obj.getMember("length") : 0) - 1;
}
 
Example 13
Source File: ReverseJSObjectIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
ReverseJSObjectIterator(final JSObject obj, final boolean includeUndefined) {
    super(obj, includeUndefined);
    this.index = JSType.toUint32(obj.hasMember("length")? obj.getMember("length") : 0) - 1;
}
 
Example 14
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 15
Source File: ScriptObjectIterator.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
ScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) {
    super(includeUndefined);
    this.obj    = obj;
    this.length = JSType.toUint32(obj.getLength());
    this.index  = 0;
}
 
Example 16
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 17
Source File: LiteralNode.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch uint32 value of node.
 *
 * @return uint32 value of node.
 */
public long getUint32() {
    return JSType.toUint32(value);
}
 
Example 18
Source File: LiteralNode.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch uint32 value of node.
 *
 * @return uint32 value of node.
 */
public long getUint32() {
    return JSType.toUint32(value);
}
 
Example 19
Source File: LiteralNode.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch uint32 value of node.
 *
 * @return uint32 value of node.
 */
public long getUint32() {
    return JSType.toUint32(value);
}
 
Example 20
Source File: LiteralNode.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Fetch uint32 value of node.
 *
 * @return uint32 value of node.
 */
public long getUint32() {
    return JSType.toUint32(value);
}