/** * 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); }
/** * 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); }
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); }
/** * 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(); }
private void initScripting(final ScriptEnvironment scriptEnv) { ScriptObject value; value = ScriptFunction.createBuiltin("readLine", ScriptingFunctions.READLINE); addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value); value = ScriptFunction.createBuiltin("readFully", ScriptingFunctions.READFULLY); addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value); final String execName = ScriptingFunctions.EXEC_NAME; value = ScriptFunction.createBuiltin(execName, ScriptingFunctions.EXEC); addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value); // Nashorn extension: global.echo (scripting-mode-only) // alias for "print" value = (ScriptObject)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) final ScriptObject env = newObject(); if (System.getSecurityManager() == null) { // do not fill $ENV if we have a security manager around // Retrieve current state of ENV variables. env.putAll(System.getenv(), scriptEnv._strict); // Set the PWD variable to a value that is guaranteed to be understood // by the underlying platform. env.put(ScriptingFunctions.PWD_NAME, System.getProperty("user.dir"), scriptEnv._strict); } addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env); // 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); }
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); }
private void initScripting(final ScriptEnvironment scriptEnv) { ScriptObject value; value = ScriptFunction.createBuiltin("readLine", ScriptingFunctions.READLINE); addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value); value = ScriptFunction.createBuiltin("readFully", ScriptingFunctions.READFULLY); addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value); final String execName = ScriptingFunctions.EXEC_NAME; value = ScriptFunction.createBuiltin(execName, ScriptingFunctions.EXEC); value.addOwnProperty(ScriptingFunctions.THROW_ON_ERROR_NAME, Attribute.NOT_ENUMERABLE, false); addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value); // Nashorn extension: global.echo (scripting-mode-only) // alias for "print" value = (ScriptObject)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); }
/** * 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]); }