Enum Class TruffleLanguage.ContextPolicy
- All Implemented Interfaces:
Serializable
,Comparable<TruffleLanguage.ContextPolicy>
,Constable
- Enclosing class:
TruffleLanguage<C>
languages
per context. I.e.
the policy specifies the degree of sharing that is allowed between multiple language
contexts. The default policy is exclusive
. Every language is encouraged to
try to support a context policy that is as permissive as possible, where exclusive
is the least and shared
is the most permissive policy.
Parse caching
is scoped per
language
instance, therefore the context policy influences its
behavior.
If multiple sharable languages are used at the same time, nodes of sharable languages may
adopt nodes of a non-sharable languages indirectly, e.g. through Truffle interoperability.
This would complicate language implementations as they would always need to support sharing
for their nodes. To avoid this problem, Truffle uses sharing layers. Sharing layers ensure
that every node created by a language instance can only be observed by a single language
instance at a time. Sharing layers operate on the principle that all languages share the code
with the same language instance or no language shares. As a convenient consequence any call
to TruffleLanguage.LanguageReference.get(Node)
with an adopted Node
can fold to a constant
during partial evaluation.
For any sharing to take place a context must be created with sharing enabled. By default
sharing is disabled for polyglot contexts. Sharing can be enabled by specifying an
explicit engine
or using an option. Before any language is
used for sharing TruffleLanguage.initializeMultipleContexts()
is invoked. It is
guaranteed that sharing between multiple contexts is
initialized
before any language context
is created
.
- Since:
- 19.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionUse one exclusiveTruffleLanguage
instance per language context instance.Use a singleTruffleLanguage
instance per context instance, but allow the reuse of a language instance when a context wasdisposed
.Use oneTruffleLanguage
instance for many language context instances. -
Method Summary
Modifier and TypeMethodDescriptionReturns the enum constant of this class with the specified name.static TruffleLanguage.ContextPolicy[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
EXCLUSIVE
Use one exclusiveTruffleLanguage
instance per language context instance.Using this policy has the following implications:
Parse caching
is scoped perlanguage
instance. This means the language context instance may be directly stored in instance fields of AST nodes without the use ofTruffleLanguage.ContextReference
. The use ofTruffleLanguage.ContextReference
is still recommended to simplify migration to more permissive policies.- If the language does not allow
multi-threading
(default behavior) then the language instance is guaranteed to be used from one thread at a time only. Cached ASTs will not be used from multiple threads at the same time. TruffleLanguage.initializeMultipleContexts()
is guaranteed to never be invoked.
- Since:
- 19.0
-
REUSE
Use a singleTruffleLanguage
instance per context instance, but allow the reuse of a language instance when a context wasdisposed
. This policy is useful when parsed ASTs should be shared, but multiple thread execution of the AST is not yet supported by the language.Using this policy has the following implications:
Parse caching
is scoped perlanguage
instance. This means language context instances must NOT be directly stored in instance fields of AST nodes and theTruffleLanguage.ContextReference
must be used instead.- If the language does not
allow
access from multiple threads (default behavior) then the language instance is guaranteed to be used from one thread at a time only. In this case also cached ASTs will not be used from multiple threads at the same time. If the language allows access from multiple threads then also ASTs may be used from multiple threads at the same time. TruffleLanguage.initializeMultipleContexts()
will be invoked to notify the language that multiple contexts will be used with one language instance.Language
instance fields must only be used for data that can be shared across multiple contexts.
- Since:
- 19.0
-
SHARED
Use oneTruffleLanguage
instance for many language context instances.Using this policy has the following implications:
Parse caching
is scoped perlanguage
instance. With multiple language instances per context, context instances must not be directly stored in instance fields of AST nodes and theTruffleLanguage.ContextReference
must be used instead.- All methods of the
language
instance and parsed ASTs may be called from multiple threads at the same time independent of whether multiple thread access isallowed
for the language. TruffleLanguage.initializeMultipleContexts()
will be invoked to notify the language that multiple contexts will be used with one language instance.Language
instance fields must only be used for data that can be shared across multiple contexts and mutable data held by the language instance must be synchronized to support concurrent access.
- Since:
- 19.0
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-