C - internal state of the language associated with every thread that is executing program
parsed by the
languagepublic abstract class TruffleLanguage<C> extends Object
TruffleLanguage must provide a public default constructor.
TruffleLanguage.Registration annotation and the implementation's JAR file placed on the host Java Virtual
Machine's class path.
A newly created engine locates all available language implementations and creates a descriptor for each. The descriptor holds the language's registered metadata, but its execution environment is not initialized until the language is needed for code execution. That execution environment remains initialized for the lifetime of the engine and is isolated from the environment in any other engine instance.
A new language implementation instance is instantiated for each runtime
that is created using the runtime builder. If an engine is created without a runtime then the language implementation
instance is created for each engine.
Global state can be shared between multiple context instances by saving them as in a field of the
TruffleLanguage subclass. The implementation needs to ensure data isolation between the
contexts. However ASTs or assumptions can be shared across multiple contexts if modifying them
does not affect language semantics. Languages are strongly discouraged from using static mutable
state in their languages. Instead language implementation instances should be used instead.
The methods in TruffleLanguage might be invoked from multiple threads. However, one
context instance is guaranteed to be invoked only from one single thread. Therefore context
objects don't need to be thread-safe. Code that is shared using the TruffleLanguage
instance needs to be prepared to be accessed from multiple threads.
Whenever an engine is disposed then each initialized context will be disposed
disposed.
K = number of installed languages
I = number of installed instruments
N = unbounded
- 1:Host VM Processs
- N:PolyglotRuntime
- K:TruffleLanguage
- I:PolyglotRuntime.Instrument
- 1:TruffleInstrument
- N:PolyglotEngine
- 1:Thread
- K:PolyglotEngine.Language
- 1:Language Context
MIME-type/key/object triples. A Language implementation retrieves the data
via TruffleLanguage.Env.getConfig() for configuring initial execution state.
A language manages its namespace of exported global symbols dynamically, by its response to the
query TruffleLanguage.findExportedSymbol(Object, String, boolean). No attempt is made to avoid
cross-language name conflicts.
A language implementation can also import a global symbol by name, according to the following rules:
PolyglotEngine, which should be designed in a generic way.
Language-specific entry points, for instance to emulate the command-line interface of an existing
implementation, should be handled externally.| Modifier and Type | Class and Description |
|---|---|
static class |
TruffleLanguage.ContextReference<C>
Represents a reference to the current context to be stored in an AST.
|
static class |
TruffleLanguage.Env
Represents execution environment of the
TruffleLanguage. |
static class |
TruffleLanguage.ParsingRequest
Request for parsing.
|
static interface |
TruffleLanguage.Registration
The annotation to use to register your language to the
Truffle system. |
| Modifier | Constructor and Description |
|---|---|
protected |
TruffleLanguage()
Constructor to be called by subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract C |
createContext(TruffleLanguage.Env env)
Creates internal representation of the executing context suitable for given environment.
|
protected void |
disposeContext(C context)
Disposes the context created by
TruffleLanguage.createContext(com.oracle.truffle.api.TruffleLanguage.Env). |
protected abstract Object |
findExportedSymbol(C context,
String globalName,
boolean onlyExplicit)
Called when some other language is seeking for a global symbol.
|
protected Object |
findMetaObject(C context,
Object value)
Find a meta-object of a value, if any.
|
protected SourceSection |
findSourceLocation(C context,
Object value)
Find a source location where a value is declared, if any.
|
TruffleLanguage.ContextReference<C> |
getContextReference()
Creates a reference to the current context to be stored in an AST.
|
protected abstract Object |
getLanguageGlobal(C context)
Returns global object for the language.
|
protected void |
initializeContext(C context)
Perform any complex initialization.
|
protected abstract boolean |
isObjectOfLanguage(Object object)
Checks whether the object is provided by this language.
|
protected boolean |
isVisible(C context,
Object value)
Decides whether the result of evaluating an interactive source should be printed to stdout.
|
protected CallTarget |
parse(TruffleLanguage.ParsingRequest request)
Parses the
provided source and generates its appropriate
AST representation. |
protected String |
toString(C context,
Object value)
Generates language specific textual representation of a value.
|
protected TruffleLanguage()
protected abstract C createContext(TruffleLanguage.Env env)
language is used by a new
PolyglotEngine or in a new thread, the system calls this
method to let the language prepare for execution. The
returned execution context is completely language specific; it is however expected it will
contain reference to here-in provided env and adjust itself according to
parameters provided by the env object.
The context created by this method is accessible using TruffleLanguage.getContextReference(). An
IllegalStateException is thrown if the context is tried to be accessed while the
createContext method is executed.
This method shouldn't perform any complex operations. The runtime system is just being
initialized and for example making
calls into other
languages and assuming your language is already initialized and others can see it would be
wrong - until you return from this method, the initialization isn't over. Should there be a
need to perform complex initializaton, do it by overriding the
TruffleLanguage.initializeContext(java.lang.Object) method.
env - the environment the language is supposed to operate inprotected void initializeContext(C context) throws Exception
TruffleLanguage.createContext(com.oracle.truffle.api.TruffleLanguage.Env) factory method shouldn't
do any complex operations. Just create the instance of the context, let the runtime system
register it properly. Should there be a need to perform complex initializaton, override this
method and let the runtime call it later to finish any post initialization
actions. Example:
class PostInitLanguage extendsTruffleLanguage<Context> { @Overrideprotected Context createContext(TruffleLanguage.Envenv) { // "quickly" create the context return new Context(env); } @Overrideprotected void initializeContext(Context context) throwsIOException{ // called "later" to finish the initialization // for example call into another languageSourcesource =Source.newBuilder("function mul(x, y) { return x * y }"). name("mul.js"). mimeType("text/javascript"). build(); context.mul = context.env.parse(source); } }
context - the context created by
TruffleLanguage.createContext(com.oracle.truffle.api.TruffleLanguage.Env)Exception - if something goes wrongprotected void disposeContext(C context)
TruffleLanguage.createContext(com.oracle.truffle.api.TruffleLanguage.Env). A language can be asked
by its user to clean-up. In such case the language is supposed to dispose any
resources acquired before and dispose the context - e.g. render it
useless for any future calls.context - the context created by the languageprotected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception
provided source and generates its appropriate
AST representation. The parsing should execute no user code, it should only create the
Node tree to represent the source. If the provided
source does not correspond naturally to a call target, the returned call
target should create and if necessary initialize the corresponding language entity and return
it.
The parsing may be performed in a context (specified by TruffleLanguage.ParsingRequest.getLocation())
or without context. The argumentNames may contain symbolic names for actual
parameters of the call to the returned value. The result should be a call target with method
CallTarget.call(java.lang.Object...) that accepts as many arguments as were provided
via the TruffleLanguage.ParsingRequest.getArgumentNames() method.request - request for parsingNode tree representing
just parsed codeException - exception can be thrown parsing goes wrong. Here-in thrown exception is
propagated to the user who called one of eval methods of
PolyglotEngineprotected abstract Object findExportedSymbol(C context, String globalName, boolean onlyExplicit)
The exported object can either be TruffleObject (e.g. a native object from the
other language) to support interoperability between languages, String or one of the
Java primitive wrappers ( Integer, Double, Short, Boolean,
etc.).
The way a symbol becomes exported is language dependent. In general it is preferred
to make the export explicit - e.g. call some function or method to register an object under
specific name. Some languages may however decide to support implicit export of symbols (for
example from global scope, if they have one). However explicit exports should always be
preferred. Implicitly exported object of some name should only be used when there is no
explicit export under such globalName. To ensure so the infrastructure first
asks all known languages for onlyExplicit symbols and only when none is found,
it does one more round with onlyExplicit set to false.
context - context to locate the global symbol inglobalName - the name of the global symbol to findonlyExplicit - should the language seek for implicitly exported object or only consider
the explicitly exported ones?null, if the symbol does not represent anything
meaningful in this languageprotected abstract Object getLanguageGlobal(C context)
The object is expected to be TruffleObject (e.g. a native object from the other
language) but technically it can be one of the Java primitive wrappers (Integer,
Double, Short, etc.).
context - context to find the language global innull if the language does not support such conceptprotected abstract boolean isObjectOfLanguage(Object object)
object - the object to checktrue if this language can deal with such object in native wayprotected String toString(C context, Object value)
value.as(String.class) is requested, it consults the language that produced the value by
calling this method. By default this method calls Objects.toString(java.lang.Object).context - the execution context for doing the conversionvalue - the value to convert. Either primitive type or
TruffleObjectprotected boolean isVisible(C context, Object value)
true claiming all values are visible.
This method affects behavior of PolyglotEngine.eval(com.oracle.truffle.api.source.Source) - when
evaluating an interactive source the result of the
evaluation is tested for
visibility and if the value is found
visible, it gets converted to string and printed to
standard output.
A language can control whether a value is or isn't printed by overriding this method and
returning false for some or all values. In such case it is up to the language
itself to use polyglot engine
output,
error and
input streams. When
PolyglotEngine.eval(com.oracle.truffle.api.source.Source) is called over an
interactive source of a language that controls its interactive
behavior, it is the reponsibility of the language itself to print the result to
standard output
or error output
and/or access standard input in an appropriate way.
context - the execution context for doing the conversionvalue - the value to check. Either primitive type or
TruffleObjecttrue if the language implements an interactive response to evaluation of
interactive sources.protected Object findMetaObject(C context, Object value)
A programmatic textual representation
should be provided for meta-objects, when possible. The meta-object may have properties
describing their structure.
When no meta-object is known, return null. The default implementation returns
null.
context - the execution contextvalue - a value to find the meta-object ofnullprotected SourceSection findSourceLocation(C context, Object value)
meta-objects. The default
implementation returns null.context - the execution contextvalue - a value to get the source location fornullpublic final TruffleLanguage.ContextReference<C> getContextReference()
TruffleLanguage.ContextReference.get() method of the returned reference. If a
context reference is created in the language class constructor an
IllegalStateException is thrown. The exception is also thrown if the reference is
tried to be created or accessed outside of the execution of an engine.
The returned reference identity is undefined. It might either return always the same instance
or a new reference for each invocation of the method. Please note that the current context
might vary between executions if resources or code is
shared between multiple contexts.