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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject#put() . 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 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 2
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 3
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 4
Source File: NativeError.java    From jdk8u60 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 5
Source File: NativeError.java    From hottub 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 6
Source File: NativeError.java    From openjdk-jdk8u-backup 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 7
Source File: NativeError.java    From openjdk-jdk8u 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 8
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 9
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;
}
 
Example 10
Source File: NativeError.java    From openjdk-jdk9 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 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 12
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 13
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 14
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 15
Source File: NativeError.java    From jdk8u_nashorn 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 16
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;
}
 
Example 17
Source File: ContextTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 18
Source File: ContextTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 19
Source File: ContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 20
Source File: ContextTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void reflectionTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final boolean strict = cx.getEnv()._strict;
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());

    try {
        final String code = "var obj = { x: 344, y: 42 }";
        eval(cx, "<reflectionTest>", code);

        final Object obj = Context.getGlobal().get("obj");

        assertTrue(obj instanceof ScriptObject);

        final ScriptObject sobj = (ScriptObject)obj;
        int count = 0;
        for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
            final Object key = ex.getKey();
            if (key.equals("x")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            } else if (key.equals("y")) {
                assertTrue(ex.getValue() instanceof Number);
                assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());

                count++;
            }
        }
        assertEquals(count, 2);
        assertEquals(sobj.size(), 2);

        // add property
        sobj.put("zee", "hello", strict);
        assertEquals(sobj.get("zee"), "hello");
        assertEquals(sobj.size(), 3);

    } finally {
        Context.setGlobal(oldGlobal);
    }
}