Tue, 22 Jan 2013 22:07:12 +0530
8006678: Avoid too many Context.getGlobal() calls
Reviewed-by: lagergren, jlaskey
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
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 */
26 package jdk.nashorn.internal.runtime;
28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
30 import static jdk.nashorn.internal.runtime.linker.Lookup.MH;
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.lang.invoke.MethodHandle;
37 import java.lang.invoke.MethodHandles;
39 /**
40 * Global functions supported only in scripting mode.
41 */
42 public class ScriptingFunctions {
44 /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
45 public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class);
47 /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
48 public static final MethodHandle READ = findOwnMH("read", Object.class, Object.class, Object.class);
50 /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
51 public static final MethodHandle QUIT = findOwnMH("quit", Object.class, Object.class, Object.class);
53 private ScriptingFunctions() {
54 }
56 /**
57 * Nashorn extension: global.readLine (scripting-mode-only)
58 * Read one line of input from the standard input.
59 *
60 * @param self self reference
61 *
62 * @return line that was read
63 *
64 * @throws IOException if an exception occurs
65 */
66 public static Object readLine(final Object self) throws IOException {
67 final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
68 return reader.readLine();
69 }
71 /**
72 * Nashorn extension: Read the entire contents of a text file and return as String.
73 *
74 * @param self self reference
75 * @param file The input file whose content is read.
76 *
77 * @return String content of the input file.
78 *
79 * @throws IOException if an exception occurs
80 */
81 public static Object read(final Object self, final Object file) throws IOException {
82 File f = null;
84 if (file instanceof File) {
85 f = (File)file;
86 } else if (file instanceof String) {
87 f = new java.io.File((String)file);
88 }
90 if (f == null || !f.isFile()) {
91 typeError("not.a.file", ScriptRuntime.safeToString(file));
92 return UNDEFINED;
93 }
95 return new String(Source.readFully(f));
96 }
98 /**
99 * Nashorn extension: perform a {@code System.exit} call from the script
100 *
101 * @param self self reference
102 * @param code exit code
103 *
104 * @return undefined (will never be reacheD)
105 */
106 public static Object quit(final Object self, final Object code) {
107 System.exit(JSType.toInt32(code));
108 return UNDEFINED;
109 }
111 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
112 return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types));
113 }
114 }