annotate truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java @ 22494:cff4a9d3e72a

Enumerating allowed interop types
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 14 Dec 2015 08:46:35 +0100
parents e4dd15f04c7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
1 /*
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
4 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
10 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
15 * accompanied this code).
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
16 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
20 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
23 * questions.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
24 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
25 package com.oracle.truffle.api.vm;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
26
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
27 import java.io.Closeable;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
28 import java.io.IOException;
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
29 import java.io.InputStream;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
30 import java.io.OutputStream;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
31 import java.util.ArrayList;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
32 import java.util.Arrays;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
33 import java.util.Collections;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
34 import java.util.HashMap;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
35 import java.util.HashSet;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
36 import java.util.LinkedHashSet;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
37 import java.util.List;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
38 import java.util.Map;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
39 import java.util.Set;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
40 import java.util.concurrent.Executor;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
41 import java.util.logging.Logger;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
42
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
43 import com.oracle.truffle.api.CallTarget;
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
44 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
45 import com.oracle.truffle.api.Truffle;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
46 import com.oracle.truffle.api.TruffleLanguage;
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
47 import com.oracle.truffle.api.TruffleLanguage.Env;
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
48 import com.oracle.truffle.api.TruffleLanguage.Registration;
22333
1801b7f11c64 Using TruffleOptions.AOT rather than accessing the system property directly.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22325
diff changeset
49 import com.oracle.truffle.api.TruffleOptions;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
50 import com.oracle.truffle.api.debug.Debugger;
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
51 import com.oracle.truffle.api.debug.ExecutionEvent;
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
52 import com.oracle.truffle.api.debug.SuspendedEvent;
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
53 import com.oracle.truffle.api.frame.MaterializedFrame;
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
54 import com.oracle.truffle.api.frame.VirtualFrame;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
55 import com.oracle.truffle.api.impl.Accessor;
22215
d3bdaa91bc82 Truffle/Instrumentation: method signature on TruffleLanguage for getting each language's "default" ASTProber
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22214
diff changeset
56 import com.oracle.truffle.api.instrument.Instrumenter;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
57 import com.oracle.truffle.api.instrument.Probe;
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
58 import com.oracle.truffle.api.interop.ForeignAccess;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
59 import com.oracle.truffle.api.interop.TruffleObject;
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
60 import com.oracle.truffle.api.interop.java.JavaInterop;
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
61 import com.oracle.truffle.api.nodes.Node;
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
62 import com.oracle.truffle.api.nodes.RootNode;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
63 import com.oracle.truffle.api.source.Source;
22479
e1d15415bb2e Truffle: reverse import reordering change
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22478
diff changeset
64 import java.util.WeakHashMap;
e1d15415bb2e Truffle: reverse import reordering change
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22478
diff changeset
65 import java.util.logging.Level;
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
66
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
67 /**
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
68 * Gate way into the world of {@link TruffleLanguage Truffle languages}. {@link #buildNew()
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
69 * Instantiate} your own portal into the isolated, multi language system with all the registered
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
70 * languages ready for your use. A {@link PolyglotEngine} runs inside of a <em>JVM</em>, there can
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
71 * however be multiple instances (some would say tenants) of {@link PolyglotEngine} running next to
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
72 * each other in a single <em>JVM</em> with a complete mutual isolation. There is 1:N mapping
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
73 * between <em>JVM</em> and {@link PolyglotEngine}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
74 * <p>
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
75 * It would not be correct to think of a {@link PolyglotEngine} as a runtime for a single
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
76 * {@link TruffleLanguage Truffle language} (Ruby, Python, R, C, JavaScript, etc.) either.
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
77 * {@link PolyglotEngine} can host as many of Truffle languages as {@link Registration registered on
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
78 * a class path} of your <em>JVM</em> application. {@link PolyglotEngine} orchestrates these
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
79 * languages, manages exchange of objects and calls among them. While it may happen that there is
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
80 * just one activated language inside of a {@link PolyglotEngine}, the greatest strength of
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
81 * {@link PolyglotEngine} is in inter-operability between all Truffle languages. There is 1:N
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
82 * mapping between {@link PolyglotEngine} and {@link TruffleLanguage Truffle language
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
83 * implementations}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
84 * <p>
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
85 * Use {@link #buildNew()} to create new isolated portal ready for execution of various languages.
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
86 * All the languages in a single portal see each other exported global symbols and can cooperate.
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
87 * Use {@link #buildNew()} multiple times to create different, isolated portal environment
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
88 * completely separated from each other.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
89 * <p>
22325
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
90 * Once instantiated use {@link #eval(com.oracle.truffle.api.source.Source)} with a reference to a
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
91 * file or URL or directly pass code snippet into the virtual machine via
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
92 * {@link #eval(com.oracle.truffle.api.source.Source)}. Support for individual languages is
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
93 * initialized on demand - e.g. once a file of certain MIME type is about to be processed, its
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
94 * appropriate engine (if found), is initialized. Once an engine gets initialized, it remains so,
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
95 * until the virtual machine isn't garbage collected.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
96 * <p>
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
97 * The engine is single-threaded and tries to enforce that. It records the thread it has been
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
98 * {@link Builder#build() created} by and checks that all subsequent calls are coming from the same
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
99 * thread. There is 1:1 mapping between {@link PolyglotEngine} and a thread that can tell it what to
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
100 * do.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
101 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
102 @SuppressWarnings("rawtypes")
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
103 public class PolyglotEngine {
22333
1801b7f11c64 Using TruffleOptions.AOT rather than accessing the system property directly.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22325
diff changeset
104 static final boolean JAVA_INTEROP_ENABLED = !TruffleOptions.AOT;
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
105 static final Logger LOG = Logger.getLogger(PolyglotEngine.class.getName());
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
106 private static final SPIAccessor SPI = new SPIAccessor();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
107 private final Thread initThread;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
108 private final Executor executor;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
109 private final Map<String, Language> langs;
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
110 private final InputStream in;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
111 private final OutputStream err;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
112 private final OutputStream out;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
113 private final EventConsumer<?>[] handlers;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
114 private final Map<String, Object> globals;
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
115 private final Instrumenter instrumenter;
22238
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
116 private final Debugger debugger;
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
117 private boolean disposed;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
118
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
119 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
120 * Private & temporary only constructor.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
121 */
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
122 PolyglotEngine() {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
123 this.initThread = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
124 this.in = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
125 this.err = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
126 this.out = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
127 this.langs = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
128 this.handlers = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
129 this.globals = null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
130 this.executor = null;
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
131 this.instrumenter = null;
22238
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
132 this.debugger = null;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
133 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
134
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
135 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
136 * Real constructor used from the builder.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
137 */
22174
0bcfe8c6088f The central API to invoke various programs is now PolyglotEngine - a polyglot-ready enhancement of JDK's ScriptEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167 22173
diff changeset
138 PolyglotEngine(Executor executor, Map<String, Object> globals, OutputStream out, OutputStream err, InputStream in, EventConsumer<?>[] handlers) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
139 this.executor = executor;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
140 this.out = out;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
141 this.err = err;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
142 this.in = in;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
143 this.handlers = handlers;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
144 this.initThread = Thread.currentThread();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
145 this.globals = new HashMap<>(globals);
22220
20380d1d41f2 Truffle/Instrumentation: second merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22219
diff changeset
146 this.instrumenter = SPI.createInstrumenter(this);
22238
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
147 this.debugger = SPI.createDebugger(this, this.instrumenter);
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
148 Map<String, Language> map = new HashMap<>();
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
149 /* We want to create a language instance but per LanguageCache and not per mime type. */
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
150 Set<LanguageCache> uniqueCaches = new HashSet<>(LanguageCache.languages().values());
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
151 for (LanguageCache languageCache : uniqueCaches) {
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
152 Language newLanguage = new Language(languageCache);
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
153 for (String mimeType : newLanguage.getMimeTypes()) {
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
154 map.put(mimeType, newLanguage);
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
155 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
156 }
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
157 this.langs = map;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
158 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
159
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
160 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
161 * Creation of new Truffle virtual machine. Use the {@link Builder} methods to configure your
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
162 * virtual machine and then create one using {@link Builder#build()}:
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
163 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
164 * <pre>
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
165 * {@link PolyglotEngine} vm = {@link PolyglotEngine}.{@link PolyglotEngine#buildNew() buildNew()}
22167
0480c4873a4a Fixing Javadoc, imports and adding suppression to methods that are being deprecated
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22165
diff changeset
166 * .{@link Builder#setOut(java.io.OutputStream) setOut}({@link OutputStream yourOutput})
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
167 * .{@link Builder#setErr(java.io.OutputStream) setErr}({@link OutputStream yourOutput})
22167
0480c4873a4a Fixing Javadoc, imports and adding suppression to methods that are being deprecated
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22165
diff changeset
168 * .{@link Builder#setIn(java.io.InputStream) setIn}({@link InputStream yourInput})
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
169 * .{@link Builder#build() build()};
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
170 * </pre>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
171 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
172 * It searches for {@link Registration languages registered} in the system class loader and
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
173 * makes them available for later evaluation via
22325
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
174 * {@link #eval(com.oracle.truffle.api.source.Source)} method.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
175 *
22335
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
176 * @return new builder to create isolated polyglot engine with pre-registered languages
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
177 */
22335
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
178 public static PolyglotEngine.Builder newBuilder() {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
179 // making Builder non-static inner class is a
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
180 // nasty trick to avoid the Builder class to appear
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
181 // in Javadoc next to PolyglotEngine class
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
182 PolyglotEngine vm = new PolyglotEngine();
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
183 return vm.new Builder();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
184 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
185
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
186 /**
22335
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
187 * @return new builder
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
188 * @deprecated use {@link #newBuilder()}
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
189 */
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
190 @Deprecated
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
191 public static PolyglotEngine.Builder buildNew() {
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
192 return newBuilder();
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
193 }
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
194
906a5f6e07cc Giving the PolyglotEngine builder factory method more sensible name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22334
diff changeset
195 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
196 * Builder for a new {@link PolyglotEngine}. Call various configuration methods in a chain and
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
197 * at the end create new {@link PolyglotEngine virtual machine}:
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
198 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
199 * <pre>
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
200 * {@link PolyglotEngine} vm = {@link PolyglotEngine}.{@link PolyglotEngine#buildNew() buildNew()}
22167
0480c4873a4a Fixing Javadoc, imports and adding suppression to methods that are being deprecated
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22165
diff changeset
201 * .{@link Builder#setOut(java.io.OutputStream) setOut}({@link OutputStream yourOutput})
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
202 * .{@link Builder#setErr(java.io.OutputStream) setErr}({@link OutputStream yourOutput})
22167
0480c4873a4a Fixing Javadoc, imports and adding suppression to methods that are being deprecated
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22165
diff changeset
203 * .{@link Builder#setIn(java.io.InputStream) setIn}({@link InputStream yourInput})
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
204 * .{@link Builder#build() build()};
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
205 * </pre>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
206 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
207 public class Builder {
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
208 private OutputStream out;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
209 private OutputStream err;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
210 private InputStream in;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
211 private final List<EventConsumer<?>> handlers = new ArrayList<>();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
212 private final Map<String, Object> globals = new HashMap<>();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
213 private Executor executor;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
214
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
215 Builder() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
216 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
217
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
218 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
219 * Changes the default output for languages running in <em>to be created</em>
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
220 * {@link PolyglotEngine virtual machine}. The default is to use {@link System#out}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
221 *
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
222 * @param os the stream to use as output
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
223 * @return instance of this builder
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
224 */
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
225 public Builder setOut(OutputStream os) {
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
226 out = os;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
227 return this;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
228 }
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
229
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
230 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
231 * Changes the error output for languages running in <em>to be created</em>
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
232 * {@link PolyglotEngine virtual machine}. The default is to use {@link System#err}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
233 *
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
234 * @param os the stream to use as output
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
235 * @return instance of this builder
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
236 */
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
237 public Builder setErr(OutputStream os) {
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
238 err = os;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
239 return this;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
240 }
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
241
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
242 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
243 * Changes the default input for languages running in <em>to be created</em>
22174
0bcfe8c6088f The central API to invoke various programs is now PolyglotEngine - a polyglot-ready enhancement of JDK's ScriptEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167 22173
diff changeset
244 * {@link PolyglotEngine virtual machine}. The default is to use {@link System#in}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
245 *
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
246 * @param is the stream to use as input
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
247 * @return instance of this builder
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
248 */
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
249 public Builder setIn(InputStream is) {
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
250 in = is;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
251 return this;
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
252 }
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
253
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
254 /**
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
255 * Registers another instance of {@link EventConsumer} into the to be created
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
256 * {@link PolyglotEngine}.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
257 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
258 * @param handler the handler to register
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
259 * @return instance of this builder
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
260 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
261 public Builder onEvent(EventConsumer<?> handler) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
262 handler.getClass();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
263 handlers.add(handler);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
264 return this;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
265 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
266
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
267 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
268 * Adds global named symbol into the configuration of to-be-built {@link PolyglotEngine}.
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
269 * This symbol will be accessible to all languages via
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
270 * {@link Env#importSymbol(java.lang.String)} and will take precedence over
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
271 * {@link TruffleLanguage#findExportedSymbol symbols exported by languages itself}. Repeated
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
272 * use of <code>globalSymbol</code> is possible; later definition of the same name overrides
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
273 * the previous one.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
274 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
275 * @param name name of the symbol to register
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
276 * @param obj value of the object - expected to be primitive wrapper, {@link String} or
22334
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
277 * <code>TruffleObject</code> for mutual inter-operability. If the object isn't
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
278 * of the previous types, the system tries to wrap it using
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
279 * {@link JavaInterop#asTruffleObject(java.lang.Object)}, if available
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
280 * @return instance of this builder
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
281 * @see PolyglotEngine#findGlobalSymbol(java.lang.String)
22334
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
282 * @throws IllegalArgumentException if the object isn't of primitive type and cannot be
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
283 * converted to {@link TruffleObject}
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
284 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
285 public Builder globalSymbol(String name, Object obj) {
22196
364e3f024643 Java objects passed into globalSymbol should be converted into something that Truffle languages can understand - e.g. TruffleObject instances.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22194
diff changeset
286 final Object truffleReady;
22344
ed3b78bbfa81 TruffleObject is truffleReady
Mick Jordan <mick.jordan@oracle.com>
parents: 22335
diff changeset
287 if (obj instanceof TruffleObject || obj instanceof Number || obj instanceof String || obj instanceof Character || obj instanceof Boolean) {
22196
364e3f024643 Java objects passed into globalSymbol should be converted into something that Truffle languages can understand - e.g. TruffleObject instances.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22194
diff changeset
288 truffleReady = obj;
364e3f024643 Java objects passed into globalSymbol should be converted into something that Truffle languages can understand - e.g. TruffleObject instances.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22194
diff changeset
289 } else {
22334
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
290 if (JAVA_INTEROP_ENABLED) {
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
291 truffleReady = JavaInterop.asTruffleObject(obj);
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
292 } else {
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
293 throw new IllegalArgumentException();
c12a7960bd7b Java interop code reachable without checking JAVA_INITEROP_ENABLED
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22333
diff changeset
294 }
22196
364e3f024643 Java objects passed into globalSymbol should be converted into something that Truffle languages can understand - e.g. TruffleObject instances.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22194
diff changeset
295 }
364e3f024643 Java objects passed into globalSymbol should be converted into something that Truffle languages can understand - e.g. TruffleObject instances.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22194
diff changeset
296 globals.put(name, truffleReady);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
297 return this;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
298 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
299
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
300 /**
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
301 * Provides own executor for running {@link PolyglotEngine} scripts. By default
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
302 * {@link PolyglotEngine#eval(com.oracle.truffle.api.source.Source)} and
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
303 * {@link Value#invoke(java.lang.Object, java.lang.Object[])} are executed synchronously in
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
304 * the calling thread. Sometimes, however it is more beneficial to run them asynchronously -
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
305 * the easiest way to do so is to provide own executor when configuring the {
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
306 * {@link #executor(java.util.concurrent.Executor) the builder}. The executor is expected to
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
307 * execute all {@link Runnable runnables} passed into its
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
308 * {@link Executor#execute(java.lang.Runnable)} method in the order they arrive and in a
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
309 * single (yet arbitrary) thread.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
310 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
311 * @param executor the executor to use for internal execution inside the {@link #build() to
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
312 * be created} {@link PolyglotEngine}
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
313 * @return instance of this builder
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
314 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
315 @SuppressWarnings("hiding")
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
316 public Builder executor(Executor executor) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
317 this.executor = executor;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
318 return this;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
319 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
320
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
321 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
322 * Creates the {@link PolyglotEngine Truffle virtual machine}. The configuration is taken
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
323 * from values passed into configuration methods in this class.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
324 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
325 * @return new, isolated virtual machine with pre-registered languages
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
326 */
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
327 public PolyglotEngine build() {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
328 if (out == null) {
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
329 out = System.out;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
330 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
331 if (err == null) {
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
332 err = System.err;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
333 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
334 if (in == null) {
22165
67f75f61c974 Certain languages (like FastR) prefer access to raw byte streams. Offering it. One always has an option to wrap Input and Output Streams into character based Readers and Writers
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
335 in = System.in;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
336 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
337 return new PolyglotEngine(executor, globals, out, err, in, handlers.toArray(new EventConsumer[0]));
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
338 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
339 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
340
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
341 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
342 * Descriptions of languages supported in this Truffle virtual machine.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
343 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
344 * @return an immutable map with keys being MIME types and values the {@link Language
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
345 * descriptions} of associated languages
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
346 */
22169
307a4ed5430e Two more changes to guarantee better compatibility with TruffleVM
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22168
diff changeset
347 public Map<String, ? extends Language> getLanguages() {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
348 return Collections.unmodifiableMap(langs);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
349 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
350
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
351 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
352 * Evaluates provided source. Chooses language registered for a particular
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
353 * {@link Source#getMimeType() MIME type} (throws {@link IOException} if there is none). The
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
354 * language is then allowed to parse and execute the source.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
355 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
356 * @param source code snippet to execute
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
357 * @return a {@link Value} object that holds result of an execution, never <code>null</code>
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
358 * @throws IOException thrown to signal errors while processing the code
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
359 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
360 public Value eval(Source source) throws IOException {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
361 String mimeType = source.getMimeType();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
362 checkThread();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
363 Language l = langs.get(mimeType);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
364 if (l == null) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
365 throw new IOException("No language for MIME type " + mimeType + " found. Supported types: " + langs.keySet());
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
366 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
367 return eval(l, source);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
368 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
369
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
370 /**
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
371 * Dispose instance of this engine. A user can explicitly
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
372 * {@link TruffleLanguage#disposeContext(java.lang.Object) dispose all resources} allocated by
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
373 * the languages active in this engine, when it is known the system is not going to be used in
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
374 * the future.
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
375 * <p>
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
376 * Calling any other method of this class after the dispose has been done yields an
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
377 * {@link IllegalStateException}.
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
378 */
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
379 public void dispose() {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
380 checkThread();
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
381 disposed = true;
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
382 ComputeInExecutor<Void> compute = new ComputeInExecutor<Void>(executor) {
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
383 @Override
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
384 protected Void compute() throws IOException {
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
385 for (Language language : getLanguages().values()) {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
386 TruffleLanguage<?> impl = language.getImpl(false);
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
387 if (impl != null) {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
388 try {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
389 SPI.dispose(impl, language.getEnv(true));
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
390 } catch (Exception | Error ex) {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
391 LOG.log(Level.SEVERE, "Error disposing " + impl, ex);
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
392 }
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
393 }
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
394 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
395 return null;
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
396 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
397 };
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
398 try {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
399 compute.perform();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
400 } catch (IOException ex) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
401 throw new IllegalStateException(ex);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
402 }
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
403 }
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
404
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
405 private Value eval(final Language l, final Source s) throws IOException {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
406 final TruffleLanguage[] lang = {null};
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
407 ComputeInExecutor<Object> compute = new ComputeInExecutor<Object>(executor) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
408 @Override
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
409 protected Object compute() throws IOException {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
410 return evalImpl(lang, s, l);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
411 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
412 };
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
413 compute.perform();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
414 return new Value(lang, compute);
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
415 }
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
416
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
417 Language createLanguage(Map.Entry<String, LanguageCache> en) {
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
418 return new Language(en.getValue());
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
419 }
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
420
22132
19205292b0c7 Suppressing "try" lints
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22129
diff changeset
421 @SuppressWarnings("try")
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
422 private Object evalImpl(TruffleLanguage<?>[] fillLang, Source s, Language l) throws IOException {
22275
141fe31da7a2 Avoiding check for current stack depth on the initialization of debugger. Only when debugger is suspended the real check is needed.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22274
diff changeset
423 try (Closeable d = SPI.executionStart(this, -1, debugger, s)) {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
424 TruffleLanguage<?> langImpl = l.getImpl(true);
22133
c334865b9d42 Make sure TruffleLanguage.createEnv is only called when Accessor.CURRENT_VM is known
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22132
diff changeset
425 fillLang[0] = langImpl;
22386
086776aa7548 Caching Source -> CallTarget mapping per PolyglotEngine.Language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22374
diff changeset
426 return SPI.eval(langImpl, s, l.cache);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
427 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
428 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
429
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
430 @SuppressWarnings("try")
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
431 final Object invokeForeign(final Node foreignNode, VirtualFrame frame, final TruffleObject receiver) throws IOException {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
432 Object res;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
433 if (executor == null) {
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
434 try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, debugger, null)) {
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
435 final Object[] args = ForeignAccess.getArguments(frame).toArray();
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
436 res = ForeignAccess.execute(foreignNode, frame, receiver, args);
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
437 }
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
438 } else {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
439 res = invokeForeignOnExecutor(foreignNode, frame, receiver);
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
440 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
441 if (res instanceof TruffleObject) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
442 return new EngineTruffleObject(this, (TruffleObject) res);
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
443 } else {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
444 return res;
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
445 }
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
446 }
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
447
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
448 @TruffleBoundary
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
449 private Object invokeForeignOnExecutor(final Node foreignNode, VirtualFrame frame, final TruffleObject receiver) throws IOException {
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
450 final MaterializedFrame materialized = frame.materialize();
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
451 ComputeInExecutor<Object> compute = new ComputeInExecutor<Object>(executor) {
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
452 @SuppressWarnings("try")
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
453 @Override
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
454 protected Object compute() throws IOException {
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
455 try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, debugger, null)) {
22323
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
456 final Object[] args = ForeignAccess.getArguments(materialized).toArray();
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
457 RootNode node = SymbolInvokerImpl.createTemporaryRoot(TruffleLanguage.class, foreignNode, receiver, args.length);
096e2c0fd2dc Cannot pass VirtualFrame into a Runnable. Either invoke the call directly or materialize the frame
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22314
diff changeset
458 final CallTarget target = Truffle.getRuntime().createCallTarget(node);
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
459 return target.call(args);
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
460 }
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
461 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
462 };
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
463 return compute.get();
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
464 }
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
465
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
466 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
467 * Looks global symbol provided by one of initialized languages up. First of all execute your
22325
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
468 * program via one of your {@link #eval(com.oracle.truffle.api.source.Source)} and then look
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
469 * expected symbol up using this method.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
470 * <p>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
471 * The names of the symbols are language dependent, but for example the Java language bindings
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
472 * follow the specification for method references:
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
473 * <ul>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
474 * <li>"java.lang.Exception::new" is a reference to constructor of {@link Exception}
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
475 * <li>"java.lang.Integer::valueOf" is a reference to static method in {@link Integer} class
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
476 * </ul>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
477 * Once an symbol is obtained, it remembers values for fast access and is ready for being
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
478 * invoked.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
479 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
480 * @param globalName the name of the symbol to find
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
481 * @return found symbol or <code>null</code> if it has not been found
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
482 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
483 public Value findGlobalSymbol(final String globalName) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
484 checkThread();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
485 final TruffleLanguage<?>[] lang = {null};
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
486 ComputeInExecutor<Object> compute = new ComputeInExecutor<Object>(executor) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
487 @Override
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
488 protected Object compute() throws IOException {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
489 Object obj = globals.get(globalName);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
490 if (obj == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
491 for (Language dl : langs.values()) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
492 TruffleLanguage.Env env = dl.getEnv(false);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
493 if (env == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
494 continue;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
495 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
496 obj = SPI.findExportedSymbol(env, globalName, true);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
497 if (obj != null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
498 lang[0] = dl.getImpl(true);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
499 break;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
500 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
501 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
502 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
503 if (obj == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
504 for (Language dl : langs.values()) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
505 TruffleLanguage.Env env = dl.getEnv(false);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
506 if (env == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
507 continue;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
508 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
509 obj = SPI.findExportedSymbol(env, globalName, true);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
510 if (obj != null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
511 lang[0] = dl.getImpl(true);
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
512 break;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
513 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
514 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
515 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
516 return obj;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
517 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
518 };
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
519 try {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
520 compute.perform();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
521 if (compute.get() == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
522 return null;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
523 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
524 } catch (IOException ex) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
525 // OK, go on
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
526 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
527 return new Value(lang, compute);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
528 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
529
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
530 private void checkThread() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
531 if (initThread != Thread.currentThread()) {
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
532 throw new IllegalStateException("PolyglotEngine created on " + initThread.getName() + " but used on " + Thread.currentThread().getName());
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
533 }
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
534 if (disposed) {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
535 throw new IllegalStateException("Engine has already been disposed");
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
536 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
537 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
538
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
539 @SuppressWarnings("unchecked")
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
540 void dispatch(Object ev) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
541 Class type = ev.getClass();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
542 if (type == SuspendedEvent.class) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
543 dispatchSuspendedEvent((SuspendedEvent) ev);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
544 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
545 if (type == ExecutionEvent.class) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
546 dispatchExecutionEvent((ExecutionEvent) ev);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
547 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
548 dispatch(type, ev);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
549 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
550
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
551 @SuppressWarnings("unused")
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
552 void dispatchSuspendedEvent(SuspendedEvent event) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
553 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
554
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
555 @SuppressWarnings("unused")
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
556 void dispatchExecutionEvent(ExecutionEvent event) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
557 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
558
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
559 @SuppressWarnings("unchecked")
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
560 <Event> void dispatch(Class<Event> type, Event event) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
561 for (EventConsumer handler : handlers) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
562 if (handler.type == type) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
563 handler.on(event);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
564 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
565 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
566 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
567
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
568 /**
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
569 * A future value wrapper. A user level wrapper around values returned by evaluation of various
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
570 * {@link PolyglotEngine} functions like
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
571 * {@link PolyglotEngine#findGlobalSymbol(java.lang.String)} and
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
572 * {@link PolyglotEngine#eval(com.oracle.truffle.api.source.Source)} or a value returned by
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
573 * {@link #invoke(java.lang.Object, java.lang.Object...) a subsequent execution}. In case the
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
574 * {@link PolyglotEngine} has been initialized for
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
575 * {@link Builder#executor(java.util.concurrent.Executor) asynchronous execution}, the
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
576 * {@link Value} represents a future - i.e., it is returned immediately, leaving the execution
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
577 * running on behind.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
578 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
579 public class Value {
22305
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
580 private final TruffleLanguage<?>[] language;
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
581 private final ComputeInExecutor<Object> compute;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
582 private CallTarget target;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
583
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
584 Value(TruffleLanguage<?>[] language, ComputeInExecutor<Object> compute) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
585 this.language = language;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
586 this.compute = compute;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
587 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
588
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
589 Value(TruffleLanguage<?>[] language, final Object value) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
590 this.language = language;
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
591 this.compute = new ComputeInExecutor<Object>(null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
592 @Override
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
593 protected Object compute() throws IOException {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
594 return value;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
595 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
596 };
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
597 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
598
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
599 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
600 * Obtains the object represented by this symbol. The <em>raw</em> object can either be a
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
601 * wrapper about primitive type (e.g. {@link Number}, {@link String}, {@link Character},
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
602 * {@link Boolean}) or a <em>TruffleObject</em> representing more complex object from a
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
603 * language. The method can return <code>null</code>.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
604 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
605 * @return the object or <code>null</code>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
606 * @throws IOException in case it is not possible to obtain the value of the object
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
607 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
608 public Object get() throws IOException {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
609 Object result = waitForSymbol();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
610 if (result instanceof TruffleObject) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
611 return new EngineTruffleObject(PolyglotEngine.this, (TruffleObject) result);
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
612 } else {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
613 return result;
22200
7abcbeb12d08 Creating a TruffleObject that knows its PolyglotEngine and wrapping all values returned from the engine by it, so it is always clear into which engine context an object belongs.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22196
diff changeset
614 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
615 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
616
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
617 /**
22135
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
618 * Obtains Java view of the object represented by this symbol. The method basically
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
619 * delegates to
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
620 * {@link JavaInterop#asJavaObject(java.lang.Class, com.oracle.truffle.api.interop.TruffleObject)}
22284
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
621 * . The method handles primitive types (like {@link Number}, etc.) by casting and returning
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
622 * them. When a {@link String}.<code>class</code> is requested, the method let's the
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
623 * language that produced the value to do the
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
624 * {@link TruffleLanguage#toString(java.lang.Object, java.lang.Object) necessary formating}.
22194
7afb5e5cc3ca Fix use just one language instance per LanguageCache instead of one per mime-type.
Christian Humer <christian.humer@oracle.com>
parents: 22186
diff changeset
625 *
22135
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
626 * @param <T> the type of the view one wants to obtain
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
627 * @param representation the class of the view interface (it has to be an interface)
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
628 * @return instance of the view wrapping the object of this symbol
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
629 * @throws IOException in case it is not possible to obtain the value of the object
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
630 * @throws ClassCastException if the value cannot be converted to desired view
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
631 */
22312
8322a904a472 Verifying the Truffle/Java interop works OK also in asynchronous mode
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22311
diff changeset
632 public <T> T as(final Class<T> representation) throws IOException {
8322a904a472 Verifying the Truffle/Java interop works OK also in asynchronous mode
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22311
diff changeset
633 final Object obj = get();
22274
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
634 if (obj instanceof EngineTruffleObject) {
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
635 EngineTruffleObject eto = (EngineTruffleObject) obj;
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
636 if (representation.isInstance(eto.getDelegate())) {
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
637 return representation.cast(eto.getDelegate());
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
638 }
371045b1312d Be able to extract the original type of TruffleObject from PolyglotEngine.Value via its as method.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22268
diff changeset
639 }
22284
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
640 if (representation == String.class) {
22305
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
641 final Class<? extends TruffleLanguage> clazz = language[0].getClass();
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
642 Object unwrapped = obj;
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
643 while (unwrapped instanceof EngineTruffleObject) {
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
644 unwrapped = ((EngineTruffleObject) obj).getDelegate();
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
645 }
ae5c160bd047 Fixed TruffleLanguage was null when using Value#as with asynchronous evaluation; Unbox EngineTruffleObject before casting to string.
Christian Humer <christian.humer@oracle.com>
parents: 22304
diff changeset
646 return representation.cast(SPI.toString(language[0], findEnv(clazz), unwrapped));
22284
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
647 }
22135
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
648 if (representation.isInstance(obj)) {
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
649 return representation.cast(obj);
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
650 }
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
651 if (JAVA_INTEROP_ENABLED) {
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
652 return JavaInterop.asJavaObject(representation, (TruffleObject) obj);
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
653 }
22311
3f2737e9e4e1 The method documentation says one should throw ClassCastException when the value cannot be converted
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22305
diff changeset
654 throw new ClassCastException("Value cannot be represented as " + representation.getName());
22135
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
655 }
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
656
e70b20f4bb00 Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22133
diff changeset
657 /**
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
658 * Invokes the symbol. If the symbol represents a function, then it should be invoked with
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
659 * provided arguments. If the symbol represents a field, then first argument (if provided)
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
660 * should set the value to the field; the return value should be the actual value of the
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
661 * field when the <code>invoke</code> method returns.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
662 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
663 * @param thiz this/self in language that support such concept; use <code>null</code> to let
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
664 * the language use default this/self or ignore the value
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
665 * @param args arguments to pass when invoking the symbol
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
666 * @return symbol wrapper around the value returned by invoking the symbol, never
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
667 * <code>null</code>
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
668 * @throws IOException signals problem during execution
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
669 */
22436
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
670 @Deprecated
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
671 public Value invoke(final Object thiz, final Object... args) throws IOException {
22436
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
672 return execute(args);
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
673 }
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
674
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
675 /**
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
676 * Executes the symbol. If the symbol represents a function, then it should be invoked with
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
677 * provided arguments. If the symbol represents a field, then first argument (if provided)
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
678 * should set the value to the field; the return value should be the actual value of the
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
679 * field when the <code>invoke</code> method returns.
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
680 *
22494
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
681 * @param args arguments to pass when invoking the symbol; either wrappers of Java primitive
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
682 * types (e.g. {@link java.lang.Byte}, {@link java.lang.Short},
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
683 * {@link java.lang.Integer}, {@link java.lang.Long}, {@link java.lang.Float},
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
684 * {@link java.lang.Double}, {@link java.lang.Character},
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
685 * {@link java.lang.Boolean}, and {@link java.lang.String}) or a
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
686 * {@link TruffleObject object created} by one of the languages)
cff4a9d3e72a Enumerating allowed interop types
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22482
diff changeset
687 *
22436
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
688 * @return symbol wrapper around the value returned by invoking the symbol, never
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
689 * <code>null</code>
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
690 * @throws IOException signals problem during execution
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
691 */
46a6d3eb790c Adopt TCK and Polyglot after changes to execute and invoke message
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22386
diff changeset
692 public Value execute(final Object... args) throws IOException {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
693 get();
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
694 ComputeInExecutor<Object> invokeCompute = new ComputeInExecutor<Object>(executor) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
695 @SuppressWarnings("try")
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
696 @Override
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
697 protected Object compute() throws IOException {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
698 try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, debugger, null)) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
699 List<Object> arr = new ArrayList<>();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
700 arr.addAll(Arrays.asList(args));
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
701 for (;;) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
702 try {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
703 if (target == null) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
704 target = SymbolInvokerImpl.createCallTarget(language[0], compute.get(), arr.toArray());
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
705 }
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
706 return target.call(arr.toArray());
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
707 } catch (ArgumentsMishmashException ex) {
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
708 target = null;
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
709 }
22179
ee9ae440f1eb Testing behavior of invoke when calling asTruffleFunction
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22174
diff changeset
710 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
711 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
712 }
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
713 };
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
714 invokeCompute.perform();
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
715 return new Value(language, invokeCompute);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
716 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
717
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
718 private Object waitForSymbol() throws IOException {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
719 checkThread();
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
720 return compute.get();
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
721 }
22282
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
722
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
723 @Override
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
724 public String toString() {
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
725 return "PolyglotEngine.Value[" + compute + "]";
22282
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
726 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
727 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
728
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
729 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
730 * Description of a language registered in {@link PolyglotEngine Truffle virtual machine}.
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
731 * Languages are registered by {@link Registration} annotation which stores necessary
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
732 * information into a descriptor inside of the language's JAR file. When a new
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
733 * {@link PolyglotEngine} is created, it reads all available descriptors and creates
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
734 * {@link Language} objects to represent them. One can obtain a {@link #getName() name} or list
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
735 * of supported {@link #getMimeTypes() MIME types} for each language. The actual language
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
736 * implementation is not initialized until
22325
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
737 * {@link PolyglotEngine#eval(com.oracle.truffle.api.source.Source) a code is evaluated} in it.
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
738 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
739 public class Language {
22386
086776aa7548 Caching Source -> CallTarget mapping per PolyglotEngine.Language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22374
diff changeset
740 private final Map<Source, CallTarget> cache;
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
741 private final LanguageCache info;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
742 private TruffleLanguage.Env env;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
743
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
744 Language(LanguageCache info) {
22386
086776aa7548 Caching Source -> CallTarget mapping per PolyglotEngine.Language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22374
diff changeset
745 this.cache = new WeakHashMap<>();
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
746 this.info = info;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
747 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
748
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
749 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
750 * MIME types recognized by the language.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
751 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
752 * @return returns immutable set of recognized MIME types
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
753 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
754 public Set<String> getMimeTypes() {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
755 return info.getMimeTypes();
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
756 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
757
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
758 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
759 * Human readable name of the language. Think of C, Ruby, JS, etc.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
760 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
761 * @return string giving the language a name
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
762 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
763 public String getName() {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
764 return info.getName();
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
765 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
766
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
767 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
768 * Name of the language version.
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
769 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
770 * @return string specifying the language version
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
771 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
772 public String getVersion() {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
773 return info.getVersion();
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
774 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
775
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
776 /**
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
777 * Evaluates provided source. Ignores the particular {@link Source#getMimeType() MIME type}
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
778 * and forces evaluation in the context of <code>this</code> language.
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
779 *
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
780 * @param source code snippet to execute
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
781 * @return a {@link Value} object that holds result of an execution, never <code>null</code>
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
782 * @throws IOException thrown to signal errors while processing the code
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
783 */
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
784 public Value eval(Source source) throws IOException {
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
785 checkThread();
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
786 return PolyglotEngine.this.eval(this, source);
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
787 }
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
788
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
789 /**
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
790 * Returns value representing global object of the language.
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
791 * <p>
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
792 * The object is expected to be <code>TruffleObject</code> (e.g. a native object from the
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
793 * other language) but technically it can be one of Java primitive wrappers ({@link Integer}, {@link Double}, {@link Short}, etc.).
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
794 *
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
795 * @return the global object or <code>null</code> if the language does not support such
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
796 * concept
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
797 */
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
798 public Value getGlobalObject() {
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
799 checkThread();
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
800
22350
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
801 Object res = SPI.languageGlobal(getEnv(true));
b1c71f0c1a85 Unifying the executor access into single ComputeInExecutor class and removing the need for CountDownLatch
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22344
diff changeset
802 return res == null ? null : new Value(new TruffleLanguage[]{info.getImpl(true)}, res);
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
803 }
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
804
22133
c334865b9d42 Make sure TruffleLanguage.createEnv is only called when Accessor.CURRENT_VM is known
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22132
diff changeset
805 TruffleLanguage<?> getImpl(boolean create) {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
806 getEnv(create);
22219
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22217
diff changeset
807 TruffleLanguage<?> impl = info.getImpl(false);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
808 return impl;
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
809 }
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
810
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
811 TruffleLanguage.Env getEnv(boolean create) {
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
812 if (env == null && create) {
22266
0d36601f233e Merge revised Instrumentation framework into the Polyglot API
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22261 22200
diff changeset
813 env = SPI.attachEnv(PolyglotEngine.this, info.getImpl(true), out, err, in, instrumenter);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
814 }
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
815 return env;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
816 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
817
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
818 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
819 public String toString() {
22325
414e82b9fc35 Release 0.9 is out, let's remove deprecated elements from the API
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22323
diff changeset
820 return "[" + getName() + "@ " + getVersion() + " for " + getMimeTypes() + "]";
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
821 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
822 } // end of Language
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
823
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
824 //
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
825 // Accessor helper methods
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
826 //
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
827
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
828 TruffleLanguage<?> findLanguage(Class<? extends TruffleLanguage> languageClazz) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
829 for (Map.Entry<String, Language> entrySet : langs.entrySet()) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
830 Language languageDescription = entrySet.getValue();
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
831 final TruffleLanguage<?> impl = languageDescription.getImpl(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
832 if (languageClazz.isInstance(impl)) {
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
833 return impl;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
834 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
835 }
22352
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
836 return null;
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
837 }
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
838
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
839 TruffleLanguage<?> findLanguage(String mimeType) {
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
840 Language languageDescription = this.langs.get(mimeType);
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
841 if (languageDescription != null) {
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
842 return languageDescription.getImpl(true);
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
843 }
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
844 return null;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
845 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
846
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
847 TruffleLanguage<?> findLanguage(Probe probe) {
22219
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22217
diff changeset
848 return findLanguage(SPI.findLanguage(probe));
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
849 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
850
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
851 Env findEnv(Class<? extends TruffleLanguage> languageClazz) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
852 for (Map.Entry<String, Language> entrySet : langs.entrySet()) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
853 Language languageDescription = entrySet.getValue();
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
854 Env env = languageDescription.getEnv(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
855 if (env != null && languageClazz.isInstance(languageDescription.getImpl(false))) {
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
856 return env;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
857 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
858 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
859 throw new IllegalStateException("Cannot find language " + languageClazz + " among " + langs);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
860 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
861
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
862 private static class SPIAccessor extends Accessor {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
863 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
864 public Object importSymbol(Object vmObj, TruffleLanguage<?> ownLang, String globalName) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
865 PolyglotEngine vm = (PolyglotEngine) vmObj;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
866 Object g = vm.globals.get(globalName);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
867 if (g != null) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
868 return g;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
869 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
870 Set<Language> uniqueLang = new LinkedHashSet<>(vm.langs.values());
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
871 for (Language dl : uniqueLang) {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
872 TruffleLanguage<?> l = dl.getImpl(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
873 TruffleLanguage.Env env = dl.getEnv(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
874 if (l == ownLang || l == null || env == null) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
875 continue;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
876 }
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
877 Object obj = SPI.findExportedSymbol(env, globalName, true);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
878 if (obj != null) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
879 return obj;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
880 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
881 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
882 for (Language dl : uniqueLang) {
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
883 TruffleLanguage<?> l = dl.getImpl(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
884 TruffleLanguage.Env env = dl.getEnv(false);
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
885 if (l == ownLang || l == null || env == null) {
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
886 continue;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
887 }
22137
1957c49a979d (Re)Introducing LanguageCache - e.g. data that hold the essential static information about Truffle languages. Pre-initializing the data if AOT property is specified. Use mx -J-Dcom.oracle.truffle.aot=true unittest to validate proper behavior of AOT mode.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22135
diff changeset
888 Object obj = SPI.findExportedSymbol(env, globalName, false);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
889 if (obj != null) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
890 return obj;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
891 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
892 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
893 return null;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
894 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
895
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
896 @Override
22237
c4e7737981d3 Truffle/Instrumentation: make the Instrumenter available to language implementations via Env.getInstrumenter()
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22232
diff changeset
897 protected Env attachEnv(Object obj, TruffleLanguage<?> language, OutputStream stdOut, OutputStream stdErr, InputStream stdIn, Instrumenter instrumenter) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
898 PolyglotEngine vm = (PolyglotEngine) obj;
22237
c4e7737981d3 Truffle/Instrumentation: make the Instrumenter available to language implementations via Env.getInstrumenter()
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22232
diff changeset
899 return super.attachEnv(vm, language, stdOut, stdErr, stdIn, instrumenter);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
900 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
901
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
902 @Override
22386
086776aa7548 Caching Source -> CallTarget mapping per PolyglotEngine.Language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22374
diff changeset
903 protected Object eval(TruffleLanguage<?> l, Source s, Map<Source, CallTarget> cache) throws IOException {
086776aa7548 Caching Source -> CallTarget mapping per PolyglotEngine.Language
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22374
diff changeset
904 return super.eval(l, s, cache);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
905 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
906
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
907 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
908 public Object findExportedSymbol(TruffleLanguage.Env env, String globalName, boolean onlyExplicit) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
909 return super.findExportedSymbol(env, globalName, onlyExplicit);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
910 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
911
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
912 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
913 protected Object languageGlobal(TruffleLanguage.Env env) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
914 return super.languageGlobal(env);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
915 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
916
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
917 @Override
22220
20380d1d41f2 Truffle/Instrumentation: second merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22219
diff changeset
918 protected Instrumenter createInstrumenter(Object vm) {
20380d1d41f2 Truffle/Instrumentation: second merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22219
diff changeset
919 return super.createInstrumenter(vm);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
920 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
921
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
922 @Override
22238
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
923 protected Debugger createDebugger(Object vm, Instrumenter instrumenter) {
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
924 return super.createDebugger(vm, instrumenter);
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
925 }
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
926
cccb1ac700bd Team/Instrumentation: the TruflfeVM Debugger is now created eagerly at VM "build" time
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22237
diff changeset
927 @Override
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
928 protected Instrumenter getInstrumenter(Object obj) {
22266
0d36601f233e Merge revised Instrumentation framework into the Polyglot API
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22261 22200
diff changeset
929 final PolyglotEngine vm = (PolyglotEngine) obj;
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
930 return vm.instrumenter;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
931 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
932
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
933 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
934 protected Class<? extends TruffleLanguage> findLanguage(Probe probe) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
935 return super.findLanguage(probe);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
936 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
937
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
938 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
939 protected Env findLanguage(Object obj, Class<? extends TruffleLanguage> languageClass) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
940 PolyglotEngine vm = (PolyglotEngine) obj;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
941 return vm.findEnv(languageClass);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
942 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
943
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
944 @Override
22352
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
945 protected TruffleLanguage<?> findLanguageImpl(Object obj, Class<? extends TruffleLanguage> languageClazz, String mimeType) {
22266
0d36601f233e Merge revised Instrumentation framework into the Polyglot API
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22261 22200
diff changeset
946 final PolyglotEngine vm = (PolyglotEngine) obj;
22352
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
947 TruffleLanguage<?> language = null;
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
948 if (languageClazz != null) {
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
949 language = vm.findLanguage(languageClazz);
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
950 }
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
951 if (language == null && mimeType != null) {
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
952 language = vm.findLanguage(mimeType);
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
953 }
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
954 if (language == null) {
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
955 throw new IllegalStateException("Cannot find language " + languageClazz + " with mimeType" + mimeType + " among " + vm.langs);
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
956 }
7d9b7365b675 Adding Map<String,Object> parameter to the attach(Eval*Listener) method to allow passing parameter values that the evaluated Source can reference.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22335
diff changeset
957 return language;
22214
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
958 }
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
959
3aad794eec0e Truffle/Instrumentation: first large merge of instrumentation code into the TruffleVM framework
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22135
diff changeset
960 @Override
22275
141fe31da7a2 Avoiding check for current stack depth on the initialization of debugger. Only when debugger is suspended the real check is needed.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22274
diff changeset
961 protected Closeable executionStart(Object obj, int currentDepth, Debugger debugger, Source s) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
962 PolyglotEngine vm = (PolyglotEngine) obj;
22275
141fe31da7a2 Avoiding check for current stack depth on the initialization of debugger. Only when debugger is suspended the real check is needed.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22274
diff changeset
963 return super.executionStart(vm, -1, debugger, s);
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
964 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
965
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
966 @Override
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
967 protected void dispatchEvent(Object obj, Object event) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
968 PolyglotEngine vm = (PolyglotEngine) obj;
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
969 vm.dispatch(event);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
970 }
22183
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
971
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
972 @Override
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
973 protected void dispose(TruffleLanguage<?> impl, TruffleLanguage.Env env) {
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
974 super.dispose(impl, env);
1421041175a7 Adding dispose() and TruffleLanguage.disposeContext to allow user request and languages explicitly free the resources
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22167
diff changeset
975 }
22284
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
976
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
977 @Override
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
978 protected String toString(TruffleLanguage language, TruffleLanguage.Env env, Object obj) {
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
979 return super.toString(language, env, obj);
33d49924a921 PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22282
diff changeset
980 }
22128
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
981 } // end of SPIAccessor
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
982 }