Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java @ 21716:2f9e4d984d16
Give languages a chance to do implicit exports. Prefer explicit exports over implicit ones.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 04 Jun 2015 08:08:05 +0200 |
parents | |
children | 45083be8a812 |
comparison
equal
deleted
inserted
replaced
21715:67e28e817d32 | 21716:2f9e4d984d16 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.test.vm; | |
24 | |
25 import com.oracle.truffle.api.TruffleLanguage; | |
26 import com.oracle.truffle.api.source.Source; | |
27 import com.oracle.truffle.api.vm.TruffleVM; | |
28 import java.io.IOException; | |
29 import java.io.Reader; | |
30 import java.util.Enumeration; | |
31 import java.util.HashMap; | |
32 import java.util.Map; | |
33 import java.util.Properties; | |
34 import static org.junit.Assert.*; | |
35 import org.junit.Before; | |
36 import org.junit.Test; | |
37 | |
38 public class ImplicitExplicitExportTest { | |
39 private TruffleVM vm; | |
40 | |
41 @Before | |
42 public void initializeVM() { | |
43 vm = TruffleVM.newVM().build(); | |
44 assertTrue("Found " + L1 + " language", vm.getLanguages().containsKey(L1)); | |
45 assertTrue("Found " + L2 + " language", vm.getLanguages().containsKey(L2)); | |
46 assertTrue("Found " + L3 + " language", vm.getLanguages().containsKey(L3)); | |
47 } | |
48 | |
49 @Test | |
50 public void explicitExportFound() throws IOException { | |
51 // @formatter:off | |
52 vm.eval(L1, | |
53 "explicit.ahoj=42" | |
54 ); | |
55 Object ret = vm.eval(L3, | |
56 "return=ahoj" | |
57 ); | |
58 // @formatter:on | |
59 assertEquals("42", ret); | |
60 } | |
61 | |
62 @Test | |
63 public void implicitExportFound() throws IOException { | |
64 // @formatter:off | |
65 vm.eval(L1, | |
66 "implicit.ahoj=42" | |
67 ); | |
68 Object ret = vm.eval(L3, | |
69 "return=ahoj" | |
70 ); | |
71 // @formatter:on | |
72 assertEquals("42", ret); | |
73 } | |
74 | |
75 @Test | |
76 public void explicitExportPreferred2() throws IOException { | |
77 // @formatter:off | |
78 vm.eval(L1, | |
79 "implicit.ahoj=42" | |
80 ); | |
81 vm.eval(L2, | |
82 "explicit.ahoj=43" | |
83 ); | |
84 Object ret = vm.eval(L3, | |
85 "return=ahoj" | |
86 ); | |
87 // @formatter:on | |
88 assertEquals("Explicit import from L2 is used", "43", ret); | |
89 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").invoke(null)); | |
90 } | |
91 | |
92 @Test | |
93 public void explicitExportPreferred1() throws IOException { | |
94 // @formatter:off | |
95 vm.eval(L1, | |
96 "explicit.ahoj=43" | |
97 ); | |
98 vm.eval(L2, | |
99 "implicit.ahoj=42" | |
100 ); | |
101 Object ret = vm.eval(L3, | |
102 "return=ahoj" | |
103 ); | |
104 // @formatter:on | |
105 assertEquals("Explicit import from L2 is used", "43", ret); | |
106 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").invoke(null)); | |
107 } | |
108 | |
109 private abstract static class AbstractExportImportLanguage extends TruffleLanguage { | |
110 protected AbstractExportImportLanguage(Env env) { | |
111 super(env); | |
112 } | |
113 | |
114 private final Map<String, String> explicit = new HashMap<>(); | |
115 private final Map<String, String> implicit = new HashMap<>(); | |
116 | |
117 @Override | |
118 protected Object eval(Source code) throws IOException { | |
119 Properties p = new Properties(); | |
120 try (Reader r = code.getReader()) { | |
121 p.load(r); | |
122 } | |
123 Enumeration<Object> en = p.keys(); | |
124 while (en.hasMoreElements()) { | |
125 Object n = en.nextElement(); | |
126 if (n instanceof String) { | |
127 String k = (String) n; | |
128 if (k.startsWith("explicit.")) { | |
129 explicit.put(k.substring(9), p.getProperty(k)); | |
130 } | |
131 if (k.startsWith("implicit.")) { | |
132 implicit.put(k.substring(9), p.getProperty(k)); | |
133 } | |
134 if (k.equals("return")) { | |
135 return env().importSymbol(p.getProperty(k)); | |
136 } | |
137 } | |
138 } | |
139 return null; | |
140 } | |
141 | |
142 @Override | |
143 protected Object findExportedSymbol(String globalName, boolean onlyExplicit) { | |
144 if (explicit.containsKey(globalName)) { | |
145 return explicit.get(globalName); | |
146 } | |
147 if (!onlyExplicit && implicit.containsKey(globalName)) { | |
148 return implicit.get(globalName); | |
149 } | |
150 return null; | |
151 } | |
152 | |
153 @Override | |
154 protected Object getLanguageGlobal() { | |
155 return null; | |
156 } | |
157 | |
158 @Override | |
159 protected boolean isObjectOfLanguage(Object object) { | |
160 return false; | |
161 } | |
162 } | |
163 | |
164 private static final String L1 = "application/x-test-import-export-1"; | |
165 private static final String L2 = "application/x-test-import-export-2"; | |
166 private static final String L3 = "application/x-test-import-export-3"; | |
167 | |
168 @TruffleLanguage.Registration(mimeType = L1, name = "ImportExport1") | |
169 public static final class ExportImportLanguage1 extends AbstractExportImportLanguage { | |
170 public ExportImportLanguage1(Env env) { | |
171 super(env); | |
172 } | |
173 } | |
174 | |
175 @TruffleLanguage.Registration(mimeType = L2, name = "ImportExport2") | |
176 public static final class ExportImportLanguage2 extends AbstractExportImportLanguage { | |
177 public ExportImportLanguage2(Env env) { | |
178 super(env); | |
179 } | |
180 } | |
181 | |
182 @TruffleLanguage.Registration(mimeType = L3, name = "ImportExport3") | |
183 public static final class ExportImportLanguage3 extends AbstractExportImportLanguage { | |
184 public ExportImportLanguage3(Env env) { | |
185 super(env); | |
186 } | |
187 } | |
188 | |
189 } |