src/jdk/nashorn/internal/runtime/ScriptEnvironment.java

Wed, 22 May 2013 16:39:48 +0530

author
sundar
date
Wed, 22 May 2013 16:39:48 +0530
changeset 283
07cefc062032
parent 277
92164a5742db
child 297
343fd0450802
permissions
-rw-r--r--

8008947: ScriptEnvironment ctor should be public
Reviewed-by: lagergren, attila

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

mercurial