1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 package org.mozilla.javascript;
8 
9 import java.util.Set;
10 
11 import org.mozilla.javascript.ast.ErrorCollector;
12 
13 public class CompilerEnvirons
14 {
CompilerEnvirons()15     public CompilerEnvirons()
16     {
17         errorReporter = DefaultErrorReporter.instance;
18         languageVersion = Context.VERSION_DEFAULT;
19         generateDebugInfo = true;
20         reservedKeywordAsIdentifier = true;
21         allowMemberExprAsFunctionName = false;
22         xmlAvailable = true;
23         optimizationLevel = 0;
24         generatingSource = true;
25         strictMode = false;
26         warningAsError = false;
27         generateObserverCount = false;
28         allowSharpComments = false;
29     }
30 
initFromContext(Context cx)31     public void initFromContext(Context cx)
32     {
33         setErrorReporter(cx.getErrorReporter());
34         languageVersion = cx.getLanguageVersion();
35         generateDebugInfo = (!cx.isGeneratingDebugChanged()
36                              || cx.isGeneratingDebug());
37         reservedKeywordAsIdentifier
38             = cx.hasFeature(Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER);
39         allowMemberExprAsFunctionName
40             = cx.hasFeature(Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME);
41         strictMode
42             = cx.hasFeature(Context.FEATURE_STRICT_MODE);
43         warningAsError = cx.hasFeature(Context.FEATURE_WARNING_AS_ERROR);
44         xmlAvailable
45             = cx.hasFeature(Context.FEATURE_E4X);
46 
47         optimizationLevel = cx.getOptimizationLevel();
48 
49         generatingSource = cx.isGeneratingSource();
50         activationNames = cx.activationNames;
51 
52         // Observer code generation in compiled code :
53         generateObserverCount = cx.generateObserverCount;
54     }
55 
getErrorReporter()56     public final ErrorReporter getErrorReporter()
57     {
58         return errorReporter;
59     }
60 
setErrorReporter(ErrorReporter errorReporter)61     public void setErrorReporter(ErrorReporter errorReporter)
62     {
63         if (errorReporter == null) throw new IllegalArgumentException();
64         this.errorReporter = errorReporter;
65     }
66 
getLanguageVersion()67     public final int getLanguageVersion()
68     {
69         return languageVersion;
70     }
71 
setLanguageVersion(int languageVersion)72     public void setLanguageVersion(int languageVersion)
73     {
74         Context.checkLanguageVersion(languageVersion);
75         this.languageVersion = languageVersion;
76     }
77 
isGenerateDebugInfo()78     public final boolean isGenerateDebugInfo()
79     {
80         return generateDebugInfo;
81     }
82 
setGenerateDebugInfo(boolean flag)83     public void setGenerateDebugInfo(boolean flag)
84     {
85         this.generateDebugInfo = flag;
86     }
87 
isReservedKeywordAsIdentifier()88     public final boolean isReservedKeywordAsIdentifier()
89     {
90         return reservedKeywordAsIdentifier;
91     }
92 
setReservedKeywordAsIdentifier(boolean flag)93     public void setReservedKeywordAsIdentifier(boolean flag)
94     {
95         reservedKeywordAsIdentifier = flag;
96     }
97 
98     /**
99      * Extension to ECMA: if 'function <name>' is not followed
100      * by '(', assume <name> starts a {@code memberExpr}
101      */
isAllowMemberExprAsFunctionName()102     public final boolean isAllowMemberExprAsFunctionName()
103     {
104         return allowMemberExprAsFunctionName;
105     }
106 
setAllowMemberExprAsFunctionName(boolean flag)107     public void setAllowMemberExprAsFunctionName(boolean flag)
108     {
109         allowMemberExprAsFunctionName = flag;
110     }
111 
isXmlAvailable()112     public final boolean isXmlAvailable()
113     {
114         return xmlAvailable;
115     }
116 
setXmlAvailable(boolean flag)117     public void setXmlAvailable(boolean flag)
118     {
119         xmlAvailable = flag;
120     }
121 
getOptimizationLevel()122     public final int getOptimizationLevel()
123     {
124         return optimizationLevel;
125     }
126 
setOptimizationLevel(int level)127     public void setOptimizationLevel(int level)
128     {
129         Context.checkOptimizationLevel(level);
130         this.optimizationLevel = level;
131     }
132 
isGeneratingSource()133     public final boolean isGeneratingSource()
134     {
135         return generatingSource;
136     }
137 
getWarnTrailingComma()138     public boolean getWarnTrailingComma() {
139         return warnTrailingComma;
140     }
141 
setWarnTrailingComma(boolean warn)142     public void setWarnTrailingComma(boolean warn) {
143         warnTrailingComma = warn;
144     }
145 
isStrictMode()146     public final boolean isStrictMode()
147     {
148         return strictMode;
149     }
150 
setStrictMode(boolean strict)151     public void setStrictMode(boolean strict)
152     {
153         strictMode = strict;
154     }
155 
reportWarningAsError()156     public final boolean reportWarningAsError()
157     {
158         return warningAsError;
159     }
160 
161     /**
162      * Specify whether or not source information should be generated.
163      * <p>
164      * Without source information, evaluating the "toString" method
165      * on JavaScript functions produces only "[native code]" for
166      * the body of the function.
167      * Note that code generated without source is not fully ECMA
168      * conformant.
169      */
setGeneratingSource(boolean generatingSource)170     public void setGeneratingSource(boolean generatingSource)
171     {
172         this.generatingSource = generatingSource;
173     }
174 
175     /**
176      * @return true iff code will be generated with callbacks to enable
177      * instruction thresholds
178      */
isGenerateObserverCount()179     public boolean isGenerateObserverCount() {
180         return generateObserverCount;
181     }
182 
183     /**
184      * Turn on or off generation of code with callbacks to
185      * track the count of executed instructions.
186      * Currently only affects JVM byte code generation: this slows down the
187      * generated code, but code generated without the callbacks will not
188      * be counted toward instruction thresholds. Rhino's interpretive
189      * mode does instruction counting without inserting callbacks, so
190      * there is no requirement to compile code differently.
191      * @param generateObserverCount if true, generated code will contain
192      * calls to accumulate an estimate of the instructions executed.
193      */
setGenerateObserverCount(boolean generateObserverCount)194     public void setGenerateObserverCount(boolean generateObserverCount) {
195         this.generateObserverCount = generateObserverCount;
196     }
197 
isRecordingComments()198     public boolean isRecordingComments() {
199         return recordingComments;
200     }
201 
setRecordingComments(boolean record)202     public void setRecordingComments(boolean record) {
203         recordingComments = record;
204     }
205 
isRecordingLocalJsDocComments()206     public boolean isRecordingLocalJsDocComments() {
207         return recordingLocalJsDocComments;
208     }
209 
setRecordingLocalJsDocComments(boolean record)210     public void setRecordingLocalJsDocComments(boolean record) {
211         recordingLocalJsDocComments = record;
212     }
213 
214     /**
215      * Turn on or off full error recovery.  In this mode, parse errors do not
216      * throw an exception, and the parser attempts to build a full syntax tree
217      * from the input.  Useful for IDEs and other frontends.
218      */
setRecoverFromErrors(boolean recover)219     public void setRecoverFromErrors(boolean recover) {
220         recoverFromErrors = recover;
221     }
222 
recoverFromErrors()223     public boolean recoverFromErrors() {
224         return recoverFromErrors;
225     }
226 
227     /**
228      * Puts the parser in "IDE" mode.  This enables some slightly more expensive
229      * computations, such as figuring out helpful error bounds.
230      */
setIdeMode(boolean ide)231     public void setIdeMode(boolean ide) {
232         ideMode = ide;
233     }
234 
isIdeMode()235     public boolean isIdeMode() {
236         return ideMode;
237     }
238 
getActivationNames()239     public Set<String> getActivationNames() {
240         return activationNames;
241     }
242 
setActivationNames(Set<String> activationNames)243     public void setActivationNames(Set<String> activationNames) {
244         this.activationNames = activationNames;
245     }
246 
247     /**
248      * Mozilla sources use the C preprocessor.
249      */
setAllowSharpComments(boolean allow)250     public void setAllowSharpComments(boolean allow) {
251         allowSharpComments = allow;
252     }
253 
getAllowSharpComments()254     public boolean getAllowSharpComments() {
255         return allowSharpComments;
256     }
257 
258     /**
259      * Returns a {@code CompilerEnvirons} suitable for using Rhino
260      * in an IDE environment.  Most features are enabled by default.
261      * The {@link ErrorReporter} is set to an {@link ErrorCollector}.
262      */
ideEnvirons()263     public static CompilerEnvirons ideEnvirons() {
264         CompilerEnvirons env = new CompilerEnvirons();
265         env.setRecoverFromErrors(true);
266         env.setRecordingComments(true);
267         env.setStrictMode(true);
268         env.setWarnTrailingComma(true);
269         env.setLanguageVersion(170);
270         env.setReservedKeywordAsIdentifier(true);
271         env.setIdeMode(true);
272         env.setErrorReporter(new ErrorCollector());
273         return env;
274     }
275 
276     private ErrorReporter errorReporter;
277 
278     private int languageVersion;
279     private boolean generateDebugInfo;
280     private boolean reservedKeywordAsIdentifier;
281     private boolean allowMemberExprAsFunctionName;
282     private boolean xmlAvailable;
283     private int optimizationLevel;
284     private boolean generatingSource;
285     private boolean strictMode;
286     private boolean warningAsError;
287     private boolean generateObserverCount;
288     private boolean recordingComments;
289     private boolean recordingLocalJsDocComments;
290     private boolean recoverFromErrors;
291     private boolean warnTrailingComma;
292     private boolean ideMode;
293     private boolean allowSharpComments;
294     Set<String> activationNames;
295 }
296