public final class TruffleContext extends Object implements AutoCloseable
A context consists of a language
context instance for each installed language. The current
language context is created eagerly and can be
accessed using a context reference or statically with
TruffleLanguage.getCurrentContext(Class) after the context was
entered.
The configuration for each language context is inherited from its parent/creator context. In
addition to that config parameters can be passed to new
language context instance of the current language. The configuration of other installed languages
cannot be modified. To run guest language code in a context, the context needs to be first
entered and then left. The context should be
closed when it is no longer needed. If the context is not closed explicitly,
then it is automatically closed together with the parent context.
Example usage:
void executeInContext(TruffleLanguage.Envenv) { MyContext outerLangContext = getContext();TruffleContextinnerContext = env.newContextBuilder().build();Objectp = innerContext.enter(); try { MyContext innerLangContext = getContext(); assert outerLangContext != innerLangContext; } finally { innerContext.leave(p); } assert outerLangContext == getContext(); innerContext.close(); } private static MyContext getContext() { returnTruffleLanguage.getCurrentContext(MyLanguage.class); }
| Modifier and Type | Class and Description |
|---|---|
class |
TruffleContext.Builder
Builder class to create new
TruffleContext instances. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this context and disposes its resources.
|
Object |
enter()
Enters this context and returns an object representing the previous context.
|
TruffleContext |
getParent()
Get a parent context of this context, if any.
|
boolean |
isEntered()
Checks whether the context is entered.
|
void |
leave(Object prev)
Leaves this context and sets the previous context as the new current context.
|
public TruffleContext getParent()
null if there is no parentpublic Object enter()
TruffleContext.leave(Object) in a finally block and the previous
context must be passed as an argument. It is allowed to enter a context multiple times from
the same thread. If the context is currently not entered by any thread then it is allowed be
entered by an arbitrary thread. Entering the context from two or more different threads at
the same time is possible, unless one of the loaded languages denies access to the thread, in
which case an IllegalStateException is thrown. The result of the enter function is
unspecified and must only be passed to TruffleContext.leave(Object). The result value must not be
stored permanently.
Entering a language context is designed for compilation and is most efficient if the
context instance is compilation final.
Example usage:
void executeInContext(TruffleLanguage.Envenv) { MyContext outerLangContext = getContext();TruffleContextinnerContext = env.newContextBuilder().build();Objectp = innerContext.enter(); try { MyContext innerLangContext = getContext(); assert outerLangContext != innerLangContext; } finally { innerContext.leave(p); } assert outerLangContext == getContext(); innerContext.close(); } private static MyContext getContext() { returnTruffleLanguage.getCurrentContext(MyLanguage.class); }
TruffleContext.leave(Object)public void leave(Object prev)
Leaving a language context is designed for compilation and is most efficient if the
context instance is compilation final.
prev - the previous context returned by TruffleContext.enter()TruffleContext.enter()public void close()
entered by any thread. If a closed context is attempted to be accessed or
entered, then an IllegalStateException is thrown. If the context is not closed
explicitly, then it is automatically closed together with the parent context. If an attempt
to close a context was successful then consecutive calls to close have no effect.
Only contexts created by TruffleContext.Builder.build() can be explicitly closed. Other instances
throw UnsupportedOperationException on close attempts.
close in interface AutoCloseableUnsupportedOperationException - when not created by TruffleContext.Builder.build().public boolean isEntered()
TruffleContext.enter() and hasn't been left yet with
TruffleContext.leave(java.lang.Object) methods. This method is thread-safe and may be used from
multiple threads.true if the context is active, false otherwise