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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#get() . 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: NashornScriptEngine.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isInterfaceImplemented(final Class<?> iface, final ScriptObject sobj) {
    for (final Method method : iface.getMethods()) {
        // ignore methods of java.lang.Object class
        if (method.getDeclaringClass() == Object.class) {
            continue;
        }

        // skip check for default methods - non-abstract, interface methods
        if (! Modifier.isAbstract(method.getModifiers())) {
            continue;
        }

        final Object obj = sobj.get(method.getName());
        if (! (obj instanceof ScriptFunction)) {
            return false;
        }
    }
    return true;
}
 
Example 2
Source File: DataPropertyDescriptor.java    From openjdk-8-source with GNU General Public License v2.0 5 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);
    }

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

    if (sobj.has(VALUE)) {
        this.value = sobj.get(VALUE);
    } else {
        delete(VALUE, false);
    }

    return this;
}
 
Example 3
Source File: NativeArray.java    From jdk8u_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, CALLSITE_STRICT);
                sobj.set(upper, lowerValue, CALLSITE_STRICT);
            } else if (!lowerExists && upperExists) {
                sobj.set(lower, upperValue, CALLSITE_STRICT);
                sobj.delete(upper, true);
            } else if (lowerExists && !upperExists) {
                sobj.delete(lower, true);
                sobj.set(upper, lowerValue, CALLSITE_STRICT);
            }
        }
        return sobj;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 4
Source File: DataPropertyDescriptor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 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);
    }

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

    if (sobj.has(VALUE)) {
        this.value = sobj.get(VALUE);
    } else {
        delete(VALUE, false);
    }

    return this;
}
 
Example 5
Source File: NativeError.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.11.4.4 Error.prototype.toString ( )
 *
 * @param self  self reference
 *
 * @return this NativeError as a string
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    // Step 1 and 2 : check if 'self' is object it not throw TypeError
    Global.checkObject(self);

    final ScriptObject sobj = (ScriptObject)self;

    // Step 3 & 4 : get "name" and convert to String.
    // But if message is undefined make it "Error".
    Object name = sobj.get("name");
    if (name == UNDEFINED) {
        name = "Error";
    } else {
        name = JSType.toString(name);
    }

    // Steps 5, 6, & 7 : get "message" and convert to String.
    // if 'message' is undefined make it "" (empty String).
    Object msg = sobj.get("message");
    if (msg == UNDEFINED) {
        msg = "";
    } else {
        msg = JSType.toString(msg);
    }

    // Step 8 : if name is empty, return msg
    if (((String)name).isEmpty()) {
        return msg;
    }

    // Step 9 : if message is empty, return name
    if (((String)msg).isEmpty()) {
        return name;
    }
    // Step 10 : return name + ": " + msg
    return name + ": " + msg;
}
 
Example 6
Source File: DebugLogger.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void logEvent(final RuntimeEvent<?> event) {
    if (event != null) {
        final Global global = Context.getGlobal();
        if (global.has("Debug")) {
            final ScriptObject debug = (ScriptObject)global.get("Debug");
            final ScriptFunction addRuntimeEvent = (ScriptFunction)debug.get("addRuntimeEvent");
            ScriptRuntime.apply(addRuntimeEvent, debug, event);
        }
    }
}
 
Example 7
Source File: DataPropertyDescriptor.java    From hottub with GNU General Public License v2.0 5 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);
    }

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

    if (sobj.has(VALUE)) {
        this.value = sobj.get(VALUE);
    } else {
        delete(VALUE, false);
    }

    return this;
}
 
Example 8
Source File: DataPropertyDescriptor.java    From nashorn with GNU General Public License v2.0 5 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);
    }

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

    if (sobj.has(VALUE)) {
        this.value = sobj.get(VALUE);
    } else {
        delete(VALUE, false);
    }

    return this;
}
 
Example 9
Source File: NashornScriptEngine.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isInterfaceImplemented(final Class<?> iface, final ScriptObject sobj) {
    for (final Method method : iface.getMethods()) {
        // ignore methods of java.lang.Object class
        if (method.getDeclaringClass() == Object.class) {
            continue;
        }

        Object obj = sobj.get(method.getName());
        if (! (obj instanceof ScriptFunction)) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: NativeArray.java    From openjdk-jdk8u-backup 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: NativeArray.java    From openjdk-8-source 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 12
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 13
Source File: NativeArray.java    From openjdk-jdk8u 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: LinkerCallSite.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private void printObject(final PrintWriter out, final Object arg) {
    if (!getNashornDescriptor().isTraceObjects()) {
        out.print((arg instanceof ScriptObject) ? "ScriptObject" : arg);
        return;
    }

    if (arg instanceof ScriptObject) {
        final ScriptObject object = (ScriptObject)arg;

        boolean isFirst = true;
        final Set<Object> keySet = object.keySet();

        if (keySet.isEmpty()) {
            out.print(ScriptRuntime.safeToString(arg));
        } else {
            out.print("{ ");

            for (final Object key : keySet) {
                if (!isFirst) {
                    out.print(", ");
                }

                out.print(key);
                out.print(":");

                final Object value = object.get(key);

                if (value instanceof ScriptObject) {
                    out.print("...");
                } else {
                    printObject(out, value);
                }

                isFirst = false;
            }

            out.print(" }");
        }
     } else {
        out.print(ScriptRuntime.safeToString(arg));
    }
}
 
Example 15
Source File: NativeError.java    From TencentKona-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) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}
 
Example 16
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.fileName
 *
 * @param self self reference
 *
 * @return file name from which error was thrown
 */
public static Object getFileName(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject)self);
}
 
Example 17
Source File: NativeError.java    From hottub 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) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}
 
Example 18
Source File: NativeError.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.fileName
 *
 * @param self self reference
 *
 * @return file name from which error was thrown
 */
public static Object getFileName(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject)self);
}
 
Example 19
Source File: NativeError.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.fileName
 *
 * @param self self reference
 *
 * @return file name from which error was thrown
 */
public static Object getFileName(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject)self);
}
 
Example 20
Source File: NativeError.java    From jdk8u_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) {
    final ScriptObject sobj = Global.checkObject(self);
    return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
}