jdk.nashorn.api.scripting.NashornException Java Examples

The following examples show how to use jdk.nashorn.api.scripting.NashornException. 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: ECMAErrors.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check if a stack trace element is in JavaScript
 *
 * @param frame frame
 *
 * @return true if frame is in the script
 */
public static boolean isScriptFrame(final StackTraceElement frame) {
    final String className = frame.getClassName();

    // Look for script package in class name (into which compiler puts generated code)
    if (className.startsWith(scriptPackage)) {
        final String source = frame.getFileName();
        /*
         * Make sure that it is not some Java code that Nashorn has in that package!
         * also, we don't want to report JavaScript code that lives in script engine implementation
         * We want to report only user's own scripts and not any of our own scripts like "engine.js"
         */
        return source != null && !source.endsWith(".java") && !source.contains(NashornException.ENGINE_SCRIPT_SOURCE_NAME);
    }
    return false;
}
 
Example #2
Source File: ECMAErrors.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check if a stack trace element is in JavaScript
 *
 * @param frame frame
 *
 * @return true if frame is in the script
 */
public static boolean isScriptFrame(final StackTraceElement frame) {
    final String className = frame.getClassName();

    // Look for script package in class name (into which compiler puts generated code)
    if (className.startsWith(scriptPackage)) {
        final String source = frame.getFileName();
        /*
         * Make sure that it is not some Java code that Nashorn has in that package!
         * also, we don't want to report JavaScript code that lives in script engine implementation
         * We want to report only user's own scripts and not any of our own scripts like "engine.js"
         */
        return source != null && !source.endsWith(".java") && !source.contains(NashornException.ENGINE_SCRIPT_SOURCE_NAME);
    }
    return false;
}
 
Example #3
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 #4
Source File: ECMAException.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the column number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the column number, or undefined if wrapped exception is not a ParserException
 */
public static Object getColumnNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getColumnNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getColumnNumber();
    }

    return UNDEFINED;
}
 
Example #5
Source File: Shell.java    From TencentKona-8 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 Global global, final List<String> files) throws IOException {
    final Global 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 #6
Source File: NativeError.java    From jdk8u60 with 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 #7
Source File: ECMAException.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the column number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the column number, or undefined if wrapped exception is not a ParserException
 */
public static Object getColumnNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getColumnNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getColumnNumber();
    }

    return UNDEFINED;
}
 
Example #8
Source File: ECMAException.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #9
Source File: ECMAException.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the column number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the column number, or undefined if wrapped exception is not a ParserException
 */
public static Object getColumnNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getColumnNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getColumnNumber();
    }

    return UNDEFINED;
}
 
Example #10
Source File: ECMAException.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #11
Source File: ECMAException.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the file name for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the file name, or undefined if wrapped exception is not a ParserException
 */
public static Object getFileName(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getFileName();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getFileName();
    }

    return UNDEFINED;
}
 
Example #12
Source File: NativeError.java    From hottub with 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 #13
Source File: Shell.java    From openjdk-jdk8u-backup 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 Global global, final List<String> files) throws IOException {
    final Global 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 #14
Source File: ECMAException.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #15
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CompilationUnitTree parse(final File file, final DiagnosticListener listener) throws IOException, NashornException {
    if (moduleMode) {
        return parseModule(file, listener);
    }
    final Source src = Source.sourceFor(Objects.requireNonNull(file).getName(), file);
    return translate(makeParser(src, listener).parse());
}
 
Example #16
Source File: ECMAException.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the file name for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the file name, or undefined if wrapped exception is not a ParserException
 */
public static Object getFileName(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getFileName();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getFileName();
    }

    return UNDEFINED;
}
 
Example #17
Source File: ECMAException.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the column number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the column number, or undefined if wrapped exception is not a ParserException
 */
public static Object getColumnNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getColumnNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getColumnNumber();
    }

    return UNDEFINED;
}
 
Example #18
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CompilationUnitTree parse(final String name, final Reader reader, final DiagnosticListener listener) throws IOException, NashornException {
    if (moduleMode) {
        return parseModule(name, reader, listener);
    }
    final Source src = Source.sourceFor(Objects.requireNonNull(name), Objects.requireNonNull(reader));
    return translate(makeParser(src, listener).parse());
}
 
Example #19
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CompilationUnitTree parse(final String name, final String code, final DiagnosticListener listener) throws NashornException {
    if (moduleMode) {
        return parseModule(name, code, listener);
    }
    final Source src = Source.sourceFor(name, code);
    return translate(makeParser(src, listener).parse());
}
 
Example #20
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CompilationUnitTree parse(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException {
    if (moduleMode) {
        return parseModule(scriptObj, listener);
    }
    final Map<?, ?> map = Objects.requireNonNull(scriptObj);
    if (map.containsKey("script") && map.containsKey("name")) {
        final String script = JSType.toString(map.get("script"));
        final String name = JSType.toString(map.get("name"));
        final Source src = Source.sourceFor(name, script);
        return translate(makeParser(src, listener).parse());
    } else {
        throw new IllegalArgumentException("can't find 'script' and 'name' properties");
    }
}
 
Example #21
Source File: ECMAException.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #22
Source File: Shell.java    From openjdk-jdk9 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 Global global, final List<String> files) throws IOException {
    final Global 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 #23
Source File: ECMAException.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the file name for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the file name, or undefined if wrapped exception is not a ParserException
 */
public static Object getFileName(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getFileName();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getFileName();
    }

    return UNDEFINED;
}
 
Example #24
Source File: ParserImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private CompilationUnitTree parseModule(final ScriptObjectMirror scriptObj, final DiagnosticListener listener) throws NashornException {
    final Map<?, ?> map = Objects.requireNonNull(scriptObj);
    if (map.containsKey("script") && map.containsKey("name")) {
        final String script = JSType.toString(map.get("script"));
        final String name = JSType.toString(map.get("name"));
        final Source src = Source.sourceFor(name, script);
        return makeModule(src, listener);
    } else {
        throw new IllegalArgumentException("can't find 'script' and 'name' properties");
    }
}
 
Example #25
Source File: NativeError.java    From jdk8u_nashorn with 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 #26
Source File: ECMAException.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #27
Source File: ECMAException.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the file name for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the file name, or undefined if wrapped exception is not a ParserException
 */
public static Object getFileName(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getFileName();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getFileName();
    }

    return UNDEFINED;
}
 
Example #28
Source File: ECMAException.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the column number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the column number, or undefined if wrapped exception is not a ParserException
 */
public static Object getColumnNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getColumnNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getColumnNumber();
    }

    return UNDEFINED;
}
 
Example #29
Source File: ECMAException.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the line number for a {@code ScriptObject} representing an error
 *
 * @param errObj the error object
 * @return the line number, or undefined if wrapped exception is not a ParserException
 */
public static Object getLineNumber(final ScriptObject errObj) {
    final Object e = getException(errObj);
    if (e instanceof NashornException) {
        return ((NashornException)e).getLineNumber();
    } else if (e instanceof ScriptException) {
        return ((ScriptException)e).getLineNumber();
    }

    return UNDEFINED;
}
 
Example #30
Source File: NativeError.java    From openjdk-jdk9 with 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);
}