com.google.gwt.dev.js.rhino
Class Context

java.lang.Object
  extended by com.google.gwt.dev.js.rhino.Context

public class Context
extends java.lang.Object

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.

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.


Field Summary
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 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 Summary
Context()
          Create a new Context.
 
Method Summary
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.
 int getLanguageVersion()
          Get the current language version.
 java.util.Locale getLocale()
          Get the current locale.
 boolean hasFeature(int featureIndex)
          Controls certain aspects of script semantics.
static void reportError(java.lang.String message, java.lang.String sourceName, int lineno, java.lang.String lineSource, int lineOffset)
          Report an error using the error reporter for the current thread.
static void reportWarning(java.lang.String message, java.lang.String sourceName, int lineno, java.lang.String lineSource, int lineOffset)
          Report a warning using the error reporter for the current thread.
 ErrorReporter setErrorReporter(ErrorReporter reporter)
          Change the current error reporter.
 void setLanguageVersion(int version)
          Set the language version.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION_UNKNOWN

public static final int VERSION_UNKNOWN
The unknown version.

See Also:
Constant Field Values

VERSION_DEFAULT

public static final int VERSION_DEFAULT
The default version.

See Also:
Constant Field Values

VERSION_1_0

public static final int VERSION_1_0
JavaScript 1.0

See Also:
Constant Field Values

VERSION_1_1

public static final int VERSION_1_1
JavaScript 1.1

See Also:
Constant Field Values

VERSION_1_2

public static final int VERSION_1_2
JavaScript 1.2

See Also:
Constant Field Values

VERSION_1_3

public static final int VERSION_1_3
JavaScript 1.3

See Also:
Constant Field Values

VERSION_1_4

public static final int VERSION_1_4
JavaScript 1.4

See Also:
Constant Field Values

VERSION_1_5

public static final int VERSION_1_5
JavaScript 1.5

See Also:
Constant Field Values

FEATURE_NON_ECMA_GET_YEAR

public static final 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

See Also:
Constant Field Values

FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME

public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME
if hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME) returns true, allow 'function (...) { ... }' to be syntax sugar for ' = function(...) { ... }', when is not simply identifier. See Ecma-262, section 11.2 for definition of

See Also:
Constant Field Values

FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER

public static final 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

See Also:
Constant Field Values

FEATURE_TO_STRING_AS_SOURCE

public static final 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 ]". By default hasFeature(int) returns true only if the current JS version is set to VERSION_1_2.

See Also:
Constant Field Values
Constructor Detail

Context

public Context()
Create a new Context. Note that the Context must be associated with a thread before it can be used to execute a script.

Method Detail

enter

public static Context enter()
Get a context associated with the current thread, creating one if need be. The Context stores the execution state of the JavaScript engine, so it is required that the context be entered before execution may begin. Once a thread has entered a Context, then getCurrentContext() may be called to find the context that is associated with the current thread.

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(); }
 

Returns:
a Context associated with the current thread

enter

public static Context enter(Context cx)
Get a Context associated with the current thread, using the given Context if need be.

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.

Parameters:
cx - a Context to associate with the thread if possible
Returns:
a Context associated with the current thread

exit

public static void exit()
Exit a block of code requiring a Context. Calling 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.


getCurrentContext

public static Context getCurrentContext()
Get the current Context. The current Context is per-thread; this method looks up the Context associated with the current thread.

Returns:
the Context associated with the current thread, or null if no context is associated with the current thread.

getLanguageVersion

public int getLanguageVersion()
Get the current language version.

The language version number affects JavaScript semantics as detailed in the overview documentation.

Returns:
an integer that is one of VERSION_1_0, VERSION_1_1, etc.

setLanguageVersion

public void setLanguageVersion(int version)
Set the language version.

Setting the language version will affect functions and scripts compiled subsequently. See the overview documentation for version-specific behavior.

Parameters:
version - the version as specified by VERSION_1_0, VERSION_1_1, etc.

getErrorReporter

public ErrorReporter getErrorReporter()
Get the current error reporter.


setErrorReporter

public ErrorReporter setErrorReporter(ErrorReporter reporter)
Change the current error reporter.

Returns:
the previous error reporter

getLocale

public java.util.Locale getLocale()
Get the current locale. Returns the default locale if none has been set.

See Also:
Locale

reportWarning

public static void reportWarning(java.lang.String message,
                                 java.lang.String sourceName,
                                 int lineno,
                                 java.lang.String lineSource,
                                 int lineOffset)
Report a warning using the error reporter for the current thread.

Parameters:
message - the warning message to report
sourceName - a string describing the source, such as a filename
lineno - the starting line number
lineSource - the text of the line (may be null)
lineOffset - the offset into lineSource where problem was detected

reportError

public static void reportError(java.lang.String message,
                               java.lang.String sourceName,
                               int lineno,
                               java.lang.String lineSource,
                               int lineOffset)
Report an error using the error reporter for the current thread.

Parameters:
message - the error message to report
sourceName - a string describing the source, such as a filename
lineno - the starting line number
lineSource - the text of the line (may be null)
lineOffset - the offset into lineSource where problem was detected

hasFeature

public boolean hasFeature(int featureIndex)
Controls certain aspects of script semantics. Should be overwritten to alter default behavior.

Parameters:
featureIndex - feature index to check
Returns:
true if the featureIndex feature is turned on
See Also:
FEATURE_NON_ECMA_GET_YEAR, FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME, FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER, FEATURE_TO_STRING_AS_SOURCE