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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#addOwnProperty() . 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 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 2
Source File: NativeError.java    From jdk8u_nashorn 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: NativeError.java    From openjdk-jdk9 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 4
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 5
Source File: NativeError.java    From hottub 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 6
Source File: NativeError.java    From openjdk-jdk8u 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 7
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 8
Source File: Shell.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs launches "fx:bootstrap.js" with the given JavaScript files provided
 * as arguments.
 *
 * @param context the nashorn context
 * @param global the global scope
 * @param files the list of script files to provide
 *
 * @return error code
 * @throws IOException when any script file read results in I/O error
 */
private static int runFXScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }

        global.addOwnProperty("$GLOBAL", Property.NOT_ENUMERABLE, global);
        global.addOwnProperty("$SCRIPTS", Property.NOT_ENUMERABLE, files);
        context.load(global, "fx:bootstrap.js");
    } catch (final NashornException e) {
        context.getErrorManager().error(e.toString());
        if (context.getEnv()._dump_on_error) {
            e.printStackTrace(context.getErr());
        }

        return RUNTIME_ERROR;
    } finally {
        context.getOut().flush();
        context.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}
 
Example 9
Source File: Global.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void initErrorObjects() {
    // Error objects
    this.builtinError = (ScriptFunction)initConstructor("Error");
    final ScriptObject errorProto = getErrorPrototype();

    // Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName
    final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", NativeError.GET_STACK);
    final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", NativeError.SET_STACK);
    errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    final ScriptFunction getLineNumber = ScriptFunctionImpl.makeFunction("getLineNumber", NativeError.GET_LINENUMBER);
    final ScriptFunction setLineNumber = ScriptFunctionImpl.makeFunction("setLineNumber", NativeError.SET_LINENUMBER);
    errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
    final ScriptFunction getColumnNumber = ScriptFunctionImpl.makeFunction("getColumnNumber", NativeError.GET_COLUMNNUMBER);
    final ScriptFunction setColumnNumber = ScriptFunctionImpl.makeFunction("setColumnNumber", NativeError.SET_COLUMNNUMBER);
    errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
    final ScriptFunction getFileName = ScriptFunctionImpl.makeFunction("getFileName", NativeError.GET_FILENAME);
    final ScriptFunction setFileName = ScriptFunctionImpl.makeFunction("setFileName", NativeError.SET_FILENAME);
    errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);

    // ECMA 15.11.4.2 Error.prototype.name
    // Error.prototype.name = "Error";
    errorProto.set(NativeError.NAME, "Error", false);
    // ECMA 15.11.4.3 Error.prototype.message
    // Error.prototype.message = "";
    errorProto.set(NativeError.MESSAGE, "", false);

    this.builtinEvalError = initErrorSubtype("EvalError", errorProto);
    this.builtinRangeError = initErrorSubtype("RangeError", errorProto);
    this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
    this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
    this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
    this.builtinURIError = initErrorSubtype("URIError", errorProto);
}
 
Example 10
Source File: NashornScriptEngine.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private ScriptObject createNashornGlobal(final ScriptContext ctxt) {
    final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
        @Override
        public ScriptObject run() {
            try {
                return nashornContext.newGlobal();
            } catch (final RuntimeException e) {
                if (Context.DEBUG) {
                    e.printStackTrace();
                }
                throw e;
            }
        }
    }, CREATE_GLOBAL_ACC_CTXT);

    nashornContext.initGlobal(newGlobal);

    final int NON_ENUMERABLE_CONSTANT = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE;
    // current ScriptContext exposed as "context"
    // "context" is non-writable from script - but script engine still
    // needs to set it and so save the context Property object
    contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, ctxt);
    // current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as
    // NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property
    // in the Global of a Context we just created - both the Context and the Global were just created and can not be
    // seen from another thread outside of this constructor.
    newGlobal.addOwnProperty("engine", NON_ENUMERABLE_CONSTANT, this);
    // global script arguments with undefined value
    newGlobal.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED);
    // file name default is null
    newGlobal.addOwnProperty(ScriptEngine.FILENAME, Property.NOT_ENUMERABLE, null);
    // evaluate engine.js initialization script this new global object
    try {
        evalImpl(compileImpl(ENGINE_SCRIPT_SRC, newGlobal), ctxt, newGlobal);
    } catch (final ScriptException exp) {
        throw new RuntimeException(exp);
    }
    return newGlobal;
}
 
Example 11
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.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
 * 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 13
Source File: NashornScriptEngine.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private ScriptObject createNashornGlobal(final ScriptContext ctxt) {
    final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
        @Override
        public ScriptObject run() {
            try {
                return nashornContext.newGlobal();
            } catch (final RuntimeException e) {
                if (Context.DEBUG) {
                    e.printStackTrace();
                }
                throw e;
            }
        }
    }, CREATE_GLOBAL_ACC_CTXT);

    nashornContext.initGlobal(newGlobal);

    final int NON_ENUMERABLE_CONSTANT = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE;
    // current ScriptContext exposed as "context"
    // "context" is non-writable from script - but script engine still
    // needs to set it and so save the context Property object
    contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, ctxt);
    // current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as
    // NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property
    // in the Global of a Context we just created - both the Context and the Global were just created and can not be
    // seen from another thread outside of this constructor.
    newGlobal.addOwnProperty("engine", NON_ENUMERABLE_CONSTANT, this);
    // global script arguments with undefined value
    newGlobal.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED);
    // file name default is null
    newGlobal.addOwnProperty(ScriptEngine.FILENAME, Property.NOT_ENUMERABLE, null);
    // evaluate engine.js initialization script this new global object
    try {
        evalImpl(compileImpl(ENGINE_SCRIPT_SRC, newGlobal), ctxt, newGlobal);
    } catch (final ScriptException exp) {
        throw new RuntimeException(exp);
    }
    return newGlobal;
}
 
Example 14
Source File: NativeError.java    From openjdk-8-source 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
 */
@SuppressWarnings("unused")
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    Global.checkObject(errorObj);
    final ScriptObject sobj = (ScriptObject)errorObj;
    new ECMAException(sobj, null); //constructor has side effects
    sobj.delete("stack", false);
    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 15
Source File: Global.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void initErrorObjects() {
    // Error objects
    this.builtinError = initConstructor("Error", ScriptFunction.class);
    final ScriptObject errorProto = getErrorPrototype();

    // Nashorn specific accessors on Error.prototype - stack, lineNumber, columnNumber and fileName
    final ScriptFunction getStack = ScriptFunction.createBuiltin("getStack", NativeError.GET_STACK);
    final ScriptFunction setStack = ScriptFunction.createBuiltin("setStack", NativeError.SET_STACK);
    errorProto.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    final ScriptFunction getLineNumber = ScriptFunction.createBuiltin("getLineNumber", NativeError.GET_LINENUMBER);
    final ScriptFunction setLineNumber = ScriptFunction.createBuiltin("setLineNumber", NativeError.SET_LINENUMBER);
    errorProto.addOwnProperty("lineNumber", Attribute.NOT_ENUMERABLE, getLineNumber, setLineNumber);
    final ScriptFunction getColumnNumber = ScriptFunction.createBuiltin("getColumnNumber", NativeError.GET_COLUMNNUMBER);
    final ScriptFunction setColumnNumber = ScriptFunction.createBuiltin("setColumnNumber", NativeError.SET_COLUMNNUMBER);
    errorProto.addOwnProperty("columnNumber", Attribute.NOT_ENUMERABLE, getColumnNumber, setColumnNumber);
    final ScriptFunction getFileName = ScriptFunction.createBuiltin("getFileName", NativeError.GET_FILENAME);
    final ScriptFunction setFileName = ScriptFunction.createBuiltin("setFileName", NativeError.SET_FILENAME);
    errorProto.addOwnProperty("fileName", Attribute.NOT_ENUMERABLE, getFileName, setFileName);

    // ECMA 15.11.4.2 Error.prototype.name
    // Error.prototype.name = "Error";
    errorProto.set(NativeError.NAME, "Error", 0);
    // ECMA 15.11.4.3 Error.prototype.message
    // Error.prototype.message = "";
    errorProto.set(NativeError.MESSAGE, "", 0);

    tagBuiltinProperties("Error", builtinError);

    this.builtinReferenceError = initErrorSubtype("ReferenceError", errorProto);
    this.builtinSyntaxError = initErrorSubtype("SyntaxError", errorProto);
    this.builtinTypeError = initErrorSubtype("TypeError", errorProto);
}
 
Example 16
Source File: NativeError.java    From jdk8u_nashorn 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 jdk8u_nashorn 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 18
Source File: NativeError.java    From openjdk-jdk9 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 19
Source File: NativeError.java    From openjdk-jdk9 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 20
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.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;
}