/** process exception */ private void processRhinoException(RhinoException e) { if (e instanceof JavaScriptException) processJavaScriptException((JavaScriptException) e); String msg = "exception: " + e.getClass().getName() + ": " + e.details(); throw new BuildException(msg, e, getLocation()); }
private static void testScriptStackTrace( final String script, final String expectedStackTrace, final int optimizationLevel) { try { Utilities.executeScript(script, optimizationLevel); } catch (final RhinoException e) { Assert.assertEquals(expectedStackTrace, e.getScriptStackTrace()); } }
private Object executeRhinoScript() { Context cx = Context.enter(); try { cx.setOptimizationLevel(optLevel); Scriptable scope = cx.initStandardObjects(); for (String harnessFile : harnessFiles) { if (!HARNESS_SCRIPT_CACHE.get(optLevel).containsKey(harnessFile)) { HARNESS_SCRIPT_CACHE .get(optLevel) .put( harnessFile, cx.compileReader( new FileReader("test262/harness/" + harnessFile), "test262/harness/" + harnessFile, 1, null)); } HARNESS_SCRIPT_CACHE.get(optLevel).get(harnessFile).exec(cx, scope); } String str = jsFileStr; if (useStrict) { // taken from test262.py str = "\"use strict\";\nvar strict_mode = true;\n" + jsFileStr; } Object result = cx.evaluateString(scope, str, jsFilePath.replaceAll("\\\\", "/"), 1, null); if (errorType != EcmaErrorType.NONE) { fail(String.format("failed negative test. expected error: %s", errorType)); return null; } return result; } catch (RhinoException ex) { if (errorType == EcmaErrorType.NONE || !(ex instanceof EcmaError)) { fail(String.format("%s%n%s", ex.getMessage(), ex.getScriptStackTrace())); } else { if (errorType == EcmaErrorType.ANY) { // passed } else { EcmaError ecmaError = (EcmaError) ex; assertEquals(errorType.name(), ecmaError.getName()); } } return null; } catch (Exception e) { throw new RuntimeException(e); } finally { Context.exit(); } }
protected void rulesInit() { String mappings; try { mappings = jsRules.invokeMethod("attribute_mappings"); this.attributesToRules = parseAttributeMappings(mappings); } catch (RhinoException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Executes an arbitrary expression.<br> * It fails if the expression throws a JsAssertException.<br> * It fails if the expression throws a RhinoException.<br> * Code from JsTester (http://jstester.sf.net/) */ public Object eval(String expr) { Object value = null; try { value = context.evaluateString(globalScope, expr, "", 1, null); } catch (JavaScriptException jse) { Scriptable jsAssertException = (Scriptable) globalScope.get("currentException", globalScope); jse.printStackTrace(); String message = (String) jsAssertException.get("message", jsAssertException); if (message != null) { fail(message); } } catch (RhinoException re) { fail(re.getMessage()); } return value; }
public void loadFiles() { for (String filename : new HashSet<String>(jslibFiles.keySet())) { try { call(PersevereContextFactory.getContext(), global, global, new Object[] {filename}); } catch (RhinoException e) { log.error( e.details() + " on line " + e.lineNumber() + " in " + e.sourceName() + '\n' + e.getScriptStackTrace()); } catch (Throwable e) { throw new RuntimeException("Trying to load " + filename, e); } } }
public void freezeExports() { for (String filename : new HashSet<String>(jslibFiles.keySet())) { try { ScriptableObject exportObject = (ScriptableObject) call(PersevereContextFactory.getContext(), global, global, new Object[] {filename}); exportObject.sealObject(); } catch (RhinoException e) { log.error( e.details() + " on line " + e.lineNumber() + " in " + e.sourceName() + '\n' + e.getScriptStackTrace()); } } }
public String getJavaScriptStackTrace(Object jsError) { if (jsError instanceof ScriptableObject) { ScriptableObject jsErrorScriptable = (ScriptableObject) jsError; Object wrappedRhinoException = ScriptableObject.getProperty(jsErrorScriptable, "rhinoException"); if (wrappedRhinoException != null) { RhinoException rhinoException = (RhinoException) ((NativeJavaObject) wrappedRhinoException).unwrap(); return rhinoException.getScriptStackTrace(); // StringWriter stringWriter = new StringWriter(); // PrintWriter printWriter = new PrintWriter(stringWriter); // rhinoException.printStackTrace(printWriter); // return stringWriter.toString(); } } return null; // return null; }
@Override public Object evaluate(String script, String filename, Map<String, Object> args) throws ScriptException, Throwable { RhinoContextFactory factory = new RhinoContextFactory(timeLimit); Context cx = factory.enterContext(); ScriptableObject scriptable = new ImporterTopLevel(cx); Scriptable scope = cx.initStandardObjects(scriptable); for (Map.Entry<String, Object> entry : args.entrySet()) { ScriptableObject.putProperty( scope, entry.getKey(), Context.javaToJS(entry.getValue(), scope)); } try { return cx.evaluateString(scope, script, filename, 1, null); } catch (Error e) { throw new ScriptException(e.getMessage()); } catch (RhinoException e) { if (e instanceof WrappedException) { Throwable cause = e.getCause(); if (cause instanceof WorldEditException) { throw cause; } } String msg; int line = (line = e.lineNumber()) == 0 ? -1 : line; if (e instanceof JavaScriptException) { msg = String.valueOf(((JavaScriptException) e).getValue()); } else { msg = e.getMessage(); } ScriptException scriptException = new ScriptException(msg, e.sourceName(), line); scriptException.initCause(e); throw scriptException; } finally { Context.exit(); } }
/** * Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to * throw an exception. * * @param expectingException * @param functionName * @param args * @return */ public Object rhinoCallExpectingException( final Object expectingException, final String functionName, final Object... args) { Object fObj = rhinoScope.get(functionName, rhinoScope); if (!(fObj instanceof Function)) { throw new RuntimeException("Missing test function " + functionName); } Function function = (Function) fObj; try { return function.call(rhinoContext, rhinoScope, rhinoScope, args); } catch (RhinoException angryRhino) { if (expectingException != null && angryRhino instanceof JavaScriptException) { JavaScriptException jse = (JavaScriptException) angryRhino; Assert.assertEquals(jse.getValue(), expectingException); return null; } String trace = angryRhino.getScriptStackTrace(); Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace); } catch (JavaScriptAssertionFailed assertion) { Assert.fail(assertion.getMessage()); } return null; }
/** * Converts general exception to more readable format. * * @param ex * @return */ protected ChartException convertException(Exception ex) { if (ex instanceof RhinoException) { RhinoException e = (RhinoException) ex; String lineSource = e.lineSource(); String details = e.details(); String lineNumber = String.valueOf(e.lineNumber()); if (lineSource == null) lineSource = ""; // $NON-NLS-1$ return new ChartException( ChartEnginePlugin.ID, ChartException.SCRIPT, "exception.javascript.error", //$NON-NLS-1$ new Object[] {details, lineNumber, lineSource}, Messages.getResourceBundle(csc.getULocale()), e); } /* * TODO convert those exceptions too else if ( ex instanceof * IllegalAccessException ) {} else if ( ex instanceof * InstantiationException ) {} else if ( ex instanceof * InvocationTargetException ) { } */ else return new ChartException(ChartEnginePlugin.ID, ChartException.SCRIPT, ex); }