| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.tools.shell; |
| 8 |
|
| 9 |
import org.mozilla.javascript.*; |
| 10 |
|
| 11 |
public class ShellContextFactory extends ContextFactory |
| 12 |
{ |
| 13 |
private boolean strictMode; |
| 14 |
private boolean warningAsError; |
| 15 |
private int languageVersion = Context.VERSION_1_8; |
| 16 |
private int optimizationLevel; |
| 17 |
private boolean generatingDebug; |
| 18 |
private boolean allowReservedKeywords = true; |
| 19 |
private ErrorReporter errorReporter; |
| 20 |
private String characterEncoding; |
| 21 |
|
| 22 |
@Override |
| 23 |
protected boolean hasFeature(Context cx, int featureIndex) |
| 24 |
{ |
| 25 |
switch (featureIndex) { |
| 26 |
case Context.FEATURE_STRICT_VARS: |
| 27 |
case Context.FEATURE_STRICT_EVAL: |
| 28 |
case Context.FEATURE_STRICT_MODE: |
| 29 |
return strictMode; |
| 30 |
|
| 31 |
case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER: |
| 32 |
return allowReservedKeywords; |
| 33 |
|
| 34 |
case Context.FEATURE_WARNING_AS_ERROR: |
| 35 |
return warningAsError; |
| 36 |
|
| 37 |
case Context.FEATURE_LOCATION_INFORMATION_IN_ERROR: |
| 38 |
return generatingDebug; |
| 39 |
} |
| 40 |
return super.hasFeature(cx, featureIndex); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void onContextCreated(Context cx) |
| 45 |
{ |
| 46 |
cx.setLanguageVersion(languageVersion); |
| 47 |
cx.setOptimizationLevel(optimizationLevel); |
| 48 |
if (errorReporter != null) { |
| 49 |
cx.setErrorReporter(errorReporter); |
| 50 |
} |
| 51 |
cx.setGeneratingDebug(generatingDebug); |
| 52 |
super.onContextCreated(cx); |
| 53 |
} |
| 54 |
|
| 55 |
public void setStrictMode(boolean flag) |
| 56 |
{ |
| 57 |
checkNotSealed(); |
| 58 |
this.strictMode = flag; |
| 59 |
} |
| 60 |
|
| 61 |
public void setWarningAsError(boolean flag) |
| 62 |
{ |
| 63 |
checkNotSealed(); |
| 64 |
this.warningAsError = flag; |
| 65 |
} |
| 66 |
|
| 67 |
public void setLanguageVersion(int version) |
| 68 |
{ |
| 69 |
Context.checkLanguageVersion(version); |
| 70 |
checkNotSealed(); |
| 71 |
this.languageVersion = version; |
| 72 |
} |
| 73 |
|
| 74 |
public void setOptimizationLevel(int optimizationLevel) |
| 75 |
{ |
| 76 |
Context.checkOptimizationLevel(optimizationLevel); |
| 77 |
checkNotSealed(); |
| 78 |
this.optimizationLevel = optimizationLevel; |
| 79 |
} |
| 80 |
|
| 81 |
public void setErrorReporter(ErrorReporter errorReporter) |
| 82 |
{ |
| 83 |
if (errorReporter == null) throw new IllegalArgumentException(); |
| 84 |
this.errorReporter = errorReporter; |
| 85 |
} |
| 86 |
|
| 87 |
public void setGeneratingDebug(boolean generatingDebug) |
| 88 |
{ |
| 89 |
this.generatingDebug = generatingDebug; |
| 90 |
} |
| 91 |
|
| 92 |
public String getCharacterEncoding() |
| 93 |
{ |
| 94 |
return characterEncoding; |
| 95 |
} |
| 96 |
|
| 97 |
public void setCharacterEncoding(String characterEncoding) |
| 98 |
{ |
| 99 |
this.characterEncoding = characterEncoding; |
| 100 |
} |
| 101 |
|
| 102 |
public void setAllowReservedKeywords(boolean allowReservedKeywords) { |
| 103 |
this.allowReservedKeywords = allowReservedKeywords; |
| 104 |
} |
| 105 |
} |