Search in sources :

Example 6 with RhinoException

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

the class RhinoScriptEngine method evalCompiled.

/**
     * Evaluates a pre-compiled script against this script engine. This should only be called by
     * {@link org.forgerock.openam.scripting.factories.RhinoCompiledScript#eval(javax.script.ScriptContext)}.
     *
     * @param compiledScript The compiled script. Must not be null.
     * @param scriptContext The JSR 223 script context. Must not be null.
     * @return the result of evaluating the compiled script.
     * @throws ScriptException if an error occurs during script execution.
     */
Object evalCompiled(final Script compiledScript, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(compiledScript, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        result = compiledScript.exec(context, scope);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 7 with RhinoException

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

the class Main method processSource.

/**
     * Evaluate JavaScript source.
     *
     * @param cx the current context
     * @param filename the name of the file to compile, or null
     *                 for interactive mode.
     */
public static void processSource(Context cx, String filename) {
    if (filename == null || filename.equals("-")) {
        PrintStream ps = global.getErr();
        if (filename == null) {
            // print implementation version
            ps.println(cx.getImplementationVersion());
        }
        String charEnc = shellContextFactory.getCharacterEncoding();
        if (charEnc == null) {
            charEnc = System.getProperty("file.encoding");
        }
        BufferedReader in;
        try {
            in = new BufferedReader(new InputStreamReader(global.getIn(), charEnc));
        } catch (UnsupportedEncodingException e) {
            throw new UndeclaredThrowableException(e);
        }
        int lineno = 1;
        boolean hitEOF = false;
        while (!hitEOF) {
            String[] prompts = global.getPrompts(cx);
            if (filename == null)
                ps.print(prompts[0]);
            ps.flush();
            String source = "";
            // Collect lines of source to compile.
            while (true) {
                String newline;
                try {
                    newline = in.readLine();
                } catch (IOException ioe) {
                    ps.println(ioe.toString());
                    break;
                }
                if (newline == null) {
                    hitEOF = true;
                    break;
                }
                source = source + newline + "\n";
                lineno++;
                if (cx.stringIsCompilableUnit(source))
                    break;
                ps.print(prompts[1]);
            }
            Script script = loadScriptFromSource(cx, source, "<stdin>", lineno, null);
            if (script != null) {
                Object result = evaluateScript(script, cx, global);
                // Avoid printing out undefined or function definitions.
                if (result != Context.getUndefinedValue() && !(result instanceof Function && source.trim().startsWith("function"))) {
                    try {
                        ps.println(Context.toString(result));
                    } catch (RhinoException rex) {
                        ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
                    }
                }
                NativeArray h = global.history;
                h.put((int) h.getLength(), h, source);
            }
        }
        ps.println();
    } else if (filename.equals(mainModule)) {
        try {
            require.requireMain(cx, filename);
        } 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());
            exitCode = EXITCODE_RUNTIME_ERROR;
            Context.reportError(msg);
        }
    } else {
        processFile(cx, global, filename);
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) PrintStream(java.io.PrintStream) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Function(org.mozilla.javascript.Function) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BufferedReader(java.io.BufferedReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException)

Example 8 with RhinoException

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

the class RhinoScriptEngine method compile.

/**
     * {@inheritDoc}
     */
@Override
public CompiledScript compile(final Reader reader) throws ScriptException {
    Reject.ifNull(reader);
    final Context context = factory.getContext();
    try {
        // Use configured ScriptContext from parent class, if specified
        final String filename = getFilename(getContext());
        final Script compiledScript = context.compileReader(reader, filename, 1, null);
        return new RhinoCompiledScript(this, compiledScript);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) Script(org.mozilla.javascript.Script) CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException)

Example 9 with RhinoException

use of org.mozilla.javascript.RhinoException in project jmeter by apache.

the class JavaScript method executeWithRhino.

/**
     * @param previousResult {@link SampleResult}
     * @param currentSampler {@link Sampler}
     * @param jmctx {@link JMeterContext}
     * @param vars {@link JMeterVariables}
     * @param script Javascript code
     * @param varName variable name
     * @return result as String
     * @throws InvalidVariableException
     */
private String executeWithRhino(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    Context cx = Context.enter();
    String resultStr = null;
    try {
        Scriptable scope = cx.initStandardObjects(null);
        // Set up some objects for the script to play with
        //$NON-NLS-1$
        scope.put("log", scope, log);
        //$NON-NLS-1$
        scope.put("ctx", scope, jmctx);
        //$NON-NLS-1$
        scope.put("vars", scope, vars);
        //$NON-NLS-1$
        scope.put("props", scope, JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        scope.put("threadName", scope, Thread.currentThread().getName());
        //$NON-NLS-1$
        scope.put("sampler", scope, currentSampler);
        //$NON-NLS-1$
        scope.put("sampleResult", scope, previousResult);
        //$NON-NLS-1$
        Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
        resultStr = Context.toString(result);
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (RhinoException e) {
        log.error("Error processing Javascript: [" + script + "]\n", e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    } finally {
        Context.exit();
    }
    return resultStr;
}
Also used : Context(org.mozilla.javascript.Context) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) JMeterContext(org.apache.jmeter.threads.JMeterContext) RhinoException(org.mozilla.javascript.RhinoException) 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