comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java @ 22046:e7c2d36daf72

TruffleLanguage.parse method to convert a source to CallTarget. Basic caching to make sure the code is shared among tenants in one JVM.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 30 Jul 2015 17:36:34 +0200
parents 9c8c0937da41
children 6dd4ab4d76d3
comparison
equal deleted inserted replaced
22045:ffbc7f472438 22046:e7c2d36daf72
29 import java.lang.reflect.*; 29 import java.lang.reflect.*;
30 30
31 import com.oracle.truffle.api.debug.*; 31 import com.oracle.truffle.api.debug.*;
32 import com.oracle.truffle.api.impl.*; 32 import com.oracle.truffle.api.impl.*;
33 import com.oracle.truffle.api.instrument.*; 33 import com.oracle.truffle.api.instrument.*;
34 import com.oracle.truffle.api.nodes.Node;
34 import com.oracle.truffle.api.source.*; 35 import com.oracle.truffle.api.source.*;
35 import com.oracle.truffle.api.vm.*; 36 import com.oracle.truffle.api.vm.*;
36 import com.oracle.truffle.api.vm.TruffleVM.Language; 37 import com.oracle.truffle.api.vm.TruffleVM.Language;
38 import java.util.Collections;
39 import java.util.Map;
40 import java.util.WeakHashMap;
37 41
38 /** 42 /**
39 * An entry point for everyone who wants to implement a Truffle based language. By providing 43 * An entry point for everyone who wants to implement a Truffle based language. By providing
40 * implementation of this type and registering it using {@link Registration} annotation, your 44 * implementation of this type and registering it using {@link Registration} annotation, your
41 * language becomes accessible to users of the {@link TruffleVM Truffle virtual machine} - all they 45 * language becomes accessible to users of the {@link TruffleVM Truffle virtual machine} - all they
96 throw new NullPointerException("Accessing env before initialization is finished"); 100 throw new NullPointerException("Accessing env before initialization is finished");
97 } 101 }
98 return this.env; 102 return this.env;
99 } 103 }
100 104
101 protected abstract Object eval(Source code) throws IOException; 105 /**
106 * Parses the provided source and generates appropriate AST. The parsing should execute no user
107 * code, it should only create the {@link Node} tree to represent the source. The parsing may be
108 * performed in a context (specified as another {@link Node}) or without context. The
109 * {@code argumentNames} may contain symbolic names for actual parameters of the call to the
110 * returned value. The result should be a call target with method
111 * {@link CallTarget#call(java.lang.Object...)} that accepts as many arguments as were provided
112 * via the {@code argumentNames} array.
113 *
114 * @param code source code to parse
115 * @param context a {@link Node} defining context for the parsing
116 * @param argumentNames symbolic names for parameters of
117 * {@link CallTarget#call(java.lang.Object...)}
118 * @return a call target to invoke which also keeps in memory the {@link Node} tree representing
119 * just parsed <code>code</code>
120 * @throws IOException thrown when I/O or parsing goes wrong
121 */
122 protected abstract CallTarget parse(Source code, Node context, String... argumentNames) throws IOException;
102 123
103 /** 124 /**
104 * Called when some other language is seeking for a global symbol. This method is supposed to do 125 * Called when some other language is seeking for a global symbol. This method is supposed to do
105 * lazy binding, e.g. there is no need to export symbols in advance, it is fine to wait until 126 * lazy binding, e.g. there is no need to export symbols in advance, it is fine to wait until
106 * somebody asks for it (by calling this method). 127 * somebody asks for it (by calling this method).
148 protected abstract ToolSupportProvider getToolSupport(); 169 protected abstract ToolSupportProvider getToolSupport();
149 170
150 protected abstract DebugSupportProvider getDebugSupport(); 171 protected abstract DebugSupportProvider getDebugSupport();
151 172
152 /** 173 /**
174 * Finds the currently executing context for current thread.
175 *
176 * @param <Language> type of language making the query
177 * @param language the language class
178 * @return the context associated with current execution
179 * @throws IllegalStateException if no context is associated with the execution
180 */
181 protected static <Language extends TruffleLanguage> Language findContext(Class<Language> language) {
182 return language.cast(API.findLanguage(null, language));
183 }
184
185 /**
153 * Represents execution environment of the {@link TruffleLanguage}. Each active 186 * Represents execution environment of the {@link TruffleLanguage}. Each active
154 * {@link TruffleLanguage} receives instance of the environment before any code is executed upon 187 * {@link TruffleLanguage} receives instance of the environment before any code is executed upon
155 * it. The environment has knowledge of all active languages and can exchange symbols between 188 * it. The environment has knowledge of all active languages and can exchange symbols between
156 * them. 189 * them.
157 */ 190 */
227 @Override 260 @Override
228 public Object importSymbol(TruffleVM vm, TruffleLanguage queryingLang, String globalName) { 261 public Object importSymbol(TruffleVM vm, TruffleLanguage queryingLang, String globalName) {
229 return super.importSymbol(vm, queryingLang, globalName); 262 return super.importSymbol(vm, queryingLang, globalName);
230 } 263 }
231 264
232 @Override 265 private static final Map<Source, CallTarget> COMPILED = Collections.synchronizedMap(new WeakHashMap<Source, CallTarget>());
233 protected Object eval(TruffleLanguage l, Source s) throws IOException { 266
234 return l.eval(s); 267 @Override
268 protected Object eval(TruffleLanguage language, Source source) throws IOException {
269 CallTarget target = COMPILED.get(source);
270 if (target == null) {
271 target = language.parse(source, null);
272 if (target == null) {
273 throw new IOException("Parsing has not produced a CallTarget for " + source);
274 }
275 COMPILED.put(source, target);
276 }
277 try {
278 return target.call();
279 } catch (Exception ex) {
280 throw new IOException(ex);
281 }
235 } 282 }
236 283
237 @Override 284 @Override
238 protected Object findExportedSymbol(TruffleLanguage l, String globalName, boolean onlyExplicit) { 285 protected Object findExportedSymbol(TruffleLanguage l, String globalName, boolean onlyExplicit) {
239 return l.findExportedSymbol(globalName, onlyExplicit); 286 return l.findExportedSymbol(globalName, onlyExplicit);
240 } 287 }
241 288
242 @Override 289 @Override
290 protected TruffleLanguage findLanguage(TruffleVM vm, Class<? extends TruffleLanguage> languageClass) {
291 return super.findLanguage(vm, languageClass);
292 }
293
294 @Override
243 protected Object languageGlobal(TruffleLanguage l) { 295 protected Object languageGlobal(TruffleLanguage l) {
244 return l.getLanguageGlobal(); 296 return l.getLanguageGlobal();
245 } 297 }
246 298
247 @Override 299 @Override