24 */
25
26 package jdk.nashorn.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.io.StreamTokenizer;
38 import java.io.StringReader;
39 import java.lang.invoke.MethodHandle;
40 import java.lang.invoke.MethodHandles;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map;
44
45 /**
46 * Global functions supported only in scripting mode.
47 */
48 public final class ScriptingFunctions {
49
50 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
51 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
52
53 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
54 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
55
56 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
57 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class);
58
59 /** EXEC name - special property used by $EXEC API. */
60 public static final String EXEC_NAME = "$EXEC";
61
62 /** OUT name - special property used by $EXEC API. */
63 public static final String OUT_NAME = "$OUT";
64
65 /** ERR name - special property used by $EXEC API. */
66 public static final String ERR_NAME = "$ERR";
67
68 /** EXIT name - special property used by $EXEC API. */
69 public static final String EXIT_NAME = "$EXIT";
70
71 /** Names of special properties used by $ENV API. */
72 public static final String ENV_NAME = "$ENV";
73
74 /** Name of the environment variable for the current working directory. */
75 public static final String PWD_NAME = "PWD";
76
77 private ScriptingFunctions() {
111
112 if (file instanceof File) {
113 f = (File)file;
114 } else if (JSType.isString(file)) {
115 f = new java.io.File(((CharSequence)file).toString());
116 }
117
118 if (f == null || !f.isFile()) {
119 throw typeError("not.a.file", ScriptRuntime.safeToString(file));
120 }
121
122 return new String(Source.readFully(f));
123 }
124
125 /**
126 * Nashorn extension: exec a string in a separate process.
127 *
128 * @param self self reference
129 * @param string string to execute
130 * @param input input
131 *
132 * @return output string from the request
133 * @throws IOException if any stream access fails
134 * @throws InterruptedException if execution is interrupted
135 */
136 public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException {
137 // Current global is need to fetch additional inputs and for additional results.
138 final ScriptObject global = Context.getGlobal();
139
140 // Set up initial process.
141 final ProcessBuilder processBuilder = new ProcessBuilder(tokenizeString(JSType.toString(string)));
142
143 // Current ENV property state.
144 final Object env = global.get(ENV_NAME);
145 if (env instanceof ScriptObject) {
146 final ScriptObject envProperties = (ScriptObject)env;
147
148 // If a working directory is present, use it.
149 final Object pwd = envProperties.get(PWD_NAME);
150 if (pwd != UNDEFINED) {
151 processBuilder.directory(new File(JSType.toString(pwd)));
152 }
153
154 // Set up ENV variables.
155 final Map<String, String> environment = processBuilder.environment();
156 environment.clear();
157 for (final Map.Entry<Object, Object> entry : envProperties.entrySet()) {
158 environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue()));
159 }
160 }
161
|
24 */
25
26 package jdk.nashorn.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
31
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.io.StreamTokenizer;
38 import java.io.StringReader;
39 import java.lang.invoke.MethodHandle;
40 import java.lang.invoke.MethodHandles;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map;
44 import jdk.nashorn.internal.objects.NativeArray;
45
46 /**
47 * Global functions supported only in scripting mode.
48 */
49 public final class ScriptingFunctions {
50
51 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
52 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
53
54 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
55 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
56
57 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
58 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class, Object[].class);
59
60 /** EXEC name - special property used by $EXEC API. */
61 public static final String EXEC_NAME = "$EXEC";
62
63 /** OUT name - special property used by $EXEC API. */
64 public static final String OUT_NAME = "$OUT";
65
66 /** ERR name - special property used by $EXEC API. */
67 public static final String ERR_NAME = "$ERR";
68
69 /** EXIT name - special property used by $EXEC API. */
70 public static final String EXIT_NAME = "$EXIT";
71
72 /** Names of special properties used by $ENV API. */
73 public static final String ENV_NAME = "$ENV";
74
75 /** Name of the environment variable for the current working directory. */
76 public static final String PWD_NAME = "PWD";
77
78 private ScriptingFunctions() {
112
113 if (file instanceof File) {
114 f = (File)file;
115 } else if (JSType.isString(file)) {
116 f = new java.io.File(((CharSequence)file).toString());
117 }
118
119 if (f == null || !f.isFile()) {
120 throw typeError("not.a.file", ScriptRuntime.safeToString(file));
121 }
122
123 return new String(Source.readFully(f));
124 }
125
126 /**
127 * Nashorn extension: exec a string in a separate process.
128 *
129 * @param self self reference
130 * @param string string to execute
131 * @param input input
132 * @param argv additional arguments, to be appended to {@code string}. Additional arguments can be passed as
133 * either one JavaScript array, whose elements will be converted to strings; or as a sequence of
134 * varargs, each of which will be converted to a string.
135 *
136 * @return output string from the request
137 *
138 * @throws IOException if any stream access fails
139 * @throws InterruptedException if execution is interrupted
140 */
141 public static Object exec(final Object self, final Object string, final Object input, final Object... argv) throws IOException, InterruptedException {
142 // Current global is need to fetch additional inputs and for additional results.
143 final ScriptObject global = Context.getGlobal();
144
145 // Assemble command line, process additional arguments.
146 final List<String> cmdLine = tokenizeString(JSType.toString(string));
147 final Object[] additionalArgs = argv.length == 1 && argv[0] instanceof NativeArray ?
148 ((NativeArray) argv[0]).asObjectArray() :
149 argv;
150 for (Object arg : additionalArgs) {
151 cmdLine.add(JSType.toString(arg));
152 }
153
154 // Set up initial process.
155 final ProcessBuilder processBuilder = new ProcessBuilder(cmdLine);
156
157 // Current ENV property state.
158 final Object env = global.get(ENV_NAME);
159 if (env instanceof ScriptObject) {
160 final ScriptObject envProperties = (ScriptObject)env;
161
162 // If a working directory is present, use it.
163 final Object pwd = envProperties.get(PWD_NAME);
164 if (pwd != UNDEFINED) {
165 processBuilder.directory(new File(JSType.toString(pwd)));
166 }
167
168 // Set up ENV variables.
169 final Map<String, String> environment = processBuilder.environment();
170 environment.clear();
171 for (final Map.Entry<Object, Object> entry : envProperties.entrySet()) {
172 environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue()));
173 }
174 }
175
|