* @exception Exception if an error occurs
*/
protected Scriptable enterContext(Environment environment)
throws Exception
{
Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
Scriptable thrScope = null;
// Try to retrieve the scope object from the session instance. If
// no scope is found, we create a new one, but don't place it in
// the session.
//
// When a user script "creates" a session using
// cocoon.createSession() in JavaScript, the thrScope is placed in
// the session object, where it's later retrieved from here. This
// behaviour allows multiple JavaScript functions to share the
// same global scope.
thrScope = getSessionScope(environment);
// The Cocoon object exported to JavaScript needs to be setup here
JSCocoon cocoon;
boolean newScope = false;
long lastExecTime = 0;
if (thrScope == null) {
newScope = true;
thrScope = context.newObject(scope);
thrScope.setPrototype(scope);
// We want 'thrScope' to be a new top-level scope, so set its
// parent scope to null. This means that any variables created
// by assignments will be properties of "thrScope".
thrScope.setParentScope(null);
// Put in the thread scope the Cocoon object, which gives access
// to the interpreter object, and some Cocoon objects. See
// JSCocoon for more details.
Object args[] = {};
cocoon = (JSCocoon)context.newObject(thrScope, "Cocoon", args);
cocoon.setInterpreter(this);
cocoon.setParentScope(thrScope);
thrScope.put("cocoon", thrScope, cocoon);
((ScriptableObject)thrScope).defineProperty(LAST_EXEC_TIME,
new Long(0),