コード例 #1
0
  /**
   * Main entry point.
   *
   * <p>Process arguments as would a normal Java program. Also create a new Context and associate it
   * with the current thread. Then set up the execution environment and begin to execute scripts.
   */
  public static void exec(String args[], ErrorReporter reporter) {
    // Associate a new Context with this thread
    Context cx = Context.enter();
    cx.setErrorReporter(reporter);
    try {
      // Initialize the standard objects (Object, Function, etc.)
      // This must be done before scripts can be executed.
      BasicRhinoShell BasicRhinoShell = new BasicRhinoShell();
      cx.initStandardObjects(BasicRhinoShell);

      // Define some global functions particular to the BasicRhinoShell.
      // Note
      // that these functions are not part of ECMA.
      String[] names = {"print", "quit", "version", "load", "help", "readFile"};
      BasicRhinoShell.defineFunctionProperties(
          names, BasicRhinoShell.class, ScriptableObject.DONTENUM);

      args = processOptions(cx, args);

      // Set up "arguments" in the global scope to contain the command
      // line arguments after the name of the script to execute
      Object[] array;
      if (args.length == 0) {
        array = new Object[0];
      } else {
        int length = args.length - 1;
        array = new Object[length];
        System.arraycopy(args, 1, array, 0, length);
      }
      Scriptable argsObj = cx.newArray(BasicRhinoShell, array);
      BasicRhinoShell.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);

      BasicRhinoShell.processSource(cx, args.length == 0 ? null : args[0]);
    } finally {
      Context.exit();
    }
  }
コード例 #2
0
 /**
  * Load and execute a set of JavaScript source files.
  *
  * <p>This method is defined as a JavaScript function.
  */
 public static void load(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
   BasicRhinoShell BasicRhinoShell = (BasicRhinoShell) getTopLevelScope(thisObj);
   for (Object element : args) {
     BasicRhinoShell.processSource(cx, Context.toString(element));
   }
 }