public static final class TruffleLanguage.Env extends Object
TruffleLanguage. Each active
TruffleLanguage receives instance of the environment before any code is executed upon
it. The environment has knowledge of all active languages and can exchange symbols between
them.| Modifier and Type | Method and Description |
|---|---|
void |
addToHostClassPath(TruffleFile entry)
Adds an entry to the Java host class loader.
|
Object |
asBoxedGuestValue(Object guestObject)
Wraps primitive interop values in a TruffleObject exposing their methods as members.
|
Object |
asGuestValue(Object hostObject)
Converts a existing Java host object to a guest language value.
|
Throwable |
asHostException(Throwable exception)
Unwraps a host exception thrown by a Java method invocation.
|
Object |
asHostObject(Object value)
Returns the java host representation of a Truffle guest object if it represents a Java
host language object.
|
Object |
asHostSymbol(Class<?> symbolClass)
Converts a Java class to a host symbol as if by
lookupHostSymbol(symbolClass.getName()) but without an actual lookup. |
Thread |
createThread(Runnable runnable)
Creates a new thread that has access to the current language context.
|
Thread |
createThread(Runnable runnable,
TruffleContext context)
Creates a new thread that has access to the given context.
|
Thread |
createThread(Runnable runnable,
TruffleContext context,
ThreadGroup group)
Creates a new thread that has access to the given context.
|
Thread |
createThread(Runnable runnable,
TruffleContext context,
ThreadGroup group,
long stackSize)
Creates a new thread that has access to the given context.
|
OutputStream |
err()
Standard error writer provided by
Context.Builder.err(OutputStream) this language is being
executed in. |
void |
exportSymbol(String symbolName,
Object value)
Explicitly exports a symbol to the polyglot bindings object.
|
Object |
findMetaObject(Object value)
Find a meta-object of a value, if any.
|
String[] |
getApplicationArguments()
Returns the application arguments that were provided for this context.
|
Map<String,Object> |
getConfig()
Configuration arguments passed from an outer language context to an inner language
context.
|
TruffleContext |
getContext()
Returns the polyglot context associated with this environment.
|
Map<String,InstrumentInfo> |
getInstruments()
Returns a map instrument-id to instrument instance of all instruments that are installed
in the environment.
|
Map<String,LanguageInfo> |
getLanguages()
Returns a map mime-type to language instance of all languages that are installed in the
environment.
|
org.graalvm.options.OptionValues |
getOptions()
Returns option values for the options described in
TruffleLanguage.getOptionDescriptors(). |
Object |
getPolyglotBindings()
Returns a TruffleObject that represents the polyglot bindings.
|
TruffleFile |
getTruffleFile(String path)
Returns a
TruffleFile for given path. |
TruffleFile |
getTruffleFile(URI uri)
Returns a
TruffleFile for given URI. |
Object |
importSymbol(String symbolName)
Explicitly imports a symbol from the polyglot bindings.
|
InputStream |
in()
Input stream provided by
Context.Builder.in(InputStream)
this language is being executed in. |
boolean |
isCreateThreadAllowed()
Returns
true if the creation of new threads is allowed in the current
environment. |
boolean |
isHostException(Throwable exception)
Tests whether an exception is a host exception thrown by a Java Interop method
invocation.
|
boolean |
isHostFunction(Object value)
Returns
true if the argument is a Java host language function wrapped using
Truffle interop. |
boolean |
isHostLookupAllowed()
Returns
true if host access is generally allowed. |
boolean |
isHostObject(Object value)
Returns
true if the argument is Java host language object wrapped using
Truffle interop. |
boolean |
isHostSymbol(Object guestObject)
Returns
true if the argument is a host symbol, representing the constructor and
static members of a Java class, as obtained by e.g. |
boolean |
isMimeTypeSupported(String mimeType)
Allows it to be determined if this
Context can execute code
written in a language with a given MIME type. |
boolean |
isNativeAccessAllowed()
Returns
true if access to native code is generally allowed. |
<T> T |
lookup(Class<T> type)
Looks additional service up.
|
<S> S |
lookup(InstrumentInfo instrument,
Class<S> type)
Returns an additional service provided by this instrument, specified by type.
|
<S> S |
lookup(LanguageInfo language,
Class<S> type)
Returns an additional service provided by the given language, specified by type.
|
Object |
lookupHostSymbol(String symbolName)
Looks up a Java class in the top-most scope the host environment.
|
TruffleContext.Builder |
newContextBuilder()
Returns a new context builder useful to create inner context instances.
|
OutputStream |
out()
Standard output writer provided by
Context.Builder.out(OutputStream) this language is being
executed in. |
CallTarget |
parse(Source source,
String... argumentNames)
Evaluates source of (potentially different) language.
|
public org.graalvm.options.OptionValues getOptions()
TruffleLanguage.getOptionDescriptors(). The returned options are never
null.public String[] getApplicationArguments()
null. It is up to the language
implementation whether and how they are accessible within the guest language scripts.public boolean isCreateThreadAllowed()
true if the creation of new threads is allowed in the current
environment.TruffleLanguage.Env.createThread(Runnable)public Thread createThread(Runnable runnable)
TruffleLanguage.Env.createThread(Runnable, TruffleContext, ThreadGroup, long) for a detailed
description of the parameters. The group is null and stackSize
set to 0.public Thread createThread(Runnable runnable, TruffleContext context)
TruffleLanguage.Env.createThread(Runnable, TruffleContext, ThreadGroup, long) for a detailed
description of the parameters. The group is null and stackSize
set to 0.TruffleLanguage.Env.getContext(),
TruffleLanguage.Env.newContextBuilder()public Thread createThread(Runnable runnable, TruffleContext context, ThreadGroup group)
TruffleLanguage.Env.createThread(Runnable, TruffleContext, ThreadGroup, long) for a detailed
description of the parameters. The stackSize set to 0.TruffleLanguage.Env.getContext(),
TruffleLanguage.Env.newContextBuilder()public Thread createThread(Runnable runnable, TruffleContext context, ThreadGroup group, long stackSize)
initialized when it is
started and disposed as soon as the thread finished the execution.
It is recommended to set an
uncaught exception handler for the created thread. For example the thread can throw an
uncaught exception if one of the initialized language contexts don't support execution on
this thread.
The language that created and started the thread is responsible to complete all running
or waiting threads when the context is disposed.
The TruffleContext can be either an inner context created by
TruffleLanguage.Env.newContextBuilder().build(), or the
context associated with this environment obtained from TruffleLanguage.Env.getContext().
runnable - the runnable to run on this thread.context - the context to enter and leave when the thread is started.group - the thread group, passed on to the underlying Thread.stackSize - the desired stack size for the new thread, or zero if this parameter is
to be ignored.IllegalStateException - if thread creation is not allowed.TruffleLanguage.Env.getContext(),
TruffleLanguage.Env.newContextBuilder()public TruffleContext.Builder newContextBuilder()
for details on language inner contexts.public Object getPolyglotBindings()
public Object importSymbol(String symbolName)
polyglot
bindings object. Reading a symbol that does not exist will return null.
The returned symbol value can either be a 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,
Byte, Boolean, etc.).
symbolName - the name of the symbol to search fornull if it does not existpublic void exportSymbol(String symbolName, Object value)
polyglot
bindings object. Exporting a symbol with a null value will remove the
symbol from the polyglot object.
The exported symbol value can either be a 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,
Byte, Boolean, etc.).
symbolName - the name with which the symbol should be exported into the polyglot
scopevalue - the value to export forpublic boolean isHostLookupAllowed()
true if host access is generally allowed. If this method returns
false then TruffleLanguage.Env.lookupHostSymbol(String) will always fail.public void addToHostClassPath(TruffleFile entry)
TruffleLanguage.Env.lookupHostSymbol(String) will lookup classes with this new entry. If the entry
was already added then calling this method again for the same entry has no effect. Given
entry must not be null.SecurityException - if the file is not readable.public Object lookupHostSymbol(String symbolName)
class filter prevents access. The returned object is always a TruffleObject
.symbolName - the name of the symbol in the the host language.public boolean isHostObject(Object value)
true if the argument is Java host language object wrapped using
Truffle interop.TruffleLanguage.Env.asHostObject(Object)public Object asHostObject(Object value)
ClassCastException if the provided argument is not a
host object.public Object asGuestValue(Object hostObject)
Context.asValue(Object).
This method should be used exclusively to convert already allocated Java objects to a
guest language representation. To allocate new host objects users should use
TruffleLanguage.Env.lookupHostSymbol(String) to lookup the class and then send a NEW interop message
to that object to instantiate it. This method does not respect configured
class filters.
hostObject - the host object to convertpublic Object asBoxedGuestValue(Object guestObject)
Boolean,
Byte, Short, Integer, Long, Float, Double
, Character, String. If the provided value is already boxed then the
current value will be returned. If the provided value is not an interop value then an
IllegalArgumentException will be thrown.guestObject - the primitive guest value to boxIllegalArgumentException - if value is an invalid interop value.TruffleLanguage.Env.asGuestValue(Object)public boolean isHostFunction(Object value)
true if the argument is a Java host language function wrapped using
Truffle interop.public Object findMetaObject(Object value)
When no meta-object is known, returns null. The meta-object is an interop
value. An interop value can be either a TruffleObject (e.g. a native object
from the other language) to support interoperability between languages or a
String.
value - the value to find the meta object for.public boolean isHostException(Throwable exception)
EXECUTE, INVOKE, NEW). The host exception may be
unwrapped using TruffleLanguage.Env.asHostException(Throwable).exception - the Throwable to testtrue if the exception is a host exception, false
otherwiseTruffleLanguage.Env.asHostException(Throwable)public Throwable asHostException(Throwable exception)
EXECUTE, INVOKE, NEW). Host exceptions can be
identified using TruffleLanguage.Env.isHostException(Throwable) .exception - the host exception to unwrapIllegalArgumentException - if the exception is not a host exceptionTruffleLanguage.Env.isHostException(Throwable)public boolean isHostSymbol(Object guestObject)
true if the argument is a host symbol, representing the constructor and
static members of a Java class, as obtained by e.g. TruffleLanguage.Env.lookupHostSymbol(java.lang.String).TruffleLanguage.Env.lookupHostSymbol(String)public Object asHostSymbol(Class<?> symbolClass)
lookupHostSymbol(symbolClass.getName()) but without an actual lookup. Must not be
used with Truffle or guest language classes.TruffleLanguage.Env.lookupHostSymbol(String)public boolean isNativeAccessAllowed()
true if access to native code is generally allowed. If this method
returns false then loading native libraries with the Truffle NFI will fail.public boolean isMimeTypeSupported(String mimeType)
Context can execute code
written in a language with a given MIME type.Source.getMimeType(),
TruffleLanguage.Env.parse(Source, String...)public CallTarget parse(Source source, String... argumentNames)
MIME type is used to identify the TruffleLanguage to use to perform the
TruffleLanguage.parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest) . The names of
arguments are parameters for the resulting {#link CallTarget} that allow the
source to reference the actual parameters passed to
CallTarget.call(java.lang.Object...).source - the source to evaluateargumentNames - the names of CallTarget.call(java.lang.Object...) arguments
that can be referenced from the sourcepublic InputStream in()
Context.Builder.in(InputStream)
this language is being executed in.nullpublic OutputStream out()
Context.Builder.out(OutputStream) this language is being
executed in.nullpublic OutputStream err()
Context.Builder.err(OutputStream) this language is being
executed in.nullpublic <T> T lookup(Class<T> type)
language may also be associated with additional services. One can request
implementations of such services by calling this method with the type identifying the
requested service and its API.
Services that can be obtained via this method include
Instrumenter and others.T - type of requested servicetype - class of requested servicenull if there is no such service availablepublic <S> S lookup(InstrumentInfo instrument, Class<S> type)
S - the requested typeinstrument - identification of the instrument to querytype - the class of the requested typenull if none is foundpublic <S> S lookup(LanguageInfo language, Class<S> type)
Source must be
parsed first.S - the requested typelanguage - the language to querytype - the class of the requested typenull if none is foundpublic Map<String,LanguageInfo> getLanguages()
looked up .public Map<String,InstrumentInfo> getInstruments()
looked up .public Map<String,Object> getConfig()
TruffleLanguage.Env.newContextBuilder().to create inner contexts.,
to pass configuration objects to the
inner context.public TruffleContext getContext()
public TruffleFile getTruffleFile(String path)
TruffleFile for given path.path - the absolute or relative path to create TruffleFile forTruffleFilepublic TruffleFile getTruffleFile(URI uri)
TruffleFile for given URI.uri - the URI to create TruffleFile forTruffleFile