Java源码示例:jdk.nashorn.api.scripting.NashornException

示例1
/**
 * 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;
}
 
示例2
/**
 * 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;
}
 
示例3
/**
 * 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);
}
 
示例4
/**
 * 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;
}
 
示例5
/**
 * 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;
}
 
示例6
/**
 * 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;
}
 
示例7
/**
 * 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;
}
 
示例8
/**
 * 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;
}
 
示例9
/**
 * 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);
}
 
示例10
/**
 * 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;
}
 
示例11
/**
 * 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;
}
 
示例12
/**
 * 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;
}
 
示例13
/**
 * 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;
}
 
示例14
/**
 * 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;
}
 
示例15
/**
 * 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;
}
 
示例16
/**
 * 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;
}
 
示例17
/**
 * 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;
}
 
示例18
/**
 * 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;
}
 
示例19
/**
 * 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;
}
 
示例20
/**
 * 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;
}
 
示例21
/**
 * 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;
}
 
示例22
@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());
}
 
示例23
/**
 * 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;
}
 
示例24
/**
 * 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;
}
 
示例25
@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());
}
 
示例26
@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());
}
 
示例27
@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");
    }
}
 
示例28
/**
 * 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;
}
 
示例29
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");
    }
}
 
示例30
/**
 * 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;
}