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 */ 25 26 package jdk.nashorn.internal.runtime; 27 28 import java.io.PrintWriter; 29 import java.util.HashSet; 30 import java.util.List; 31 import java.util.Locale; 32 import java.util.Set; 33 import java.util.StringTokenizer; 34 import java.util.TimeZone; 35 36 import jdk.nashorn.internal.codegen.Namespace; 37 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 38 import jdk.nashorn.internal.runtime.options.KeyValueOption; 39 import jdk.nashorn.internal.runtime.options.Option; 40 import jdk.nashorn.internal.runtime.options.Options; 41 42 /** 43 * Script environment consists of command line options, arguments, script files 44 * and output and error writers, top level Namespace etc. 45 */ 46 public final class ScriptEnvironment { 47 /** Output writer for this environment */ 48 private final PrintWriter out; 49 50 /** Error writer for this environment */ 51 private final PrintWriter err; 52 53 /** Top level namespace. */ 54 private final Namespace namespace; 55 56 /** Current Options object. */ 57 private final Options options; 58 59 /** Size of the per-global Class cache size */ 60 public final int _class_cache_size; 61 62 /** Only compile script, do not run it or generate other ScriptObjects */ 63 public final boolean _compile_only; 64 65 /** Accumulated callsite flags that will be used when bootstrapping script callsites */ 66 public final int _callsite_flags; 67 68 /** Generate line number table in class files */ 69 public final boolean _debug_lines; 70 71 /** Package to which generated class files are added */ 72 public final String _dest_dir; 73 74 /** Display stack trace upon error, default is false */ 75 public final boolean _dump_on_error; 76 77 /** Invalid lvalue expressions should be reported as early errors */ 78 public final boolean _early_lvalue_error; 79 80 /** Empty statements should be preserved in the AST */ 81 public final boolean _empty_statements; 82 83 /** Show full Nashorn version */ 84 public final boolean _fullversion; 85 86 /** Launch using as fx application */ 87 public final boolean _fx; 88 89 /** 90 * Behavior when encountering a function declaration in a lexical context where only statements are acceptable 91 * (function declarations are source elements, but not statements). 92 */ 93 public enum FunctionStatementBehavior { 94 /** 95 * Accept the function declaration silently and treat it as if it were a function expression assigned to a local 96 * variable. 97 */ 98 ACCEPT, 99 /** 100 * Log a parser warning, but accept the function declaration and treat it as if it were a function expression 101 * assigned to a local variable. 102 */ 103 WARNING, 104 /** 105 * Raise a {@code SyntaxError}. 106 */ 107 ERROR 108 } 109 110 /** 111 * Behavior when encountering a function declaration in a lexical context where only statements are acceptable 112 * (function declarations are source elements, but not statements). 113 */ 114 public final FunctionStatementBehavior _function_statement; 115 116 /** Should lazy compilation take place */ 117 public final boolean _lazy_compilation; 118 119 /** Create a new class loaded for each compilation */ 120 public final boolean _loader_per_compile; 121 122 /** Do not support non-standard syntax extensions. */ 123 public final boolean _no_syntax_extensions; 124 125 /** Package to which generated class files are added */ 126 public final String _package; 127 128 /** Only parse the source code, do not compile */ 129 public final boolean _parse_only; 130 131 /** Print the AST before lowering */ 132 public final boolean _print_ast; 133 134 /** Print the AST after lowering */ 135 public final boolean _print_lower_ast; 136 137 /** Print resulting bytecode for script */ 138 public final boolean _print_code; 139 140 /** Print memory usage for IR after each phase */ 141 public final boolean _print_mem_usage; 142 143 /** Print function will no print newline characters */ 144 public final boolean _print_no_newline; 145 146 /** Print AST in more human readable form */ 147 public final boolean _print_parse; 148 149 /** Print AST in more human readable form after Lowering */ 150 public final boolean _print_lower_parse; 151 152 /** print symbols and their contents for the script */ 153 public final boolean _print_symbols; 154 155 /** range analysis for known types */ 156 public final boolean _range_analysis; 157 158 /** is this environment in scripting mode? */ 159 public final boolean _scripting; 160 161 /** is the JIT allowed to specializ calls based on callsite types? */ 162 public final Set<String> _specialize_calls; 163 164 /** is this environment in strict mode? */ 165 public final boolean _strict; 166 167 /** print version info of Nashorn */ 168 public final boolean _version; 169 170 /** should code verification be done of generated bytecode */ 171 public final boolean _verify_code; 172 173 /** time zone for this environment */ 174 public final TimeZone _timezone; 175 176 /** Local for error messages */ 177 public final Locale _locale; 178 179 /** 180 * Constructor 181 * 182 * @param options a Options object 183 * @param out output print writer 184 * @param err error print writer 185 */ 186 public ScriptEnvironment(final Options options, final PrintWriter out, final PrintWriter err) { 187 this.out = out; 188 this.err = err; 189 this.namespace = new Namespace(); 190 this.options = options; 191 192 _class_cache_size = options.getInteger("class.cache.size"); 193 _compile_only = options.getBoolean("compile.only"); 194 _debug_lines = options.getBoolean("debug.lines"); 195 _dest_dir = options.getString("d"); 196 _dump_on_error = options.getBoolean("doe"); 197 _early_lvalue_error = options.getBoolean("early.lvalue.error"); 198 _empty_statements = options.getBoolean("empty.statements"); 199 _fullversion = options.getBoolean("fullversion"); 200 if(options.getBoolean("function.statement.error")) { 201 _function_statement = FunctionStatementBehavior.ERROR; 202 } else if(options.getBoolean("function.statement.warning")) { 203 _function_statement = FunctionStatementBehavior.WARNING; 204 } else { 205 _function_statement = FunctionStatementBehavior.ACCEPT; 206 } 207 _fx = options.getBoolean("fx"); 208 _lazy_compilation = options.getBoolean("lazy.compilation"); 209 _loader_per_compile = options.getBoolean("loader.per.compile"); 210 _no_syntax_extensions = options.getBoolean("no.syntax.extensions"); 211 _package = options.getString("package"); 212 _parse_only = options.getBoolean("parse.only"); 213 _print_ast = options.getBoolean("print.ast"); 214 _print_lower_ast = options.getBoolean("print.lower.ast"); 215 _print_code = options.getBoolean("print.code"); 216 _print_mem_usage = options.getBoolean("print.mem.usage"); 217 _print_no_newline = options.getBoolean("print.no.newline"); 218 _print_parse = options.getBoolean("print.parse"); 219 _print_lower_parse = options.getBoolean("print.lower.parse"); 220 _print_symbols = options.getBoolean("print.symbols"); 221 _range_analysis = options.getBoolean("range.analysis"); 222 _scripting = options.getBoolean("scripting"); 223 _strict = options.getBoolean("strict"); 224 _version = options.getBoolean("version"); 225 _verify_code = options.getBoolean("verify.code"); 226 227 final String specialize = options.getString("specialize.calls"); 228 if (specialize == null) { 229 _specialize_calls = null; 230 } else { 231 _specialize_calls = new HashSet<>(); 232 final StringTokenizer st = new StringTokenizer(specialize, ","); 233 while (st.hasMoreElements()) { 234 _specialize_calls.add(st.nextToken()); 235 } 236 } 237 238 int callSiteFlags = 0; 239 if (options.getBoolean("profile.callsites")) { 240 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_PROFILE; 241 } 242 243 if (options.get("trace.callsites") instanceof KeyValueOption) { 244 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE; 245 final KeyValueOption kv = (KeyValueOption)options.get("trace.callsites"); 246 if (kv.hasValue("miss")) { 247 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES; 248 } 249 if (kv.hasValue("enterexit") || (callSiteFlags & NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES) == 0) { 250 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_ENTEREXIT; 251 } 252 if (kv.hasValue("objects")) { 253 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_VALUES; 254 } 255 if (kv.hasValue("scope")) { 256 callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_SCOPE; 257 } 258 } 259 this._callsite_flags = callSiteFlags; 260 261 final Option<?> timezoneOption = options.get("timezone"); 262 if (timezoneOption != null) { 263 this._timezone = (TimeZone)timezoneOption.getValue(); 264 } else { 265 this._timezone = TimeZone.getDefault(); 266 } 267 268 final Option<?> localeOption = options.get("locale"); 269 if (localeOption != null) { 270 this._locale = (Locale)localeOption.getValue(); 271 } else { 272 this._locale = Locale.getDefault(); 273 } 274 } 275 276 /** 277 * Can we specialize a particular method name? 278 * @param functionName method name 279 * @return true if we are allowed to generate versions of this method 280 */ 281 public boolean canSpecialize(final String functionName) { 282 if (_specialize_calls == null) { 283 return false; 284 } 285 return _specialize_calls.isEmpty() || _specialize_calls.contains(functionName); 286 } 287 288 /** 289 * Get the output stream for this environment 290 * @return output print writer 291 */ 292 public PrintWriter getOut() { 293 return out; 294 } 295 296 /** 297 * Get the error stream for this environment 298 * @return error print writer 299 */ 300 public PrintWriter getErr() { 301 return err; 302 } 303 304 /** 305 * Get the namespace for this environment 306 * @return namespace 307 */ 308 public Namespace getNamespace() { 309 return namespace; 310 } 311 312 /** 313 * Return the JavaScript files passed to the program 314 * 315 * @return a list of files 316 */ 317 public List<String> getFiles() { 318 return options.getFiles(); 319 } 320 321 /** 322 * Return the user arguments to the program, i.e. those trailing "--" after 323 * the filename 324 * 325 * @return a list of user arguments 326 */ 327 public List<String> getArguments() { 328 return options.getArguments(); 329 } 330 } --- EOF ---