コード例 #1
0
 /**
  * Evaluates a script.
  *
  * @param script string representation of the script to evaluate.
  * @param sourceName the name of the evaluated script.
  * @return evaluated object.
  * @throws IOException if the script couldn't be retrieved.
  */
 public Object evaluate(final String script, final String sourceName) {
   if (script == null) {
     throw new IllegalArgumentException("script cannot be null");
   }
   try {
     return context.evaluateString(scope, script, sourceName, 1, null);
   } catch (final JavaScriptException e) {
     LOG.error("JavaScriptException occured: " + e.getMessage());
     throw e;
   }
 }
コード例 #2
0
 /**
  * Evaluates a script from a reader.
  *
  * @param reader {@link Reader} of the script to evaluate.
  * @param sourceName the name of the evaluated script.
  * @return evaluated object.
  * @throws IOException if the script couldn't be retrieved.
  */
 public Object evaluate(final Reader reader, final String sourceName) throws IOException {
   if (reader == null) {
     throw new IllegalArgumentException("reader cannot be null");
   }
   try {
     return context.evaluateReader(scope, reader, sourceName, 1, null);
   } catch (final JavaScriptException e) {
     LOG.error("JavaScriptException occured: " + e.getMessage());
     throw e;
   } finally {
     reader.close();
   }
 }
コード例 #3
0
  /**
   * Evaluate JavaScript source.
   *
   * @param cx the current context
   * @param filename the name of the file to compile, or null for interactive mode.
   */
  private void processSource(Context cx, String filename) {
    if (filename == null) {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String sourceName = "<stdin>";
      int lineno = 1;
      boolean hitEOF = false;
      do {
        int startline = lineno;
        System.err.print("js> ");
        System.err.flush();
        try {
          String source = "";
          // Collect lines of source to compile.
          while (true) {
            String newline;
            newline = in.readLine();
            if (newline == null) {
              hitEOF = true;
              break;
            }
            source = source + newline + "\n";
            lineno++;
            // Continue collecting as long as more lines
            // are needed to complete the current
            // statement. stringIsCompilableUnit is also
            // true if the source statement will result in
            // any error other than one that might be
            // resolved by appending more source.
            if (cx.stringIsCompilableUnit(source)) {
              break;
            }
          }
          Object result = cx.evaluateString(this, source, sourceName, startline, null);
          if (result != Context.getUndefinedValue()) {
            System.err.println(Context.toString(result));
          }
        } catch (WrappedException we) {
          // Some form of exception was caught by JavaScript and
          // propagated up.
          System.err.println(we.getWrappedException().toString());
          we.printStackTrace();
        } catch (EvaluatorException ee) {
          // Some form of JavaScript error.
          System.err.println("js: " + ee.getMessage());
        } catch (JavaScriptException jse) {
          // Some form of JavaScript error.
          System.err.println("js: " + jse.getMessage());
        } catch (IOException ioe) {
          System.err.println(ioe.toString());
        }
        if (quitting) {
          // The user executed the quit() function.
          break;
        }
      } while (!hitEOF);
      System.err.println();
    } else {
      FileReader in = null;
      try {
        in = new FileReader(filename);
      } catch (FileNotFoundException ex) {
        Context.reportError("Couldn't open file \"" + filename + "\".");
        return;
      }

      try {
        // Here we evalute the entire contents of the file as
        // a script. Text is printed only if the print() function
        // is called.
        cx.evaluateReader(this, in, filename, 1, null);
      } catch (WrappedException we) {
        System.err.println(we.getWrappedException().toString());
        we.printStackTrace();
      } catch (EvaluatorException ee) {
        System.err.println("js: " + ee.getMessage());
      } catch (JavaScriptException jse) {
        System.err.println("js: " + jse.getMessage());
      } catch (IOException ioe) {
        System.err.println(ioe.toString());
      } finally {
        try {
          in.close();
        } catch (IOException ioe) {
          System.err.println(ioe.toString());
        }
      }
    }
  }