Search in sources :

Example 1 with RhinoException

use of org.mozilla.javascript.RhinoException in project hackpad by dropbox.

the class Main method loadCompiledScript.

private static Script loadCompiledScript(Context cx, String path, byte[] data, Object securityDomain) {
    if (data == null) {
        exitCode = EXITCODE_FILE_NOT_FOUND;
        return null;
    }
    // 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 (RhinoException rex) {
        ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (IllegalAccessException iaex) {
        exitCode = EXITCODE_RUNTIME_ERROR;
        Context.reportError(iaex.toString());
    } catch (InstantiationException inex) {
        exitCode = EXITCODE_RUNTIME_ERROR;
        Context.reportError(inex.toString());
    }
    return null;
}
Also used : Script(org.mozilla.javascript.Script) GeneratedClassLoader(org.mozilla.javascript.GeneratedClassLoader) RhinoException(org.mozilla.javascript.RhinoException)

Example 2 with RhinoException

use of org.mozilla.javascript.RhinoException in project hackpad by dropbox.

the class ContextFactoryTest method testCustomContextFactory.

public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
    // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
    /* TODO(stevey): fix this functionality in parser
            Object result = cx.evaluateString(globalScope,
                    "var obj = {};" +
                    "function obj.foo() { return 'bar'; }" +
                    "obj.foo();",
                    "test source", 1, null);
            assertEquals("bar", result);
            */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 3 with RhinoException

use of org.mozilla.javascript.RhinoException in project neo4j by neo4j.

the class JavascriptExecutor method execute.

@Override
public Object execute(Map<String, Object> variables) throws EvaluationException {
    Context cx = Context.enter();
    try {
        Scriptable scope = cx.newObject(prototype);
        scope.setPrototype(prototype);
        if (variables != null) {
            for (String k : variables.keySet()) {
                scope.put(k, scope, variables.get(k));
            }
        }
        Object out = compiledScript.exec(cx, scope);
        if (out instanceof NativeJavaObject) {
            return ((NativeJavaObject) out).unwrap();
        } else if (out instanceof Undefined) {
            return null;
        } else {
            return out;
        }
    } catch (RhinoException e) {
        throw new EvaluationException("Failed to execute script, see nested exception.", e);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Undefined(org.mozilla.javascript.Undefined) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) RhinoException(org.mozilla.javascript.RhinoException) EvaluationException(org.neo4j.server.rest.domain.EvaluationException) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 4 with RhinoException

use of org.mozilla.javascript.RhinoException in project scriptographer by scriptographer.

the class RhinoScriptException method getFullMessage.

public String getFullMessage() {
    Throwable cause = getCause();
    String separator = System.getProperty("file.separator");
    if (cause instanceof RhinoException) {
        RhinoException re = (RhinoException) cause;
        StringWriter buf = new StringWriter();
        PrintWriter writer = new PrintWriter(buf);
        if (re instanceof WrappedException) {
            // Make sure we're not printing the "Wrapped ...Exception:" part
            writer.println(((WrappedException) re).getWrappedException().getMessage());
        } else {
            writer.println(re.details());
        }
        String[] stackTrace = re.getScriptStackTrace().split("\\r\\n|\\n|\\r");
        String sourceName = re.sourceName();
        if (sourceName != null) {
            int lineNumber = re.lineNumber();
            // TODO Why is this needed? Rhino bug?
            if (stackTrace.length == 0 || stackTrace[0].indexOf(sourceName + ":" + lineNumber) == -1) {
                String[] path = engine.getScriptPath(new File(sourceName));
                if (path != null)
                    writer.println("\tat " + StringUtils.join(path, separator) + ":" + lineNumber);
            }
        }
        // Parse the lines for filename:linenumber
        Pattern pattern = Pattern.compile("\\s+at\\s+(.+):(\\d+)");
        for (int i = 0; i < stackTrace.length; i++) {
            String line = stackTrace[i];
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                String file = matcher.group(1);
                // that are located in base:
                if (file.indexOf(separator + "__") == -1) {
                    String[] path = engine.getScriptPath(new File(file));
                    if (path != null) {
                        writer.println("\tat " + StringUtils.join(path, separator) + ":" + matcher.group(2));
                    }
                }
            }
        }
        return buf.toString().trim();
    } else {
        String message = cause.getMessage();
        String error = cause.getClass().getSimpleName();
        if (message != null && message.length() != 0)
            error += ": " + message;
        return error;
    }
}
Also used : Pattern(java.util.regex.Pattern) WrappedException(org.mozilla.javascript.WrappedException) StringWriter(java.io.StringWriter) Matcher(java.util.regex.Matcher) RhinoException(org.mozilla.javascript.RhinoException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 5 with RhinoException

use of org.mozilla.javascript.RhinoException in project OpenAM by OpenRock.

the class RhinoScriptEngine method eval.

/**
     * {@inheritDoc}
     */
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(reader, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        final String filename = getFilename(scriptContext);
        result = context.evaluateReader(scope, reader, filename, 1, null);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptException(javax.script.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

RhinoException (org.mozilla.javascript.RhinoException)9 Context (org.mozilla.javascript.Context)6 Scriptable (org.mozilla.javascript.Scriptable)5 ScriptContext (javax.script.ScriptContext)4 IOException (java.io.IOException)3 Script (org.mozilla.javascript.Script)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 ScriptException (javax.script.ScriptException)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CompiledScript (javax.script.CompiledScript)1 SimpleScriptContext (javax.script.SimpleScriptContext)1