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 enterContext()
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 enterContext()
must have a matching call to {@link Context#exit()}.
Context cx = contextFactory.enterContext(); try { ... cx.evaluateString(...); } finally { Context.exit(); }
Instead of using
enterContext(),
exit() pair consider using {@link #call(ContextAction)} which guarantees proper association of Context instances with the current thread. With this method the above example becomes:
ContextFactory.call(new ContextAction() { public Object run(Context cx) { ... cx.evaluateString(...); return null; } });
@return a Context associated with the current thread
@see Context#getCurrentContext()
@see Context#exit()
@see #call(ContextAction)