comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java @ 21481:bb51b9a142b3

Enforcing public, one parameter constructor for each TruffleLanguage by annotation processor and required call to super.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 25 May 2015 12:26:53 +0200
parents 99942eac9c6d
children 3286fb5fea4a b1530a6cce8c
comparison
equal deleted inserted replaced
21480:c2b006c5e15f 21481:bb51b9a142b3
31 import java.io.IOException; 31 import java.io.IOException;
32 import java.lang.annotation.ElementType; 32 import java.lang.annotation.ElementType;
33 import java.lang.annotation.Retention; 33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy; 34 import java.lang.annotation.RetentionPolicy;
35 import java.lang.annotation.Target; 35 import java.lang.annotation.Target;
36 import java.lang.reflect.Constructor;
36 37
37 /** 38 /**
38 * An entry point for everyone who wants to implement a Truffle based language. By providing 39 * An entry point for everyone who wants to implement a Truffle based language. By providing
39 * implementation of this type and registering it using {@link Registration} annotation, your 40 * implementation of this type and registering it using {@link Registration} annotation, your
40 * language becomes accessible to users of the {@link TruffleVM Truffle virtual machine} - all they 41 * language becomes accessible to users of the {@link TruffleVM Truffle virtual machine} - all they
41 * will need to do is to include your JAR into their application and all the Truffle goodies (multi 42 * will need to do is to include your JAR into their application and all the Truffle goodies (multi
42 * language support, multi tennat hosting, debugging, etc.) will be made available to them. 43 * language support, multi tennat hosting, debugging, etc.) will be made available to them.
43 */ 44 */
44 public abstract class TruffleLanguage { 45 public abstract class TruffleLanguage {
45 private Env env; 46 private final Env env;
47
48 /**
49 * Constructor to be called by subclasses.
50 *
51 * @param env language environment that will be available via {@link #env()} method to
52 * subclasses.
53 */
54 protected TruffleLanguage(Env env) {
55 this.env = env;
56 }
46 57
47 /** 58 /**
48 * The annotation to use to register your language to the {@link TruffleVM Truffle} system. By 59 * The annotation to use to register your language to the {@link TruffleVM Truffle} system. By
49 * annotating your implementation of {@link TruffleLanguage} by this annotation you are just a 60 * annotating your implementation of {@link TruffleLanguage} by this annotation you are just a
50 * <em>one JAR drop to the classpath</em> away from your users. Once they include your JAR in 61 * <em>one JAR drop to the classpath</em> away from your users. Once they include your JAR in
68 * their code snippets or their {@link TruffleVM#eval(java.net.URI) files}. 79 * their code snippets or their {@link TruffleVM#eval(java.net.URI) files}.
69 * 80 *
70 * @return array of mime types assigned to your language files 81 * @return array of mime types assigned to your language files
71 */ 82 */
72 String[] mimeType(); 83 String[] mimeType();
73 }
74
75 @SuppressWarnings("all")
76 void attachEnv(Env env) {
77 this.env = env;
78 } 84 }
79 85
80 protected final Env env() { 86 protected final Env env() {
81 if (this.env == null) { 87 if (this.env == null) {
82 throw new NullPointerException("Accessing env before initialization is finished"); 88 throw new NullPointerException("Accessing env before initialization is finished");
128 */ 134 */
129 public static final class Env { 135 public static final class Env {
130 private final TruffleVM vm; 136 private final TruffleVM vm;
131 private final TruffleLanguage lang; 137 private final TruffleLanguage lang;
132 138
133 Env(TruffleVM vm, TruffleLanguage lang) { 139 Env(TruffleVM vm, Constructor<?> langConstructor) {
134 this.vm = vm; 140 this.vm = vm;
135 this.lang = lang; 141 try {
142 this.lang = (TruffleLanguage) langConstructor.newInstance(this);
143 } catch (Exception ex) {
144 throw new IllegalStateException("Cannot construct language " + langConstructor.getClass().getName(), ex);
145 }
136 } 146 }
137 147
138 /** 148 /**
139 * Asks the environment to go through other registered languages and find whether they 149 * Asks the environment to go through other registered languages and find whether they
140 * export global symbol of specified name. The expected return type is either 150 * export global symbol of specified name. The expected return type is either
152 private static final AccessAPI API = new AccessAPI(); 162 private static final AccessAPI API = new AccessAPI();
153 163
154 private static final class AccessAPI extends Accessor { 164 private static final class AccessAPI extends Accessor {
155 165
156 @Override 166 @Override
157 protected Env attachEnv(TruffleVM vm, TruffleLanguage l) { 167 protected TruffleLanguage attachEnv(TruffleVM vm, Constructor<?> langClazz) {
158 Env env = new Env(vm, l); 168 Env env = new Env(vm, langClazz);
159 l.attachEnv(env); 169 return env.lang;
160 return env;
161 } 170 }
162 171
163 @Override 172 @Override
164 public Object importSymbol(TruffleVM vm, TruffleLanguage queryingLang, String globalName) { 173 public Object importSymbol(TruffleVM vm, TruffleLanguage queryingLang, String globalName) {
165 return super.importSymbol(vm, queryingLang, globalName); 174 return super.importSymbol(vm, queryingLang, globalName);