Modifier and Type | Method and Description |
---|---|
Context.Builder |
allowAllAccess(boolean enabled)
Sets the default value for all privileges.
|
Context.Builder |
allowCreateProcess(boolean enabled)
If
true , allows guest language to execute external processes. |
Context.Builder |
allowCreateThread(boolean enabled)
If
true , allows guest languages to create new threads. |
Context.Builder |
allowEnvironmentAccess(EnvironmentAccess accessPolicy)
Allow environment access using the provided policy.
|
Context.Builder |
allowExperimentalOptions(boolean enabled)
Allow experimental options to be used for language options.
|
Context.Builder |
allowHostAccess(HostAccess config)
Configures which public constructors, methods or fields of public classes are accessible
by guest applications.
|
Context.Builder |
allowHostClassLoading(boolean enabled)
If host class loading is enabled, then the guest language is allowed to load new host
classes via jar or class files.
|
Context.Builder |
allowHostClassLookup(Predicate<String> classFilter)
Sets a filter that specifies the Java host classes that can be looked up by the guest
application.
|
Context.Builder |
allowInnerContextOptions(boolean enabled)
Allows this context to spawn inner contexts that may change option values set for the
outer context.
|
Context.Builder |
allowIO(IOAccess ioAccess)
Configures guest language access to host IO.
|
Context.Builder |
allowNativeAccess(boolean enabled)
Allows guest languages to access the native interface.
|
Context.Builder |
allowPolyglotAccess(PolyglotAccess accessPolicy)
Allow polyglot access using the provided policy.
|
Context.Builder |
allowValueSharing(boolean enabled)
Enables or disables sharing of any
value between contexts. |
Context.Builder |
arguments(String language,
String[] args)
Sets the guest language application arguments for a language
context . |
Context |
build()
Creates a new context instance from the configuration provided in the builder.
|
Context.Builder |
currentWorkingDirectory(Path workingDirectory)
Sets the current working directory used by the guest application to resolve relative
paths.
|
Context.Builder |
engine(Engine engine)
Explicitly sets the underlying engine to use.
|
Context.Builder |
environment(Map<String,String> env)
Shortcut for setting multiple
environment variables
using a map. |
Context.Builder |
environment(String name,
String value)
Sets an environment variable.
|
Context.Builder |
err(OutputStream err)
Sets the error output stream to be used for the context.
|
Context.Builder |
hostClassLoader(ClassLoader classLoader)
Sets a host class loader.
|
Context.Builder |
in(InputStream in)
Sets the input stream to be used for the context.
|
Context.Builder |
logHandler(Handler logHandler)
Installs a new logging
Handler . |
Context.Builder |
logHandler(OutputStream logOut)
Installs a new logging
Handler using given OutputStream . |
Context.Builder |
option(String key,
String value)
Sets an option for this
context . |
Context.Builder |
options(Map<String,String> options)
Shortcut for setting multiple
options using a map. |
Context.Builder |
out(OutputStream out)
Sets the standard output stream to be used for the context.
|
Context.Builder |
processHandler(ProcessHandler handler)
Installs a
ProcessHandler responsible for external process creation. |
Context.Builder |
resourceLimits(ResourceLimits limits)
Assigns resource limit configuration to a context.
|
Context.Builder |
sandbox(SandboxPolicy policy)
Sets a code sandbox policy to a context.
|
Context.Builder |
serverTransport(MessageTransport serverTransport)
Take over the transport of messages communication with a server peer.
|
Context.Builder |
timeZone(ZoneId zone)
Sets the default time zone to be used for this context.
|
Context.Builder |
useSystemExit(boolean enabled)
Specifies whether
System.exit(int) may be used to improve efficiency of stack
unwinding for context exit requested by the guest application. |
public Context.Builder engine(Engine engine)
Engine
for more details about system resource sharing.public Context.Builder out(OutputStream out)
engine
or standard error stream
is used.public Context.Builder err(OutputStream err)
engine
or standard error stream
is used.public Context.Builder in(InputStream in)
engine
or standard in stream is used.public Context.Builder allowHostAccess(HostAccess config)
Context.Builder.allowAllAccess(boolean)
is
false
the HostAccess.EXPLICIT
policy will be used, otherwise
HostAccess.ALL
.public Context.Builder allowNativeAccess(boolean enabled)
public Context.Builder allowCreateThread(boolean enabled)
true
, allows guest languages to create new threads. Default is
false
. If all access
is set to
true
, then the creation of threads is enabled if not allowed explicitly.
Threads created by guest languages are closed, when the context is closed
.public Context.Builder allowAllAccess(boolean enabled)
false
. If all access is enabled then certain privileges may still be
disabled by configuring it explicitly using the builder (either before or after the call
to allowAllAccess()
). Allowing all access should only be
set if the guest application is fully trusted.
If true
, grants the context the same access privileges as the host virtual
machine. If the host VM runs without a security manager
enabled,
then enabling all access gives the guest languages full control over the host process.
Otherwise, Java security manager
is in control of restricting the
privileges of the polyglot context. If new privilege restrictions are added to the
polyglot API, then they will default to full access.
Grants full access to the following privileges by default:
creation
and use of new threads.
host classes
.
host classes
by adding
entries to the class path.
bindings
.
IO operations
on host system.
experimental options
.
creation
and use of new sub-processes.
access
to
process environment variables.
changing
of options for of inner
contexts spawned by the language.
enabled
- true
for all access by default.public Context.Builder allowHostClassLoading(boolean enabled)
all access
is set to
true
, then the host class loading is enabled if it is not disallowed
explicitly. For host class loading to be useful, IO
operations
host class lookup
, and the
host access policy
needs to be
configured as well.Context.Builder.allowHostAccess(HostAccess)
,
Context.Builder.allowHostClassLookup(Predicate)
public Context.Builder allowHostClassLookup(Predicate<String> classFilter)
null
then no class lookup is allowed and relevant
language builtins are not available (e.g. Java.type
in JavaScript). If the
classFilter
parameter is set to a filter predicate, then language builtins
are available and classes can be looked up if the filter predicate returns
true
for the fully qualified class name. If the filter returns
false
, then the class cannot be looked up and as a result throws a guest
language error when accessed. By default and if all
access
is false
, host class lookup is disabled. By default and if
all access
is true
, then all classes may be
looked up by the guest application.
In order to access class members looked up by the guest application a
host access policy
needs to be
set or all access
needs to be set to true
.
To load new classes the context uses the
hostClassLoader
if
specified or the context class loader
that will be
captured when the context is built
. If an explicit
engine
was specified, then the context class loader at engine
build-time
will be used instead. When the Java module
system is available (>= JDK 9) then only classes are accessible that are exported to the
unnamed module of the captured class loader.
public class MyClass { @HostAccess.Export public int accessibleMethod() { return 42; } public static void main(String[] args) { try (Context context = Context.newBuilder() // .allowHostClassLookup(c -> c.equals("myPackage.MyClass")) // .build()) { int result = context.eval("js", "" + "var MyClass = Java.type('myPackage.MyClass');" + "new MyClass().accessibleMethod()").asInt(); assert result == 42; } } }
permission
to look up the class myPackage.MyClass
in the guest language
application.
myPackage.MyClass
using the Java.type
builtin provided by the
JavaScript language implementation. Other classes can only be looked up if the provided
class filter returns true
for their name.
MyClass
by using the
JavaScript new
keyword.
accessibleMethod
which returns 42
. The
method is accessible to the guest language because because the enclosing class and the
declared method are public, as well as annotated with the
@HostAccess.Export
annotation. Which Java members of classes
are accessible can be configured using the host
access policy
.
classFilter
- a predicate that returns true
or false
for a
qualified Java class name or null
to disable host class lookup.allowHostClassLoading - to allow loading of classes.
,
allowHostAccess - to configure the access policy of
host values for guest languages.
public Context.Builder allowExperimentalOptions(boolean enabled)
false
(the default), then passing
an experimental option results in an IllegalArgumentException
when the context is
built.
Alternatively Engine.Builder.allowExperimentalOptions(boolean)
may be used when
constructing the context using an explicit engine
.
public Context.Builder allowPolyglotAccess(PolyglotAccess accessPolicy)
all
access
is true
then the default polyglot access policy is
PolyglotAccess.ALL
, otherwise PolyglotAccess.NONE
. The provided access
policy must not be null
.public Context.Builder allowValueSharing(boolean enabled)
value
between contexts. Value sharing is
enabled by default and is not affected by Context.Builder.allowAllAccess(boolean)
.
If this option is set to true
(default) then any value that is associated
with one context will be automatically migrated when passed to another context.
Primitive, host
and proxy
values can be migrated without limitation. When guest language values are migrated, they
capture and remember their original context. Guest language objects need to be accessed
when their respective context is entered
, therefore before any
access their original context is entered and subsequently left. Entering the original
context may fail, for example when the context of a original context value only allows
single threaded access to values or if it was closed
in the mean
time.
If this option is set to false
then any value passed from one context to
another will fail with an error indicating that sharing is disallowed. Turning sharing
off can be useful when strict safety is required and it would be considered an error if a
value of one context is passed to another.
Values of a guest language that are passed from one context to another are restricted to using the interoperability protocol only. In practice this often leads to slight changes and incompatibilities in behavior. For example, the prototype of a JavaScript object passed from one context to another is not writable, as the interoperability protocol does not allow such an operation (yet). A typical use-case is passing big immutable data structures that are infrequently accessed from one context to another, without copying them. In practice, passing values from one context to another should be avoided if possible, as their access is slower and their language compatibility reduced. This feature was introduced in 21.3. Older versions fail when guest values are passed from one context to another.
public Context.Builder allowInnerContextOptions(boolean enabled)
false
then inner contexts are
only allowed to use the same option values as its outer context. If this privilege is set
to true
then the context may modify option values for inner contexts it
creates. This privilege should not be enabled for security sensitive use-cases. The
default value for this privilege is inherited from Context.Builder.allowAllAccess(boolean)
.
Inner contexts are spawned by language implementations to implement language embeddings of their own. For example, some languages provide dedicated APIs to spawn language virtual machines. Such APIs are often implemented using the inner context mechanism.
Context.Builder.allowAllAccess(boolean)
public Context.Builder option(String key, String value)
context
. By default, any options for the
engine
, language
or
instrument
can be set for a context. If an
explicit engine
is set for the context, then only language
options can be set. Instrument and engine options can be set exclusively on the explicit
engine instance. If a language option is set for the context and the engine, then the
option of the context is going to take precedence.
If one of the set option keys or values is invalid, then an
IllegalArgumentException
is thrown when the context is built
.
The given key and value must not be null
.
To specify an option for the engine.
public Context.Builder options(Map<String,String> options)
options
using a map. All
values of the provided map must be non-null.options
- a map options.To set a single option.
public Context.Builder arguments(String language, String[] args)
context
.
Application arguments are typically made available to guest language implementations. It
depends on the language if and how they are accessible within the
evaluated
guest language scripts. Passing no arguments to a
language is equivalent to providing an empty arguments array.language
- the language id of the primary language.args
- an array of arguments passed to the guest language program.IllegalArgumentException
- if an invalid language id was specified.public Context.Builder allowIO(IOAccess ioAccess)
IOAccess.NONE
. If
all access
is set to true
, then
IOAccess.ALL
is used unless explicitly set. This method can be used to virtualize
file system access in the guest language by using an IOAccess
with a custom
filesystem.ioAccess
- the IO configurationContext.Builder
- to allow full host IO access
,
- to disable host IO access
,
- to create a custom configuration.
public Context.Builder serverTransport(MessageTransport serverTransport)
MessageTransport
to virtualize a transport of messages to a
server endpoint.
MessageTransport.open(java.net.URI, org.graalvm.polyglot.io.MessageEndpoint)
corresponds to accept of a server socket.serverTransport
- an implementation of message transport interceptorMessageTransport
public Context.Builder logHandler(Handler logHandler)
Handler
. The logger's Level
configuration is done
using the Context's options
. The level option key has the
following format: log.languageId.loggerName.level
or
log.instrumentId.loggerName.level
. The value is either the name of a pre-defined
Level
constant or a numeric Level
value. If not explicitly set in
options, the level is inherited from the parent logger.
Examples of setting log level options:
builder.option("log.level","FINE");
sets the FINE level
to all
TruffleLogger
s.
builder.option("log.js.level","FINE");
sets the FINE level
to
JavaScript TruffleLogger
s.
builder.option("log.js.com.oracle.truffle.js.parser.JavaScriptLanguage.level","FINE");
sets the FINE level
to TruffleLogger
for the
JavaScriptLanguage
class.
If the logHandler
is not set on Engine
nor on Context
, the log
messages are printed to Context's error output stream
.
logHandler
- the Handler
to use for logging in built Context
. The
passed logHandler
is closed when the context is closed
.Context.Builder
public Context.Builder timeZone(ZoneId zone)
null
then the system default
zone will be
used.Context.Builder
ZoneId.systemDefault()
public Context.Builder logHandler(OutputStream logOut)
Handler
using given OutputStream
. The logger's
Level
configuration is done using the Context's
options
. The level option key has the following format:
log.languageId.loggerName.level
or log.instrumentId.loggerName.level
. The
value is either the name of pre-defined Level
constant or a numeric Level
value. If not explicitly set in options the level is inherited from the parent logger.
Examples of setting log level options:
builder.option("log.level","FINE");
sets the FINE level
to all
TruffleLogger
s.
builder.option("log.js.level","FINE");
sets the FINE level
to
JavaScript TruffleLogger
s.
builder.option("log.js.com.oracle.truffle.js.parser.JavaScriptLanguage.level","FINE");
sets the FINE level
to TruffleLogger
for the
JavaScriptLanguage
class.
If the logHandler
is not set on Engine
nor on Context
the log
messages are printed to Context's standard output
stream
.
logOut
- the OutputStream
to use for logging in built Context
. The
passed logOut
stream is closed when the context is
closed
.Context.Builder
public Context.Builder allowCreateProcess(boolean enabled)
true
, allows guest language to execute external processes. Default is
false
. If all access
is set to
true
, then process creation is enabled if not denied explicitly.enabled
- true
to enable external process creationpublic Context.Builder processHandler(ProcessHandler handler)
ProcessHandler
responsible for external process creation.handler
- the handler to be installedpublic Context.Builder resourceLimits(ResourceLimits limits)
for usage examples
public Context.Builder sandbox(SandboxPolicy policy)
SandboxPolicy.TRUSTED
, there are no restrictions to the context configuration.SandboxPolicy
public Context.Builder allowEnvironmentAccess(EnvironmentAccess accessPolicy)
all access
is true
then the default environment access policy is
EnvironmentAccess.INHERIT
, otherwise EnvironmentAccess.NONE
. The provided
access policy must not be null
.accessPolicy
- the environment access policy
public Context.Builder environment(String name, String value)
name
- the environment variable namevalue
- the environment variable valuepublic Context.Builder environment(Map<String,String> env)
environment variables
using a map. All values of the provided map must be non-null.env
- environment variablesTo set a single environment variable.
public Context.Builder currentWorkingDirectory(Path workingDirectory)
FileSystem.setCurrentWorkingDirectory
method.workingDirectory
- the new current working directoryNullPointerException
- when workingDirectory
is null
IllegalArgumentException
- when workingDirectory
is a relative pathpublic Context.Builder hostClassLoader(ClassLoader classLoader)
classLoader
is used to load host
classes and it's also set as a context ClassLoader
during code execution. Otherwise the ClassLoader that was captured
when the context was built
is used to to load host classes and the
context ClassLoader
is not
set during code execution. Setting the hostClassLoader has a negative effect on enter and
leave performance.classLoader
- the host class loaderpublic Context.Builder useSystemExit(boolean enabled)
System.exit(int)
may be used to improve efficiency of stack
unwinding for context exit requested by the guest application.Context
public Context build()