Java Code Examples for org.mozilla.javascript.Context#reportError()

The following examples show how to use org.mozilla.javascript.Context#reportError() . 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: Main.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
static void evalInlineScript(Context cx, String scriptText) {
    try {
        Script script = cx.compileString(scriptText, "<command>", 1, null);
        if (script != null) {
            script.exec(cx, getShellScope());
        }
    } catch (RhinoException rex) {
        ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (VirtualMachineError ex) {
        // Treat StackOverflow and OutOfMemory as runtime errors
        ex.printStackTrace();
        String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
        Context.reportError(msg);
        exitCode = EXITCODE_RUNTIME_ERROR;
    }
}
 
Example 2
Source File: Main.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
public static void processFileNoThrow(Context cx, Scriptable scope, String filename) {
    try {
        processFile(cx, scope, filename);
    } catch (IOException ioex) {
        Context.reportError(ToolErrorReporter.getMessage(
                "msg.couldnt.read.source", filename, ioex.getMessage()));
        exitCode = EXITCODE_FILE_NOT_FOUND;
    } catch (RhinoException rex) {
        ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (VirtualMachineError ex) {
        // Treat StackOverflow and OutOfMemory as runtime errors
        ex.printStackTrace();
        String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
        Context.reportError(msg);
        exitCode = EXITCODE_RUNTIME_ERROR;
    }
}
 
Example 3
Source File: Main.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
static void evalInlineScript(Context cx, String scriptText) {
    try {
        Script script = cx.compileString(scriptText, "<command>", 1, null);
        if (script != null) {
            script.exec(cx, getShellScope());
        }
    } catch (RhinoException rex) {
        ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (VirtualMachineError ex) {
        // Treat StackOverflow and OutOfMemory as runtime errors
        ex.printStackTrace();
        String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
        Context.reportError(msg);
        exitCode = EXITCODE_RUNTIME_ERROR;
    }
}
 
Example 4
Source File: Main.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void processFileNoThrow(Context cx, Scriptable scope, String filename) {
    try {
        processFile(cx, scope, filename);
    } catch (IOException ioex) {
        Context.reportError(ToolErrorReporter.getMessage(
                "msg.couldnt.read.source", filename, ioex.getMessage()));
        exitCode = EXITCODE_FILE_NOT_FOUND;
    } catch (RhinoException rex) {
        ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (VirtualMachineError ex) {
        // Treat StackOverflow and OutOfMemory as runtime errors
        ex.printStackTrace();
        String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
        Context.reportError(msg);
        exitCode = EXITCODE_RUNTIME_ERROR;
    }
}
 
Example 5
Source File: Main.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
static void processFiles(Context cx, String[] args)
{
    // define "arguments" array in the top-level object:
    // need to allocate new array since newArray requires instances
    // of exactly Object[], not ObjectSubclass[]
    Object[] array = new Object[args.length];
    System.arraycopy(args, 0, array, 0, args.length);
    Scriptable argsObj = cx.newArray(global, array);
    global.defineProperty("arguments", argsObj,
                          ScriptableObject.DONTENUM);

    for (String file: fileList) {
        try {
            processSource(cx, file);
        } catch (IOException ioex) {
            Context.reportError(ToolErrorReporter.getMessage(
                    "msg.couldnt.read.source", file, ioex.getMessage()));
            exitCode = EXITCODE_FILE_NOT_FOUND;
        } catch (RhinoException rex) {
            ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
            exitCode = EXITCODE_RUNTIME_ERROR;
        } catch (VirtualMachineError ex) {
            // Treat StackOverflow and OutOfMemory as runtime errors
            ex.printStackTrace();
            String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
            Context.reportError(msg);
            exitCode = EXITCODE_RUNTIME_ERROR;
        }
    }
}
 
Example 6
Source File: Main.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private static Script loadCompiledScript(Context cx, String path,
                                         byte[] data, Object securityDomain)
        throws FileNotFoundException
{
    if (data == null) {
        throw new FileNotFoundException(path);
    }
    // XXX: For now extract class name of compiled Script from path
    // instead of parsing class bytes
    int nameStart = path.lastIndexOf('/');
    if (nameStart < 0) {
        nameStart = 0;
    } else {
        ++nameStart;
    }
    int nameEnd = path.lastIndexOf('.');
    if (nameEnd < nameStart) {
        // '.' does not exist in path (nameEnd < 0)
        // or it comes before nameStart
        nameEnd = path.length();
    }
    String name = path.substring(nameStart, nameEnd);
    try {
        GeneratedClassLoader loader = SecurityController.createLoader(cx.getApplicationClassLoader(), securityDomain);
        Class<?> clazz = loader.defineClass(name, data);
        loader.linkClass(clazz);
        if (!Script.class.isAssignableFrom(clazz)) {
            throw Context.reportRuntimeError("msg.must.implement.Script");
        }
        return (Script) clazz.newInstance();
    } catch (IllegalAccessException iaex) {
        Context.reportError(iaex.toString());
        throw new RuntimeException(iaex);
    } catch (InstantiationException inex) {
        Context.reportError(inex.toString());
        throw new RuntimeException(inex);
    }
}
 
Example 7
Source File: Main.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static void processFiles(Context cx, String[] args)
{
    // define "arguments" array in the top-level object:
    // need to allocate new array since newArray requires instances
    // of exactly Object[], not ObjectSubclass[]
    Object[] array = new Object[args.length];
    System.arraycopy(args, 0, array, 0, args.length);
    Scriptable argsObj = cx.newArray(global, array);
    global.defineProperty("arguments", argsObj,
                          ScriptableObject.DONTENUM);

    for (String file: fileList) {
        try {
            processSource(cx, file);
        } catch (IOException ioex) {
            Context.reportError(ToolErrorReporter.getMessage(
                    "msg.couldnt.read.source", file, ioex.getMessage()));
            exitCode = EXITCODE_FILE_NOT_FOUND;
        } catch (RhinoException rex) {
            ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
            exitCode = EXITCODE_RUNTIME_ERROR;
        } catch (VirtualMachineError ex) {
            // Treat StackOverflow and OutOfMemory as runtime errors
            ex.printStackTrace();
            String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
            Context.reportError(msg);
            exitCode = EXITCODE_RUNTIME_ERROR;
        }
    }
}
 
Example 8
Source File: Main.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static Script loadCompiledScript(Context cx, String path,
                                         byte[] data, Object securityDomain)
        throws FileNotFoundException
{
    if (data == null) {
        throw new FileNotFoundException(path);
    }
    // XXX: For now extract class name of compiled Script from path
    // instead of parsing class bytes
    int nameStart = path.lastIndexOf('/');
    if (nameStart < 0) {
        nameStart = 0;
    } else {
        ++nameStart;
    }
    int nameEnd = path.lastIndexOf('.');
    if (nameEnd < nameStart) {
        // '.' does not exist in path (nameEnd < 0)
        // or it comes before nameStart
        nameEnd = path.length();
    }
    String name = path.substring(nameStart, nameEnd);
    try {
        GeneratedClassLoader loader = SecurityController.createLoader(cx.getApplicationClassLoader(), securityDomain);
        Class<?> clazz = loader.defineClass(name, data);
        loader.linkClass(clazz);
        if (!Script.class.isAssignableFrom(clazz)) {
            throw Context.reportRuntimeError("msg.must.implement.Script");
        }
        return (Script) clazz.newInstance();
    } catch (IllegalAccessException iaex) {
        Context.reportError(iaex.toString());
        throw new RuntimeException(iaex);
    } catch (InstantiationException inex) {
        Context.reportError(inex.toString());
        throw new RuntimeException(inex);
    }
}