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;
}
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);
}
}
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);
}
}
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;
}
Aggregations