public class PolyglotEngine extends Object
For a simple example see eval(Source). The
Truffle
Tutorial provides much more general information about Truffle. For information
specifically related to Java applications, the tutorial
Embedding
Truffle Languages in Java explains, with examples, how Java code can directly access
guest language functions, objects, classes, and some complex data structures with Java-typed
accessors. In the reverse direction, guest language code can access Java objects, classes, and
constructors.
Builder creates new engine instances and allows both
application- and language-specific configuration. The following example creates a default
instance.
PolyglotEngineengine =PolyglotEngine.newBuilder().build();
Languages are initialized on demand, the first time an engine evaluates code of a matching
MIME type. The engine throws an IllegalStateException
if no matching language is available. Languages remain initialized for the lifetime of the
engine.
Specific language environments can be configured, for example in response to command line
options, by building the engine with combinations of language-specific MIME-key-value settings (
Builder.config(String, String, Object)) and
pre-registered global symbols (Builder.globalSymbol(String, Object) )
Each language dynamically manages a namespace of global symbols. The engine provides its own
(static) namespace, configured when the engine is built (
Builder.globalSymbol(String, Object)).
An engine retrieves global symbols by name, first searching the engine's namespace, and then
searching all language namespaces in unspecified order, returning the first one found (
findGlobalSymbol(String)). Name collisions across namespaces
are possible and can only be discovered by explicitly retrieving all global symbols with a
particular name (findGlobalSymbols(String)).
Value instances. The content of a Value may be a boxed Java primitive type or a foreign object
(implemented internally as a TruffleObject). Foreign objects support a message-based
protocol for access to their contents, possibly including fields and methods.
Foreign objects may be functional, in which case cross-language method/procedure calls are
possible. Foreign calls return results wrapped in non-null Value instances.
Builder.globalSymbol(String, Object)) and
import them at any time (findGlobalSymbol(String)).
The content of a Value (for example retrieved from an
imported symbol) can be accessed (Value.as(Class)) as an object of
requested type (a kind of cross-langauge "cast"). A Value determined to be
functional can be called (Value.execute(Object...)) and will
return the result wrapped in a non-null Value instance.
Java clients export values in two ways:
Builder.globalSymbol(String, Object)), orValue.execute(Object...)).Value.get()) is preserved.
Exporting a Java object grants guest language access to the object's public fields and methods. Exporting a Java class grants guest language access to the class's public static fields and public constructors.
runtime they are
assocated with. A runtime is associated with one or more
polyglot engines. One runtime is a isolated
tenant on a host Virtual Machine. Other
than shared host resources such as memory, no aspects of program execution, language
environments, or global symbols are shared with other runtimes.
eval(Source)) is by default synchronous (performed
on the calling thread) and only permitted on the thread that created the engine.Builder.executor(Executor)) that performs executions on a different thread. In this case the
engine requires only that all executions are performed on the same thread.In contrast, instrumentation-based access to engine state is thread-safe, both from built-in services such as debugging and profiling as well as from external tools.
More information for language implementors.| Modifier and Type | Class and Description |
|---|---|
class |
PolyglotEngine.Builder
A builder for creating an engine instance.
|
class |
PolyglotEngine.Language
A handle for a Truffle language installed in a
PolyglotEngine. |
class |
PolyglotEngine.Value
A future value wrapper.
|
| Modifier and Type | Method and Description |
|---|---|
void |
dispose()
Disposes this engine instance and
releases all
resources allocated by languages active in this engine. |
PolyglotEngine.Value |
eval(Source source)
|
PolyglotEngine.Value |
findGlobalSymbol(String globalName)
Finds a global symbol by name.
|
Iterable<PolyglotEngine.Value> |
findGlobalSymbols(String globalName)
Finds all global symbols with a specified name by searching every language's
namespace of exported symbols, together with the the engine's namespace of
preconfigured symbols.
|
Map<String,? extends PolyglotEngine.Language> |
getLanguages()
Gets the map: MIME type --> metadata for the matching language
installed in this engine, whether or not the language has been initialized.
|
PolyglotRuntime |
getRuntime()
Access to associated runtime.
|
static PolyglotEngine.Builder |
newBuilder()
Returns a builder for creating an engine instance.
|
public static PolyglotEngine.Builder newBuilder()
build() step creates the engine and installs all
available languages. For example:
PolyglotEngineengine =PolyglotEngine.newBuilder().setOut(yourOutput) .setErr(yourOutput) .setIn(yourInput) .build();
public Map<String,? extends PolyglotEngine.Language> getLanguages()
public PolyglotRuntime getRuntime()
public PolyglotEngine.Value eval(Source source)
Language that
matches the code's MIME type. Source code is provided to the
engine as Source objects, which may wrap references to guest language code (e.g. a
filename or URL) or may represent code literally as in the example below. The engine returns
the result wrapped in an instance of Value, for which Java-typed access
(objects) can be created using Value.as(Class).
For sources marked asSourcesrc =Source.newBuilder("3 + 39"). mimeType("application/my-test-lang"). name("example.test-lang"). build();PolyglotEngineengine =PolyglotEngine.newBuilder().build();PolyglotEngine.Valueresult = engine.eval(src); int answer = result.as(Integer.class);
interactive, the engine will will do
more, depending the language. This may include printing the result, in a language-specific
format, to the engine's standard output. It might read
input values queried by the language.
This method is useful for Java applications that interoperate with guest languages. The general strategy is to evaluate guest language code that produces the desired language element and then create a Java object of the appropriate type for Java foreign access to the result. The tutorial "Embedding Truffle Languages in Java" contains examples.
source - guest language codePolyglotEngine.ValueIllegalStateException - if no installed language matches the code's MIME typeException - thrown to signal errors while processing the codePolyglotEngine.Value.as(Class)public void dispose()
releases all
resources allocated by languages active in this engine. If a default/private
runtime is configured for this engine then it is disposed automatically
with this engine.
Calling any other method on this instance after disposal throws an
IllegalStateException.
public PolyglotEngine.Value findGlobalSymbol(String globalName)
Symbol names are language dependent. Cross-language name collisions are possible, in which
case this method only returns one of them (use PolyglotEngine.findGlobalSymbols(String) to return
all of them).
globalName - a global symbol namenull if nonepublic Iterable<PolyglotEngine.Value> findGlobalSymbols(String globalName)
The following example shows how this method can be used to retrieve a single global symbol, while treating name collisions as an error.
staticPolyglotEngine.ValuefindAndReportMultipleExportedSymbols(PolyglotEngineengine,Stringname) {PolyglotEngine.Valuefound = null; for (PolyglotEngine.Valuevalue : engine.findGlobalSymbols(name)) { if (found != null) { throw newIllegalStateException( "Multiple global symbols exported with " + name + " name" ); } found = value; } return found; }
globalName - a global symbol name