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