comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ImplicitExplicitExportTest.java @ 22121:d045a505c2b3

Asynchronous TruffleVM can be created by providing own Executor when configuring the TruffleVM.Builder
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 02 Sep 2015 10:44:54 +0200
parents cf19259edf87
children dc83cc1f94f2 ffadd23c63c8 3aad794eec0e
comparison
equal deleted inserted replaced
22120:fe5df1f36fec 22121:d045a505c2b3
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 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 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package com.oracle.truffle.api.test.vm; 23 package com.oracle.truffle.api.test.vm;
24
25 import static org.junit.Assert.*;
26
27 import java.io.*;
28 import java.util.*;
29
30 import org.junit.*;
31 24
32 import com.oracle.truffle.api.*; 25 import com.oracle.truffle.api.*;
33 import com.oracle.truffle.api.TruffleLanguage.Env; 26 import com.oracle.truffle.api.TruffleLanguage.Env;
34 import com.oracle.truffle.api.debug.*; 27 import com.oracle.truffle.api.debug.*;
35 import com.oracle.truffle.api.instrument.*; 28 import com.oracle.truffle.api.instrument.*;
36 import com.oracle.truffle.api.nodes.Node; 29 import com.oracle.truffle.api.nodes.Node;
37 import com.oracle.truffle.api.nodes.RootNode; 30 import com.oracle.truffle.api.nodes.RootNode;
38 import com.oracle.truffle.api.source.*; 31 import com.oracle.truffle.api.source.*;
39 import com.oracle.truffle.api.vm.*; 32 import com.oracle.truffle.api.vm.*;
33 import java.io.*;
34 import java.util.*;
35 import java.util.concurrent.Executors;
36 import org.junit.*;
37 import static org.junit.Assert.*;
40 38
41 public class ImplicitExplicitExportTest { 39 public class ImplicitExplicitExportTest {
40 private static Thread mainThread;
42 private TruffleVM vm; 41 private TruffleVM vm;
43 42
44 @Before 43 @Before
45 public void initializeVM() { 44 public void initializeVM() {
46 vm = TruffleVM.newVM().build(); 45 mainThread = Thread.currentThread();
46 vm = TruffleVM.newVM().executor(Executors.newSingleThreadExecutor()).build();
47 assertTrue("Found " + L1 + " language", vm.getLanguages().containsKey(L1)); 47 assertTrue("Found " + L1 + " language", vm.getLanguages().containsKey(L1));
48 assertTrue("Found " + L2 + " language", vm.getLanguages().containsKey(L2)); 48 assertTrue("Found " + L2 + " language", vm.getLanguages().containsKey(L2));
49 assertTrue("Found " + L3 + " language", vm.getLanguages().containsKey(L3)); 49 assertTrue("Found " + L3 + " language", vm.getLanguages().containsKey(L3));
50 } 50 }
51 51
52 @After
53 public void cleanThread() {
54 mainThread = null;
55 }
56
52 @Test 57 @Test
53 public void explicitExportFound() throws IOException { 58 public void explicitExportFound() throws IOException {
54 // @formatter:off 59 // @formatter:off
55 vm.eval(Source.fromText("explicit.ahoj=42", "Fourty two").withMimeType(L1)); 60 vm.eval(Source.fromText("explicit.ahoj=42", "Fourty two").withMimeType(L1));
56 Object ret = vm.eval( 61 Object ret = vm.eval(
57 Source.fromText("return=ahoj", "Return").withMimeType(L3) 62 Source.fromText("return=ahoj", "Return").withMimeType(L3)
58 ); 63 ).get();
59 // @formatter:on 64 // @formatter:on
60 assertEquals("42", ret); 65 assertEquals("42", ret);
61 } 66 }
62 67
63 @Test 68 @Test
66 vm.eval( 71 vm.eval(
67 Source.fromText("implicit.ahoj=42", "Fourty two").withMimeType(L1) 72 Source.fromText("implicit.ahoj=42", "Fourty two").withMimeType(L1)
68 ); 73 );
69 Object ret = vm.eval( 74 Object ret = vm.eval(
70 Source.fromText("return=ahoj", "Return").withMimeType(L3) 75 Source.fromText("return=ahoj", "Return").withMimeType(L3)
71 ); 76 ).get();
72 // @formatter:on 77 // @formatter:on
73 assertEquals("42", ret); 78 assertEquals("42", ret);
74 } 79 }
75 80
76 @Test 81 @Test
82 vm.eval( 87 vm.eval(
83 Source.fromText("explicit.ahoj=43", "Fourty three").withMimeType(L2) 88 Source.fromText("explicit.ahoj=43", "Fourty three").withMimeType(L2)
84 ); 89 );
85 Object ret = vm.eval( 90 Object ret = vm.eval(
86 Source.fromText("return=ahoj", "Return").withMimeType(L3) 91 Source.fromText("return=ahoj", "Return").withMimeType(L3)
87 ); 92 ).get();
88 // @formatter:on 93 // @formatter:on
89 assertEquals("Explicit import from L2 is used", "43", ret); 94 assertEquals("Explicit import from L2 is used", "43", ret);
90 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").invoke(null)); 95 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").get());
91 } 96 }
92 97
93 @Test 98 @Test
94 public void explicitExportPreferred1() throws IOException { 99 public void explicitExportPreferred1() throws IOException {
95 // @formatter:off 100 // @formatter:off
99 vm.eval( 104 vm.eval(
100 Source.fromText("implicit.ahoj=42", "Fourty two").withMimeType(L2) 105 Source.fromText("implicit.ahoj=42", "Fourty two").withMimeType(L2)
101 ); 106 );
102 Object ret = vm.eval( 107 Object ret = vm.eval(
103 Source.fromText("return=ahoj", "Return").withMimeType(L3) 108 Source.fromText("return=ahoj", "Return").withMimeType(L3)
104 ); 109 ).get();
105 // @formatter:on 110 // @formatter:on
106 assertEquals("Explicit import from L2 is used", "43", ret); 111 assertEquals("Explicit import from L2 is used", "43", ret);
107 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").invoke(null)); 112 assertEquals("Global symbol is also 43", "43", vm.findGlobalSymbol("ahoj").invoke(null).get());
108 } 113 }
109 114
110 private static final class Ctx { 115 private static final class Ctx {
111 final Map<String, String> explicit = new HashMap<>(); 116 final Map<String, String> explicit = new HashMap<>();
112 final Map<String, String> implicit = new HashMap<>(); 117 final Map<String, String> implicit = new HashMap<>();
119 124
120 private abstract static class AbstractExportImportLanguage extends TruffleLanguage<Ctx> { 125 private abstract static class AbstractExportImportLanguage extends TruffleLanguage<Ctx> {
121 126
122 @Override 127 @Override
123 protected Ctx createContext(Env env) { 128 protected Ctx createContext(Env env) {
129 if (mainThread != null) {
130 assertNotEquals("Should run asynchronously", Thread.currentThread(), mainThread);
131 }
124 return new Ctx(env); 132 return new Ctx(env);
125 } 133 }
126 134
127 @Override 135 @Override
128 protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException { 136 protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException {
162 protected DebugSupportProvider getDebugSupport() { 170 protected DebugSupportProvider getDebugSupport() {
163 return null; 171 return null;
164 } 172 }
165 173
166 private Object importExport(Source code) { 174 private Object importExport(Source code) {
175 assertNotEquals("Should run asynchronously", Thread.currentThread(), mainThread);
167 final Node node = createFindContextNode(); 176 final Node node = createFindContextNode();
168 Ctx ctx = findContext(node); 177 Ctx ctx = findContext(node);
169 Properties p = new Properties(); 178 Properties p = new Properties();
170 try (Reader r = code.getReader()) { 179 try (Reader r = code.getReader()) {
171 p.load(r); 180 p.load(r);