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