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

Thu, 31 Aug 2017 15:30:47 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:30:47 +0800
changeset 952
6d5471a497fb
parent 655
adab2c628923
parent 0
b1a7da25b547
child 1205
4112748288bb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package jdk.nashorn.internal.runtime;
aoqi@0 27
aoqi@0 28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
aoqi@0 29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
aoqi@0 30 import static jdk.nashorn.internal.lookup.Lookup.MH;
aoqi@0 31
aoqi@0 32 import java.io.BufferedReader;
aoqi@0 33 import java.io.File;
aoqi@0 34 import java.io.IOException;
aoqi@0 35 import java.io.InputStreamReader;
aoqi@0 36 import java.io.OutputStreamWriter;
aoqi@0 37 import java.lang.invoke.MethodHandle;
aoqi@0 38 import java.lang.invoke.MethodHandles;
aoqi@0 39 import java.util.Map;
aoqi@0 40 import java.util.StringTokenizer;
aoqi@0 41
aoqi@0 42 /**
aoqi@0 43 * Global functions supported only in scripting mode.
aoqi@0 44 */
aoqi@0 45 public final class ScriptingFunctions {
aoqi@0 46
aoqi@0 47 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
aoqi@0 48 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
aoqi@0 49
aoqi@0 50 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
aoqi@0 51 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
aoqi@0 52
aoqi@0 53 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
aoqi@0 54 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class);
aoqi@0 55
aoqi@0 56 /** EXEC name - special property used by $EXEC API. */
aoqi@0 57 public static final String EXEC_NAME = "$EXEC";
aoqi@0 58
aoqi@0 59 /** OUT name - special property used by $EXEC API. */
aoqi@0 60 public static final String OUT_NAME = "$OUT";
aoqi@0 61
aoqi@0 62 /** ERR name - special property used by $EXEC API. */
aoqi@0 63 public static final String ERR_NAME = "$ERR";
aoqi@0 64
aoqi@0 65 /** EXIT name - special property used by $EXEC API. */
aoqi@0 66 public static final String EXIT_NAME = "$EXIT";
aoqi@0 67
aoqi@0 68 /** Names of special properties used by $ENV API. */
aoqi@0 69 public static final String ENV_NAME = "$ENV";
aoqi@0 70
aoqi@0 71 private static final String PWD_NAME = "PWD";
aoqi@0 72
aoqi@0 73 private ScriptingFunctions() {
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Nashorn extension: global.readLine (scripting-mode-only)
aoqi@0 78 * Read one line of input from the standard input.
aoqi@0 79 *
aoqi@0 80 * @param self self reference
aoqi@0 81 * @param prompt String used as input prompt
aoqi@0 82 *
aoqi@0 83 * @return line that was read
aoqi@0 84 *
aoqi@0 85 * @throws IOException if an exception occurs
aoqi@0 86 */
aoqi@0 87 public static Object readLine(final Object self, final Object prompt) throws IOException {
aoqi@0 88 if (prompt != UNDEFINED) {
aoqi@0 89 System.out.print(JSType.toString(prompt));
aoqi@0 90 }
aoqi@0 91 final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
aoqi@0 92 return reader.readLine();
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Nashorn extension: Read the entire contents of a text file and return as String.
aoqi@0 97 *
aoqi@0 98 * @param self self reference
aoqi@0 99 * @param file The input file whose content is read.
aoqi@0 100 *
aoqi@0 101 * @return String content of the input file.
aoqi@0 102 *
aoqi@0 103 * @throws IOException if an exception occurs
aoqi@0 104 */
aoqi@0 105 public static Object readFully(final Object self, final Object file) throws IOException {
aoqi@0 106 File f = null;
aoqi@0 107
aoqi@0 108 if (file instanceof File) {
aoqi@0 109 f = (File)file;
aoqi@0 110 } else if (file instanceof String) {
aoqi@0 111 f = new java.io.File((String)file);
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 if (f == null || !f.isFile()) {
aoqi@0 115 throw typeError("not.a.file", ScriptRuntime.safeToString(file));
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 return new String(Source.readFully(f));
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 /**
aoqi@0 122 * Nashorn extension: exec a string in a separate process.
aoqi@0 123 *
aoqi@0 124 * @param self self reference
aoqi@0 125 * @param string string to execute
aoqi@0 126 * @param input input
aoqi@0 127 *
aoqi@0 128 * @return output string from the request
aoqi@0 129 * @throws IOException if any stream access fails
aoqi@0 130 * @throws InterruptedException if execution is interrupted
aoqi@0 131 */
aoqi@0 132 public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException {
aoqi@0 133 // Current global is need to fetch additional inputs and for additional results.
aoqi@0 134 final ScriptObject global = Context.getGlobal();
aoqi@0 135
aoqi@0 136 // Break exec string into tokens.
aoqi@0 137 final StringTokenizer tokenizer = new StringTokenizer(JSType.toString(string));
aoqi@0 138 final String[] cmdArray = new String[tokenizer.countTokens()];
aoqi@0 139 for (int i = 0; tokenizer.hasMoreTokens(); i++) {
aoqi@0 140 cmdArray[i] = tokenizer.nextToken();
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 // Set up initial process.
aoqi@0 144 final ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
aoqi@0 145
aoqi@0 146 // Current ENV property state.
aoqi@0 147 final Object env = global.get(ENV_NAME);
aoqi@0 148 if (env instanceof ScriptObject) {
aoqi@0 149 final ScriptObject envProperties = (ScriptObject)env;
aoqi@0 150
aoqi@0 151 // If a working directory is present, use it.
aoqi@0 152 final Object pwd = envProperties.get(PWD_NAME);
aoqi@0 153 if (pwd != UNDEFINED) {
aoqi@0 154 processBuilder.directory(new File(JSType.toString(pwd)));
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 // Set up ENV variables.
aoqi@0 158 final Map<String, String> environment = processBuilder.environment();
aoqi@0 159 environment.clear();
aoqi@0 160 for (Map.Entry<Object, Object> entry : envProperties.entrySet()) {
aoqi@0 161 environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue()));
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 // Start the process.
aoqi@0 166 final Process process = processBuilder.start();
aoqi@0 167 final IOException exception[] = new IOException[2];
aoqi@0 168
aoqi@0 169 // Collect output.
aoqi@0 170 final StringBuilder outBuffer = new StringBuilder();
aoqi@0 171 Thread outThread = new Thread(new Runnable() {
aoqi@0 172 @Override
aoqi@0 173 public void run() {
aoqi@0 174 char buffer[] = new char[1024];
aoqi@0 175 try (final InputStreamReader inputStream = new InputStreamReader(process.getInputStream())) {
aoqi@0 176 for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) {
aoqi@0 177 outBuffer.append(buffer, 0, length);
aoqi@0 178 }
aoqi@0 179 } catch (IOException ex) {
aoqi@0 180 exception[0] = ex;
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183 }, "$EXEC output");
aoqi@0 184
aoqi@0 185 // Collect errors.
aoqi@0 186 final StringBuilder errBuffer = new StringBuilder();
aoqi@0 187 Thread errThread = new Thread(new Runnable() {
aoqi@0 188 @Override
aoqi@0 189 public void run() {
aoqi@0 190 char buffer[] = new char[1024];
aoqi@0 191 try (final InputStreamReader inputStream = new InputStreamReader(process.getErrorStream())) {
aoqi@0 192 for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) {
aoqi@0 193 errBuffer.append(buffer, 0, length);
aoqi@0 194 }
aoqi@0 195 } catch (IOException ex) {
aoqi@0 196 exception[1] = ex;
aoqi@0 197 }
aoqi@0 198 }
aoqi@0 199 }, "$EXEC error");
aoqi@0 200
aoqi@0 201 // Start gathering output.
aoqi@0 202 outThread.start();
aoqi@0 203 errThread.start();
aoqi@0 204
aoqi@0 205 // If input is present, pass on to process.
aoqi@0 206 try (OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream())) {
aoqi@0 207 if (input != UNDEFINED) {
aoqi@0 208 String in = JSType.toString(input);
aoqi@0 209 outputStream.write(in, 0, in.length());
aoqi@0 210 }
aoqi@0 211 } catch (IOException ex) {
aoqi@0 212 // Process was not expecting input. May be normal state of affairs.
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 // Wait for the process to complete.
aoqi@0 216 final int exit = process.waitFor();
aoqi@0 217 outThread.join();
aoqi@0 218 errThread.join();
aoqi@0 219
aoqi@0 220 final String out = outBuffer.toString();
aoqi@0 221 final String err = errBuffer.toString();
aoqi@0 222
aoqi@0 223 // Set globals for secondary results.
aoqi@0 224 global.set(OUT_NAME, out, false);
aoqi@0 225 global.set(ERR_NAME, err, false);
aoqi@0 226 global.set(EXIT_NAME, exit, false);
aoqi@0 227
aoqi@0 228 // Propagate exception if present.
aoqi@0 229 for (int i = 0; i < exception.length; i++) {
aoqi@0 230 if (exception[i] != null) {
aoqi@0 231 throw exception[i];
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 // Return the result from stdout.
aoqi@0 236 return out;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
aoqi@0 240 return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types));
aoqi@0 241 }
aoqi@0 242 }

mercurial