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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#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: NativeError.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.stack
 * "stack" property is a string typed value containing JavaScript stack frames.
 * Each frame information is separated bv "\n" character.
 *
 * @param self  self reference
 *
 * @return      value of "stack" property
 */
public static Object getStack(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.has(STACK)) {
        return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
        final Object value = getScriptStackString(sobj, (Throwable)exception);
        if (sobj.hasOwnProperty(STACK)) {
            sobj.put(STACK, value, false);
        } else {
            sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
        }

        return value;
    }

    return UNDEFINED;
}
 
Example 2
Source File: GenericPropertyDescriptor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public PropertyDescriptor fillFrom(final ScriptObject sobj) {
    if (sobj.has(CONFIGURABLE)) {
        this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
    } else {
        delete(CONFIGURABLE, false);
    }

    if (sobj.has(ENUMERABLE)) {
        this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
    } else {
        delete(ENUMERABLE, false);
    }

    return this;
}
 
Example 3
Source File: GenericPropertyDescriptor.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public PropertyDescriptor fillFrom(final ScriptObject sobj) {
    if (sobj.has(CONFIGURABLE)) {
        this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
    } else {
        delete(CONFIGURABLE, false);
    }

    if (sobj.has(ENUMERABLE)) {
        this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
    } else {
        delete(ENUMERABLE, false);
    }

    return this;
}
 
Example 4
Source File: NativeError.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.stack
 * "stack" property is a string typed value containing JavaScript stack frames.
 * Each frame information is separated bv "\n" character.
 *
 * @param self  self reference
 *
 * @return      value of "stack" property
 */
public static Object getStack(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.has(STACK)) {
        return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
        final Object value = getScriptStackString(sobj, (Throwable)exception);
        if (sobj.hasOwnProperty(STACK)) {
            sobj.put(STACK, value, false);
        } else {
            sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
        }

        return value;
    }

    return UNDEFINED;
}
 
Example 5
Source File: NativeArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
 *
 * @param self           self reference
 * @param searchElement  element to search for
 * @param fromIndex      start index of search
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double indexOf(final Object self, final Object searchElement, final Object fromIndex) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());
        if (len == 0) {
            return -1;
        }

        final long         n = JSType.toLong(fromIndex);
        if (n >= len) {
            return -1;
        }


        for (long k = Math.max(0, n < 0 ? len - Math.abs(n) : n); k < len; k++) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        //fallthru
    }

    return -1;
}
 
Example 6
Source File: NativeError.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 *
 * @param self self reference
 * @param errorObj the error object
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    final ScriptObject sobj = Global.checkObject(errorObj);
    initException(sobj);
    sobj.delete(STACK, false);
    if (! sobj.has("stack")) {
        final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", GET_STACK);
        final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", SET_STACK);
        sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    }
    return UNDEFINED;
}
 
Example 7
Source File: NativeError.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 *
 * @param self self reference
 * @param errorObj the error object
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    final ScriptObject sobj = Global.checkObject(errorObj);
    initException(sobj);
    sobj.delete(STACK, false);
    if (! sobj.has("stack")) {
        final ScriptFunction getStack = ScriptFunction.createBuiltin("getStack", GET_STACK);
        final ScriptFunction setStack = ScriptFunction.createBuiltin("setStack", SET_STACK);
        sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    }
    return UNDEFINED;
}
 
Example 8
Source File: NativeError.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 *
 * @param self self reference
 * @param errorObj the error object
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    final ScriptObject sobj = Global.checkObject(errorObj);
    initException(sobj);
    sobj.delete(STACK, false);
    if (! sobj.has("stack")) {
        final ScriptFunction getStack = ScriptFunction.createBuiltin("getStack", GET_STACK);
        final ScriptFunction setStack = ScriptFunction.createBuiltin("setStack", SET_STACK);
        sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    }
    return UNDEFINED;
}
 
Example 9
Source File: NativeDebug.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the capacity of the event queue
 * @param self self reference
 * @return capacity of event queue as an integer
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getEventQueueCapacity(final Object self) {
    final ScriptObject sobj = (ScriptObject)self;
    Integer cap;
    if (sobj.has(EVENT_QUEUE_CAPACITY)) {
        cap = JSType.toInt32(sobj.get(EVENT_QUEUE_CAPACITY));
    } else {
        setEventQueueCapacity(self, cap = RuntimeEvent.RUNTIME_EVENT_QUEUE_SIZE);
    }
    return cap;
}
 
Example 10
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
 *
 * @param self           self reference
 * @param searchElement  element to search for
 * @param fromIndex      start index of search
 * @return index of element, or -1 if not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double indexOf(final Object self, final Object searchElement, final Object fromIndex) {
    try {
        final ScriptObject sobj = (ScriptObject)Global.toObject(self);
        final long         len  = JSType.toUint32(sobj.getLength());
        if (len == 0) {
            return -1;
        }

        final long         n = JSType.toLong(fromIndex);
        if (n >= len) {
            return -1;
        }


        for (long k = Math.max(0, n < 0 ? len - Math.abs(n) : n); k < len; k++) {
            if (sobj.has(k)) {
                if (ScriptRuntime.EQ_STRICT(sobj.get(k), searchElement)) {
                    return k;
                }
            }
        }
    } catch (final ClassCastException | NullPointerException e) {
        //fallthru
    }

    return -1;
}
 
Example 11
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 12
Source File: NativeArray.java    From openjdk-8 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 13
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 14
Source File: NativeArray.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.4.4.9 Array.prototype.shift ()
 *
 * @param self self reference
 * @return shifted array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object shift(final Object self) {
    final Object obj = Global.toObject(self);

    Object first = ScriptRuntime.UNDEFINED;

    if (!(obj instanceof ScriptObject)) {
        return first;
    }

    final ScriptObject sobj   = (ScriptObject) obj;

    long len = JSType.toUint32(sobj.getLength());

    if (len > 0) {
        first = sobj.get(0);

        if (bulkable(sobj)) {
            sobj.getArray().shiftLeft(1);
        } else {
            boolean hasPrevious = true;
            for (long k = 1; k < len; k++) {
                final boolean hasCurrent = sobj.has(k);
                if (hasCurrent) {
                    sobj.set(k - 1, sobj.get(k), CALLSITE_STRICT);
                } else if (hasPrevious) {
                    sobj.delete(k - 1, true);
                }
                hasPrevious = hasCurrent;
            }
        }
        sobj.delete(--len, true);
    } else {
        len = 0;
    }

    sobj.set("length", len, CALLSITE_STRICT);

    return first;
}
 
Example 15
Source File: NativeArray.java    From nashorn 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, true);
            } else {
                sobj.delete(to, true);
            }
        }

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

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

    return newLength;
}
 
Example 16
Source File: NativeError.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self self reference
 *
 * @return column number from which error was thrown
 */
public static Object getColumnNumber(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}
 
Example 17
Source File: NativeError.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self self reference
 *
 * @return line number from which error was thrown
 */
public static Object getLineNumber(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    return sobj.has(LINENUMBER) ? sobj.get(LINENUMBER) : ECMAException.getLineNumber(sobj);
}
 
Example 18
Source File: NativeError.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self self reference
 *
 * @return column number from which error was thrown
 */
public static Object getColumnNumber(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}
 
Example 19
Source File: NativeError.java    From nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self self reference
 *
 * @return column number from which error was thrown
 */
public static Object getColumnNumber(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}
 
Example 20
Source File: NativeError.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self self reference
 *
 * @return line number from which error was thrown
 */
public static Object getLineNumber(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(LINENUMBER) ? sobj.get(LINENUMBER) : ECMAException.getLineNumber(sobj);
}