Mercurial > hg > truffle
diff 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 |
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java Thu Jul 30 17:16:59 2015 +0200 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java Thu Jul 30 17:36:34 2015 +0200 @@ -32,6 +32,8 @@ import com.oracle.truffle.api.*; import com.oracle.truffle.api.debug.*; import com.oracle.truffle.api.instrument.*; +import com.oracle.truffle.api.nodes.Node; +import com.oracle.truffle.api.nodes.RootNode; import com.oracle.truffle.api.source.*; import com.oracle.truffle.api.vm.*; @@ -115,28 +117,8 @@ private final Map<String, String> implicit = new HashMap<>(); @Override - protected Object eval(Source code) throws IOException { - Properties p = new Properties(); - try (Reader r = code.getReader()) { - p.load(r); - } - Enumeration<Object> en = p.keys(); - while (en.hasMoreElements()) { - Object n = en.nextElement(); - if (n instanceof String) { - String k = (String) n; - if (k.startsWith("explicit.")) { - explicit.put(k.substring(9), p.getProperty(k)); - } - if (k.startsWith("implicit.")) { - implicit.put(k.substring(9), p.getProperty(k)); - } - if (k.equals("return")) { - return env().importSymbol(p.getProperty(k)); - } - } - } - return null; + protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException { + return new ValueCallTarget(code, getClass()); } @Override @@ -150,6 +132,10 @@ return null; } + public static <Language extends TruffleLanguage> Language findContext(Class<Language> type) { + return TruffleLanguage.findContext(type); + } + @Override protected Object getLanguageGlobal() { return null; @@ -169,6 +155,53 @@ protected DebugSupportProvider getDebugSupport() { return null; } + + private Object importExport(Source code) { + Properties p = new Properties(); + try (Reader r = code.getReader()) { + p.load(r); + } catch (IOException ex) { + throw new IllegalStateException(ex); + } + Enumeration<Object> en = p.keys(); + while (en.hasMoreElements()) { + Object n = en.nextElement(); + if (n instanceof String) { + String k = (String) n; + if (k.startsWith("explicit.")) { + explicit.put(k.substring(9), p.getProperty(k)); + } + if (k.startsWith("implicit.")) { + implicit.put(k.substring(9), p.getProperty(k)); + } + if (k.equals("return")) { + return env().importSymbol(p.getProperty(k)); + } + } + } + return null; + } + } + + private static final class ValueCallTarget implements RootCallTarget { + private final Source code; + private final Class<? extends AbstractExportImportLanguage> language; + + private ValueCallTarget(Source code, Class<? extends AbstractExportImportLanguage> language) { + this.code = code; + this.language = language; + } + + @Override + public RootNode getRootNode() { + throw new UnsupportedOperationException(); + } + + @Override + public Object call(Object... arguments) { + AbstractExportImportLanguage context = AbstractExportImportLanguage.findContext(language); + return context.importExport(code); + } } static final String L1 = "application/x-test-import-export-1";