author | attila |
Mon, 02 Mar 2015 14:33:55 +0100 | |
changeset 29282 | a8523237b66c |
parent 26765 | 97501edd2979 |
child 30700 | b60ef3a8b185 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
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 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24743
diff
changeset
|
28 |
import static jdk.nashorn.internal.lookup.Lookup.MH; |
16147 | 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; |
|
17984 | 36 |
import java.io.OutputStreamWriter; |
16147 | 37 |
import java.lang.invoke.MethodHandle; |
38 |
import java.lang.invoke.MethodHandles; |
|
16211 | 39 |
import java.util.Map; |
40 |
import java.util.StringTokenizer; |
|
16147 | 41 |
|
42 |
/** |
|
43 |
* Global functions supported only in scripting mode. |
|
44 |
*/ |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16219
diff
changeset
|
45 |
public final class ScriptingFunctions { |
16147 | 46 |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16196
diff
changeset
|
47 |
/** Handle to implementation of {@link ScriptingFunctions#readLine} - Nashorn extension */ |
17761
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
48 |
public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class, Object.class); |
16147 | 49 |
|
16196
58f6f046bb5e
8006983: Introduce a command line option to switch off syntactic extensions of nashorn
sundar
parents:
16188
diff
changeset
|
50 |
/** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */ |
58f6f046bb5e
8006983: Introduce a command line option to switch off syntactic extensions of nashorn
sundar
parents:
16188
diff
changeset
|
51 |
public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class); |
16147 | 52 |
|
16219 | 53 |
/** Handle to implementation of {@link ScriptingFunctions#exec} - Nashorn extension */ |
16211 | 54 |
public static final MethodHandle EXEC = findOwnMH("exec", Object.class, Object.class, Object.class, Object.class); |
55 |
||
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
56 |
/** EXEC name - special property used by $EXEC API. */ |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
57 |
public static final String EXEC_NAME = "$EXEC"; |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
58 |
|
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
59 |
/** OUT name - special property used by $EXEC API. */ |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
60 |
public static final String OUT_NAME = "$OUT"; |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
61 |
|
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
62 |
/** ERR name - special property used by $EXEC API. */ |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
63 |
public static final String ERR_NAME = "$ERR"; |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
64 |
|
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
65 |
/** EXIT name - special property used by $EXEC API. */ |
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
66 |
public static final String EXIT_NAME = "$EXIT"; |
16211 | 67 |
|
68 |
/** Names of special properties used by $ENV API. */ |
|
69 |
public static final String ENV_NAME = "$ENV"; |
|
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16226
diff
changeset
|
70 |
|
16211 | 71 |
private static final String PWD_NAME = "PWD"; |
72 |
||
16147 | 73 |
private ScriptingFunctions() { |
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Nashorn extension: global.readLine (scripting-mode-only) |
|
78 |
* Read one line of input from the standard input. |
|
79 |
* |
|
17761
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
80 |
* @param self self reference |
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
81 |
* @param prompt String used as input prompt |
16147 | 82 |
* |
83 |
* @return line that was read |
|
84 |
* |
|
85 |
* @throws IOException if an exception occurs |
|
86 |
*/ |
|
17761
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
87 |
public static Object readLine(final Object self, final Object prompt) throws IOException { |
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
88 |
if (prompt != UNDEFINED) { |
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
89 |
System.out.print(JSType.toString(prompt)); |
bdd9a66c535d
8014827: readLine should accept a prompt as an argument
jlaskey
parents:
16277
diff
changeset
|
90 |
} |
16147 | 91 |
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); |
92 |
return reader.readLine(); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Nashorn extension: Read the entire contents of a text file and return as String. |
|
97 |
* |
|
98 |
* @param self self reference |
|
99 |
* @param file The input file whose content is read. |
|
100 |
* |
|
101 |
* @return String content of the input file. |
|
102 |
* |
|
103 |
* @throws IOException if an exception occurs |
|
104 |
*/ |
|
16196
58f6f046bb5e
8006983: Introduce a command line option to switch off syntactic extensions of nashorn
sundar
parents:
16188
diff
changeset
|
105 |
public static Object readFully(final Object self, final Object file) throws IOException { |
16147 | 106 |
File f = null; |
107 |
||
108 |
if (file instanceof File) { |
|
109 |
f = (File)file; |
|
29282 | 110 |
} else if (JSType.isString(file)) { |
25246
133774398ceb
8048505: ScriptingFunctions.readFully couldn't handle file names represented as ConsStrings
lagergren
parents:
24778
diff
changeset
|
111 |
f = new java.io.File(((CharSequence)file).toString()); |
16147 | 112 |
} |
113 |
||
114 |
if (f == null || !f.isFile()) { |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16252
diff
changeset
|
115 |
throw typeError("not.a.file", ScriptRuntime.safeToString(file)); |
16147 | 116 |
} |
117 |
||
118 |
return new String(Source.readFully(f)); |
|
119 |
} |
|
120 |
||
121 |
/** |
|
16211 | 122 |
* Nashorn extension: exec a string in a separate process. |
123 |
* |
|
124 |
* @param self self reference |
|
125 |
* @param string string to execute |
|
16252
3bfe9b68a0fa
8008648: Lazy JIT scope and callee semantics bugfixes. Broke out wallclock timer.
lagergren
parents:
16233
diff
changeset
|
126 |
* @param input input |
16211 | 127 |
* |
128 |
* @return output string from the request |
|
16252
3bfe9b68a0fa
8008648: Lazy JIT scope and callee semantics bugfixes. Broke out wallclock timer.
lagergren
parents:
16233
diff
changeset
|
129 |
* @throws IOException if any stream access fails |
3bfe9b68a0fa
8008648: Lazy JIT scope and callee semantics bugfixes. Broke out wallclock timer.
lagergren
parents:
16233
diff
changeset
|
130 |
* @throws InterruptedException if execution is interrupted |
16211 | 131 |
*/ |
132 |
public static Object exec(final Object self, final Object string, final Object input) throws IOException, InterruptedException { |
|
133 |
// Current global is need to fetch additional inputs and for additional results. |
|
134 |
final ScriptObject global = Context.getGlobal(); |
|
135 |
||
136 |
// Break exec string into tokens. |
|
137 |
final StringTokenizer tokenizer = new StringTokenizer(JSType.toString(string)); |
|
138 |
final String[] cmdArray = new String[tokenizer.countTokens()]; |
|
139 |
for (int i = 0; tokenizer.hasMoreTokens(); i++) { |
|
140 |
cmdArray[i] = tokenizer.nextToken(); |
|
141 |
} |
|
142 |
||
143 |
// Set up initial process. |
|
144 |
final ProcessBuilder processBuilder = new ProcessBuilder(cmdArray); |
|
145 |
||
16213
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
146 |
// Current ENV property state. |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
147 |
final Object env = global.get(ENV_NAME); |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
148 |
if (env instanceof ScriptObject) { |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
149 |
final ScriptObject envProperties = (ScriptObject)env; |
16211 | 150 |
|
16213
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
151 |
// If a working directory is present, use it. |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
152 |
final Object pwd = envProperties.get(PWD_NAME); |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
153 |
if (pwd != UNDEFINED) { |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
154 |
processBuilder.directory(new File(JSType.toString(pwd))); |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
155 |
} |
16211 | 156 |
|
16213
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
157 |
// Set up ENV variables. |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
158 |
final Map<String, String> environment = processBuilder.environment(); |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
159 |
environment.clear(); |
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
160 |
for (final Map.Entry<Object, Object> entry : envProperties.entrySet()) { |
16213
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
161 |
environment.put(JSType.toString(entry.getKey()), JSType.toString(entry.getValue())); |
66cb726a76d5
8007521: $ENV should be undefined when security manager is present
sundar
parents:
16211
diff
changeset
|
162 |
} |
16211 | 163 |
} |
164 |
||
165 |
// Start the process. |
|
166 |
final Process process = processBuilder.start(); |
|
17984 | 167 |
final IOException exception[] = new IOException[2]; |
168 |
||
169 |
// Collect output. |
|
170 |
final StringBuilder outBuffer = new StringBuilder(); |
|
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
171 |
final Thread outThread = new Thread(new Runnable() { |
17984 | 172 |
@Override |
173 |
public void run() { |
|
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
174 |
final char buffer[] = new char[1024]; |
17984 | 175 |
try (final InputStreamReader inputStream = new InputStreamReader(process.getInputStream())) { |
176 |
for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) { |
|
177 |
outBuffer.append(buffer, 0, length); |
|
178 |
} |
|
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
179 |
} catch (final IOException ex) { |
17984 | 180 |
exception[0] = ex; |
181 |
} |
|
182 |
} |
|
183 |
}, "$EXEC output"); |
|
184 |
||
185 |
// Collect errors. |
|
186 |
final StringBuilder errBuffer = new StringBuilder(); |
|
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
187 |
final Thread errThread = new Thread(new Runnable() { |
17984 | 188 |
@Override |
189 |
public void run() { |
|
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
190 |
final char buffer[] = new char[1024]; |
17984 | 191 |
try (final InputStreamReader inputStream = new InputStreamReader(process.getErrorStream())) { |
192 |
for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) { |
|
21460
5fb1bd6fb113
8027447: The wrong string buffer is specified for stderr in $EXEC
jlaskey
parents:
18328
diff
changeset
|
193 |
errBuffer.append(buffer, 0, length); |
17984 | 194 |
} |
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
195 |
} catch (final IOException ex) { |
17984 | 196 |
exception[1] = ex; |
197 |
} |
|
198 |
} |
|
199 |
}, "$EXEC error"); |
|
200 |
||
201 |
// Start gathering output. |
|
202 |
outThread.start(); |
|
203 |
errThread.start(); |
|
16211 | 204 |
|
205 |
// If input is present, pass on to process. |
|
17984 | 206 |
try (OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream())) { |
16211 | 207 |
if (input != UNDEFINED) { |
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
208 |
final String in = JSType.toString(input); |
17984 | 209 |
outputStream.write(in, 0, in.length()); |
16211 | 210 |
} |
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
211 |
} catch (final IOException ex) { |
17984 | 212 |
// Process was not expecting input. May be normal state of affairs. |
16211 | 213 |
} |
214 |
||
215 |
// Wait for the process to complete. |
|
216 |
final int exit = process.waitFor(); |
|
17984 | 217 |
outThread.join(); |
218 |
errThread.join(); |
|
16211 | 219 |
|
17984 | 220 |
final String out = outBuffer.toString(); |
221 |
final String err = errBuffer.toString(); |
|
16211 | 222 |
|
223 |
// Set globals for secondary results. |
|
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
224 |
global.set(OUT_NAME, out, 0); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
225 |
global.set(ERR_NAME, err, 0); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
25865
diff
changeset
|
226 |
global.set(EXIT_NAME, exit, 0); |
16211 | 227 |
|
17984 | 228 |
// Propagate exception if present. |
24743
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
229 |
for (final IOException element : exception) { |
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
230 |
if (element != null) { |
138ee369abd8
8033105: Make sure Nashorn test harness can run zlib benchmark
lagergren
parents:
21460
diff
changeset
|
231 |
throw element; |
17984 | 232 |
} |
233 |
} |
|
234 |
||
16211 | 235 |
// Return the result from stdout. |
236 |
return out; |
|
237 |
} |
|
238 |
||
16147 | 239 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
240 |
return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types)); |
|
241 |
} |
|
242 |
} |