Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.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 | 09531c471176 |
children | 78c3d3d8d86e |
comparison
equal
deleted
inserted
replaced
22045:ffbc7f472438 | 22046:e7c2d36daf72 |
---|---|
30 import org.junit.*; | 30 import org.junit.*; |
31 | 31 |
32 import com.oracle.truffle.api.*; | 32 import com.oracle.truffle.api.*; |
33 import com.oracle.truffle.api.debug.*; | 33 import com.oracle.truffle.api.debug.*; |
34 import com.oracle.truffle.api.instrument.*; | 34 import com.oracle.truffle.api.instrument.*; |
35 import com.oracle.truffle.api.nodes.Node; | |
36 import com.oracle.truffle.api.nodes.RootNode; | |
35 import com.oracle.truffle.api.source.*; | 37 import com.oracle.truffle.api.source.*; |
36 import com.oracle.truffle.api.vm.*; | 38 import com.oracle.truffle.api.vm.*; |
37 | 39 |
38 public class ImplicitExplicitExportTest { | 40 public class ImplicitExplicitExportTest { |
39 private TruffleVM vm; | 41 private TruffleVM vm; |
113 | 115 |
114 private final Map<String, String> explicit = new HashMap<>(); | 116 private final Map<String, String> explicit = new HashMap<>(); |
115 private final Map<String, String> implicit = new HashMap<>(); | 117 private final Map<String, String> implicit = new HashMap<>(); |
116 | 118 |
117 @Override | 119 @Override |
118 protected Object eval(Source code) throws IOException { | 120 protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException { |
121 return new ValueCallTarget(code, getClass()); | |
122 } | |
123 | |
124 @Override | |
125 protected Object findExportedSymbol(String globalName, boolean onlyExplicit) { | |
126 if (explicit.containsKey(globalName)) { | |
127 return explicit.get(globalName); | |
128 } | |
129 if (!onlyExplicit && implicit.containsKey(globalName)) { | |
130 return implicit.get(globalName); | |
131 } | |
132 return null; | |
133 } | |
134 | |
135 public static <Language extends TruffleLanguage> Language findContext(Class<Language> type) { | |
136 return TruffleLanguage.findContext(type); | |
137 } | |
138 | |
139 @Override | |
140 protected Object getLanguageGlobal() { | |
141 return null; | |
142 } | |
143 | |
144 @Override | |
145 protected boolean isObjectOfLanguage(Object object) { | |
146 return false; | |
147 } | |
148 | |
149 @Override | |
150 protected ToolSupportProvider getToolSupport() { | |
151 return null; | |
152 } | |
153 | |
154 @Override | |
155 protected DebugSupportProvider getDebugSupport() { | |
156 return null; | |
157 } | |
158 | |
159 private Object importExport(Source code) { | |
119 Properties p = new Properties(); | 160 Properties p = new Properties(); |
120 try (Reader r = code.getReader()) { | 161 try (Reader r = code.getReader()) { |
121 p.load(r); | 162 p.load(r); |
163 } catch (IOException ex) { | |
164 throw new IllegalStateException(ex); | |
122 } | 165 } |
123 Enumeration<Object> en = p.keys(); | 166 Enumeration<Object> en = p.keys(); |
124 while (en.hasMoreElements()) { | 167 while (en.hasMoreElements()) { |
125 Object n = en.nextElement(); | 168 Object n = en.nextElement(); |
126 if (n instanceof String) { | 169 if (n instanceof String) { |
136 } | 179 } |
137 } | 180 } |
138 } | 181 } |
139 return null; | 182 return null; |
140 } | 183 } |
141 | 184 } |
142 @Override | 185 |
143 protected Object findExportedSymbol(String globalName, boolean onlyExplicit) { | 186 private static final class ValueCallTarget implements RootCallTarget { |
144 if (explicit.containsKey(globalName)) { | 187 private final Source code; |
145 return explicit.get(globalName); | 188 private final Class<? extends AbstractExportImportLanguage> language; |
146 } | 189 |
147 if (!onlyExplicit && implicit.containsKey(globalName)) { | 190 private ValueCallTarget(Source code, Class<? extends AbstractExportImportLanguage> language) { |
148 return implicit.get(globalName); | 191 this.code = code; |
149 } | 192 this.language = language; |
150 return null; | 193 } |
151 } | 194 |
152 | 195 @Override |
153 @Override | 196 public RootNode getRootNode() { |
154 protected Object getLanguageGlobal() { | 197 throw new UnsupportedOperationException(); |
155 return null; | 198 } |
156 } | 199 |
157 | 200 @Override |
158 @Override | 201 public Object call(Object... arguments) { |
159 protected boolean isObjectOfLanguage(Object object) { | 202 AbstractExportImportLanguage context = AbstractExportImportLanguage.findContext(language); |
160 return false; | 203 return context.importExport(code); |
161 } | |
162 | |
163 @Override | |
164 protected ToolSupportProvider getToolSupport() { | |
165 return null; | |
166 } | |
167 | |
168 @Override | |
169 protected DebugSupportProvider getDebugSupport() { | |
170 return null; | |
171 } | 204 } |
172 } | 205 } |
173 | 206 |
174 static final String L1 = "application/x-test-import-export-1"; | 207 static final String L1 = "application/x-test-import-export-1"; |
175 private static final String L2 = "application/x-test-import-export-2"; | 208 private static final String L2 = "application/x-test-import-export-2"; |