public class PolyglotEngine.Builder extends Object
build() step creates the engine and installs all available
languages. For example:
PolyglotEngineengine =PolyglotEngine.newBuilder().setOut(yourOutput) .setErr(yourOutput) .setIn(yourInput) .build();
| Modifier and Type | Method and Description |
|---|---|
PolyglotEngine |
build()
Creates an
engine configured by builder methods. |
PolyglotEngine.Builder |
config(String mimeType,
String key,
Object value)
|
PolyglotEngine.Builder |
executor(Executor executor)
Provides an
Executor for running guest language code asynchronously, on a thread
other than the calling thread. |
PolyglotEngine.Builder |
globalSymbol(String name,
Object obj)
Adds a global symbol (named value) to be exported by the
engine
being built. |
PolyglotEngine.Builder |
runtime(PolyglotRuntime polyglotRuntime)
|
PolyglotEngine.Builder |
setErr(OutputStream os)
Configures error output for languages running in the
engine being
built, defaults to System.err. |
PolyglotEngine.Builder |
setIn(InputStream is)
|
PolyglotEngine.Builder |
setOut(OutputStream os)
Configures default output for languages running in the
engine
being built, defaults to System.out. |
public PolyglotEngine.Builder setOut(OutputStream os)
engine
being built, defaults to System.out.os - the stream to use as outputpublic PolyglotEngine.Builder setErr(OutputStream os)
engine being
built, defaults to System.err.os - the stream to use as outputpublic PolyglotEngine.Builder setIn(InputStream is)
is - the stream to use as inputpublic PolyglotEngine.Builder config(String mimeType, String key, Object value)
Language-specific initialization data to the engine being built. For example:
If the same key is specified multiple times for the same language, only the last one specified applies.String[] args = {"--kernel", "Kernel.som", "--instrument", "dyn-metrics"};PolyglotEngine.PolyglotEngine.Builderbuilder =PolyglotEngine.newBuilder(); builder.config(YourLang.MIME_TYPE, "CMD_ARGS", args);PolyglotEngineengine = builder.build();
mimeType - identification of the language to which the configuration data is
provided; any of the language's declared MIME types may be usedkey - to identify a language-specific configuration elementvalue - to parameterize initial state of a languagepublic PolyglotEngine.Builder globalSymbol(String name, Object obj)
engine
being built. Any guest Language can import this symbol, which
takes precedence over any symbols exported under the same name by languages. Any number
of symbols may be added; in case of name-collision only the last one added will be
exported. The namespace of exported global symbols is immutable once the engine is built.
See "Truffle-Java Interoperation" for the implications of exporting Java data to guest languages. The following example demonstrates the export of both a Java class and a Java object:
public static final class Multiplier {
public static int mul(int x, int y) {
return x * y;
}
}
public interface Multiply {
int mul(int x, int y);
}
public static PolyglotEngine configureJavaInterop(Multiply multiply) {
TruffleObject staticAccess = JavaInterop.asTruffleObject(Multiplier.class);
TruffleObject instanceAccess = JavaInterop.asTruffleObject(multiply);
PolyglotEngine engine = PolyglotEngine.newBuilder().
globalSymbol("mul", staticAccess).
globalSymbol("compose", instanceAccess).
build();
return engine;
}
The mul and compose objects are then available to any guest
language.name - name of the global symbol to registerobj - value of the symbol to exportIllegalArgumentException - if the object isn't of primitive type and cannot be
converted to TruffleObjectPolyglotEngine.findGlobalSymbol(String)public PolyglotEngine.Builder executor(Executor executor)
Executor for running guest language code asynchronously, on a thread
other than the calling thread.
By default engines execute both PolyglotEngine.eval(Source) and
PolyglotEngine.Value.execute(java.lang.Object...) synchronously in the calling thread.
A custom Executor is expected to perform every execution it is given (via
Executor.execute(Runnable)) in order of arrival. An arbitrary thread may be used,
but the engine requires that there be only one.
executor - the executor of code to be used by engine being
builtpublic PolyglotEngine.Builder runtime(PolyglotRuntime polyglotRuntime)
engine with an
execution runtime. By default each engine
gets its own private runtime. If the same runtime is used to
create multiple engines then resources and code might be cached
between these engines. A private/default or disposed runtime cannot be used to construct
an engine. If attempted an IllegalArgumentException is
thrown.
Sample usage:
PolyglotRuntimeruntime =PolyglotRuntime.newBuilder(). setOut(out).setErr(err).build();PolyglotEngine.Builderbuilder =PolyglotEngine.newBuilder(). runtime(runtime);PolyglotEngineengine1 = builder.build();PolyglotEngineengine2 = builder.build();PolyglotEngineengine3 = builder.build();
polyglotRuntime - an instance of runtime to associate this engine withPolyglotRuntimepublic PolyglotEngine build()
engine configured by builder methods.