annotate truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java @ 22325:414e82b9fc35

Release 0.9 is out, let's remove deprecated elements from the API
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Fri, 23 Oct 2015 20:23:00 +0200
parents 096e2c0fd2dc
children 1801b7f11c64
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;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
30 import java.io.InterruptedIOException;
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
31 import java.io.OutputStream;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
32 import java.util.ArrayList;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
33 import java.util.Arrays;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
34 import java.util.Collections;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
35 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
36 import java.util.HashSet;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
37 import java.util.LinkedHashSet;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
38 import java.util.List;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
39 import java.util.Map;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
40 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
41 import java.util.concurrent.CountDownLatch;
f879b1fe3773 Separating 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 import java.util.concurrent.Executor;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
43 import java.util.logging.Level;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22141
diff changeset
44 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
45
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.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
47 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
48 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
49 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
50 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
51 import com.oracle.truffle.api.TruffleLanguage.Registration;
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.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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 import com.oracle.truffle.api.source.Source;
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 {
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
104 static final boolean JAVA_INTEROP_ENABLED = !Boolean.getBoolean("com.oracle.truffle.aot");
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
105 private static final Executor DIRECT_EXECUTOR = new Executor() {
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
106 @Override
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
107 public void execute(Runnable command) {
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
108 command.run();
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
109 }
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
110 };
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
111
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125
f879b1fe3773 Separating 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 /**
f879b1fe3773 Separating 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 * 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
128 */
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating 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 /**
f879b1fe3773 Separating 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 * 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
144 */
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
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 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
154 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
155 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
156 /* 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
157 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
158 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
159 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
160 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
161 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
162 }
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
163 }
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
164 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
165 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
166
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
167 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
168 * 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
169 * 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
170 *
f879b1fe3773 Separating 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 * <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
172 * {@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
173 * .{@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
174 * .{@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
175 * .{@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
176 * .{@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
177 * </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
178 *
f879b1fe3773 Separating 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 * 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
180 * 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
181 * {@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
182 *
f879b1fe3773 Separating 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 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
184 */
22171
41c5f430ce37 Using buildNew as that more directly indicates the use of Builder pattern
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22170
diff changeset
185 public static PolyglotEngine.Builder buildNew() {
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
186 // 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
187 // 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
188 // in Javadoc next to PolyglotEngine class
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
189 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
190 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
191 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
192
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
193 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
194 * 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
195 * 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
196 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
197 * <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
198 * {@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
199 * .{@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
200 * .{@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
201 * .{@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
202 * .{@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
203 * </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
204 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212
f879b1fe3773 Separating 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 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
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
f879b1fe3773 Separating 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 * 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
218 * {@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
219 *
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
220 * @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
221 * @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
222 */
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
223 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
224 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
225 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
226 }
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
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 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
229 * 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
230 * {@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
231 *
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
232 * @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
233 * @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
234 */
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
235 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
236 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
237 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
238 }
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
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 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
241 * 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
242 * {@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
243 *
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
244 * @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
245 * @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
246 */
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
247 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
248 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
249 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
250 }
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
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 /**
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
253 * 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
254 * {@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
255 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
256 * @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
257 * @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
258 */
f879b1fe3773 Separating 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 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
260 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
261 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
262 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
263 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating 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 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
266 * 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
267 * 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
268 * {@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
269 * {@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
270 * 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
271 * 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
272 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
273 * @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
274 * @param obj value of the object - expected to be primitive wrapper, {@link String} 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
275 * <code>TruffleObject</code> for mutual inter-operability
f879b1fe3773 Separating 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 * @return instance of this builder
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
277 * @see PolyglotEngine#findGlobalSymbol(java.lang.String)
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
278 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
279 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
280 final Object truffleReady;
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
281 if (obj instanceof Number || obj instanceof String || obj instanceof Character || obj instanceof Boolean) {
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
282 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
283 } else {
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
284 truffleReady = JavaInterop.asTruffleObject(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
285 }
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 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
287 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
288 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
289
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
290 /**
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
291 * Provides own executor for running {@link PolyglotEngine} scripts. By default
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
292 * {@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
293 * {@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
294 * 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
295 * 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
296 * {@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
297 * 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
298 * {@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
299 * 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
300 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
301 * @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
302 * 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
303 * @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
304 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
305 @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
306 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
307 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
308 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
309 }
f879b1fe3773 Separating 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 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
312 * 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
313 * 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
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 * @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
316 */
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
317 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
318 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
319 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
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 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
322 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
323 }
f879b1fe3773 Separating 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 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
325 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
326 }
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
327 Executor nonNullExecutor = executor != null ? executor : DIRECT_EXECUTOR;
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
328 return new PolyglotEngine(nonNullExecutor, 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
329 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
332 /**
f879b1fe3773 Separating 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 * 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
334 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
335 * @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
336 * 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
337 */
22169
307a4ed5430e Two more changes to guarantee better compatibility with TruffleVM
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22168
diff changeset
338 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
339 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
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 /**
f879b1fe3773 Separating 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 * 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
344 * {@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
345 * 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
346 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
347 * @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
348 * @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
349 * @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
350 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
351 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
352 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
353 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
354 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
355 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
356 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
357 }
f879b1fe3773 Separating 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 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
359 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
360
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
361 /**
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
362 * 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
363 * {@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
364 * 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
365 * 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
366 * <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
367 * 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
368 * {@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
369 */
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 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
371 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
372 disposed = 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
373 executor.execute(new Runnable() {
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 @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
375 public void run() {
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 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
377 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
378 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
379 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
380 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
381 } 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
382 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
383 }
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
384 }
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 }
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 }
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 });
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 }
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
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
390 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
391 final Object[] result = {null, 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
392 final CountDownLatch ready = new CountDownLatch(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
393 final TruffleLanguage[] lang = {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
394 executor.execute(new Runnable() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
395 @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
396 public void run() {
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
397 evalImpl(lang, s, result, l, ready);
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
398 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
399 });
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
400 exceptionCheck(result);
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
401 return createValue(lang, result, ready);
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
402 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
403
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
404 Value createValue(TruffleLanguage[] lang, Object[] result, CountDownLatch ready) {
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
405 return new Value(lang, result, ready);
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
406 }
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
407
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
408 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
409 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
410 }
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
411
22132
19205292b0c7 Suppressing "try" lints
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22129
diff changeset
412 @SuppressWarnings("try")
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
413 private void evalImpl(TruffleLanguage<?>[] fillLang, Source s, Object[] result, Language l, CountDownLatch ready) {
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
414 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
415 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
416 fillLang[0] = langImpl;
c334865b9d42 Make sure TruffleLanguage.createEnv is only called when Accessor.CURRENT_VM is known
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22132
diff changeset
417 result[0] = SPI.eval(langImpl, 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
418 } catch (IOException ex) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
419 result[1] = ex;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
420 } finally {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
421 ready.countDown();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
422 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
423 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
424
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
425 @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
426 final Object invokeForeign(final Node foreignNode, VirtualFrame frame, final TruffleObject receiver) throws IOException {
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
427 final Object[] res = {null, 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
428 if (executor == DIRECT_EXECUTOR) {
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
429 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
430 final Object[] args = ForeignAccess.getArguments(frame).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
431 res[0] = ForeignAccess.execute(foreignNode, frame, receiver, args);
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
432 }
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
433 } else {
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 invokeForeignOnExecutor(foreignNode, frame, receiver, res);
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 }
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
436 exceptionCheck(res);
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 if (res[0] instanceof TruffleObject) {
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 return new EngineTruffleObject(this, (TruffleObject) res[0]);
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
439 } else {
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 return res[0];
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
441 }
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
442 }
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
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
444 @TruffleBoundary
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 private void invokeForeignOnExecutor(final Node foreignNode, VirtualFrame frame, final TruffleObject receiver, final Object[] res) throws IOException {
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 final MaterializedFrame materialized = frame.materialize();
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
447 final CountDownLatch computed = new CountDownLatch(1);
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
448 executor.execute(new Runnable() {
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
449 @SuppressWarnings("try")
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
450 @Override
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
451 public void run() {
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
452 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
453 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
454 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
455 final CallTarget target = Truffle.getRuntime().createCallTarget(node);
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 res[0] = target.call(args);
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
457 } catch (Exception ex) {
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
458 res[1] = ex;
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
459 } finally {
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
460 computed.countDown();
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
461 }
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
462 }
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
463 });
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
464 try {
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
465 computed.await();
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
466 } catch (InterruptedException ex) {
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
467 throw new InterruptedIOException(ex.getMessage());
22312
8322a904a472 Verifying the Truffle/Java interop works OK also in asynchronous mode
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22311
diff changeset
468 }
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
469 }
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
470
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
471 /**
f879b1fe3773 Separating 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 * 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
473 * 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
474 * 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
475 * <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
476 * 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
477 * 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
478 * <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
479 * <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
480 * <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
481 * </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
482 * 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
483 * 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
484 *
f879b1fe3773 Separating 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 * @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
486 * @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
487 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
488 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
489 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
490 final TruffleLanguage<?>[] lang = {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
491 final Object[] obj = {globals.get(globalName), 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
492 final CountDownLatch ready = new CountDownLatch(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
493 if (obj[0] == 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
494 executor.execute(new Runnable() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
495 @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
496 public void run() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
497 findGlobalSymbolImpl(obj, globalName, lang, ready);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
498 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
499 });
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
500 try {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
501 ready.await();
f879b1fe3773 Separating 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 } catch (InterruptedException ex) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
503 LOG.log(Level.SEVERE, null, ex);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
504 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
505 } else {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
506 ready.countDown();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
507 }
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
508 return obj[0] == null ? null : createValue(lang, obj, ready);
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
509 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
510
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
511 private void findGlobalSymbolImpl(Object[] obj, String globalName, TruffleLanguage<?>[] lang, CountDownLatch ready) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
512 if (obj[0] == 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
513 for (Language dl : langs.values()) {
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
514 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
515 if (env == null) {
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
516 continue;
c334865b9d42 Make sure TruffleLanguage.createEnv is only called when Accessor.CURRENT_VM is known
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22132
diff changeset
517 }
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
518 obj[0] = 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
519 if (obj[0] != null) {
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
520 lang[0] = dl.getImpl(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
521 break;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
522 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
523 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
524 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
525 if (obj[0] == 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
526 for (Language dl : langs.values()) {
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
527 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
528 if (env == null) {
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
529 continue;
c334865b9d42 Make sure TruffleLanguage.createEnv is only called when Accessor.CURRENT_VM is known
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22132
diff changeset
530 }
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
531 obj[0] = 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
532 if (obj[0] != null) {
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
533 lang[0] = dl.getImpl(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
534 break;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
535 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
536 }
f879b1fe3773 Separating 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 ready.countDown();
f879b1fe3773 Separating 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 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating 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 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
542 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
543 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
544 }
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
545 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
546 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
547 }
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
548 }
f879b1fe3773 Separating 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 @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
551 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
552 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
553 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
554 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
555 }
f879b1fe3773 Separating 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 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
557 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
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 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
560 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating 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 @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
563 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
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 @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
567 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
568 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
569
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
570 @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
571 <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
572 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
573 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
574 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
575 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
576 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
577 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
579 static void exceptionCheck(Object[] result) throws RuntimeException, IOException {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
580 if (result[1] instanceof IOException) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
581 throw (IOException) result[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
582 }
f879b1fe3773 Separating 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 if (result[1] instanceof RuntimeException) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
584 throw (RuntimeException) result[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
585 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
586 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
587
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
588 /**
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
589 * 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
590 * {@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
591 * {@link PolyglotEngine#findGlobalSymbol(java.lang.String)} and
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
592 * {@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
593 * {@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
594 * {@link PolyglotEngine} has been initialized for
22212
71ec6de11cea fix a few typos
Andreas Woess <andreas.woess@oracle.com>
parents: 22209
diff changeset
595 * {@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
596 * {@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
597 * 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
598 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
599 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
600 private final TruffleLanguage<?>[] language;
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
601 private final Object[] result;
f879b1fe3773 Separating 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 private final CountDownLatch ready;
f879b1fe3773 Separating 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 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
604
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
605 Value(TruffleLanguage<?>[] language, Object[] result, CountDownLatch ready) {
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
606 this.language = 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
607 this.result = result;
f879b1fe3773 Separating 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 this.ready = ready;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
609 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
610
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
611 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
612 * 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
613 * 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
614 * {@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
615 * 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
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 * @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
618 * @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
619 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
620 public Object get() throws IOException {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
621 waitForSymbol();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
622 exceptionCheck(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
623 if (result[0] instanceof TruffleObject) {
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
624 return new EngineTruffleObject(PolyglotEngine.this, (TruffleObject) result[0]);
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
625 } else {
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
626 return result[0];
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
627 }
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
628 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
629
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
630 /**
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
631 * 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
632 * 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
633 * {@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
634 * . 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
635 * 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
636 * 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
637 * {@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
638 *
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
639 * @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
640 * @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
641 * @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
642 * @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
643 * @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
644 */
22312
8322a904a472 Verifying the Truffle/Java interop works OK also in asynchronous mode
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22311
diff changeset
645 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
646 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
647 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
648 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
649 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
650 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
651 }
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
652 }
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
653 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
654 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
655 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
656 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
657 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
658 }
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
659 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
660 }
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
661 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
662 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
663 }
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
664 if (JAVA_INTEROP_ENABLED) {
22314
e56d1a618f64 Need to send every invokeForeign into the executor
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22312
diff changeset
665 return JavaInterop.asJavaObject(representation, (TruffleObject) obj);
22304
7e2abecabcac Allow to disable Java interoperability
Christian Wimmer <christian.wimmer@oracle.com>
parents: 22284
diff changeset
666 }
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
667 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
668 }
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
669
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
670 /**
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
671 * 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
672 * 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
673 * 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
674 * 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
675 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
676 * @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
677 * 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
678 * @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
679 * @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
680 * <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
681 * @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
682 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
683 public Value invoke(final Object thiz, 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
684 get();
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
685 final CountDownLatch done = new CountDownLatch(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
686 final Object[] res = {null, 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
687 executor.execute(new Runnable() {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
688 @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
689 public void run() {
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
690 invokeImpl(thiz, args, res, done);
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
691 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
692 });
f879b1fe3773 Separating 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 exceptionCheck(res);
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
694 return createValue(language, res, done);
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
695 }
f879b1fe3773 Separating 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
22132
19205292b0c7 Suppressing "try" lints
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22129
diff changeset
697 @SuppressWarnings("try")
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
698 private void invokeImpl(Object thiz, Object[] args, Object[] res, CountDownLatch done) {
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
699 try (final Closeable c = SPI.executionStart(PolyglotEngine.this, -1, 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
700 List<Object> arr = new ArrayList<>();
22179
ee9ae440f1eb Testing behavior of invoke when calling asTruffleFunction
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22174
diff changeset
701 if (thiz == null) {
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
702 if (language[0] != null) {
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
703 Object global = SPI.languageGlobal(SPI.findLanguage(PolyglotEngine.this, language[0].getClass()));
22179
ee9ae440f1eb Testing behavior of invoke when calling asTruffleFunction
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22174
diff changeset
704 if (global != null) {
ee9ae440f1eb Testing behavior of invoke when calling asTruffleFunction
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22174
diff changeset
705 arr.add(global);
ee9ae440f1eb Testing behavior of invoke when calling asTruffleFunction
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22174
diff changeset
706 }
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
707 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
708 } else {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
709 arr.add(thiz);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
710 }
f879b1fe3773 Separating 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 arr.addAll(Arrays.asList(args));
f879b1fe3773 Separating 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 for (;;) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
713 try {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
714 if (target == null) {
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
715 target = SymbolInvokerImpl.createCallTarget(language[0], result[0], arr.toArray());
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 res[0] = target.call(arr.toArray());
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
718 break;
22129
0589cc5cab30 TruffleVM can now depend on api.interop and thus there is no need for indirection between SymbolInvoker and its Impl. Enough to do direct calls.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22128
diff changeset
719 } catch (ArgumentsMishmashException ex) {
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
720 target = 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
721 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
722 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
723 } catch (IOException ex) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
724 res[1] = ex;
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
725 } catch (RuntimeException ex) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
726 res[1] = ex;
f879b1fe3773 Separating 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 } finally {
f879b1fe3773 Separating 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 done.countDown();
f879b1fe3773 Separating 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 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
730 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
731
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
732 private void waitForSymbol() throws InterruptedIOException {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
733 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
734 try {
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
735 if (ready != null) {
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
736 ready.await();
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
737 }
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 } catch (InterruptedException ex) {
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
739 throw (InterruptedIOException) new InterruptedIOException(ex.getMessage()).initCause(ex);
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
740 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
741 }
22282
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
742
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
743 @Override
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
744 public String toString() {
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
745 return "PolyglotEngine.Value[value=" + result[0] + ",exception=" + result[1] + ",computed=" + (ready.getCount() == 0) + "]";
088b3121f5ae Giving Value instances toString() useful for debugging purposes.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22278
diff changeset
746 }
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 /**
22172
b31dcacfc8ff Replacing more TruffleVM references in comments with the new PolyglotEngine name
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22171
diff changeset
750 * 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
751 * 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
752 * 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
753 * {@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
754 * {@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
755 * 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
756 * 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
757 * {@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
758 */
22168
ffadd23c63c8 Introducing Portaal into the isolated world of each and any TruffleLanguage.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22137
diff changeset
759 public class Language {
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
760 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
761 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
762
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
763 Language(LanguageCache info) {
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 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
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 * 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
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 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
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 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
773 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
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 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
777 * 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
778 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
779 * @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
780 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
781 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
782 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
783 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
784
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
785 /**
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
786 * 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
787 *
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
788 * @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
789 */
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
790 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
791 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
792 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
793
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
794 /**
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
795 * 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
796 * 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
797 *
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
798 * @param source code snippet to execute
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
799 * @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
800 * @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
801 */
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
802 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
803 checkThread();
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
804 return PolyglotEngine.this.eval(this, source);
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
805 }
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
806
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
807 /**
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
808 * 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
809 * <p>
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
810 * 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
811 * 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
812 *
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
813 * @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
814 * concept
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
815 */
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
816 public Value getGlobalObject() {
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
817 checkThread();
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
818
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
819 Object[] res = {SPI.languageGlobal(getEnv(true)), null};
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
820 return res[0] == null ? null : new Value(new TruffleLanguage[]{info.getImpl(true)}, res, null);
22278
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
821 }
85a6db6624ab Simplifying the way to obtain Language.globalObject
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22277
diff changeset
822
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
823 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
824 getEnv(create);
22219
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22217
diff changeset
825 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
826 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
827 }
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
828
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
829 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
830 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
831 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
832 }
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
833 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
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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
836 @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
837 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
838 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
839 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
840 } // 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
841
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
842 //
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
843 // 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
844 //
f879b1fe3773 Separating 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
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
846 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
847 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
848 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
849 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
850 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
851 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
852 }
f879b1fe3773 Separating 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 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
854 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
855 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
856
f879b1fe3773 Separating 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 TruffleLanguage<?> findLanguage(Probe probe) {
22219
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22217
diff changeset
858 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
859 }
f879b1fe3773 Separating 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 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
862 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
863 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
864 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
865 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
866 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
867 }
f879b1fe3773 Separating 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 }
f879b1fe3773 Separating 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 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
870 }
f879b1fe3773 Separating 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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
872 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
873 @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
874 public Object importSymbol(Object vmObj, TruffleLanguage<?> ownLang, String globalName) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
875 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
876 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
877 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
878 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
879 }
f879b1fe3773 Separating 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 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
881 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
882 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
883 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
884 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
885 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
886 }
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
887 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
888 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
889 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
890 }
f879b1fe3773 Separating 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 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
893 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
894 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
895 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
896 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
897 }
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
898 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
899 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
900 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
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 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
903 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
904 }
f879b1fe3773 Separating 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 @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
907 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
908 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
909 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
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 public Object eval(TruffleLanguage<?> l, Source s) throws IOException {
f879b1fe3773 Separating 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.eval(l, s);
f879b1fe3773 Separating 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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
918 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
919 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
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
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
923 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
924 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
925 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
926
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
927 @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
928 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
929 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
930 }
f879b1fe3773 Separating 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 @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
933 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
934 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
935 }
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
936
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
937 @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
938 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
939 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
940 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
941 }
f879b1fe3773 Separating 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 @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
944 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
945 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
946 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
947
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
948 @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
949 protected Env findLanguage(Object obj, Class<? extends TruffleLanguage> languageClass) {
22170
796f0fef110e Renaming to PolyglotEngine
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22169
diff changeset
950 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
951 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
952 }
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
953
f879b1fe3773 Separating the TruffleVM into its own project makes it possible to cleanup various system parts interations
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
diff changeset
954 @Override
22268
b56fe0d3b560 Merge with acfea340b19199faaa44bd5e103c716c17ea2b21
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22266 22212
diff changeset
955 protected TruffleLanguage<?> findLanguageImpl(Object obj, Class<? extends TruffleLanguage> languageClazz) {
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
956 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
957 return vm.findLanguage(languageClazz);
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 }