Ejemplo n.º 1
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();
    }
  }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
  @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();
    }
  }