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());
   }
 }
Esempio n. 2
0
  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();
    }
  }
Esempio n. 3
0
 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);
     }
   }
 }
Esempio n. 4
0
 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());
     }
   }
 }
Esempio n. 5
0
  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;
  }
 /**
  * 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;
 }