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

The following are top voted examples for showing how to use jdk.nashorn.internal.runtime.ECMAException. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: OpenJSharp   File: NativeError.java   Source Code and License 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
Project: openjdk-jdk10   File: ScriptObjectMirror.java   Source Code and License 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
Project: openjdk-jdk10   File: NativeError.java   Source Code and License 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
Project: openjdk9   File: ScriptObjectMirror.java   Source Code and License 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 5
Project: openjdk9   File: NativeError.java   Source Code and License 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 6
Project: kaziranga   File: ScriptObjectMirror.java   Source Code and License 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 7
Project: kaziranga   File: NativeError.java   Source Code and License 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
Project: lookaside_java-1.8.0-openjdk   File: ScriptObjectMirror.java   Source Code and License 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 9
Project: lookaside_java-1.8.0-openjdk   File: NativeError.java   Source Code and License 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 10
Project: jdk8u_nashorn   File: ScriptObjectMirror.java   Source Code and License 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 11
Project: jdk8u_nashorn   File: NativeError.java   Source Code and License 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
Project: infobip-open-jdk-8   File: NativeError.java   Source Code and License 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
Project: Nasapi   File: AbstractExceptionMapper.java   Source Code and License 5 votes vote down vote up
Response buildResponse(NasapiException ex, ECMAException outerScriptException, HttpServletRequest request) {
	LoggerFactory.getLogger(ex.getClass()).warn(ex.getMessage() + buildScriptInfoMessage(outerScriptException) + buildRequestInfoMessage(request));
	Map<String,Object> body = new HashMap<String,Object>();
	body.put("$error", ex.getMessage());
	return Response.status(ex.getStatusCode())
			.entity(body)
			.build();
}
 
Example 14
Project: Nasapi   File: AbstractExceptionMapper.java   Source Code and License 5 votes vote down vote up
String buildScriptInfoMessage(ECMAException outerScriptException) {
	String result = "";
	if (outerScriptException != null) {
		result = " [at:- " + outerScriptException.getFileName() + ":" + outerScriptException.getLineNumber() + "," + outerScriptException.getColumnNumber() + "]";
	}
	return result;
}
 
Example 15
Project: Nasapi   File: ECMAExceptionMapper.java   Source Code and License 5 votes vote down vote up
@Override
@Produces(MediaType.APPLICATION_JSON)
public Response toResponse(ECMAException ex) {
	Object cause = ex.getCause();
	if (cause != null && cause instanceof NasapiException) {
		return buildResponse((NasapiException)cause, ex, request);
	}
	LoggerFactory.getLogger(ex.getClass()).warn(ex.getMessage() + buildScriptInfoMessage(ex));
	Map<String,Object> body = new HashMap<String,Object>();
	body.put("$error", ex.getMessage());
	return Response.status(500).entity(body).build();
}
 
Example 16
Project: OpenJSharp   File: NativeError.java   Source Code and License 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 17
Project: openjdk-jdk10   File: NativeError.java   Source Code and License 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
Project: openjdk-jdk10   File: NashornCompleter.java   Source Code and License 5 votes vote down vote up
/**
 * 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 19
Project: openjdk9   File: NativeError.java   Source Code and License 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 20
Project: openjdk9   File: NashornCompleter.java   Source Code and License 5 votes vote down vote up
/**
 * 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 21
Project: kaziranga   File: NativeError.java   Source Code and License 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 22
Project: lookaside_java-1.8.0-openjdk   File: NativeError.java   Source Code and License 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 23
Project: jdk8u_nashorn   File: NativeError.java   Source Code and License 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
Project: infobip-open-jdk-8   File: NativeError.java   Source Code and License 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 25
Project: OLD-OpenJDK8   File: NativeError.java   Source Code and License 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 26
Project: OLD-OpenJDK8   File: NativeError.java   Source Code and License 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 27
Project: OLD-OpenJDK8   File: NativeError.java   Source Code and License 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 28
Project: OLD-OpenJDK8   File: CodeGenerator.java   Source Code and License 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 29
Project: nashorn-backport   File: NativeError.java   Source Code and License 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) {
    Global.checkObject(errorObj);
    final ScriptObject sobj = (ScriptObject)errorObj;
    final ECMAException exp = new ECMAException(sobj, null);
    sobj.set("stack", getScriptStackString(sobj, exp), false);
    return UNDEFINED;
}
 
Example 30
Project: nashorn-backport   File: NativeError.java   Source Code and License 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 31
Project: nashorn-backport   File: NativeError.java   Source Code and License 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 32
Project: hsac-fitnesse-fixtures   File: ScriptLanguageFixture.java   Source Code and License 5 votes vote down vote up
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 33
Project: nashorn   File: NativeError.java   Source Code and License 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) {
    Global.checkObject(errorObj);
    final ScriptObject sobj = (ScriptObject)errorObj;
    final ECMAException exp = new ECMAException(sobj, null);
    sobj.set("stack", getScriptStackString(sobj, exp), false);
    return UNDEFINED;
}
 
Example 34
Project: nashorn   File: NativeError.java   Source Code and License 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 35
Project: nashorn   File: NativeError.java   Source Code and License 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 36
Project: OpenJSharp   File: NativeError.java   Source Code and License 4 votes vote down vote up
@SuppressWarnings("unused")
static void initException(final ScriptObject self) {
    // ECMAException constructor has side effects
    new ECMAException(self, null);
}
 
Example 37
Project: OpenJSharp   File: NativeObject.java   Source Code and License 4 votes vote down vote up
private static ECMAException notAnObject(final Object obj) {
    return typeError("not.an.object", ScriptRuntime.safeToString(obj));
}
 
Example 38
Project: OpenJSharp   File: AdaptationResult.java   Source Code and License 4 votes vote down vote up
ECMAException typeError() {
    return ECMAErrors.typeError("extend." + outcome, classList);
}
 
Example 39
Project: OpenJSharp   File: Bootstrap.java   Source Code and License 4 votes vote down vote up
private static ECMAException notFunction(final Object obj) {
    return typeError("not.a.function", ScriptRuntime.safeToString(obj));
}
 
Example 40
Project: OpenJSharp   File: CodeGenerator.java   Source Code and License 4 votes vote down vote up
@Override
public boolean enterThrowNode(final ThrowNode throwNode) {
    if(!method.isReachable()) {
        return false;
    }
    enterStatement(throwNode);

    if (throwNode.isSyntheticRethrow()) {
        method.beforeJoinPoint(throwNode);

        //do not wrap whatever this is in an ecma exception, just rethrow it
        final IdentNode exceptionExpr = (IdentNode)throwNode.getExpression();
        final Symbol exceptionSymbol = exceptionExpr.getSymbol();
        method.load(exceptionSymbol, EXCEPTION_TYPE);
        method.checkcast(EXCEPTION_TYPE.getTypeClass());
        method.athrow();
        return false;
    }

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

    // NOTE: we first evaluate the expression, and only after it was evaluated do we create the new ECMAException
    // object and then somewhat cumbersomely move it beneath the evaluated expression on the stack. The reason for
    // this is that if expression is optimistic (or contains an optimistic subexpression), we'd potentially access
    // the not-yet-<init>ialized object on the stack from the UnwarrantedOptimismException handler, and bytecode
    // verifier forbids that.
    loadExpressionAsObject(expression);

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

    method.beforeJoinPoint(throwNode);
    method.athrow();

    return false;
}