|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.javascript.rhino.Context
public class Context
This class represents the runtime context of an executing script.
Before executing a script, an instance of Context must be created
and associated with the thread that will be executing the script.
The Context will be used to store information about the executing
of the script such as the call stack. Contexts are associated with
the current thread using the enter()
method.
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.
Field Summary | |
---|---|
static Object[] |
emptyArgs
Convinient value to use as zero-length array of objects. |
static String |
errorReporterProperty
|
static int |
FEATURE_DYNAMIC_SCOPE
Control if dynamic scope should be used for name access. |
static int |
FEATURE_E4X
Control if support for E4X(ECMAScript for XML) extension is available. |
static int |
FEATURE_LOCATION_INFORMATION_IN_ERROR
When the feature is on Rhino will add a "fileName" and "lineNumber" properties to Error objects automatically. |
static int |
FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
Control if member expression as function name extension is available. |
static int |
FEATURE_NON_ECMA_GET_YEAR
Controls behaviour of Date.prototype.getYear(). |
static int |
FEATURE_PARENT_PROTO_PROPRTIES
Control if properties __proto__ and __parent__ are treated specially. |
static int |
FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER
Control if reserved keywords are treated as identifiers. |
static int |
FEATURE_STRICT_EVAL
Control if strict eval mode is enabled. |
static int |
FEATURE_STRICT_MODE
Controls whether JS 1.5 'strict mode' is enabled. |
static int |
FEATURE_STRICT_VARS
Control if strict variable mode is enabled. |
static int |
FEATURE_TO_STRING_AS_SOURCE
Control if toString() should returns the same result as toSource() when applied to objects and arrays. |
static int |
FEATURE_WARNING_AS_ERROR
Controls whether a warning should be treated as an error. |
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_1_6
JavaScript 1.5 |
static int |
VERSION_DEFAULT
The default version. |
static int |
VERSION_UNKNOWN
The unknown version. |
Constructor Summary | |
---|---|
Context()
Create a new Context. |
Method Summary | |
---|---|
void |
addActivationName(String name)
Add a name to the list of names forcing the creation of real activation objects for functions. |
void |
addPropertyChangeListener(PropertyChangeListener l)
Register an object to receive notifications when a bound property has changed |
static void |
checkLanguageVersion(int version)
|
static void |
checkOptimizationLevel(int optimizationLevel)
|
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. |
Object |
getDebuggerContextData()
Return the debugger context data associated with current context. |
ErrorReporter |
getErrorReporter()
Get the current error reporter. |
String |
getImplementationVersion()
Get the implementation version. |
int |
getInstructionObserverThreshold()
Get/Set threshold of executed instructions counter that triggers call to observeInstructionCount() . |
int |
getLanguageVersion()
Get the current language version. |
Locale |
getLocale()
Get the current locale. |
int |
getOptimizationLevel()
Get the current optimization level. |
Object |
getThreadLocal(Object key)
Get a value corresponding to a key. |
boolean |
hasCompileFunctionsWithDynamicScope()
Deprecated. |
protected boolean |
hasFeature(int featureIndex)
Implementation of hasFeature(int featureIndex) . |
boolean |
isActivationNeeded(String name)
Check whether the name is in the list of names of objects forcing the creation of activation objects. |
boolean |
isGeneratingDebug()
Tell whether debug information is being generated. |
boolean |
isGeneratingDebugChanged()
|
boolean |
isGeneratingSource()
Tell whether source information is being generated. |
boolean |
isSealed()
Checks if this is a sealed Context. |
static boolean |
isValidLanguageVersion(int version)
|
static boolean |
isValidOptimizationLevel(int optimizationLevel)
|
void |
putThreadLocal(Object key,
Object value)
Put a value that can later be retrieved using a given key. |
void |
removeActivationName(String name)
Remove a name from the list of names forcing the creation of real activation objects for functions. |
void |
removePropertyChangeListener(PropertyChangeListener l)
Remove an object from the list of objects registered to receive notification of changes to a bounded property |
void |
removeThreadLocal(Object key)
Remove values from thread-local storage. |
static void |
reportError(String message)
Report an error using the error reporter for the current thread. |
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 EvaluatorException |
reportRuntimeError(String message)
Report a runtime error using the error reporter for the current thread. |
static EvaluatorException |
reportRuntimeError(String message,
String sourceName,
int lineno,
String lineSource,
int lineOffset)
Report a runtime error using the error reporter for the current thread. |
static void |
reportWarning(String message)
Report a warning 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 |
seal(Object sealKey)
Seal this Context object so any attempt to modify any of its properties including calling enter() and exit() methods will
throw an exception. |
void |
setCompileFunctionsWithDynamicScope(boolean flag)
Deprecated. |
ErrorReporter |
setErrorReporter(ErrorReporter reporter)
Change the current error reporter. |
void |
setGeneratingSource(boolean generatingSource)
Specify whether or not source information should be generated. |
void |
setInstructionObserverThreshold(int threshold)
|
void |
setLanguageVersion(int version)
Set the language version. |
Locale |
setLocale(Locale loc)
Set the current locale. |
void |
unseal(Object sealKey)
Unseal previously sealed Context object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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 VERSION_1_6
public static final int FEATURE_NON_ECMA_GET_YEAR
hasFeature(int)
is always to subtruct
1900 as rquired by ECMAScript B.2.4.
public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
hasFeature(int)
returns false.
public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER
hasFeature(int)
returns false.
public static final int FEATURE_TO_STRING_AS_SOURCE
By default hasFeature(int)
returns true only if
the current JS version is set to VERSION_1_2
.
public static final int FEATURE_PARENT_PROTO_PROPRTIES
The properties allow to query and set scope and prototype chains for the
objects. The special meaning of the properties is available
only when they are used as the right hand side of the dot operator.
For example, while x.__proto__ = y changes the prototype
chain of the object x to point to y,
x["__proto__"] = y simply assigns a new value to the property
__proto__ in x even when the feature is on.
By default hasFeature(int)
returns true.
public static final int FEATURE_E4X
By default hasFeature(int)
returns true if
the current JS version is set to VERSION_DEFAULT
or is greater then VERSION_1_6
.
public static final int FEATURE_DYNAMIC_SCOPE
This is useful to define shared scope containing functions that can be called from scripts and functions using private scopes.
By default hasFeature(int)
returns false.
public static final int FEATURE_STRICT_VARS
By default hasFeature(int)
returns false.
public static final int FEATURE_STRICT_EVAL
By default hasFeature(int)
returns false.
public static final int FEATURE_LOCATION_INFORMATION_IN_ERROR
By default hasFeature(int)
returns false.
public static final int FEATURE_STRICT_MODE
By default hasFeature(int)
returns false.
public static final int FEATURE_WARNING_AS_ERROR
public static final String languageVersionProperty
public static final String errorReporterProperty
public static final Object[] emptyArgs
Constructor Detail |
---|
public Context()
enter()
Method Detail |
---|
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(); }
getCurrentContext()
,
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 possible
public 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.
enter()
public static Context getCurrentContext()
public final boolean isSealed()
seal(Object sealKey)
public final void seal(Object sealKey)
enter()
and exit()
methods will
throw an exception.
If sealKey is not null, calling
unseal(Object sealKey)
with the same key unseals
the object. If sealKey is null, unsealing is no longer possible.
isSealed()
,
unseal(Object)
public final void unseal(Object sealKey)
seal(Object)
or an exception will be thrown.
isSealed()
,
seal(Object sealKey)
public final 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 static boolean isValidLanguageVersion(int version)
public static void checkLanguageVersion(int version)
public final 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 final ErrorReporter getErrorReporter()
ErrorReporter
public final ErrorReporter setErrorReporter(ErrorReporter reporter)
ErrorReporter
public final Locale getLocale()
Locale
public final Locale setLocale(Locale loc)
Locale
public final void addPropertyChangeListener(PropertyChangeListener l)
l
- the listenerPropertyChangeEvent
,
removePropertyChangeListener(java.beans.PropertyChangeListener)
public final void removePropertyChangeListener(PropertyChangeListener l)
l
- the listenerPropertyChangeEvent
,
addPropertyChangeListener(java.beans.PropertyChangeListener)
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 detectedErrorReporter
public static void reportWarning(String message)
message
- the warning message to reportErrorReporter
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 detectedErrorReporter
public static void reportError(String message)
message
- the error message to reportErrorReporter
public static EvaluatorException reportRuntimeError(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 detected
ErrorReporter
public static EvaluatorException reportRuntimeError(String message)
message
- the error message to reportErrorReporter
public final boolean isGeneratingDebug()
public final boolean isGeneratingSource()
public final void setGeneratingSource(boolean generatingSource)
Without source information, evaluating the "toString" method on JavaScript functions produces only "[native code]" for the body of the function. Note that code generated without source is not fully ECMA conformant.
public final int getOptimizationLevel()
The optimization level is expressed as an integer between -1 and 9.
public static boolean isValidOptimizationLevel(int optimizationLevel)
public static void checkOptimizationLevel(int optimizationLevel)
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 value
public final void putThreadLocal(Object key, Object value)
key
- the key used to index the valuevalue
- the value to savepublic final void removeThreadLocal(Object key)
key
- the key for the entry to remove.@Deprecated public final boolean hasCompileFunctionsWithDynamicScope()
FEATURE_DYNAMIC_SCOPE
,
hasFeature(int)
@Deprecated public final void setCompileFunctionsWithDynamicScope(boolean flag)
FEATURE_DYNAMIC_SCOPE
,
hasFeature(int)
public final Object getDebuggerContextData()
protected boolean hasFeature(int featureIndex)
hasFeature(int featureIndex)
.
This can be used to customize Context
without introducing
additional subclasses.
public final int getInstructionObserverThreshold()
observeInstructionCount()
.
When the threshold is zero, instruction counting is disabled,
otherwise each time the run-time executes at least the threshold value
of script instructions, observeInstructionCount()
will
be called.
public final void setInstructionObserverThreshold(int threshold)
public final boolean isGeneratingDebugChanged()
public void addActivationName(String name)
name
- the name of the object to add to the listpublic final boolean isActivationNeeded(String name)
name
- the name of the object to test
public void removeActivationName(String name)
name
- the name of the object to remove from the list
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |