Java Code Examples for jdk.nashorn.internal.runtime.ECMAException

The following examples show how to use jdk.nashorn.internal.runtime.ECMAException. These examples are extracted from open source projects. 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 Project: TencentKona-8   Source File: NativeError.java    License: 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 Project: jdk8u60   Source File: ScriptObjectMirror.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getDefaultValue(final Class<?> hint) {
    return inGlobal(new Callable<Object>() {
        @Override public Object call() {
            try {
                return sobj.getDefaultValue(hint);
            } catch (final ECMAException e) {
                // We're catching ECMAException (likely TypeError), and translating it to
                // UnsupportedOperationException. This in turn will be translated into TypeError of the
                // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
                // recognized as "instanceof TypeError" in the caller.
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        }
    });
}
 
Example 3
@Override
public Object getDefaultValue(final Class<?> hint) {
    return inGlobal(new Callable<Object>() {
        @Override public Object call() {
            try {
                return sobj.getDefaultValue(hint);
            } catch (final ECMAException e) {
                // We're catching ECMAException (likely TypeError), and translating it to
                // UnsupportedOperationException. This in turn will be translated into TypeError of the
                // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
                // recognized as "instanceof TypeError" in the caller.
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        }
    });
}
 
Example 4
Source Project: jdk8u60   Source File: NativeError.java    License: 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
@Override
public Object getDefaultValue(final Class<?> hint) {
    return inGlobal(new Callable<Object>() {
        @Override public Object call() {
            try {
                return sobj.getDefaultValue(hint);
            } catch (final ECMAException e) {
                // We're catching ECMAException (likely TypeError), and translating it to
                // UnsupportedOperationException. This in turn will be translated into TypeError of the
                // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
                // recognized as "instanceof TypeError" in the caller.
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        }
    });
}
 
Example 6
Source Project: openjdk-jdk8u   Source File: NativeError.java    License: 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
@Override
public Object getDefaultValue(final Class<?> hint) {
    return inGlobal(new Callable<Object>() {
        @Override public Object call() {
            try {
                return sobj.getDefaultValue(hint);
            } catch (final ECMAException e) {
                // We're catching ECMAException (likely TypeError), and translating it to
                // UnsupportedOperationException. This in turn will be translated into TypeError of the
                // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
                // recognized as "instanceof TypeError" in the caller.
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        }
    });
}
 
Example 8
Source Project: jdk8u_nashorn   Source File: NativeError.java    License: 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
/**
 * 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 10
@Override
public Object getDefaultValue(final Class<?> hint) {
    return inGlobal(new Callable<Object>() {
        @Override public Object call() {
            try {
                return sobj.getDefaultValue(hint);
            } catch (final ECMAException e) {
                // We're catching ECMAException (likely TypeError), and translating it to
                // UnsupportedOperationException. This in turn will be translated into TypeError of the
                // caller's Global by JSType#toPrimitive(JSObject,Class) therefore ensuring that it's
                // recognized as "instanceof TypeError" in the caller.
                throw new UnsupportedOperationException(e.getMessage(), e);
            }
        }
    });
}
 
Example 11
Source Project: openjdk-jdk9   Source File: NativeError.java    License: 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 12
Source Project: hottub   Source File: NativeError.java    License: 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 13
Source Project: openjdk-8   Source File: NativeError.java    License: GNU General Public License v2.0 5 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) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    if (sobj.has(STACK)) {
        return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
        return getScriptStackString(sobj, (Throwable)exception);
    }

    return "";
}
 
Example 14
Source Project: TencentKona-8   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 15
Source Project: jdk8u60   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 16
protected RuntimeException getExceptionToThrow(ScriptException e) {
    Throwable cause = e.getCause();
    String message;
    if (cause instanceof ECMAException) {
        message = cause.toString();
    } else {
        message = e.getMessage();
    }
    return new SlimFixtureException(false, message, e);
}
 
Example 17
Source Project: openjdk-jdk8u   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 18
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 19
Source Project: nashorn   Source File: NativeError.java    License: GNU General Public License v2.0 5 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) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    if (sobj.has(STACK)) {
        return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
        return getScriptStackString(sobj, (Throwable)exception);
    }

    return "";
}
 
Example 20
Source Project: openjdk-jdk9   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 21
/**
 * Is this a ECMAScript SyntaxError thrown for parse issue at the given line and column?
 *
 * @param exp Throwable to check
 * @param line line number to check
 * @param column column number to check
 *
 * @return true if the given Throwable is a ECMAScript SyntaxError at given line, column
 */
boolean isSyntaxErrorAt(final Throwable exp, final int line, final int column) {
    if (exp instanceof ECMAException) {
        final ECMAException eexp = (ECMAException)exp;
        if (eexp.getThrown() instanceof NativeSyntaxError) {
            return isParseErrorAt(eexp.getCause(), line, column);
        }
    }

    return false;
}
 
Example 22
Source Project: openjdk-8   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 23
Source Project: hottub   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 24
Source Project: openjdk-8-source   Source File: NativeError.java    License: 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 25
Source Project: openjdk-8-source   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.getStackTrace()
 * "stack" property is an array typed value containing {@link StackTraceElement}
 * objects of JavaScript stack frames.
 *
 * @param self  self reference
 *
 * @return      stack trace as a script array.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object getStackTrace(final Object self) {
    Global.checkObject(self);
    final ScriptObject sobj = (ScriptObject)self;
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
        res = NashornException.getScriptFrames((Throwable)exception);
    } else {
        res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
}
 
Example 26
@Override
public boolean enterThrowNode(final ThrowNode throwNode) {
    lineNumber(throwNode);

    if (throwNode.isSyntheticRethrow()) {
        //do not wrap whatever this is in an ecma exception, just rethrow it
        load(throwNode.getExpression());
        method.athrow();
        return false;
    }

    method._new(ECMAException.class).dup();

    final Source source     = lc.getCurrentFunction().getSource();

    final Expression expression = throwNode.getExpression();
    final int        position   = throwNode.position();
    final int        line       = throwNode.getLineNumber();
    final int        column     = source.getColumn(position);

    load(expression, Type.OBJECT);

    method.load(source.getName());
    method.load(line);
    method.load(column);
    method.invoke(ECMAException.THROW_INIT);

    method.athrow();

    return false;
}
 
Example 27
Source Project: openjdk-8   Source File: CodeGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterThrowNode(final ThrowNode throwNode) {
    lineNumber(throwNode);

    if (throwNode.isSyntheticRethrow()) {
        //do not wrap whatever this is in an ecma exception, just rethrow it
        load(throwNode.getExpression());
        method.athrow();
        return false;
    }

    method._new(ECMAException.class).dup();

    final Source source     = lc.getCurrentFunction().getSource();

    final Expression expression = throwNode.getExpression();
    final int        position   = throwNode.position();
    final int        line       = throwNode.getLineNumber();
    final int        column     = source.getColumn(position);

    load(expression, Type.OBJECT);

    method.load(source.getName());
    method.load(line);
    method.load(column);
    method.invoke(ECMAException.THROW_INIT);

    method.athrow();

    return false;
}
 
Example 28
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static ECMAException notAnObject(final Object obj) {
    return typeError("not.an.object", ScriptRuntime.safeToString(obj));
}
 
Example 29
Source Project: jdk8u_nashorn   Source File: NativeObject.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static ECMAException notAnObject(final Object obj) {
    return typeError("not.an.object", ScriptRuntime.safeToString(obj));
}
 
Example 30
Source Project: TencentKona-8   Source File: Bootstrap.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static ECMAException notFunction(final Object obj) {
    return typeError("not.a.function", ScriptRuntime.safeToString(obj));
}