jdk.nashorn.internal.runtime.ScriptingFunctions Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ScriptingFunctions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Parser.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #2
Source File: Parser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #3
Source File: Parser.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example #4
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #5
Source File: Parser.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #6
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #7
Source File: Parser.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example #8
Source File: Parser.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments, false);
}
 
Example #9
Source File: Parser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert execString to a call to $EXEC.
 *
 * @param primaryToken Original string token.
 * @return callNode to $EXEC.
 */
CallNode execString(final int primaryLine, final long primaryToken) {
    // Synthesize an ident to call $EXEC.
    final IdentNode execIdent = new IdentNode(primaryToken, finish, ScriptingFunctions.EXEC_NAME);
    // Skip over EXECSTRING.
    next();
    // Set up argument list for call.
    // Skip beginning of edit string expression.
    expect(LBRACE);
    // Add the following expression to arguments.
    final List<Expression> arguments = Collections.singletonList(expression());
    // Skip ending of edit string expression.
    expect(RBRACE);

    return new CallNode(primaryLine, primaryToken, finish, execIdent, arguments);
}
 
Example #10
Source File: Global.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example #11
Source File: Global.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example #12
Source File: Global.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example #13
Source File: ShellFunctions.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: global.input (shell-interactive-mode-only)
 * Read one or more lines of input from the standard input till the
 * given end marker is seen in standard input.
 *
 * @param self   self reference
 * @param endMarker String used as end marker for input
 * @param prompt String used as input prompt
 *
 * @return line that was read
 *
 * @throws IOException if an exception occurs
 */
public static Object input(final Object self, final Object endMarker, final Object prompt) throws IOException {
    final String endMarkerStr = (endMarker != UNDEFINED)? JSType.toString(endMarker) : "";
    final String promptStr = (prompt != UNDEFINED)? JSType.toString(prompt)  : ">> ";
    final StringBuilder buf = new StringBuilder();
    while (true) {
        final String line = ScriptingFunctions.readLine(promptStr);
        if (line == null || line.equals(endMarkerStr)) {
            break;
        }
        buf.append(line);
        buf.append('\n');
    }
    return buf.toString();
}
 
Example #14
Source File: Shell.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example #15
Source File: Shell.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example #16
Source File: Shell.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}
 
Example #17
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);

        // Some platforms, e.g., Windows, do not define the PWD environment
        // variable, so that the $ENV.PWD property needs to be explicitly
        // set.
        if (!env.containsKey(ScriptingFunctions.PWD_NAME)) {
            env.put(ScriptingFunctions.PWD_NAME, System.getProperty("user.dir"), scriptEnv._strict);
        }

        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example #18
Source File: Shell.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Preprocess the command line arguments passed in by the shell. This method checks, for the first non-option
 * argument, whether the file denoted by it begins with a shebang line. If so, it is assumed that execution in
 * shebang mode is intended. The consequence of this is that the identified script file will be treated as the
 * <em>only</em> script file, and all subsequent arguments will be regarded as arguments to the script.
 * <p>
 * This method canonicalizes the command line arguments to the form {@code <options> <script> -- <arguments>} if a
 * shebang script is identified. On platforms that pass shebang arguments as single strings, the shebang arguments
 * will be broken down into single arguments; whitespace is used as separator.
 * <p>
 * Shebang mode is entered regardless of whether the script is actually run directly from the shell, or indirectly
 * via the {@code jjs} executable. It is the user's / script author's responsibility to ensure that the arguments
 * given on the shebang line do not lead to a malformed argument sequence. In particular, the shebang arguments
 * should not contain any whitespace for purposes other than separating arguments, as the different platforms deal
 * with whitespace in different and incompatible ways.
 * <p>
 * @implNote Example:<ul>
 * <li>Shebang line in {@code script.js}: {@code #!/path/to/jjs --language=es6}</li>
 * <li>Command line: {@code ./script.js arg2}</li>
 * <li>{@code args} array passed to Nashorn: {@code --language=es6,./script.js,arg}</li>
 * <li>Required canonicalized arguments array: {@code --language=es6,./script.js,--,arg2}</li>
 * </ul>
 *
 * @param args the command line arguments as passed into Nashorn.
 * @return the passed and possibly canonicalized argument list
 */
private static String[] preprocessArgs(final String[] args) {
    if (args.length == 0) {
        return args;
    }

    final List<String> processedArgs = new ArrayList<>();
    processedArgs.addAll(Arrays.asList(args));

    // Nashorn supports passing multiple shebang arguments. On platforms that pass anything following the
    // shebang interpreter notice as one argument, the first element of the argument array needs to be special-cased
    // as it might actually contain several arguments. Mac OS X splits shebang arguments, other platforms don't.
    // This special handling is also only necessary if the first argument actually starts with an option.
    if (args[0].startsWith("-") && !System.getProperty("os.name", "generic").startsWith("Mac OS X")) {
        processedArgs.addAll(0, ScriptingFunctions.tokenizeString(processedArgs.remove(0)));
    }

    int shebangFilePos = -1; // -1 signifies "none found"
    // identify a shebang file and its position in the arguments array (if any)
    for (int i = 0; i < processedArgs.size(); ++i) {
        final String a = processedArgs.get(i);
        if (!a.startsWith("-")) {
            final Path p = Paths.get(a);
            String l = "";
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                l = r.readLine();
            } catch (IOException ioe) {
                // ignore
            }
            if (l.startsWith("#!")) {
                shebangFilePos = i;
            }
            // We're only checking the first non-option argument. If it's not a shebang file, we're in normal
            // execution mode.
            break;
        }
    }
    if (shebangFilePos != -1) {
        // Insert the argument separator after the shebang script file.
        processedArgs.add(shebangFilePos + 1, "--");
    }
    return processedArgs.toArray(new String[0]);
}