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. |
TruffleFile |
createTempDirectory(TruffleFile dir,
String prefix,
FileAttribute<?>... attrs)
Creates a new directory in the specified or default temporary directory, using the given
prefix to generate its name.
|
TruffleFile |
createTempFile(TruffleFile dir,
String prefix,
String suffix,
FileAttribute<?>... attrs)
Creates a new empty file in the specified or default temporary directory, using the given
prefix and suffix to generate its name.
|
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 metaobject 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.
|
TruffleFile |
getCurrentWorkingDirectory()
Gets the current working directory.
|
Map<String,String> |
getEnvironment()
Returns an unmodifiable map of the process environment.
|
String |
getFileNameSeparator()
Returns the name separator used to separate names in
TruffleFile's path string. |
Map<String,InstrumentInfo> |
getInstruments()
Returns a map instrument-id to instrument instance of all instruments that are installed
in the environment.
|
Map<String,LanguageInfo> |
getInternalLanguages()
Returns all languages that are installed and internally accessible in the environment.
|
TruffleFile |
getInternalTruffleFile(String path)
Returns a public or internal
TruffleFile for given path. |
TruffleFile |
getInternalTruffleFile(URI uri)
Returns a public or internal
TruffleFile for given URI. |
org.graalvm.options.OptionValues |
getOptions()
Returns option values for the options described in
TruffleLanguage.getOptionDescriptors(). |
String |
getPathSeparator()
Returns the path separator used to separate filenames in a path list.
|
Object |
getPolyglotBindings()
Returns a TruffleObject that represents the polyglot bindings.
|
Map<String,LanguageInfo> |
getPublicLanguages()
Returns all languages that are installed and publicly accessible in the environment.
|
TruffleFile |
getPublicTruffleFile(String path)
Returns a public
TruffleFile for given path. |
TruffleFile |
getPublicTruffleFile(URI uri)
Returns a public
TruffleFile for given URI. |
ZoneId |
getTimeZone()
Returns the default time zone of this environment.
|
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 |
initializeLanguage(LanguageInfo targetLanguage)
Ensures that the target language is initialized.
|
boolean |
isCreateProcessAllowed()
Returns
true if the creation of a sub-process is allowed in the current
environment. |
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. |
boolean |
isPolyglotBindingsAccessAllowed()
Returns
true if polyglot bindings access is allowed, else false
. |
boolean |
isPolyglotEvalAllowed()
Returns
true if polyglot evaluation is allowed, else false. |
boolean |
isPreInitialization()
Returns
true if this Context is being
pre-initialized. |
<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.
|
TruffleProcessBuilder |
newProcessBuilder(String... command)
Creates a new process builder with the specified operating program and arguments.
|
OutputStream |
out()
Standard output writer provided by
Context.Builder.out(OutputStream) this language is being
executed in. |
CallTarget |
parseInternal(Source source,
String... argumentNames)
Parses the source of a public or internal language and returns the parse result as
CallTarget. |
CallTarget |
parsePublic(Source source,
String... argumentNames)
Parses the source of a public language and returns the parse result as
CallTarget
. |
void |
registerService(Object service)
Registers additional services provided by the language.
|
void |
setCurrentWorkingDirectory(TruffleFile currentWorkingDirectory)
Sets the current working directory.
|
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()
SecurityException - if polyglot access is not enabledTruffleLanguage.Env.isPolyglotBindingsAccessAllowed()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.).
Polyglot symbols can only be imported if the polyglot bindings access is allowed.
symbolName - the name of the symbol to search fornull if it does not existSecurityException - if importing polyglot symbols is not allowedpublic 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.).
Polyglot symbols can only be export if the polyglot bindings access is allowed.
symbolName - the name with which the symbol should be exported into the polyglot
scopevalue - the value to export forSecurityException - if exporting polyglot symbols is not allowedpublic boolean isHostLookupAllowed()
true if host access is generally allowed. If this method returns
false then TruffleLanguage.Env.lookupHostSymbol(String) will always fail. Host lookup
is generally disallowed if the embedder provided a null
host class filter.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)
host class filter prevents access. The returned object is always a
TruffleObject that represents the class symbol.symbolName - the qualified class name in 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 metaobject is known, returns null. The metaobject 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)
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)
TruffleLanguage.Env.asHostException(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 isPolyglotEvalAllowed()
true if polyglot evaluation is allowed, else false.
Guest languages should hide or disable all polyglot evaluation builtins if this flag is
set to false. Note that if polyglot evaluation access is disabled, then the
available languages list only shows the current language,
dependent languages and
internal languages.Context.Builder.allowPolyglotAccess(org.graalvm.polyglot.PolyglotAccess)public boolean isPolyglotBindingsAccessAllowed()
true if polyglot bindings access is allowed, else false
. Guest languages should hide or disable all polyglot bindings builtins if this flag is
set to false. If polyglot bindings access is disabled then
TruffleLanguage.Env.getPolyglotBindings(), TruffleLanguage.Env.importSymbol(String) or
TruffleLanguage.Env.exportSymbol(String, Object) fails with a SecurityException.Context.Builder.allowPolyglotAccess(org.graalvm.polyglot.PolyglotAccess)public boolean isMimeTypeSupported(String mimeType)
Context can execute code
written in a language with a given MIME type.Source.getMimeType(),
TruffleLanguage.Env.parsePublic(Source, String...)public CallTarget parseInternal(Source source, String... argumentNames)
CallTarget. The language id 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...).
Compared to TruffleLanguage.Env.parsePublic(Source, String...) this method provides also access to
internal and dependent languages in
addition to public languages. For example, in JavaScript, a call to the eval builtin
should forward to TruffleLanguage.Env.parsePublic(Source, String...) as it contains code provided by
the guest language user. Parsing regular expressions with the internal regular expression
engine should call TruffleLanguage.Env.parseInternal(Source, String...) instead, as this is
considered an implementation detail of the language.
It is recommended that the language uses TruffleLanguage.Env.parsePublic(Source, String...) or
TruffleLanguage.Env.parseInternal(Source, String...) instead of directly passing the Source to
the parser, in order to support code caching with TruffleLanguage.ContextPolicy.SHARED and
TruffleLanguage.ContextPolicy.REUSE.
source - the source to evaluateargumentNames - the names of CallTarget.call(java.lang.Object...) arguments
that can be referenced from the sourceTruffleLanguage.Env.parsePublic(Source, String...)public CallTarget parsePublic(Source source, String... argumentNames)
CallTarget
. The language id 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...).
Compared to TruffleLanguage.Env.parseInternal(Source, String...) this method does only provide
access to non internal, non dependent, public languages. Public languages are configured
by the embedder to be accessible to the guest language program. For example, in
JavaScript, a call to the eval builtin should forward to
TruffleLanguage.Env.parsePublic(Source, String...) as it contains code provided by the guest
language user. Parsing regular expressions with the internal regular expression engine
should call TruffleLanguage.Env.parseInternal(Source, String...) instead, as this is considered an
implementation detail of the language.
It is recommended that the language uses TruffleLanguage.Env.parsePublic(Source, String...) or
TruffleLanguage.Env.parseInternal(Source, String...) instead of directly passing the Source to
the parser, in order to support code caching with TruffleLanguage.ContextPolicy.SHARED and
TruffleLanguage.ContextPolicy.REUSE.
source - the source to evaluateargumentNames - the names of CallTarget.call(java.lang.Object...) arguments
that can be referenced from the sourceTruffleLanguage.Env.parseInternal(Source, String...)public 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)
TruffleLanguage.Registration.services() the service lookup will ensure
that the language is loaded and services are registered. The provided langauge and type
must not be null.S - the requested typelanguage - the language to querytype - the class of the requested typenull if none is foundregisterServicepublic boolean initializeLanguage(LanguageInfo targetLanguage)
SecurityException is
thrown. Then the target language initialization is performed if not already done.targetLanguage - the language to initializeSecurityException - if an access to targetLanguage is not permittedpublic Map<String,LanguageInfo> getInternalLanguages()
looked up. TruffleLanguage.Env.parseInternal(Source, String...)
is allowed for all languages returned by this method. This list of languages should not
be exposed to guest language programs, as it lists internal languages.public Map<String,LanguageInfo> getPublicLanguages()
looked up. TruffleLanguage.Env.parsePublic(Source, String...)
is allowed for all languages returned by this method. This list of languages may be
exposed ot the guest language program.TruffleLanguage.Env.parsePublic(Source, String...)public Map<String,InstrumentInfo> getInstruments()
looked up .public ZoneId getTimeZone()
system default time-zone will
be returned.ZoneId.systemDefault()public Map<String,Object> getConfig()
TruffleLanguage.Env.newContextBuilder().to create inner contexts.,
to pass configuration objects to the
inner context.public TruffleContext getContext()
public boolean isPreInitialization()
true if this Context is being
pre-initialized. For a given environment, the return value of this method
never changes.TruffleLanguage.initializeContext(Object),
TruffleLanguage.patchContext(Object, Env)public TruffleFile getPublicTruffleFile(String path)
TruffleFile for given path. The access to public files depends
on Context's IO policy. When IO is not enabled by the
Context the TruffleFile operations throw SecurityException. The
getPublicTruffleFile method should be used to access user files or to implement
language IO builtins.path - the absolute or relative path to create TruffleFile forTruffleFilepublic TruffleFile getPublicTruffleFile(URI uri)
TruffleFile for given URI. The access to public files
depends on Context's IO policy. When IO is not enabled
by the Context the TruffleFile operations throw SecurityException
. The getPublicTruffleFile method should be used to access user files or to
implement language IO builtins.uri - the URI to create TruffleFile forTruffleFilepublic TruffleFile getInternalTruffleFile(String path)
TruffleFile for given path. Unlike
getPublicTruffleFile the
TruffleFile returned by this method for a file in a language home is readable
even when IO is not enabled by the Context. The getInternalTruffleFile
method should be used to read language standard libraries in a language home. For
security reasons the language should check that the file is a language source file in
language standard libraries folder before using this method for a file in a language
home.path - the absolute or relative path to create TruffleFile forTruffleFileTruffleLanguage.Env.getPublicTruffleFile(java.lang.String)public TruffleFile getInternalTruffleFile(URI uri)
TruffleFile for given URI. Unlike
getPublicTruffleFile the
TruffleFile returned by this method for a file in a language home is readable
even when IO is not enabled by the Context. The getInternalTruffleFile
method should be used to read language standard libraries in a language home. For
security reasons the language should check that the file is a language source file in
language standard libraries folder before using this method for a file in a language
home.uri - the URI to create TruffleFile forTruffleFileTruffleLanguage.Env.getPublicTruffleFile(java.net.URI)public TruffleFile getCurrentWorkingDirectory()
TruffleFile methods.SecurityException - if the filesystem denies reading of the
current working directorypublic void setCurrentWorkingDirectory(TruffleFile currentWorkingDirectory)
TruffleFile methods.currentWorkingDirectory - the new current working directoryUnsupportedOperationException - if setting of the current working directory is not
supportedIllegalArgumentException - if the currentWorkingDirectory is not a valid
current working directorySecurityException - if currentWorkingDirectory is not readablepublic String getFileNameSeparator()
TruffleFile's path string.public String getPathSeparator()
':'. On Windows it's ';'.public void registerService(Object service)
TruffleLanguage.Env.lookup(com.oracle.truffle.api.nodes.LanguageInfo, java.lang.Class) query method.
For each service interface enumerated in language
registration the language has to register a single service implementation.
This method can be called only during the execution of the
createContext method,
then the services are collected and cannot be changed anymore.
service - a service to be returned from associated
lookup methodIllegalStateException - if the method is called outside of
TruffleLanguage.createContext(com.oracle.truffle.api.TruffleLanguage.Env) methodpublic boolean isCreateProcessAllowed()
true if the creation of a sub-process is allowed in the current
environment.TruffleLanguage.Env.newProcessBuilder(java.lang.String...)public TruffleProcessBuilder newProcessBuilder(String... command)
command - the executable and its argumentsSecurityException - when process creation is not allowedpublic Map<String,String> getEnvironment()
Context is
configured with EnvironmentAccess.INHERIT it returns the System.getenv()
and the environment variables configured on the Context. For the
EnvironmentAccess.NONE only the environment variables configured on the
Context are returned.public TruffleFile createTempFile(TruffleFile dir, String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
This method provides only part of a temporary file facility. To arrange for a file
created by this method to be deleted automatically the resulting file must be opened
using the DELETE_ON_CLOSE option. In this case
the file is deleted when the appropriate close method is invoked. Alternatively,
a shutdown hook may be used to delete the file
automatically.
dir - the directory in which the file should be created or null for a
default temporary directoryprefix - the prefix to generate the file's name or nullsuffix - the suffix to generate the file's name or null in which case
".tmp" is usedattrs - the optional attributes to set atomically when creating the fileTruffleFile representing the newly created file that did not exist
before this method was invokedIOException - in case of IO errorIllegalArgumentException - if the prefix or suffix cannot be used to generate a
valid file nameUnsupportedOperationException - if the attributes contain an attribute which cannot
be set atomically or FileSystem does not support default temporary
directorySecurityException - if the FileSystem denied the operationpublic TruffleFile createTempDirectory(TruffleFile dir, String prefix, FileAttribute<?>... attrs) throws IOException
This method provides only part of a temporary file facility. A
shutdown hook may be used to delete the directory
automatically.
dir - the directory in which the directory should be created or null for a
default temporary directoryprefix - the prefix to generate the directory's name or nullattrs - the optional attributes to set atomically when creating the directoryTruffleFile representing the newly created directory that did not
exist before this method was invokedIOException - in case of IO errorIllegalArgumentException - if the prefix cannot be used to generate a valid file
nameUnsupportedOperationException - if the attributes contain an attribute which cannot
be set atomically or FileSystem does not support default temporary
directorySecurityException - if the FileSystem denied the operation