src/jdk/nashorn/internal/runtime/ScriptingFunctions.java

changeset 1403
b39a918a34a4
parent 1398
2f1b9f4daec1
child 1405
98b090e45df3
     1.1 --- a/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Mon Jun 08 10:28:04 2015 +0200
     1.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Tue Jun 09 09:27:02 2015 +0200
     1.3 @@ -41,6 +41,7 @@
     1.4  import java.util.ArrayList;
     1.5  import java.util.List;
     1.6  import java.util.Map;
     1.7 +import jdk.nashorn.internal.objects.NativeArray;
     1.8  
     1.9  /**
    1.10   * Global functions supported only in scripting mode.
    1.11 @@ -54,7 +55,7 @@
    1.12      public static final MethodHandle READFULLY = findOwnMH("readFully",     Object.class, Object.class, Object.class);
    1.13  
    1.14      /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
    1.15 -    public static final MethodHandle EXEC = findOwnMH("exec",     Object.class, Object.class, Object.class, Object.class);
    1.16 +    public static final MethodHandle EXEC = findOwnMH("exec",     Object.class, Object.class, Object.class, Object.class, Object[].class);
    1.17  
    1.18      /** EXEC name - special property used by $EXEC API. */
    1.19      public static final String EXEC_NAME = "$EXEC";
    1.20 @@ -128,17 +129,30 @@
    1.21       * @param self   self reference
    1.22       * @param string string to execute
    1.23       * @param input  input
    1.24 +     * @param argv   additional arguments, to be appended to {@code string}. Additional arguments can be passed as
    1.25 +     *               either one JavaScript array, whose elements will be converted to strings; or as a sequence of
    1.26 +     *               varargs, each of which will be converted to a string.
    1.27       *
    1.28       * @return output string from the request
    1.29 +     *
    1.30       * @throws IOException           if any stream access fails
    1.31       * @throws InterruptedException  if execution is interrupted
    1.32       */
    1.33 -    public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException {
    1.34 +    public static Object exec(final Object self, final Object string, final Object input, final Object... argv) throws IOException, InterruptedException {
    1.35          // Current global is need to fetch additional inputs and for additional results.
    1.36          final ScriptObject global = Context.getGlobal();
    1.37  
    1.38 +        // Assemble command line, process additional arguments.
    1.39 +        final List<String> cmdLine = tokenizeString(JSType.toString(string));
    1.40 +        final Object[] additionalArgs = argv.length == 1 && argv[0] instanceof NativeArray ?
    1.41 +                ((NativeArray) argv[0]).asObjectArray() :
    1.42 +                argv;
    1.43 +        for (Object arg : additionalArgs) {
    1.44 +            cmdLine.add(JSType.toString(arg));
    1.45 +        }
    1.46 +
    1.47          // Set up initial process.
    1.48 -        final ProcessBuilder processBuilder = new ProcessBuilder(tokenizeString(JSType.toString(string)));
    1.49 +        final ProcessBuilder processBuilder = new ProcessBuilder(cmdLine);
    1.50  
    1.51          // Current ENV property state.
    1.52          final Object env = global.get(ENV_NAME);

mercurial