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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#hasOwnProperty() . 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: JavaAdapterServices.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example 2
Source File: NativeError.java    From TencentKona-8 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 3
Source File: JavaAdapterServices.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example 4
Source File: JavaAdapterServices.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example 5
Source File: JavaAdapterServices.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example 6
Source File: NativeError.java    From openjdk-jdk8u 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 7
Source File: NativeError.java    From jdk8u60 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 8
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 9
Source File: JavaAdapterServices.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a JS script object, retrieves a function from it by name, binds it to the script object as its "this", and
 * adapts its parameter types, return types, and arity to the specified type and arity. This method is public mainly
 * for implementation reasons, so the adapter classes can invoke it from their constructors that take a Object
 * in its first argument to obtain the method handles for their method implementations.
 * @param obj the script obj
 * @param name the name of the property that contains the function
 * @param type the method type it has to conform to
 * @return the appropriately adapted method handle for invoking the script function, or null if the value of the
 * property is either null or undefined, or "toString" was requested as the name, but the object doesn't directly
 * define it but just inherits it through prototype.
 */
public static MethodHandle getHandle(final Object obj, final String name, final MethodType type) {
    if (! (obj instanceof ScriptObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final ScriptObject sobj = (ScriptObject)obj;
    // Since every JS Object has a toString, we only override "String toString()" it if it's explicitly specified
    if ("toString".equals(name) && !sobj.hasOwnProperty("toString")) {
        return null;
    }

    final Object fnObj = sobj.get(name);
    if (fnObj instanceof ScriptFunction) {
        return bindAndAdaptHandle((ScriptFunction)fnObj, sobj, type);
    } else if(fnObj == null || fnObj instanceof Undefined) {
        return null;
    } else {
        throw typeError("not.a.function", name);
    }
}
 
Example 10
Source File: NativeError.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self  self reference
 * @param value value of column number
 *
 * @return value that was set
 */
public static Object setColumnNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(COLUMNNUMBER)) {
        sobj.put(COLUMNNUMBER, value, false);
    } else {
        sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 11
Source File: NativeError.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self  self reference
 * @param value value of line number
 *
 * @return value that was set
 */
public static Object setLineNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(LINENUMBER)) {
        sobj.put(LINENUMBER, value, false);
    } else {
        sobj.addOwnProperty(LINENUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 12
Source File: NativeError.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self  self reference
 * @param value value of line number
 *
 * @return value that was set
 */
public static Object setLineNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(LINENUMBER)) {
        sobj.put(LINENUMBER, value, false);
    } else {
        sobj.addOwnProperty(LINENUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 13
Source File: NativeError.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension
 * Accessed from {@link Global} while setting up the Error.prototype
 *
 * @param self   self reference
 * @param value  value to set "stack" property to, must be {@code ScriptObject}
 *
 * @return value that was set
 */
public static Object setStack(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(STACK)) {
        sobj.put(STACK, value, false);
    } else {
        sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 14
Source File: NativeError.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self  self reference
 * @param value value of column number
 *
 * @return value that was set
 */
public static Object setColumnNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(COLUMNNUMBER)) {
        sobj.put(COLUMNNUMBER, value, false);
    } else {
        sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 15
Source File: NativeError.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self  self reference
 * @param value value of line number
 *
 * @return value that was set
 */
public static Object setLineNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(LINENUMBER)) {
        sobj.put(LINENUMBER, value, false);
    } else {
        sobj.addOwnProperty(LINENUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 16
Source File: NativeError.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.fileName
 *
 * @param self  self reference
 * @param value value of file name
 *
 * @return value that was set
 */
public static Object setFileName(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(FILENAME)) {
        sobj.put(FILENAME, value, false);
    } else {
        sobj.addOwnProperty(FILENAME, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 17
Source File: NativeError.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self  self reference
 * @param value value of column number
 *
 * @return value that was set
 */
public static Object setColumnNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(COLUMNNUMBER)) {
        sobj.put(COLUMNNUMBER, value, false);
    } else {
        sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 18
Source File: NativeError.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.lineNumber
 *
 * @param self  self reference
 * @param value value of line number
 *
 * @return value that was set
 */
public static Object setLineNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(LINENUMBER)) {
        sobj.put(LINENUMBER, value, false);
    } else {
        sobj.addOwnProperty(LINENUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 19
Source File: NativeError.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.fileName
 *
 * @param self  self reference
 * @param value value of file name
 *
 * @return value that was set
 */
public static Object setFileName(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(FILENAME)) {
        sobj.put(FILENAME, value, false);
    } else {
        sobj.addOwnProperty(FILENAME, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 20
Source File: NativeError.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self  self reference
 * @param value value of column number
 *
 * @return value that was set
 */
public static Object setColumnNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(COLUMNNUMBER)) {
        sobj.put(COLUMNNUMBER, value, false);
    } else {
        sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}