public class Context extends Object
The behavior of the execution engine may be altered through methods such as setErrorReporter.
Different forms of script execution are supported. Scripts may be evaluated from the source directly, or first compiled and then later executed. Interactive execution is also supported.
Some aspects of script execution, such as type conversions and object creation, may be accessed directly through methods of Context.
Scriptable
Modifier and Type | Field and Description |
---|---|
static String |
errorReporterProperty |
static int |
FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
if hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME) returns true,
allow 'function
|
static int |
FEATURE_NON_ECMA_GET_YEAR
if hasFeature(FEATURE_NON_ECMA_GET_YEAR) returns true,
Date.prototype.getYear subtructs 1900 only if 1900 <= date < 2000
in deviation with Ecma B.2.4
|
static int |
FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER
if hasFeature(RESERVED_KEYWORD_AS_IDENTIFIER) returns true,
treat future reserved keyword (see Ecma-262, section 7.5.3) as ordinary
identifiers but warn about this usage
|
static int |
FEATURE_TO_STRING_AS_SOURCE
if hasFeature(FEATURE_TO_STRING_AS_SOURCE) returns true,
calling toString on JS objects gives JS source with code to create an
object with all enumeratable fields of the original object instead of
printing "[object
|
static String |
languageVersionProperty |
static int |
VERSION_1_0
JavaScript 1.0
|
static int |
VERSION_1_1
JavaScript 1.1
|
static int |
VERSION_1_2
JavaScript 1.2
|
static int |
VERSION_1_3
JavaScript 1.3
|
static int |
VERSION_1_4
JavaScript 1.4
|
static int |
VERSION_1_5
JavaScript 1.5
|
static int |
VERSION_DEFAULT
The default version.
|
static int |
VERSION_UNKNOWN
The unknown version.
|
Constructor and Description |
---|
Context()
Create a new Context.
|
Modifier and Type | Method and Description |
---|---|
static Context |
enter()
Get a context associated with the current thread, creating
one if need be.
|
static Context |
enter(Context cx)
Get a Context associated with the current thread, using
the given Context if need be.
|
static void |
exit()
Exit a block of code requiring a Context.
|
static Context |
getCurrentContext()
Get the current Context.
|
ErrorReporter |
getErrorReporter()
Get the current error reporter.
|
String |
getImplementationVersion()
Get the implementation version.
|
int |
getLanguageVersion()
Get the current language version.
|
Locale |
getLocale()
Get the current locale.
|
Object |
getThreadLocal(Object key)
Get a value corresponding to a key.
|
boolean |
hasCompileFunctionsWithDynamicScope()
Return whether functions are compiled by this context using
dynamic scope.
|
boolean |
hasFeature(int featureIndex)
Controls certain aspects of script semantics.
|
void |
putThreadLocal(Object key,
Object value)
Put a value that can later be retrieved using a given key.
|
void |
removeThreadLocal(Object key)
Remove values from thread-local storage.
|
static void |
reportError(String message,
String sourceName,
int lineno,
String lineSource,
int lineOffset)
Report an error using the error reporter for the current thread.
|
static void |
reportWarning(String message,
String sourceName,
int lineno,
String lineSource,
int lineOffset)
Report a warning using the error reporter for the current thread.
|
void |
setCompileFunctionsWithDynamicScope(boolean flag)
Set whether functions compiled by this context should use
dynamic scope.
|
ErrorReporter |
setErrorReporter(ErrorReporter reporter)
Change the current error reporter.
|
void |
setLanguageVersion(int version)
Set the language version.
|
Locale |
setLocale(Locale loc)
Set the current locale.
|
public static final String languageVersionProperty
public static final String errorReporterProperty
public static final int VERSION_UNKNOWN
public static final int VERSION_DEFAULT
public static final int VERSION_1_0
public static final int VERSION_1_1
public static final int VERSION_1_2
public static final int VERSION_1_3
public static final int VERSION_1_4
public static final int VERSION_1_5
public static final int FEATURE_NON_ECMA_GET_YEAR
public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER
public static final int FEATURE_TO_STRING_AS_SOURCE
hasFeature(int)
returns true only if
the current JS version is set to VERSION_1_2
.public Context()
org.mozilla.javascript.Context#enter
public static Context enter()
Calling enter()
will
return either the Context currently associated with the
thread, or will create a new context and associate it
with the current thread. Each call to enter()
must have a matching call to exit()
. For example,
Context cx = Context.enter(); try { ... cx.evaluateString(...); } finally { Context.exit(); }
org.mozilla.javascript.Context#getCurrentContext
,
org.mozilla.javascript.Context#exit
public static Context enter(Context cx)
The same as enter()
except that cx
is associated with the current thread and returned if
the current thread has no associated context and cx
is not associated with any other thread.
cx
- a Context to associate with the thread if possiblepublic static void exit()
exit()
will remove the association between
the current thread and a Context if the prior call to
enter()
on this thread newly associated a Context
with this thread.
Once the current thread no longer has an associated Context,
it cannot be used to execute JavaScript until it is again associated
with a Context.org.mozilla.javascript.Context#enter
public static Context getCurrentContext()
org.mozilla.javascript.Context#enter
,
org.mozilla.javascript.Context#exit
public int getLanguageVersion()
The language version number affects JavaScript semantics as detailed in the overview documentation.
public void setLanguageVersion(int version)
Setting the language version will affect functions and scripts compiled subsequently. See the overview documentation for version-specific behavior.
version
- the version as specified by VERSION_1_0, VERSION_1_1, etc.public String getImplementationVersion()
The implementation version is of the form
"name langVer release
relNum date"
where name is the name of the product, langVer is
the language version, relNum is the release number, and
date is the release date for that specific
release in the form "yyyy mm dd".public ErrorReporter getErrorReporter()
org.mozilla.javascript.ErrorReporter
public ErrorReporter setErrorReporter(ErrorReporter reporter)
org.mozilla.javascript.ErrorReporter
public Locale getLocale()
Locale
public static void reportWarning(String message, String sourceName, int lineno, String lineSource, int lineOffset)
message
- the warning message to reportsourceName
- a string describing the source, such as a filenamelineno
- the starting line numberlineSource
- the text of the line (may be null)lineOffset
- the offset into lineSource where problem was detectedorg.mozilla.javascript.ErrorReporter
public static void reportError(String message, String sourceName, int lineno, String lineSource, int lineOffset)
message
- the error message to reportsourceName
- a string describing the source, such as a filenamelineno
- the starting line numberlineSource
- the text of the line (may be null)lineOffset
- the offset into lineSource where problem was detectedorg.mozilla.javascript.ErrorReporter
public final Object getThreadLocal(Object key)
Since the Context is associated with a thread it can be used to maintain values that can be later retrieved using the current thread.
Note that the values are maintained with the Context, so if the Context is disassociated from the thread the values cannot be retreived. Also, if private data is to be maintained in this manner the key should be a java.lang.Object whose reference is not divulged to untrusted code.
key
- the key used to lookup the valuepublic void putThreadLocal(Object key, Object value)
key
- the key used to index the valuevalue
- the value to savepublic void removeThreadLocal(Object key)
key
- the key for the entry to remove.public final boolean hasCompileFunctionsWithDynamicScope()
If functions are compiled with dynamic scope, then they execute in the scope of their caller, rather than in their parent scope. This is useful for sharing functions across multiple scopes.
public void setCompileFunctionsWithDynamicScope(boolean flag)
flag
- if true, compile functions with dynamic scopepublic boolean hasFeature(int featureIndex)
featureIndex
- feature index to checkfeatureIndex
feature is turned onFEATURE_NON_ECMA_GET_YEAR
,
FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
,
FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER
,
FEATURE_TO_STRING_AS_SOURCE
Copyright © 2018. All rights reserved.