22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
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() {
110 public static Object readFully(final Object self, final Object file) throws IOException {
111 File f = null;
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) {
|
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
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.Arrays;
43 import java.util.List;
44 import java.util.Map;
45 import jdk.nashorn.internal.objects.NativeArray;
46
47 /**
48 * Global functions supported only in scripting mode.
49 */
50 public final class ScriptingFunctions {
51
52 /** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */
53 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class);
54
55 /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
56 public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
57
58 /** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */
59 public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object[].class);
60
61 /** EXEC name - special property used by $EXEC API. */
62 public static final String EXEC_NAME = "$EXEC";
63
64 /** OUT name - special property used by $EXEC API. */
65 public static final String OUT_NAME = "$OUT";
66
67 /** ERR name - special property used by $EXEC API. */
68 public static final String ERR_NAME = "$ERR";
69
70 /** EXIT name - special property used by $EXEC API. */
71 public static final String EXIT_NAME = "$EXIT";
72
73 /** Names of special properties used by $ENV API. */
74 public static final String ENV_NAME = "$ENV";
75
76 /** Name of the environment variable for the current working directory. */
77 public static final String PWD_NAME = "PWD";
78
79 private ScriptingFunctions() {
111 public static Object readFully(final Object self, final Object file) throws IOException {
112 File f = null;
113
114 if (file instanceof File) {
115 f = (File)file;
116 } else if (JSType.isString(file)) {
117 f = new java.io.File(((CharSequence)file).toString());
118 }
119
120 if (f == null || !f.isFile()) {
121 throw typeError("not.a.file", ScriptRuntime.safeToString(file));
122 }
123
124 return new String(Source.readFully(f));
125 }
126
127 /**
128 * Nashorn extension: exec a string in a separate process.
129 *
130 * @param self self reference
131 * @param args string to execute, input and additional arguments, to be appended to {@code string}. Additional arguments can be passed as
132 * either one JavaScript array, whose elements will be converted to strings; or as a sequence of
133 * varargs, each of which will be converted to a string.
134 *
135 * @return output string from the request
136 *
137 * @throws IOException if any stream access fails
138 * @throws InterruptedException if execution is interrupted
139 */
140 public static Object exec(final Object self, final Object... args) throws IOException, InterruptedException {
141 // Current global is need to fetch additional inputs and for additional results.
142 final ScriptObject global = Context.getGlobal();
143 final Object string = args.length > 0? args[0] : UNDEFINED;
144 final Object input = args.length > 1? args[1] : UNDEFINED;
145 final Object[] argv = (args.length > 2)? Arrays.copyOfRange(args, 2, args.length) : ScriptRuntime.EMPTY_ARRAY;
146 // Assemble command line, process additional arguments.
147 final List<String> cmdLine = tokenizeString(JSType.toString(string));
148 final Object[] additionalArgs = argv.length == 1 && argv[0] instanceof NativeArray ?
149 ((NativeArray) argv[0]).asObjectArray() :
150 argv;
151 for (Object arg : additionalArgs) {
152 cmdLine.add(JSType.toString(arg));
153 }
154
155 // Set up initial process.
156 final ProcessBuilder processBuilder = new ProcessBuilder(cmdLine);
157
158 // Current ENV property state.
159 final Object env = global.get(ENV_NAME);
160 if (env instanceof ScriptObject) {
161 final ScriptObject envProperties = (ScriptObject)env;
162
163 // If a working directory is present, use it.
164 final Object pwd = envProperties.get(PWD_NAME);
165 if (pwd != UNDEFINED) {
|